Reputation: 215
I have the following example table:
|| *Column 1* || *Column 2* || *Column 3* || *Column 4* ||
|| a || null || b || a ||
|| b || f || null || f ||
|| null || a || a || b ||
The result table has to be:
|| *Column 1* || *Column 2* ||
|| a || b ||
|| b || f ||
|| a || b ||
Thanks!
Upvotes: 1
Views: 2443
Reputation: 10996
SELECT one,
CASE WHEN one != two THEN two
WHEN one != three THEN three
WHEN one != four THEN four END AS two
FROM ( SELECT COALESCE(col1, col2, col3, col4) AS one,
COALESCE(col2, col3, col4, col1) AS two,
COALESCE(col3, col4, col1, col2) AS three,
COALESCE(col4, col1, col2, col3) AS four
FROM four_columns) AS h
Can't think of a simplier solution considering what result you wish in the example.
This obviously obsoletes cases where these 4 columns have more than 2 different letters, due to only 2 columns existing. But it won't show duplicates and will not show NULL
if there is at least 2 unique letters.
Upvotes: 4