如何根据条件获取列名

How to get column name based on condition

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

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 中做到这一点?从查询返回的输出列名似乎并不容易。

一种方法是:

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  ;

我知道您希望结果在单独的行中而不是在串联的字符串中。

如果是这样,您可以使用 unnest() 和数组对现有结果集进行反透视;

select t2.key
from (
    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 
) t1
cross join unnest(
    array['Column1', 'Column_2', 'Column_3'],
    array[Column1, Column_2, Column_3]
) t2 (key, value)
where t2.value = 0