简体   繁体   English

如何对一个表中的一列求和并使用另一表中的列的数据将总和分组

[英]how to sum a column from one table and group the sum using data from column of another table

I am really having problem trying to sum up a column of one table and then group it according to the data from another table. 我真的有一个问题,试图总结一个表的一列,然后根据来自另一个表的数据将其分组。 the only common key that they have is account number. 他们唯一的公用密钥是帐号。

here is the scenario 这是场景

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

Result 结果

Zone Total
A     173
B     146
C     50

so far this is how my query goes 到目前为止,这就是我的查询的方式

Select sum(volume) as Total, read_date as bDate 
GROUP BY bDate 
ORDER By bDate

it was able to sum all the volumes based on the read_date any help would be much appreciated... 它能够基于read_date对所有卷求和,任何帮助将不胜感激...

You just have to JOIN the two tables together and do a GROUP BY Zone : 您只需要将两个表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

Try this, This will sum up based on read_dates and Zones 试试这个,这将基于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

This can be done by using : 这可以通过使用以下命令完成:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查询从一个表中汇总金额并按另一表中的列对其进行分组 - Query to sum amounts from one table and group it by a column in another table 从一个表中返回 SUM(column) 和从另一个表中返回 SUM(column) - Return SUM(column) from a table and SUM(column) from another table Mysql group by 和表一中一列的总和 - Mysql group by and having & sum of one column from table one 使用来自另一个表 PostgreSQL 的 SUM 更新列 - Update column with SUM from another table, PostgreSQL 如何对两个表中的列值求和并保留仅在一个表中的另一列 - How to sum column values from two tables and preserve another column which is only in one table 对表中的一列求和,并将其减去另一个表中的求和列 - Sum a column from a table and subtract it to a summed column in another table 将一个表中的列值与另一个表中的列值相加 - sum column values from one table in relation with values of column from another table 如何计算一个单元格中的数字与另一表中列的SUM之差 - How to count the difference between number from one cell and SUM of the column from another table SQL 如何从一个列表中连接相同的值然后求和 - SQL how to join same value from one column table and then sum it 如何对查询创建的表中的列求和 - How to sum a column from a query created table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM