我想返回类型为 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;
你需要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)
);