Counting null and non-null values in a single query

后端 未结 26 1079
星月不相逢
星月不相逢 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:56

    If I understood correctly you want to count all NULL and all NOT NULL in a column...

    If that is correct:

    SELECT count(*) FROM us WHERE a IS NULL 
    UNION ALL
    SELECT count(*) FROM us WHERE a IS NOT NULL
    

    Edited to have the full query, after reading the comments :]


    SELECT COUNT(*), 'null_tally' AS narrative 
      FROM us 
     WHERE a IS NULL 
    UNION
    SELECT COUNT(*), 'not_null_tally' AS narrative 
      FROM us 
     WHERE a IS NOT NULL;
    

提交回复
热议问题