我有一张桌子
create table us
(
a number
);
现在我有如下数据:
a
1
2
3
4
null
null
null
8
9
现在我需要一个查询来计算列a中的空值和非空值
我有一张桌子
create table us
(
a number
);
现在我有如下数据:
a
1
2
3
4
null
null
null
8
9
现在我需要一个查询来计算列a中的空值和非空值
当前回答
用于计数非空值
select count(*) from us where a is not null;
用于计算空值
select count(*) from us where a is null;
其他回答
为了提供另一种选择,Postgres 9.4+允许对聚合应用FILTER:
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
a为空的元素个数:
select count(a) from us where a is null;
a不为空的元素个数:
select count(a) from us where a is not null;
使用ISNULL嵌入函数。
用于计数非空值
select count(*) from us where a is not null;
用于计算空值
select count(*) from us where a is null;
下面是一个在Oracle上运行的快速而简单的版本:
select sum(case a when null then 1 else 0) "Null values",
sum(case a when null then 0 else 1) "Non-null values"
from us