我在表格中有3列:
v_type1|v_type2|v_type3
这些列可以包含NULL。我需要的是计算3列中每一列的NULL和NOT NULL值的数量。结果必须是一行(实际上有6列):
v_type1_null_counts|v_type1_notnull_counts|v_type2_null_counts|v_type2_notnull_counts|v_type3_null_counts|v_type3_notnull_counts
我尝试加入但未完成结果。 我需要做什么?
答案 0 :(得分:3)
select c1 as v_type1_notnull_cnt,
total - c1 as v_type1_null_cnt,
c2 as v_type2_notnull_cnt,
total - c2 as v_type2_null_cnt,
c3 as v_type3_notnull_cnt ,
total - c3 as v_type3_null_cnt
from (
select count(*) as total,
count(v_type1) as c1,
count(v_type2) as c2,
count(v_type3) as c3
FROM your_table
) t
答案 1 :(得分:2)
count()
计算非NULL值。所以最简单的方法是:
select count(v_type1) as v_type1_not_null,
count(v_type2) as v_type2_not_null,
count(v_type3) as v_type3_not_null,
(count(*) - count(v_type1)) as v_type1_null,
(count(*) - count(v_type2)) as v_type2_null,
(count(*) - count(v_type3)) as v_type3_null
from t;