删除重复数据

删除重复数据

准备测试表

删除测试表
drop table test;

create table test as select * from dba_objects;

insert into test select * from test;

进行删除操作

方法一
delete from test where rowid not in (select max(rowid) from test group by object_id);

方法二

delete test a
where a.object_id in
(select b.object_id from test b where a.object_id =b.object_id and a.rowid<b.rowid);

方法三
执行效率很慢的一种方式 速度比in快

delete test a where exists (select null from test b where a.object_id=b. object_id and a.rowid<b.rowid );

方法三

根据oracle数据库中rowid进行删除
delete test 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);

为了方便可以

create table test_tmp as select distinct * from test;
truncate table test;
insert into test select * from test_tmp;
drop table test_tmp ;
为了保留原表相关信息,但是效率差,容易卡死。但是方便。

全字段重复和除了主键剩下都重复两种情况,第二种可以分局主键进行确认是否删除,那全字段重复没有主键或是rowid伪列进行确认怎么删除?

gp数据库
DELETE FROM test WHERE (gp_segment_id, ctid) NOT IN (SELECT gp_segment_id, min(ctid) FROM test GROUP BY city, gp_segment_id);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值