Solved

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

Posted on 2006-10-31
4
8,235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 32

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

717 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