oracle中删除两条相同记录中的一条,该怎么操作?

假如 dept表里有两条相同的记录如下

deptno dname loc
----------------
200008 hello XAN
200008 hello XAN

请问如何只删除其中的一条?

delete from dept where 呢?

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);

温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-09-01
Oracle中有一个伪列,rownum,用来标识行,可以用这个来试试
第2个回答  2008-09-01
delete from dept where rowid not in
(select min(rowid) from dept group by deptno , dname ,loc)
这样可以保证所有的重复数据仅保留一条,其余的删除本回答被提问者和网友采纳
第3个回答  2008-09-01
Delete from dept where rownum not in (select max(rownum) from dept group by deptno,dname,loc)
第4个回答  2008-09-01
不能用delete删除,要单独删吧。
相似回答