Oracle: How to count null and non-null rows Oracle: How to count null and non-null rows oracle oracle

Oracle: How to count null and non-null rows


COUNT(expr) will count the number of rows where expr is not null, thus you can count the number of nulls with expressions like these:

SELECT count(a) nb_a_not_null,       count(b) nb_b_not_null,       count(*) - count(a) nb_a_null,       count(*) - count(b) nb_b_null,       count(case when a is not null and b is not null then 1 end)nb_a_b_not_null       count(case when a is null and b is null then 1 end) nb_a_and_b_null  FROM my_table


Something like this:

SELECT sum(case                when a is null and b is null then 1               else 0           end) as both_null_count,       sum(case               when a is null and b is not null then 1               else 0           end) as only_a_is_null_countFROM your_table

You can extend that for other combinations of null/not null


select sum(decode(a,null,0,1)) as "NotNullCount", sum(decode(a,null,1,0)) as "NullCount"from myTable;

Repeat for as many fields as you like.