我有这个表结构:
UNIQUE ID | TYPE | QUARTER | DATE
1 | 1 | JAN | 2/2/2222
2 | 1 | JAN | 4/2/2222
如何删除旧日期的重复记录?
答案 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
)
假设没有重复的日期字段。