SQL checks if group contains NULL

Is there any function to check if a column in a group contains NULL, or how can I solve it? An example below is a data structure.

id | value ---------- 1 | NULL 1 | 56 2 | 98 2 | 14 

Result:

 id | value ---------- 1 | 1 2 | 0 
+6
source share
2 answers

try

 select id, count(*) - count(value) as null_value_count from your_table group by id 

SQLFiddle demo

+11
source

Another possibility that does not take advantage of the fact that count(value) ignores NULL values:

 select id, sum(case when value is null then 1 else 0 end) as null_count from your_table group by id; 
+7
source

Source: https://habr.com/ru/post/1437028/


All Articles