Skip to content

Instantly share code, notes, and snippets.

@jasonicarter
Last active December 31, 2015 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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