我在尝试总结一个表的列然后根据另一个表中的数据对其进行分组时遇到了问题。他们唯一的共同关键是帐号。
这是方案
table 1.
account_no volume read_date
1 32 12016
2 22 12016
3 20 12016
4 21 12016
5 54 12016
3 65 12016
7 84 12016
8 21 12016
9 20 12016
10 30 12016
=============================================== ==========
table 2
account_no Zone
1 A
2 A
3 B
4 B
5 A
3 A
7 B
8 B
9 C
10 C
结果
Zone Total
A 173
B 146
C 50
到目前为止这是我的查询的方式
Select sum(volume) as Total, read_date as bDate
GROUP BY bDate
ORDER By bDate
它能够根据read_date对所有卷进行求和 任何帮助将不胜感激......
答案 0 :(得分:1)
您只需将JOIN
两个表放在一起,然后执行GROUP BY Zone
:
SELECT t2.Zone, SUM(volume) AS Total
FROM table1 AS t1
INNER JOIN table2 AS t2
ON t1.account_no = t2.account_no
GROUP BY t2.Zone
答案 1 :(得分:0)
试试这个,这将基于read_dates和Zones
进行总结SELECT A.read_date
,B.Zone
,sum(A.volume) SumOfVolume
FROM @Table1 A
INNER JOIN @Table2 B ON A.account_no = B.account_no
GROUP BY A.read_date
,B.Zone
答案 2 :(得分:0)
这可以通过使用:
来完成SELECT SUM(TBL1.Volume) AS Total, TBL2.Zone
FROM TABLE1 AS TBL1
INNER JOIN TABLE2 AS TBL3
ON TBL1.Account_No = TBL2.Account_No
GROUP BY TBL2.Zone