如何选择row_number和count over partition?

时间:2015-08-26 16:24:12

标签: sql oracle window-functions

我需要找到重复记录(包含主记录ID和重复记录ID):

select ciid, name from (
select ciid, name, row_number() over (
  partition by related_id, name order by updatedate desc) rn
) where rn = 1;

这为我提供了主记录ID,但它也包含没有重复记录的记录。

如果我使用

select ciid, name from (
select ciid, name, row_number() over (
  partition by related_id, name order by updatedate desc) rn
) where rn > 1;

这会获取所有重复记录,但不会记录主记录。

我希望如果我这样做:

select ciid, name from (
select ciid, name, row_number()  over (
    partition by related_id, name order by updatedate desc
  ) rn, count(*)  over (
    partition by related_id, name order by updatedate desc
  ) cnt
) where rn = 1 and cnt > 1;

但我担心表现,甚至实际上是在做我想要的事情。

如何仅为具有重复项的主记录获取主记录?请注意,name不是唯一列。只有ciid是唯一的。

1 个答案:

答案 0 :(得分:1)

我还没有对此进行过测试(因为我没有真正的数据而且懒得创建一些数据),但似乎这些内容可能会起作用:

with has_duplicates as (
  select related_id, name
  from yourtable
  group by related_id, name
  having count (*) > 1
),
with_dupes as (
  select
    y.ccid, y.name,
    row_number() over (partition by y.related_id, y.name order by y.updatedate desc) rn
  from
    yourtable y,
    has_duplicates d
  where
    y.related_id = d.related_id and
    y.name = d.name
)
select
  ccid, name
from with_dupes
where rn = 1