question

InwoodGuy avatar image
InwoodGuy asked

Match and Unmatach Records

I have 3 sets of Data from 3 Database sources. My question is how will I find which records match and which did not? its okay to have Nulls in Master table

Table 1:

ID  Name


1   AAA

2   BBB

3   CCC

4   DDD

5   EEE

6   FFF

Table 2:

ID  City

1   NYC

2   LA

3   SFA

7   ATL

8   HTOWN

100 SJC

Table 3:

ID  SSN

3   1111

4   1112

7   1113

5   1114

100 1115

6   1116

Master Table should look something like this (Its OKAY TO HAVE NULLS)

ID  Name	City	SSN

1   AAA	NYC	NULL

2   	

3   	

4   

5

6   

7   

8

100 NULL	NULL	1115

YOUR HELP WILL BE GREATLY APPRECIATED?

sql-server-2005queryjoins
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Scot Hauder avatar image
Scot Hauder answered
;WITH cte AS(
SELECT ID, Name, NULL [City], NULL [SSN] FROM tab1
UNION ALL SELECT ID, NULL, City, NULL FROM tab2
UNION ALL SELECT ID, NULL, NULL, SSN FROM tab3
)
SELECT ID, MAX(cte.Name)[Name], MAX(cte.City)[City], MAX(cte.SSN)[SSN]
FROM cte
GROUP BY ID
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.