我想返回类型为6,类型为7或类型为5的元素。对于同一个register_document_id,它们中的任何一个都不具有类型1或2。
register_document:
id register_document_id type_document
0 10 5
1 10 6
2 10 1
3 15 6
4 15 7
5 17 5
6 18 2
7 18 1
在该示例中,我想仅返回其type_document具有值6,7或5由register_document_id分组的行。通过相同的register_document_id,我想排除类型为2和1的那些。
输出查询:
register_document_id type_Document
15 6
15 7
17 5
select x.register_document_id,x.type_document
from register_document x
where x.type_document= 6 or x.type_document = 7 or x.type_document = 5 AND NOT EXISTS (
SELECT *
FROM register y
WHERE y.number_id = x.number_document AND (y.type_document <> '1' <> y.type_document <> '2'
)
group by x.register_document_id,x.type_document;
答案 0 :(得分:0)
您需要in
!查询应如下所示:
select rd.register_document_id, rd.type_document
from register_document rd
where rd.type_document in (5, 6, 7) and
not exists (select 1
from register_document rd2
where rd2.number_id = rd.number_document and
rd2.type_document in (1, 2)
);