Nirpeksh
Nirpeksh

Reputation: 99

How to update a Column from a Bunch of other columns

I have a Table A where i column 1 Column 2 Column 3 Column 4 and Column 5.

Column 1,2,3,4 already have data and we need to update Column 5 based on that data and on priority .

Column 1 has Priority 5 , Col 2 has Priority 4 ,Col 3 has priority 3 and Col 4 has priority 2.

So if a particular row has all the column , then it should pick up Col 1 since it has highest priority and update Col 5 , If a record has data only in Col 3 and 4 then it should be Col3 and update in Col 5 since 3 has higher priority than Col4 . If there is no data from Col 1-4 , col 5 should be null.

I have 24k records in my Table and i need to run this for all rows.

Any pointers for this query would he highly appreciated .

Upvotes: 0

Views: 48

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269873

I think you want coalesce() -- assuming that the columns with no values have NULL:

update t
    set col5 = coalesce(col1, col2, col3, col4);

You can also put the coalesce() in a select, if you don't want to actually change the data.

Upvotes: 2

Related Questions