Counting null and non-null values in a single query

后端 未结 26 1616
星月不相逢
星月不相逢 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 19:57

    SELECT SUM(NULLs) AS 'NULLS', SUM(NOTNULLs) AS 'NOTNULLs' FROM 
        (select count(*) AS 'NULLs', 0 as 'NOTNULLs' FROM us WHERE a is null
        UNION select 0 as 'NULLs', count(*) AS 'NOTNULLs' FROM us WHERE a is not null) AS x
    

    It's fugly, but it will return a single record with 2 cols indicating the count of nulls vs non nulls.

提交回复
热议问题