x
login about faq Site discussion (meta-askssc)

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?

more ▼

asked Feb 02 '10 at 12:37 AM in Default

InwoodGuy gravatar image

InwoodGuy
44 6 7 8

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first
;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
more ▼

answered Feb 02 '10 at 01:43 AM

Scot Hauder gravatar image

Scot Hauder
5.7k 13 15 18

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x1835
x321
x100

asked: Feb 02 '10 at 12:37 AM

Seen: 661 times

Last Updated: Feb 02 '10 at 10:23 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.