Solved

Compare two tables in SQL server 2005 and get whats not common

Posted on 2006-10-31
4
8,221 Views
Last Modified: 2011-08-18
I have two almost similiar tables in sql server. I want to run a query to compare those two tables and to find out those rows that are not common. here are the columns for the two tables

Table 1              

zipcode,
name,
address1,
state,
custom1,
custom4,
city,
tel no

Table 2

zipcode,
name,
address1,
state,
custom1,
custom4,
city,
tel no

in one row everything could be similiar except the tel no, in some other row everything could be similiar except the zip code. how do i run a query to figure out which row is not similiar.

0
Comment
Question by:pratikshahse
4 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 17843896
SELECT * FROM Table1 T1 WHERE NOT EXISTS(SELECT 1 FROM Table2 WHERE ZipCode = T1.ZipCode AND Name = T1.Name AND Address1=T1.Address1 AND State = t1.State and Custom1 = t1.Custom1 and custom4 = T1.Custome4 and City = t1.city and telNo = t1.telNo )
UNION ALL
SELECT * FROM Table2 T1 WHERE NOT EXISTS(SELECT 1 FROM  Table1  WHERE ZipCode = T1.ZipCode AND Name = T1.Name AND Address1=T1.Address1 AND State = t1.State and Custom1 = t1.Custom1 and custom4 = T1.Custome4 and City = t1.city and telNo = t1.telNo )
0
 
LVL 31

Expert Comment

by:awking00
ID: 17845500
To get all not common records
select * from table1
minus
select * from table2
union
select * from table2
minus
select * from table1;

and to identify them -
select 'Table1', t1.* from table1 t1
minus
select 'Table1', t2.* from table2 t2
union
select 'Table2', t3.* from table2 t3
minus
select 'Table2', t4.* from table1 t4;
0
 
LVL 3

Expert Comment

by:jay_gadhavi
ID: 17848766
In your table it shoud be one unique id field (empid or anything u want)....I used the unique id for the comparision.
Use this sql statement :

SELECT     t1.empid,t1.zipcode, t1.telno, t1.name, t2.name AS table2_name, t2.phoneno AS table2_phoneno, t2.zipcode                      
FROM         dbo.table1 t1 INNER JOIN
dbo.Table2 t2 ON t1.empid = t2.empid AND t1.telno <> t2.telno
union
SELECT     t1.empid,t1.zipcode, t1.phoneno, t1.name, t2.name AS table2_name, t2.phoneno AS table2_phoneno, t2.zipcode
FROM         dbo.table1 t1 INNER JOIN
dbo.Table2 t2 ON t1.empid = t2.empid AND t1.zipcode <> t2.zipcode

(Note : After run this sql statement i got all the rows which telno and zipcode is different )



      

0
 
LVL 5

Expert Comment

by:prajapati84
ID: 17849231
If u want to get the records where only zipcode and tel_no are not similar and other field are similar then try the first query. But If you want the records where every column must be checked whether they are similar or not, you must be having a common field like ID or anything else to join both tables. I have used here ID column to join both tables, u need to replace ur common field with ID. For this solution, try the second query.

First Query:
(select t1.*,t2.* from table1 t1,table2 t2 where t1.name=t2.name and t1.address1=t2.address1 and t1.state=t2.state and t1.custom1=t2.custom1 and t1.custom4=t2.custom4 and t1.custom4=t2.custom4 and t1.zipcode=t2.zipcode and
t1.tel_no<>t2.tel_no) union (select t1.*,t2.* from table1 t1,table2 t2 where t1.name=t2.name and t1.address1=t2.address1 and t1.state=t2.state and t1.custom1=t2.custom1 and t1.custom4=t2.custom4 and t1.custom4=t2.custom4 and t1.zipcode<>t2.zipcode and t1.tel_no=t2.tel_no)

Second Query:
(select t1.*,t2.* from table1 t1,table2 t2 where t1.id=t2.id and t1.telno<>t2.telno)
union (select t1.*,t2.* from table1 t1,table2 t2 where t1.id=t2.id and t1.zipcode<>t2.zipcode)

Regards,
Mukesh
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction: Often, when running a query with joins, the results show up "duplicates", and often, those duplicates can be "eliminated" in the results using DISTINCT, for example. Using DISTINCT is simple: just add it after the SELECT keyword, an…
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now