选择具有一个唯一ID和另一个唯一ID不同的ORACLE的重复行

时间:2019-06-01 23:41:58

标签: sql oracle

假设您有此表:

ID  |   DOC |   DOC_ID  |   TYPE
1   |   A   |   B       |   A
2   |   A   |   B       |   B
3   |   BB  |   CC      |   A
4   |   BB  |   DD      |   A
5   |   E   |   F       |   A
6   |   E   |   F       |   B
7   |   E   |   F       |   B
8   |   G   |   H       |   A
9   |   G   |   I       |   A
10  |   G   |   I       |   B
11  |   G   |   H       |   B

我需要使用所有这些cincuntanses标识此表中的信息:

  • DOC字段必须相同
  • DOC_ID字段必须不同
  • 每个DOC寄存器有2行以上

如何获取类似下表的信息?

ID  |   DOC |   DOC_ID  |   TYPE
3   |   BB  |   CC      |   A
4   |   BB  |   DD      |   A
10  |   G   |   I       |   B
11  |   G   |   H       |   B

我曾尝试使用分组依据(count_doc_id),但无法获得结果

1 个答案:

答案 0 :(得分:1)

我在考虑窗口功能。这些是您描述的规则:

select t.*
from (select t.*,
             row_number() over (partition by doc, doc_id order by doc_id) as seqnum,
             count(*) over (partition by doc) as cnt
      from t
     ) t
where seqnum = 1 and cnt >= 2;

我认为这些是您想要的规则:

select t.*
from (select t.*,
             row_number() over (partition by doc, doc_id order by doc_id) as seqnum,
             count(distinct doc_id) over (partition by doc) as cnt
      from t
     ) t
where seqnum = 1 and cnt >= 2;