答案 0 :(得分:1)
我会使用NOT EXISTS
:
SELECT COUNT(DISTINCT id)
FROM table t
WHERE NOT EXISTS (SELECT 1 FROM table t1 WHERE t1.id = t.id AND t1.val IS NOT NULL);
其他选项使用GROUP BY
:
SELECT COUNT(id)
FROM table t
GROUP BY id
HAVING SUM(CASE WHEN val IS NOT NULL THEN 1 ELSE 0 END) = 0;
答案 1 :(得分:1)
要获取具有NULL
值的ID,我倾向于从此开始:
select id
from t
group by id
having count(*) <> count(val);
此结构允许您检查其他值,例如非NULL
值。
获取具有NULL
值的不同ID的最简单方法是:
select distinct id
from t
where val is null;
如果您只想计数:
select count(distinct id)
from t
where val is null;