SQL Server:DELETE重复WHERE DATE IS<日期

时间:2011-06-03 02:07:12

标签: sql sql-server tsql sql-server-2008

我有这个表结构:

UNIQUE ID | TYPE | QUARTER | DATE
1         |    1 |     JAN | 2/2/2222
2         |    1 |     JAN | 4/2/2222

如何删除旧日期的重复记录?

1 个答案:

答案 0 :(得分:3)

在MS SQL 2008中:

with cte
as
(
    select *, row_number() over(partition by Quarter order by Date) RowNumber
    from MyTable
)
delete cte
where RowNumber > 1

在任何sql中

delete
from MyTable
    where Date not in
    (
        select Quarter, Min(DATE)
        from MyTable
        group by Quarter
    )

假设没有重复的日期字段。