Counting null and non-null values in a single query

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

    This works for Oracle and SQL Server (you might be able to get it to work on another RDBMS):

    select sum(case when a is null then 1 else 0 end) count_nulls
         , count(a) count_not_nulls 
      from us;
    

    Or:

    select count(*) - count(a), count(a) from us;
    

提交回复
热议问题