Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

count - How would I transpose SQL: Sum Case Script in Redshift and is it possible to Join to information table?

I currently have a script that counts the number of null records in each column. The script is below

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

The results come over like

|id|createddate|
|0 |    5      |

Is there a way to transpose this to get the results like

|id          | 0 |
|created date| 5|

Lastly, if this is possible would I be able to join this to the information table (SVV_TABLE_INFO)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use union all:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...