Skip to content

Instantly share code, notes, and snippets.

@bdcravens
Created December 19, 2012 17:49
Show Gist options
  • Save bdcravens/4338742 to your computer and use it in GitHub Desktop.
Save bdcravens/4338742 to your computer and use it in GitHub Desktop.
SQL Server: DELETE duplicates in table, keeping oldest record
; 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