Skip to content
Advertisement

SQL Server : Remove Duplicate Records by Great Than ContactID

Have been working with the following query to help me list and find duplicates within my table.

This returns just what I am searching for in the following:

Now, I want to delete the record that has the GREATER ContactID than the other duplicate record.

That is where I am stuck. I have scoured the search results but have not been able to find anything that fits this puzzle.

Thoughts?

Advertisement

Answer

You could use row_number() and a CTE. I think that this does what you want:

The query deletes records for which another record exists with the same PhoneNumber and Status and a smallest ContactID.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement