Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 row comparasion

Author  Topic 

gangadhara.ms
Aged Yak Warrior

549 Posts

Posted - 2011-06-14 : 01:00:03
Dear All,
I have 3 tables having some 10 fields of each i need to compare the each column with another 4th table to check whether these records are exist or not in these 3 tables.

We don't have any primary key as in the 4th table to match to make join.

Let me know how do we do this,


Thanks,
Gangadhar


Thanks,
Gangadhara MS
SQL Developer and DBA

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-06-14 : 01:14:15
You'll need to post sample data and expected result set as your post is way too confusing to get started.

If there is no linking data for the 4th table, I don't see how we can do this though. But sample data will help make this clear for us.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

fnirp
Starting Member

2 Posts

Posted - 2011-06-16 : 09:13:33
If the tables has identical columns, I probably would do something like this to compare two tables with four columns. The resultset is all the rows that don't match.

select col1, col2, col3, col4, count(*) as c1, 'tablename' as t1 into #tmp
from table_name
group by col1, col2, col3, col4

insert #tmp
select col1, col2, col3, col4, count(*) as c1, 'mastertable' as t1
from master_table
group by col1, col2, col3, col4


select col1, col2, col3, col4, c1, t1
from #tmp t1
join
(select col1, col2, col3, col4, c1
from #tmp
group by col1, col2, col3, col4, c1
having count(*) <> 2) t2 on t1.col1 = t2.col1 and t1.col2 = t2.col2 and t1.col3 = t2.col3 and t1.col4 = t2.col4
order by col1, col2, col3, col4, c1

//Fnirp
Go to Top of Page
   

- Advertisement -