简体   繁体   English

SQL 查询表的每一列中非 null 值的计数?

[英]SQL query to find the count of non null values in each column of table?

I can find the count of non null values by typing each column name, but is there a way to write it without manually typing the column names as I have 100+ columns in my table.我可以通过输入每个列名来找到非 null 值的计数,但是有没有办法在不手动输入列名的情况下编写它,因为我的表中有 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

You can use a case operation here您可以在此处使用案例操作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM