John Thomas
John Thomas

Reputation: 1105

How to change all non-null values in a column to the column name in SQL?

So let's say we have the following:

ID   data1  data2  data3
001   carl   NULL   NULL
002   NULL   rick   NULL
003   NULL  mitch   NULL
004   NULL   NULL   NULL

All I want to do is make every non-null value in the column name. Something like this in Snowflake.

ID   data1  data2  data3
001  data1   NULL   NULL
002   NULL  data2   NULL
003   NULL  data2   NULL
004   NULL   NULL   NULL

I have distinct rows as ID as well as a few columns I dont want this applied to. Any ideas how do tackle this in SQL?

Upvotes: 0

Views: 114

Answers (1)

Z .
Z .

Reputation: 12837

select id,
   case when data1 is not null then 'data1' else null end as data1,
...

Upvotes: 2

Related Questions