在Oracle中过滤重复的列

时间:2018-11-02 08:08:31

标签: sql database oracle

我只想使用下面的sql一次查看相同的pid(或person_info表中的id)。最后,在MySQL中可以添加“ group by pid”,但在Oracle中则不能,因为在选择之后我必须为每个group by一个group by。 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

2 个答案:

答案 0 :(得分:2)

使用窗口功能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

答案 1 :(得分:0)

如果您不在乎获得哪一行,则可以获取每一列的MAX()值。

select pi.id                 pid,
   MAX(n.id)                  nid,
   MAX(n.match_mode),
   MAX(n.match_method),
   MAX(n.match_gene_type),
   MAX(n.notification_status),
   MAX(pi.person_name),
   MAX(pi.id_card_no),
   MAX(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
group by pi.id