如何合并来自不同表的记录和 运行 使用 SQL 的查询?

How to combine records from different tables and run the query using SQL?

原题不清楚所以修改题再问

原题:


同一工作簿中有两个工作sheet具有相同的结构-相同的字段名称。

例如:

Table 1

- Officer  name   mkt 
 - s15     peter  15
 - s17     mary   18
 - S32     tom    42
 - S32     tom    89

Table 2

- Officer  name   mkt 
 - s56     jason  55
 - s31     alex   34
 - S54     gee    45

现在我正在使用 ADO 运行 excel-vba.There 中的 sql 不是 DBMS。我只是 运行 并粘贴到 excel工作-sheet.

select officer ,name ,sum(mkt) from [$table1]

现在我想合并这 2 table 条记录和 select 以后。

这意味着:

Table 3

- Officer  name   mkt 
 - s15     peter  15
 - s17     mary   18
 - S32     tom    42
 - S32     tom    89
 - s56     jason  55
 - s31     alex   34
 - S54     gee    45

然后再制作 selection(SQL) .

  select officer ,name ,sum(mkt) from [$table3] group by officer

是否可以在SQL或VBA中执行(我更喜欢在SQL语句中执行)?

*我更喜欢使用某事SQL技术来执行它。像 join table 这样的东西?但是加入 table 只加入不同的列 tables.Now 我想加入行 *

更新

人们建议使用 union all 我试试

SELECT office, name, sum(mkt)
FROM(
    SELECT *
    FROM [$table1]
    UNION ALL
    SELECT *
    FROM [$table2]
) as table3
GROUP BY officer

发生了from-clause error

您可以使用 temptable 或 CTE(常用 table 表达式)或使用 JOINs

温度table:

create table #tablename(columname datatype)

CTE:

with ctename as (select columns from tablename)

加入:

select columnname 
       from table1 t1
       left join table2 t2 on t1.commonecolumnname=t2.commoncolumnname

我想这可能对你有帮助

SELECT Officer, name, sum(mkt) FROM (
SELECT Officer,name,mkt FROM [$table1]
UNION ALL
 SELECT  Officer,name,mkt 
    FROM [$table2]
    ) as table3
GROUP BY officer,name