如何从2个重复行中仅删除一行?

时间:2018-05-31 16:04:56

标签: mysql sql postgresql

我在表中有2个重复的行,我想从中只删除1个并保留另一行。我可以这样做吗?

1 个答案:

答案 0 :(得分:0)

PostGres代码可能略有不同,但这是TSQL的一个例子,用CTE来做:

; WITH duplicates
AS (
    SELECT ServerName ,
           ProcessName ,
           DateCreated ,
           RowRank = ROW_NUMBER() OVER(PARTITION BY ServerName, ProcessName, DateCreated ORDER BY 1)
    FROM dbo.ErrorLog
    )
DELETE e
FROM dbo.ErrorLog e
    JOIN duplicates d
        ON d.ServerName = e.ServerName
        AND d.ProcessName = e.ProcessName
        AND d.DateCreated = e.DateCreated
        AND d.RowRank <> 1