本文介紹了如何使用 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 中的重復行?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!