Thursday, August 25, 2005

Finding duplicates using SQL

Heres a query that helps you find the duplicates and updates all duplicate records with error description as duplicate record leaving one record
If the primary key id is a integer based identity column, if not create a temp table with same structure and additional identity column, and use the identity column instead of primary key id

UPDATE TableName SET ErrorDescription = 'Duplicate record' WHERE PrimaryKeyId > (SELECT MIN(PrimaryKeyId) FROM TableName CheckDuplicate WHERE (CheckDuplicate.Code = TableName.Code OR CheckDuplicate.Name = TableName.Name)) AND ErrorDescription IS NULL

If records were

PrimaryKeyIdCodeNameErrorDescription
1CD1Name 1
2CD2Name 2
3CD1Name 3
4CD4Name 1



They become

PrimaryKeyIdCodeNameErrorDescription
1CD1Name 1
2CD2Name 2
3CD1Name 3Duplicate record
4CD4Name 1Duplicate record

No comments: