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 中的重复行?
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- SQL 临时表问题 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01