piface314 piface314 - 11 days ago 8
SQL Question

Union of 2 pairs columns

Let's say I have a table in the format

a b c d
1 v 3 x
2 w 4 y
3 x 5 z
4 y 1 v


with columns a,b,c, and d. I want a union of columns a and c while keeping column pair a+b and c+d locked. I want the output to be

col1 col2
1 v
2 w
3 x
4 y
5 z


I know how to use UNION ALL to join two columns, but how do I join two columns while each of these 2 columns are paired with another column each?

Answer

You need UNION and not UNION ALL to filter duplicate pairs :

SELECT a,b FROM YourTable 
UNION
SELECT c,d FROM YourTable 
Comments