简体   繁体   中英

Renaming columns in Snowflake SQL while using Common Table Expressions

I have data in Snowflake in following format;

ID |    1     |  2  
123|2019-11-30|2019-12-01
234|2019-11-29|2019-12-12

Is it possible to get data in following format;

ID|Click1|Click2
123|2019-11-30|2019-12-01
234|2019-11-29|2019-12-12

I tried to use the following code;

... ,col_rename AS( SELECT member_sk
                   ,1 AS "click1"
                   ,2 AS "click2"

FROM pivoted
ORDER BY ID)

But I m getting following output;

ID|Click1|Click2
123|1|2
234|1|2

Am not sure why are the date values disappearing? Can I kindly get some help.thanks in advance.

The date values are disappearing because you are selecting the constants 1 and 2 .

For these to be recognized as column names, try double quotes:

select member_sk, "1" as click1, "2" as click2
from pivoted;

Better yet, qualify the names so the meaning is quite clear:

select p.member_sk, p."1" as click1, p."2" as click2
from pivoted p;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM