Counting null and non-null values in a single query

后端 未结 26 1126
星月不相逢
星月不相逢 2021-01-29 19:31

I have a table

create table us
(
 a number
);

Now I have data like:

a
1
2
3
4
null
null
null
8
9

Now I need

26条回答
  •  北荒
    北荒 (楼主)
    2021-01-29 20:05

    In my case I wanted the "null distribution" amongst multiple columns:

    SELECT
           (CASE WHEN a IS NULL THEN 'NULL' ELSE 'NOT-NULL' END) AS a_null,
           (CASE WHEN b IS NULL THEN 'NULL' ELSE 'NOT-NULL' END) AS b_null,
           (CASE WHEN c IS NULL THEN 'NULL' ELSE 'NOT-NULL' END) AS c_null,
           ...
           count(*)
    FROM us
    GROUP BY 1, 2, 3,...
    ORDER BY 1, 2, 3,...
    

    As per the '...' it is easily extendable to more columns, as many as needed

提交回复
热议问题