Counting null and non-null values in a single query

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

    Just to provide yet another alternative, Postgres 9.4+ allows applying a FILTER to aggregates:

    SELECT
      COUNT(*) FILTER (WHERE a IS NULL) count_nulls,
      COUNT(*) FILTER (WHERE a IS NOT NULL) count_not_nulls
    FROM us;
    

    SQLFiddle: http://sqlfiddle.com/#!17/80a24/5

提交回复
热议问题