x

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, 2010 at 12:37 AM in Default

InwoodGuy gravatar image

InwoodGuy
44 7 8 8

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

1 answer: sort newest
 ;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, 2010 at 01:43 AM

Scot Hauder gravatar image

Scot Hauder
6k 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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

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

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1945
x369
x116

asked: Feb 02, 2010 at 12:37 AM

Seen: 1086 times

Last Updated: Feb 02, 2010 at 10:23 AM