Counting null and non-null values in a single query

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

    All the answers are either wrong or extremely out of date.

    The simple and correct way of doing this query is using COUNT_IF function.

    SELECT
      COUNT_IF(a IS NULL) AS nulls,
      COUNT_IF(a IS NOT NULL) AS not_nulls
    FROM
      us
    
    0 讨论(0)
  • 2021-01-29 20:07

    Try

    SELECT 
       SUM(ISNULL(a)) AS all_null,
       SUM(!ISNULL(a)) AS all_not_null
    FROM us;
    

    Simple!

    0 讨论(0)
提交回复
热议问题