sql查询以组和内连接两个表格总和

发布于 2025-01-23 03:52:38 字数 803 浏览 0 评论 0原文

我有两个这样的表:

”在此处输入图像描述”

”在此处输入图像描述”

两张表中的每一行都是由列周周和城市唯一识别的。 我想创建一个具有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:

enter image description here

enter image description here

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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

真心难拥有 2025-01-30 03:52:38

如果您需要依靠加入列为列表,则只需要在避免重复数据之前总结一下表格
认为如果您在表1中有一个星期,而在表2中不在表2中,则数据将不会在您的示例中是shawn

SELECT
    A1.week,
    A1.city,
    A1.value1,
    A1.value2,
    A2.value1,
    A2.value2
FROM (
    SELECT
        Week,
        city,
        sum(value1),
        sum(value2)
    FROM table1
    GROUP BY Week, city
) A1
INNER JOIN (
    SELECT
        Week,
        city,
        sum(valueA),
        sum(valueB)
    FROM table2
    GROUP BY Week, city
) A2
    ON a1.week = a2.week AND a1.city = a2.city

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

SELECT
    A1.week,
    A1.city,
    A1.value1,
    A1.value2,
    A2.value1,
    A2.value2
FROM (
    SELECT
        Week,
        city,
        sum(value1),
        sum(value2)
    FROM table1
    GROUP BY Week, city
) A1
INNER JOIN (
    SELECT
        Week,
        city,
        sum(valueA),
        sum(valueB)
    FROM table2
    GROUP BY Week, city
) A2
    ON a1.week = a2.week AND a1.city = a2.city
眼眸 2025-01-30 03:52:38

以下查询可以按预期为您提供输出:

SELECT table1.week, sum(value_a) as sum_a, sum(value_b) as sum_b, sum(value1) as sum_1, sum(value2) as sum_2
FROM table1
  INNER JOIN table2 ON table1.week = table2.week AND table1.city = table2.city
group by table1.week

可以通过检查链接

​/b4vvn.png“ alt =”在此处输入图像描述>

the below query can give you output as expected:

SELECT table1.week, sum(value_a) as sum_a, sum(value_b) as sum_b, sum(value1) as sum_1, sum(value2) as sum_2
FROM table1
  INNER JOIN table2 ON table1.week = table2.week AND table1.city = table2.city
group by table1.week

Query can be validated by checking the link db<>fiddle<>example

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文