我有以下查询返回具有类型“C”的寄存器的所有人以及具有“C”类型的每个人的行数。每个人都可以输入“C”类型或输入“V”
select count(type), number_document
from register
where type = 'C'
group by number_document ;
我想只返回那些所有行都有'C'类型的人,所以我需要做一个子查询,将总行数与count(类型)进行比较。
我不知道如何在这里引入子查询。
答案 0 :(得分:0)
您可以使用NOT EXISTS排除同时具有类型' V':
的用户select count(type), number_document
from register ro
where type = 'C'
AND NOT EXISTS
(SELECT TOP 1 NULL
FROM register ri
WHERE ri.User = ro.User
AND ri.type='V')
group by number_document;
答案 1 :(得分:0)
这对你有用吗?
select count(r.type), r.number_document
from register r
where r.type = 'C' AND NOT EXISTS (
SELECT *
FROM register r2
WHERE r.number_document = r2.number_document AND r2.type <> 'C'
)
group by r.number_document ;
答案 2 :(得分:0)
以下查询有两个条件
然后过滤“C”类型的人
select count(r.type), r.number_document
from register r
where r.type = ALL (
SELECT r2.type
FROM register r2
WHERE r.number_document = r2.number_document
)
AND r.type = 'C'
group by r.number_document ;
答案 3 :(得分:0)
这个怎么样? HAVING子句检查唯一的TYPE列值是否为'C'(因为MIN = MAX = C)。
select number_document, count(*)
from register
group by number_document
having min(type) = max(type) and min(type) = 'C';