简体   繁体   English

SQL 聚合来自多个表的数据并对指定的列求和

[英]SQL aggregate data from multiple tables and sum a specified column

I have three tables as below:我有以下三个表:
Table 1 :表格1 :

user_id   user_label   code1   count1 
-------   ----------   ------  ------
1         x            a       1 
1         x            c       1
1         y            a       1 
2         x            a       1


Table 2 :表二:

user_id   user_label   code2   count2 
-------   ----------   ------  ------
1         x            b       1 
1         x            d       2
1         y            b       1 
2         x            b       1


Table 3 :表3 :

user_id   user_label   code3   count3 
-------   ----------   ------  ------
1         x            c       1 
1         x            e       1
1         y            c       1 
2         x            c       1

And I would like to sum the count from these three tables for the same user_id + user_label + code , and keep the rest records, the desired result would look like below :我想总结这三个表中相同 user_id + user_label + code 的计数,并保留其余记录,所需的结果如下所示:

user_id   user_label   code   total_count 
-------   ----------   ------  ------
1         x            a       1 
1         x            c       2
1         x            b       1 
2         x            d       2
1         x            e       1
1         y            a       1
1         y            b       1
1         y            c       1
2         x            a       1
2         x            b       1
2         x            c       1

The records (1,x,c) can be found both in Table 1 and Table 3 and thus their count should be summed, and the rest stays the same in the result table.记录 (1,x,c) 可以在表 1 和表 3 中找到,因此它们的计数应该相加,其余的在结果表中保持不变。
Right now what I have in mind is using UNION operation like below :现在我想到的是使用 UNION 操作,如下所示:

SELECT * FROM tb1   UNION
SELECT * FROM tb2  UNION
SELECT * FROM tb3 

This will give me all distinct rows from those three tables but I am not sure how to do the summation on counts on top of that, any help or suggestion would be appreciated.这将为我提供这三个表中所有不同的行,但我不确定如何对计数进行求和,任何帮助或建议将不胜感激。

As you noted, union will remove duplicates, so you should use union all .正如您所指出的, union将删除重复项,因此您应该使用union all Once you do that, you can wrap that query with an aggregate query to get the sum of the counts:完成此操作后,您可以使用聚合查询包装该查询以获取计数的总和:

SELECT   user_id, user_label, code, SUM(cnt) AS total_count
FROM     (SELECT user_id, user_label, code1 as code, count1
          FROM   table1
          UNION ALL
          SELECT user_id, user_label, code2, count2
          FROM   table2
          UNION ALL
          SELECT user_id, user_label, code3, count3
          FROM   table3) t
GROUP BY user_id, user_label, code

I would be more explicit in the select statements then wrap the union into a subquery.我会在select语句中更明确,然后将union包装到子查询中。

SELECT user_id, user_label,code, SUM(x_count) as total_count FROM
    SELECT user_id, user_label, code1 as code, count1 as x_count
    FROM tb1 
    UNION
    SELECT user_id, user_label, code2 as code, count2 as x_count
    FROM tb2 
    UNION
    SELECT user_id, user_label, code3 as code, count3 as x_count
    FROM tb3)
GROUP BY user_id, user_label, code

You don't have duplicates within tables, so you can also use full join :您不必重复表,所以你也可以使用full join

select user_id, user_label, code1,
       (coalesce(t1.count1, 0) + coalesce(t2.count1, 0) + coalesce(t3.count1, 0)
       ) as total_count
from table1 t1 full join
     table2 t2
     using (user_id, user_label, code1) full join
     table3 t3
     using (user_id, user_label, code1) ;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM