Table 1 Table 2 Number | Code Code | Description 1234 A A Something 1235 B C Something else 1246 C D Something other 1247 A 1248 B 1249 A
I would like to find the distinct Code
values and get a return like this:
1 | 2 ------- A A B C C D
I can’t figure out how to write a SQL query that would return me the above results. Anyone have any experience with a query like this or similar?
Advertisement
Answer
In proper RDBMS:
SELECT T1.Code, T2.Code FROM (SELECT DISTINCT Code FROM Table1) T1 FULL OUTER JOIN (SELECT DISTINCT Code FROM Table2) T2 ON T1.Code = T2.Code
In MySQL… the UNION removes duplicates
SELECT T1.Code, T2.Code FROM Table1 T1 LEFT OUTER JOIN Table2 T2 ON T1.Code = T2.Code UNION SELECT T1.Code, T2.Code FROM Table1 T1 RIGHT OUTER JOIN Table2 T2 ON T1.Code = T2.Code