我有一张桌子
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;
其他回答
对于非空值
select count(a)
from us
null值
select count(*)
from us
minus
select count(a)
from us
因此
SELECT COUNT(A) NOT_NULLS
FROM US
UNION
SELECT COUNT(*) - COUNT(A) NULLS
FROM US
应该做这项工作
更好的是列标题是正确的。
SELECT COUNT(A) NOT_NULL, COUNT(*) - COUNT(A) NULLS
FROM US
在我的系统上进行的一些测试中,需要进行全表扫描。
用于计数非空值
select count(*) from us where a is not null;
用于计算空值
select count(*) from us where a is null;
a为空的元素个数:
select count(a) from us where a is null;
a不为空的元素个数:
select count(a) from us where a is not null;
如果我理解正确,你想在一个列中计数所有NULL和所有NOT NULL…
如果是正确的:
SELECT count(*) FROM us WHERE a IS NULL
UNION ALL
SELECT count(*) FROM us WHERE a IS NOT NULL
阅读评论后,编辑了完整的查询:]
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;
如果是mysql,你可以尝试这样做。
select
(select count(*) from TABLENAME WHERE a = 'null') as total_null,
(select count(*) from TABLENAME WHERE a != 'null') as total_not_null
FROM TABLENAME