Created
December 19, 2012 17:49
-
-
Save bdcravens/4338742 to your computer and use it in GitHub Desktop.
SQL Server: DELETE duplicates in table, keeping oldest record
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
; with cte | |
as (select *, ROW_NUMBER() over (PARTITION by invoiceNumber order by created desc) rn | |
from invoices | |
where accountId in (select accountId from accounts where carrierId=2)) | |
delete from cte where rn<>1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment