我有三个表,如下所示:
表1:
user_id user_label code1 count1
------- ---------- ------ ------
1 x a 1
1 x c 1
1 y a 1
2 x a 1
表2:
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
这将为我提供这三个表中所有不同的行,但是我不确定如何对计数进行求和,任何帮助或建议将不胜感激。
答案 0 :(得分: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
答案 1 :(得分:0)
我会在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
答案 2 :(得分:0)
您在表中没有重复的 ,因此您也可以使用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) ;