Technical Article

Delete Duplicates

,

Deletes the duplicates from a table based upon any fields in the table leaving a single copy of the record in the table.

ALTER TABLE TableWithDuplicates ADD EliminateDuplicates_RowID int NOT NULL IDENTITY(1, 1)

DELETE 
FROM t1
FROMTableWithDuplicates t1
JOIN(
SELECTEliminateDuplicates_RowID=MAX(EliminateDuplicates_RowID),
Field1,
Field2,
Field3,
Field4
FROMTableWithDuplicates
GROUP BY Field1,
Field2,
Field3,
Field4
HAVINGCOUNT(*) >= 2
) t2 
ON(t1.EliminateDuplicates_RowID <> t2.EliminateDuplicates_RowID
ANDt1.Field1 = t2.Field1
ANDt1.Field2 = t2.Field2
ANDt1.Field3 = t2.Field3
ANDt1.Field4 = t2.Field4)

ALTER TABLE TableWithDuplicates DROP COLUMN EliminateDuplicates_RowID

Rate

5 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (1)

You rated this post out of 5. Change rating