计算表中所有列的空值和非空值

时间:2019-05-16 07:26:51

标签: sql count null

我有一个包含两列的表格,如下所示:

name         marks
  A            15
  B            
  c            20

从上方可以清楚地看到,列name具有3个非空值和1个空值,同样,列marks具有2个空值和2个非空值。

要获得此结果的查询是什么?

4 个答案:

答案 0 :(得分:0)

您可以执行此操作-

SELECT COUNT(name) AS name_not_null_count, 
       SUM(CASE WHEN name IS NULL THEN 1 ELSE 0 END) AS name_null_count
FROM table

计算空计数的方法是:将所有空记录标记为1并取SUM

答案 1 :(得分:0)

SELECT COUNT(name) AS name_count, COUNT(marks) AS marks_count FROM table;

答案 2 :(得分:0)

count(columnname)仅计算非空值。 count(*)计算所有行。

select count(name) as names,
       count(*) - count(name) as null_names,
       count(marks) as marks,
       count(*) - count(marks) as null_marks
from tablenam

答案 3 :(得分:0)

这将选择行数,具有空值的行数和具有非空值的行数。对每个所需的列重复选择。

MOVMSKB

Num_Rows Num_Not_Null Num_Null


4 3 1