andy_111 (2/1/2016)
Daniel Matthee (1/31/2016)
I have not tested but just by looking at the code will it not delete both the records???There is nothing to indicate that you just want to get rid of all dups and only have a unique records left, or am I missing something here?
Regards
For example, you have table Customers (contactname VARCHAR(100) NOT NULL, Age INT)
You fill table
Insert into Customers (contactname, Age) VALUES ('Andrey',35),('Alexey', '40'),('Andrey',50);
Query
SELECT ROW_NUMBER() Over(PARTITION BY C.contactname ORDER BY C.contactname)
As RowNumber,C.* FROM Customers C
returns
1 Alexey 40
1 Andrey 35
2 Andrey 50
RowNumber partitioned inside contactname and it unique inside every contactname. If you delete RowNumber > 1 you delete duplicates.
Yes, code in this post will delete all the records. Thats why using old method for deleting duplicates (using group by) is 3 steps. First copy all found duplicate records (one of them) to temp table. Then delete like in post. Then insert to original table from the temp table.
Using CTE, ronumber() and delete where rowNum >1 from CTE is better by all means 🙂