问题描述
我需要计算2个表之间的差异(2天不同的表),以查看发生了什么变化。
例如表1:
表2:
我想得到这张桌子:
我尝试以下代码:
select a.of_key,case when a.color != b.color then count (a.color) ELSE 0 END AS color,case when a.side != b.side then count (a.side) else 0 end as side
from 130720 A right JOIN 100720 B
ON a.of_key = b.of_key and a.num = b.bum
group by a.of_key
它不起作用
请帮助
谢谢!
解决方法
将汇总放到case语句之外:
select a.of_key,SUM(case when a.color != b.color then 1 ELSE 0 END) AS color,SUM(case when a.side != b.side then 1 else 0 end) as side
from 130720 A right JOIN 100720 B
ON a.of_key = b.of_key and a.num = b.bum
group by a.of_key