Last active
December 31, 2015 20:59
-
-
Save jasonicarter/8043870 to your computer and use it in GitHub Desktop.
Delete duplicate records from table ( sql server )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--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