gpt4 book ai didi

sql - 需要找出表之间的差异

转载 作者:搜寻专家 更新时间:2023-10-30 23:36:00 24 4
gpt4 key购买 nike

我的表结构如下: enter image description here

我需要找到表之间的差异,因为哪些数据在另一个表中不可用(反之亦然)。我可以找到不同之处如下:

enter image description here

使用的 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

预期结果:

enter image description here

我尝试了多种方法来提取数据,但我做不到!非常感谢您的帮助。谢谢

最佳答案

两件事:(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com