Skip to content

Instantly share code, notes, and snippets.

@jasonicarter
Last active December 31, 2015 20:59
Show Gist options
  • Save jasonicarter/8043870 to your computer and use it in GitHub Desktop.
Save jasonicarter/8043870 to your computer and use it in GitHub Desktop.
Delete duplicate records from table ( sql server )
--columnField1 is the field you're looking for duplicates in
--columnField2 orderby, optional, for example a datetime stamp and you want to keep the latest record
WITH AllRecords_CTE
AS
(
SELECT columnField1,
ROW_NUMBER() over (PARTITION BY columnField1 ORDER BY columnField2 DESC) AS [rn]
FROM #AllRecords
)
DELETE AllRecords_CTE
WHERE [rn] > 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment