开发者

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

开发者 https://www.devze.com 2023-03-19 08:18 出处:网络
This question already has answers here: 开发者_运维技巧Closed 11 years ago. Possible Duplicate: How to remove duplicate records in a table?
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

精彩评论

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