[英]Selecting a COUNT of values, grouped by that same column, distinct on another
[英]Selecting distinct count in a group with only null values for a specific column
我会使用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;
为了获得具有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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.