来自两个月度销售表的客户销售总和-有效查询

时间:2018-11-17 04:44:24

标签: sql-server

寻找最有效的查询来汇总两个月客户的费用-有两个表,一个表用于1月,一个表用于2月。
我将执行内部联接还是proc sql?寻求帮助。谢谢!

Here are the two tables and the end result I'm looking for

在上面添加了一张图片,因为当我在下面键入内容时,列的排列不正确。

所需结果:

cust 1 total from both tables
cust 2 total from both tables
cust 3 total from both tables



mo  cust   $  item
jan cust1 10 shoe
jan cust1 20 coat
jan cust1 30 misc
jan cust2 11 shoe
jan cust2 22 top
jan cust3 30 jean


mo  cust   $ item
feb cust1 15 misc
feb cust1 20 misc
feb cust1 25 jean
feb cust2 35 jean
feb cust3 12 top
feb cust3 30 top

1 个答案:

答案 0 :(得分:0)

只能与表设计一样有效。我认为“ $”不是实际的列名:

select
    cust, sum(dollars) as dollars
from (
    select cust, dollars from table1 
    UNION ALL
    select cust, dollars from table2
     ) d
group by
   cust