Basically I have a table that has similar format to the below table.
What I want to do is update Col4 based on this logic
For example given this table:
| Col1 | Col2 | Col3 | Col4 |
-----------------------------
| 1 | 2 | A1 | 2 |
-----------------------------
| 2 | 3 | A2 | 3 |
-----------------------------
| 3 |{null}| A3 |{null}|
Update it to be this table
| Col1 | Col2 | Col3 | Col4 |
-----------------------------
| 1 | 2 | A1 | A2 |
-----------------------------
| 2 | 3 | A2 | A3 |
-----------------------------
| 3 |{null}| A3 | A3 |
Any direction would be greatly appreciated!
Something like this should work (untested):
UPDATE table
SET col4 = CASE WHEN table.col2 IS NULL THEN table.col3 ELSE col2Matches.col3 END
FROM table
INNER JOIN table AS col2Matches
ON table.col2 = col2Matches.col1
this should let you test it:
SELECT CASE WHEN table.col2 IS NULL THEN table.col3 ELSE col2Matches.col3 END
FROM table
INNER JOIN table AS col2Matches
ON table.col2 = col2Matches.col1
Hope this helps,
Pete
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With