假设您有此表:
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标识此表中的信息:
如何获取类似下表的信息?
ID | DOC | DOC_ID | TYPE
3 | BB | CC | A
4 | BB | DD | A
10 | G | I | B
11 | G | H | B
我曾尝试使用分组依据(count_doc_id),但无法获得结果
答案 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;