我正在Oracle中测试一些东西,并用一些示例数据填充了一个表,但是在这个过程中我不小心加载了重复的记录,所以现在我无法使用某些列创建主键.
如何删除所有重复的行并只留下其中一行?
使用rowid
伪列.
DELETE FROM your_table WHERE rowid not in (SELECT MIN(rowid) FROM your_table GROUP BY column1, column2, column3);
在哪里column1
,column2
并且column3
构成每条记录的识别密钥.您可以列出所有列.
从问汤姆
delete from t where rowid IN ( select rid from (select rowid rid, row_number() over (partition by companyid, agentid, class , status, terminationdate order by rowid) rn from t) where rn <> 1);
(修正了缺失的括号)
来自DevX.com:
DELETE FROM our_table WHERE rowid not in (SELECT MIN(rowid) FROM our_table GROUP BY column1, column2, column3...) ;
其中column1,column2等是您要使用的键.
DELETE FROM tablename a WHERE a.ROWID > ANY (SELECT b.ROWID FROM tablename b WHERE a.fieldname = b.fieldname AND a.fieldname2 = b.fieldname2)
delete from emp where rowid not in (select max(rowid) from emp group by empno);解决方案2)
delete from emp where rowid in ( select rid from ( select rowid rid, row_number() over(partition by empno order by empno) rn from emp ) where rn > 1 );解决方案3)
delete from emp e1 where rowid not in (select max(rowid) from emp e2 where e1.empno = e2.empno );
从t1创建表t2作为select distinct*;