gpt4 book ai didi

sql - 过滤Oracle中重复的列

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

我只想使用下面的 sql 查看一次相同的 pid(或 person_info 表中的 id)。最后添加“按 pid 分组”在 MySQL 中有效,但在 Oracle 中无效,因为我必须在选择后为每个分组依据。 Distinct 在这里也不起作用,因为它返回唯一组合而不仅仅是 pid。这让我发疯...提前致谢!

select pi.id                 pid,
n.id nid,
n.match_mode,
n.match_method,
n.match_gene_type,
n.notification_status,
pi.person_name,
pi.id_card_no,
pi.race
from notification n, person_info pi
where (n.src_data_id = pi.id or n.match_data_id = pi.id)
and n.match_mode = '1'
and n.match_gene_type = '1'
and n.notification_status = '2'
and pi.id_card_no IS NOT NULL

最佳答案

使用窗口函数row_number()

with cte as
(
select pi.id pid,
n.id nid,
n.match_mode,
n.match_method,
n.match_gene_type,
n.notification_status,
pi.person_name,
pi.id_card_no,
pi.race,
row_number() over(partition by pi.id order by pi.id) as rn
from notification n, person_info pi
where (n.src_data_id = pi.id or n.match_data_id = pi.id)
and n.match_mode = '1'
and n.match_gene_type = '1'
and n.notification_status = '2'
and pi.id_card_no IS NOT NULL
) select * from cte where rn=1

关于sql - 过滤Oracle中重复的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53114808/

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