删除重复的记录,但保留具有最小ID的记录

时间:2019-03-28 12:56:12

标签: sql sql-server duplicates

我要删除的表中有一些重复的记录,但将表中的最小ID记录保留为唯一记录。

返回我要保留的最小ID记录的查询如下:

select 

    MIN(ID) AS ID
,   Date
,   Portfolio
,   Instrument
,   Dimension
,   COUNT(ID) 

from mytable
group by date, Portfolio, Instrument, Dimension
having COUNT(ID) > 1
order by date, Portfolio, Instrument, Dimension

但是,我不知道如何编写查询,以确保删除上面MIN(ID)记录以外的所有重复项。我猜想它必须是一个子查询,针对除上述以外所有重复的ID?

2 个答案:

答案 0 :(得分:2)

可更新的CTE和row_number()

with todelete as (
      select t.*,
             row_number() over (partition by date, Portfolio, Instrument, Dimension order by id) as seqnum
      from mytable t
     )
delete from todelete
    where seqnum > 1;

CTE会基于四列枚举表中的行,其中最小的id的值为1。外部where子句删除所有带有枚举值> 1的列。

答案 1 :(得分:1)

使用row_number()

delete t
from (select t.*,
             row_number() over (partition by date, Portfolio, Instrument, Dimension order by id) as seq
      from mytable
     ) t
where seq > 1;

通过这种方式,您将仅获得具有最小ID的记录。