?
Solved

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

Posted on 2006-10-31
4
Medium Priority
?
8,240 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 2000 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

770 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