1.不含大字段(clob等)的表格:
--例子表格:create table test(a number,b number);
--方法一:通过group by + rowid,效率低
delete from test t
where t.rowid not in (select min(rowid) from test group by a, b);
--方法二:通过 create + rename + distinct ,效率高
create table test_tmp as
select distinct * from test t;
drop table test;
alter table test_tmp rename to test;
2.含大字段(clob等)的表格:
--例子表格:create table test(a number,b clob);
--clob 长度小于4000:
select distinct t.a,to_char(t.b) as b from test t;
--clob 长度大于4000:
select *
from test a
where a.rowid = (select max(b.rowid)
from test b
where b.a = a.a
and nvl(dbms_lob.compare(b.b, a.b), 0) = 0);
Oracle中有一个伪列,rownum,用来标识行,可以用这个来试试
delete from dept where rowid not in
(select min(rowid) from dept group by deptno , dname ,loc)
这样可以保证所有的重复数据仅保留一条,其余的删除
Delete from dept where rownum not in (select max(rownum) from dept group by deptno,dname,loc)
不能用delete删除,要单独删吧。