首页 > 解决方案 > SQL查询查找表的每一列中非空值的计数?

问题描述

我可以通过输入每个列名来找到非空值的计数,但是有没有办法在不手动输入列名的情况下编写它,因为我的表中有 100 多个列。

select 'col1Name', count(col1Name) from table where col1Name is null
union
select 'col2Name', count(col2Name) from table where col2Name is null
union ...
select 'col20Name', count(col20Name) from table where col20Name is null

标签: sqlapache-spark-sql

解决方案


您可以在此处使用案例操作

select 
  sum(case when a is null then 1 else 0 end) A,
  sum(case when b is null then 1 else 0 end) B,
  sum(case when c is null then 1 else 0 end) C
from T

推荐阅读