sql查询以组和内连接两个表格总和
我有两个这样的表:
两张表中的每一行都是由列周周和城市唯一识别的。 我想创建一个具有5列(周,Value_a,value_b,value1,value2)和3行(每周1行)的表格,值列在每个城市汇总)。最后一个表格应该完全像这样:
sum_a是所有城市中每周的值a的总和,sum_b是所有城市的value_b之和。
这是我的SQL查询: 选择 * 从table1 内联接table2 在table1.week = table2.week和 table1.city = table2.City
I have two tables like so:
Each row in both tables is uniquely identified by the columns week and city.
I want to create one table with 5 columns (week, value_a, value_b, value1, value2) and 3 rows (1 row for each week, with the value columns being summed across each city). The final table should look exactly like this:
sum_a is the sum of value a for each week across all cities, sum_b is the sum of value_b across all cities and so on.
Here is my SQL query:
SELECT *
FROM table1
INNER JOIN table2
ON table1.week = table2.week AND
table1.city = table2.city
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要依靠加入列为列表,则只需要在避免重复数据之前总结一下表格
认为如果您在表1中有一个星期,而在表2中不在表2中,则数据将不会在您的示例中是shawn
If you need to sum column relied by join you just need to sum your tables before to avoid repeat data
Considere that if you have a week in your table 1 and not in the table 2 the data will not be shawn in your example
以下查询可以按预期为您提供输出:
可以通过检查链接
/b4vvn.png“ alt =”在此处输入图像描述>
the below query can give you output as expected:
Query can be validated by checking the link db<>fiddle<>example