delete DQ from DQ as a where exists(select 1 from DQ where A=a.A and B>a.B)--保留最大的一條
create table dq (A number,B date); insert into dq values(001,to_date('2001-1-1','yyyy-mm-dd')); insert into dq values(001,to_date('2005-1-1','yyyy-mm-dd')); insert into dq values(002,to_date('2005-1-1','yyyy-mm-dd')); insert into dq values(004,to_date('2003-1-1','yyyy-mm-dd')); insert into dq values(004,to_date('2009-1-1','yyyy-mm-dd')); delete from DQ a where exists(select 1 from DQ where A=a.A and B<a.B); drop table dq;
刚发的有些错drop table dq; create table dq (A number,B date); insert into dq values(001,to_date('2001-1-1','yyyy-mm-dd')); insert into dq values(001,to_date('2005-1-1','yyyy-mm-dd')); insert into dq values(002,to_date('2005-1-1','yyyy-mm-dd')); insert into dq values(004,to_date('2003-1-1','yyyy-mm-dd')); insert into dq values(004,to_date('2009-1-1','yyyy-mm-dd')); delete from DQ a where exists (select A from DQ where A=a.A and B<a.B group by A having count(A)>0);
上面还是错,这次真没错了drop table dq; create table dq (A number,B date); insert into dq values(001,to_date('2001-1-1','yyyy-mm-dd')); insert into dq values(001,to_date('2005-1-1','yyyy-mm-dd')); insert into dq values(002,to_date('2005-1-1','yyyy-mm-dd')); insert into dq values(004,to_date('2003-1-1','yyyy-mm-dd')); insert into dq values(004,to_date('2009-1-1','yyyy-mm-dd')); delete from DQ a where not exists (select 1 from DQ where A=a.A and B>a.B) and (select count(1) from DQ where A=a.A) <>1;
create table dq (A number,B date);
insert into dq values(001,to_date('2001-1-1','yyyy-mm-dd'));
insert into dq values(001,to_date('2005-1-1','yyyy-mm-dd'));
insert into dq values(002,to_date('2005-1-1','yyyy-mm-dd'));
insert into dq values(004,to_date('2003-1-1','yyyy-mm-dd'));
insert into dq values(004,to_date('2009-1-1','yyyy-mm-dd'));
delete from DQ a where exists(select 1 from DQ where A=a.A and B<a.B);
drop table dq;
create table dq (A number,B date);
insert into dq values(001,to_date('2001-1-1','yyyy-mm-dd'));
insert into dq values(001,to_date('2005-1-1','yyyy-mm-dd'));
insert into dq values(002,to_date('2005-1-1','yyyy-mm-dd'));
insert into dq values(004,to_date('2003-1-1','yyyy-mm-dd'));
insert into dq values(004,to_date('2009-1-1','yyyy-mm-dd'));
delete from DQ a where exists (select A from DQ where A=a.A and B<a.B group by A having count(A)>0);
create table dq (A number,B date);
insert into dq values(001,to_date('2001-1-1','yyyy-mm-dd'));
insert into dq values(001,to_date('2005-1-1','yyyy-mm-dd'));
insert into dq values(002,to_date('2005-1-1','yyyy-mm-dd'));
insert into dq values(004,to_date('2003-1-1','yyyy-mm-dd'));
insert into dq values(004,to_date('2009-1-1','yyyy-mm-dd'));
delete from DQ a where not exists (select 1 from DQ where A=a.A and B>a.B) and (select count(1) from DQ where A=a.A) <>1;