我需要找到表之间的差异,因为哪些数据在另一个表中不可用(反之亦然)。 我可以找到不同之处如下:
使用的 SQL 查询:
select *
from (select input_name_id, count(1) as cnt
from Table1
group by input_name_id
) a join
(select input_name_id, count(1) as cnt
from Table2
group by input_name_id
) b
on (a.input_name_id = b.input_name_id)
where a.cnt <> b.cnt
预期结果:
我尝试了多种方法来提取数据,但我做不到! 非常感谢您的帮助。谢谢
最佳答案
两件事:(1)全外连接; (2) 枚举具有相同值的行:
select *
from (select input_name_id, match_id, name,
row_number() over (partition by input_name_id, match_id, name order by name) as seqnum
from Table1
) a full join
(select input_name_id, match_id, name,
row_number() over (partition by input_name_id, match_id, name order by name) as seqnum
from Table2
) b
on a.input_name_id = b.input_name_id and
a.match_id = b.match_id and
a.name = b.name and
a.seqnum = b.seqnum
where a.seqnum is null or b.seqnum is null;
关于sql - 需要找出表之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42392044/