首页 > 解决方案 > 获取表中所有记录都具有空值的列的列表 - SQL

问题描述

我有一个包含数百万条记录和 50 个奇数列的表,其中许多列对于表中的所有记录都有空值。如何编写一个 SQL 查询来告诉我哪些列具有所有空记录?

标签: sqlpostgresql

解决方案


如果您想知道哪些列具有所有空值,这意味着没有行具有值,则该行的计数将为 0,因此只需检查即可。

select concat_ws(',',
                 (case when count(col1) = 0 then 'col1' end),
                 (case when count(col2) = 0 then 'col2' end),
                 (case when count(col3) = 0 then 'col3' end),
                 . . .
                ) as columns_with_all_null_values
from t;

推荐阅读