SQL Server 批量删除重复记录
Posted 小蚂蚁_CrkRes
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL Server 批量删除重复记录相关的知识,希望对你有一定的参考价值。
分组编号
--按UserID,CourseCode,SubjectCode分组排序
SELECT *,ROW_NUMBER() OVER(PARTITION BY UserID,CourseCode,SubjectCode ORDER BY ID DESC) AS RowNo
FROM dbo.T_Corse;
获取重复记录
--重复记录查询
SELECT * FROM (
SELECT *,ROW_NUMBER() OVER(PARTITION BY UserID,CourseCode,SubjectCode ORDER BY ID DESC) AS RowNo
FROM dbo.T_Corse
)Ranked WHERE Ranked.RowNo>1;
删除重复记录
--删除重复记录(保留最新记录)
DELETE T_Corse
WHERE id IN(
SELECT ID FROM(
SELECT *,ROW_NUMBER() OVER(PARTITION BY UserID,CourseCode,SubjectCode ORDER BY ID DESC) AS RowNo
FROM dbo.T_Corse
)Ranked WHERE Ranked.RowNo>1
);
以上是关于SQL Server 批量删除重复记录的主要内容,如果未能解决你的问题,请参考以下文章