oacle 删除重复数据
--测试数据
create table test as select * from dba_objects;
insert into test select * from test;
--查看是否有重复数据
select * from test order by object_id;
--用rowid删除
select rowid,a.* from test a order by object_id;
--根据rowid分组
--办法(1)not in
DELETE FROM test WHERE ROWID NOT IN (SELECT MAX(ROWID) FROM test GROUP BY object_id)
--办法(2) in
DELETE FROM test a
WHERE ROWID IN (SELECT ROWID
FROM (SELECT row_number() over(PARTITION BY object_id ORDER BY ROWID) rn,
a.*
FROM test a)
WHERE rn > 1)
--办法3 exists
DELETE FROM test a
WHERE EXISTS (SELECT NULL
FROM test b
WHERE a.object_id = b.object_id
AND a.rowid < b.rowid)
--in的等价写法
DELETE FROM test a
WHERE object_id IN (SELECT object_id
FROM test b
WHERE a.object_id = b.object_id
AND a.rowid < b.rowid)
--如果要保留3条重复数据
DELETE FROM test a
WHERE ROWID IN (SELECT ROWID
FROM (SELECT row_number() over(PARTITION BY object_id ORDER BY ROWID) rn,
a.*
FROM test a)
WHERE rn > 3)
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341