首页 > 解决方案 > 我将如何转置 SQL:Redshift 中的 Sum Case 脚本,是否可以加入信息表?

问题描述

我目前有一个脚本来计算每列中空记录的数量。脚本如下

select  sum(case when id is null then 1 else 0 end)id , sum(case when createddate is null then 1 else 0 end)createddate from schema_name.Table_name

结果过来就像

|id|createddate|
|0 |    5      |

有没有办法将其转置以获得类似的结果

|id          | 0 |
|created date| 5|

最后,如果可能的话,我可以将其加入信息表(SVV_TABLE_INFO)

标签: sqlcountpivotamazon-redshiftaggregate-functions

解决方案


您可以使用union all

select 'id', count(*)
from t
where id is null
union all
select 'createddate', count(*)
from t
where createddate is null;

推荐阅读