我真的有问题,试图总结一个表的一列,然后根据从另一个表的数据分组。他们拥有的唯一公用钥匙是帐号。
下面是这个场景
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,任何帮助将不胜感激.
发布于 2016-03-07 09:01:57
尝试这样做,这将总结基于read_dates和区域
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
发布于 2016-03-07 07:25:39
您只需将两个表一起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
发布于 2016-03-07 10:18:35
这可以通过以下方法实现:
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
https://stackoverflow.com/questions/35838232
复制相似问题