【问题标题】:SQL aggregate data from multiple tables and sum a specified columnSQL 聚合多个表中的数据并对指定列求和
【发布时间】:2019-07-13 15:19:43
【问题描述】:

我有如下三个表:
表一:

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


表二:

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


表 3:

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

我想将这三个表中相同的 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

记录 (1,x,c) 可以在表 1 和表 3 中找到,因此它们的计数应该相加,其余的在结果表中保持不变。
现在我想到的是使用如下的 UNION 操作:

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

这将为我提供这三个表中所有不同的行,但我不确定如何对计数进行求和,任何帮助或建议将不胜感激。

【问题讨论】:

  • 使用联合查询作为子查询,外部查询进行分组和求和。 (我在手机上打字,否则我会提交解决方案作为答案)

标签: sql select amazon-redshift union


【解决方案1】:

如您所述,union 将删除重复项,因此您应该使用union all。一旦你这样做了,你可以用一个聚合查询来包装那个查询以获得计数的总和:

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

【讨论】:

    【解决方案2】:

    我会在 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
    

    【讨论】:

      【解决方案3】:

      你没有重复的inin表,所以你也可以使用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) ;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-17
        • 1970-01-01
        • 2015-03-06
        • 2020-12-18
        • 2019-07-20
        • 2013-10-01
        • 1970-01-01
        相关资源
        最近更新 更多