SamScib
Technical User
- Jun 18, 2013
- 6
I am working with multiple tables (6 at this point, but could be more in the future). Each table represents a separate facility and has a list of part numbers and countries of origin (COO). Each table has a different number of parts and some parts are on some tables but not others. What I would like to do is illustrated simply below, but with multiple tables:
Table 1
Part COO
1A1 US
1A2 MX
1B1 MX
1B2 MX
1C1 US
1C2 US
1C3 MX
1C4 MX
Table 2
Part COO
1A1 US
1A3 US
1B1 MX
1B3 US
1C1 MX
1C2 US
1C4 MX
1C5 MX
Merge the two tables with all possibilities of parts, leaving the parts' COO blank where it doesn't exist on Table 1 or 2:
Table 3
Part T1_COO T2_COO
1A1 US US
1A2 MX
1A3 US
1B1 MX MX
1B2 MX
1B3 US
1C1 US MX
1C2 US US
1C3 MX
1C4 MX MX
1C5 MX
I have tried this using multiple queries and combinations of left joins and outer joins and unions, but the whole thing seems very manual and I'm still having issues.
Is there a way to complete this type of merge with multiple tables all in one query in SQL?
Any help is much appreciated..
Thanks,
Sam
Table 1
Part COO
1A1 US
1A2 MX
1B1 MX
1B2 MX
1C1 US
1C2 US
1C3 MX
1C4 MX
Table 2
Part COO
1A1 US
1A3 US
1B1 MX
1B3 US
1C1 MX
1C2 US
1C4 MX
1C5 MX
Merge the two tables with all possibilities of parts, leaving the parts' COO blank where it doesn't exist on Table 1 or 2:
Table 3
Part T1_COO T2_COO
1A1 US US
1A2 MX
1A3 US
1B1 MX MX
1B2 MX
1B3 US
1C1 US MX
1C2 US US
1C3 MX
1C4 MX MX
1C5 MX
I have tried this using multiple queries and combinations of left joins and outer joins and unions, but the whole thing seems very manual and I'm still having issues.
Is there a way to complete this type of merge with multiple tables all in one query in SQL?
Any help is much appreciated..
Thanks,
Sam