Skip to content
 

Delete Duplicate Rows

The following code is useful to delete duplicate records. This is where a primary key comes in handy. Of course if you do not have one, you have to improvise. This example uses rownumber in order to create a pseudo primary key.

The Checksum function creates a single number from all the fields, then groups on that number. Please note, though highly unlikely (about 1 in 2 billion chance) the checksum function can return the same value for different row values.

SELECT
	RowNum = ROW_NUMBER() OVER(order by entryid ASC)
	,ChkSum = CHECKSUM(*)
INTO #HoldDuplicates
FROM dbo.Duplicate

DELETE
FROM #HoldDuplicates
WHERE RowNum NOT IN(
	SELECT MAX(RowNum)
	FROM #HoldDuplicates
	GROUP BY ChkSum
)

SELECT *
FROM #HoldDuplicates

Related Posts: