Groups | Blog | Home
all groups > sql server data mining > july 2005 >

sql server data mining : query that compares tables


J
7/14/2005 1:15:05 PM
I’m trying to find orphaned records that resulted when a new table was
created to normalize data but unnecessary records were added in the process.
I have 2 related tables, let's call them tblA and tblB, that I need to
compare.
I need to first know where tblA.Name = tblB.Name then from that subset I
need to know where tblA.ID(pk) does not match tblB.A_ID(fk).
Sample Data:
TblA.ID tblA.Name
1 ABC001
2 ABC321
3 ABC456

TblB.ID tblB.Name tblB.A_ID
1 DEF901 4
2 ABC001 1
3 ABC456 3

so my query would results would be:
TblB.ID tblB.Name tblB.A_ID
2 ABC001 1
3 ABC456 3

J
7/14/2005 1:19:02 PM
Sorry my example should have looked like:

Sample Data:
TblA.ID tblA.Name
1 ABC001
2 ABC321
3 ABC456

TblB.ID tblB.Name tblB.A_ID
1 DEF901 4
2 ABC001 1
3 ABC456 2

so my query would results would be:
TblB.ID tblB.Name tblB.A_ID
3 ABC456 2


[quoted text, click to view]
Michael C#
7/15/2005 12:00:00 AM
Ugggh. OK, here's a shot at it:

SELECT b.ID, b.Name, b.A_ID
FROM tblA a, tblB b
WHERE a.Name = b.Name
AND a.ID <> b.A_ID


[quoted text, click to view]

Raman Iyer [MS]
7/15/2005 11:44:34 AM
[forwarding to a more appropriate newsgroup for this SQL question]

--
-Raman Iyer
SQL Server Data Mining

[quoted text, click to view]

Gopalan
8/3/2005 5:49:05 AM
First we need to
Select * from tbla ,tblb where
tbla.name = tblb.name and tbla.id no in(
select id from tblb) I believe that this query works for you

[quoted text, click to view]
AddThis Social Bookmark Button