--表结构,没有主键
create table tb
(id varchar2(10),
trans_flag date
);
--记录
insert into tb values('1',to_date('2009-01-01','yyyy-mm-dd'));
insert into tb values('2',to_date('2009-01-01','yyyy-mm-dd'));
insert into tb values('3',to_date('2009-01-01','yyyy-mm-dd'));
insert into tb values('1',to_date('2009-02-01','yyyy-mm-dd'));
commit;
--需求
删除id重复的记录,保留trans_flag为最大值的记录,最后结果:
id trans_flag
2 2009-01-01
3 2009-01-01
1 2009-02-01
SQL:
delete from tb
where rowid in(
select rid from
(select rowid rid,row_number() over(partition by id order by trans_flag desc) rn
from tb)
where rn>1)
where rowid in(
select rid from
(select rowid rid,row_number() over(partition by id order by trans_flag desc) rn
from tb)
where rn>1)
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/13165828/viewspace-610484/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/13165828/viewspace-610484/