How to Delete Duplicate record from the sql table except one from duplicate records? [duplicate]
Possible Duplicate:
How to remove duplicate records in a table?
I'm having one table which contains one of the column with ProjectID which has duplicate records in the table. And table having Primary key column. I want to keep one record & delete the rest duplications. Following query is to find the total number of duplicate records with the no. of occurrences-
SELECT ProjectID,
COUNT(ProjectID) AS NumOccurrences
FROM MyTable
GROUP BY ProjectID
HAVING ( COUNT(ProjectID) > 1 )
How to do this?
Thanks.
;with cte as
(
select row_number() over(partition by ProjectID order by ProjectID) as rn
from MyTable
)
delete cte
where rn > 1
精彩评论