首页 > 解决方案 > 如何根据条件获取列名

问题描述

我有以下查询检查列是否为空

select 
    sum(case when Column_1 is null then 1 else 0 end) as Column_1, 
    sum(case when Column_2 is null then 1 else 0 end) as Column_2, 
    sum(case when Column_3 is null then 1 else 0 end) as Column_3,
from TestTable 

它给:

Column_1  Column_2  Column_3
0         1         0

我想获取具有空值的列名所以我想要的输出是:

Column_1
Column_3

我怎样才能在 Presto 中做到这一点?从查询返回的输出列名似乎并不容易。

标签: sqlgroup-bypivotprestounpivot

解决方案


一种方法是:

select (case when count(column_1) <> count(*) then 'Column_1;' else '' end) ||
       (case when count(column_2) <> count(*) then 'Column_2;' else '' end) ||
       (case when count(column_3) <> count(*) then 'Column_3;' else '' end)
from TestTable  ;

推荐阅读