我正在绞尽脑汁试图找出哪个群体和/或不同的count()
形成帮助,但目前已经空白了。
考虑一下表:
PersonId | PlaceName
---------+----------
1 | NULL
1 | NULL
2 | NULL
2 | Las Vegas
3 | London
4 | NULL
4 | NULL
4 | NULL
我正在寻找那些没有费心填写“地名”的人,所以我希望我的输出看起来像这样:
PersonId
--------
1
4
我实际上会加入其他一些表来提取每个“捣蛋”人的信息,但问题的关键在于上面的问题。
答案 0 :(得分:2)
使用以下查询:
SELECT PersonId
FROM TheTable
GROUP BY PersonId
HAVING COUNT(PlaceName) = 0
COUNT()
聚合函数会忽略NULL
,因此应返回正确的结果。
答案 1 :(得分:1)
select PersonId
from MyTable
group by PersonId
having count(case when PlaceName is not null then 1 end) = 0
答案 2 :(得分:1)
select distinct t1.id
from test t1
LEFT JOIN
(
select id, count(name) nm
from test
where name is not null
group by id
) x
on t1.id = x.id
where x.nm is null
答案 3 :(得分:0)
我会提供以下内容:
select distinct a.personid
from tablename a
left outer join tablename b
on a.personid=b.personid
and b.placename is not null
where b.personid is null
order by personid
答案 4 :(得分:0)
SELECT DISTINCT PersonId
FROM MyTable t
WHERE NOT EXISTS(SELECT 1 FROM MyTable WHERE PersonId = t.PersonId AND PlaceName is not null)