如何计算两张桌子之间的不同数量?

问题描述

我需要计算2个表之间的差异(2天不同的表),以查看发生了什么变化。

例如表1:

enter image description here

表2:

enter image description here

我想得到这张桌子:

enter image description here

我尝试以下代码

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