本文介紹了如何使用 OVER 子句刪除 SQL Server 中的重復(fù)行?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
這是我表中的列:
Id
EmployeeId
IncidentRecordedById
DateOfIncident
Comments
TypeId
Description
IsAttenIncident
我想刪除 EmployeeId、DateOfIncident、TypeId
和 Description
相同的重復(fù)行 - 只是為了澄清 - 我確實(shí)想保留其中之一.我想我應(yīng)該將 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.
謝謝
推薦答案
如果您想保留一行重復(fù)組,您可以使用 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
這篇關(guān)于如何使用 OVER 子句刪除 SQL Server 中的重復(fù)行?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!