How do I delete duplicate rows in SQL Server using the OVER clause?(如何使用 OVER 子句删除 SQL Server 中的重复行?)
问题描述
这是我表中的列:
Id
EmployeeId
IncidentRecordedById
DateOfIncident
Comments
TypeId
Description
IsAttenIncident
我想删除 EmployeeId、DateOfIncident、TypeId
和 Description
相同的重复行 - 只是为了澄清 - 我确实想保留其中之一.我想我应该将 OVER
子句与 PARTITION
一起使用,但我不确定.
I would like to delete duplicate rows where EmployeeId, DateOfIncident, TypeId
and Description
are the same - just to clarify - I do want to keep one of them. I think I should be using the OVER
clause with PARTITION
, but I am not sure.
谢谢
推荐答案
如果您想保留一行重复组,您可以使用 ROW_NUMBER
.在此示例中,我保留具有最低 Id
的行:
If you want to keep one row of the duplicate-groups you can use ROW_NUMBER
. In this example i keep the row with the lowest Id
:
WITH CTE AS
(
SELECT rn = ROW_NUMBER()
OVER(
PARTITION BY employeeid, dateofincident, typeid, description
ORDER BY Id ASC), *
FROM dbo.TableName
)
DELETE FROM cte
WHERE rn > 1
这篇关于如何使用 OVER 子句删除 SQL Server 中的重复行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 OVER 子句删除 SQL Server 中的重复行?


基础教程推荐
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 在多列上分布任意行 2021-01-01