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 2008 Forums
 Other SQL Server 2008 Topics
 Compare 2 records in Same Tables

Author  Topic 

leyenda
Starting Member

3 Posts

Posted - 2008-07-01 : 05:15:42
Hi,
Normally i will have 2 records in the same table and would like to compare these 2 records to see if there is any fields that is mismatch:

e.g.

VoterID SurveyId VoteDate VoteEnd Visit DataEntry
14 21 7/1/2008 2:39:02 PM 7/1/2008 2:34:59 PM ME-BAN8VMD 1
15 21 7/1/2008 3:43:09 PM 7/1/2008 3:38:06 PM ME-BAN8VMD 2

What is the query to use to check these records to pull out mismatch fields?
Both records will have the same Visit(ME-BAN8VMD)

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-07-01 : 05:18:45
what is the primary key to the table ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

leyenda
Starting Member

3 Posts

Posted - 2008-07-01 : 05:32:51
Primary Key is VoterID
Go to Top of Page

leyenda
Starting Member

3 Posts

Posted - 2008-07-01 : 05:33:40
the result should show that VoteDate ,VoteEnd and DataEntry records is mismatch.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-07-01 : 05:40:33
[code]
select *
from table t1 inner join table t2
on t1.Visit = t2.Visit
and t1.VoterID <> t2.VoterID
where t1.VoteDate <> t2.VoteDate
or t1.VoteEnd <> t2.VoteEnd
or t1.DataEntry <> t2.DataEntry
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -