开发者

How to Delete Duplicate record from the sql table except one from duplicate records? [duplicate]

This question already has answers here: 开发者_运维技巧 Closed 11 years ago.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜