温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

oacle 删除重复数据

发布时间:2020-08-03 22:44:10 来源:网络 阅读:359 作者:llc018198 栏目:关系型数据库

--测试数据

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)

                  


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI