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
 Query Duplicate Fields

Author  Topic 

runnerpaul
Starting Member

24 Posts

Posted - 2008-08-05 : 11:16:17
If I have a table with fields A, B, C and D. Can anybody tell me how to do a query that will produce any records that have buplicate fleids A, C and D or duplicate fields B, C and B?

Cheers
Paul

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-05 : 14:04:49
[code]SELECT t.*
FROM YourTable t
LEFT JOIN (SELECT A,C,D
FROM YourTable
GROUP BY A,C,D
HAVING COUNT(*) >1 )t1
ON t1.A = t.A
AND t1.C=t.C
AND t1.D=t.D
LEFT JOIN (SELECT B,C,D
FROM YourTable
GROUP BY B,C,D
HAVING COUNT(*) >1 )t2
ON t2.B = t.B
AND t2.C=t.C
AND t2.D=t.D
WHERE t1.A IS NOT NULL
OR t2.B IS NOT NULL[/code]
Go to Top of Page

runnerpaul
Starting Member

24 Posts

Posted - 2008-08-06 : 04:56:08
Cheers visakh16. That was a big help.
Go to Top of Page

runnerpaul
Starting Member

24 Posts

Posted - 2008-08-06 : 05:52:20
Would you then know how I could delete these duplicate records?

Cheers
Paul
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-07 : 13:54:55
Just modify the above query with delete
Go to Top of Page
   

- Advertisement -