0

I have two tables of data that I need to join into a single table. UNION ALL doesn't work because I need to have a single row (SN) with all associated info and not duplicate SNs. Examples of the two tables and the expected result are below. Any help would be appreciated.

Table 1
SN  Reading
 1. 12
 2. 14
 3. 12
 4. 15
 5. 11

Table 2 
SN  Reading
 1. 75 
 2. 74
 4. 74
 5. 74
 6. 75

Joined Table (1 and 2)

SN  R1    R2
 1. 12    75
 2. 14    74
 3. 12
 4. 15    74
 5. 11    74
 6.       75
potashin
  • 44,205
  • 11
  • 83
  • 107

1 Answers1

2

You can use FULL JOIN to join tables in the desired way and COALESCE to get first non-null SN from both tables :

SELECT COALESCE(t1.SN, t2.SN)
     , t1.R1
     , t2.R2 
FROM table1 t1 FULL JOIN table2 t2 ON t1.SN = t2.SN
potashin
  • 44,205
  • 11
  • 83
  • 107