oracle表中怎么去重复,Oracle里去掉表里组合字段重复的记录步骤是什么呢?

当设计表的时候没有建组合字段唯一约束,以后需要增加这一约束时,却发现表里已经有了很多重复记录了。请看看我用的去掉表里组合字段重复的记录方法:假设原始表名为source_table,字段名1为field_name1,字段名2为field_name2。

(当然稍加修改也可以用到三个及以上组合字段重复的情况)

第一步: 生成组合字段重复的临时表source_dup_simplecreate table source_dup_simplenologging pctfree 1 pctused 99 as select field_name1,field_name2,count(0) as num from source_tablegroup by field_name1,field_name2 having count(0)>1;第二步: 生成组合字段重复的主表里完整记录的临时表source_dupcreate table source_dup nologging pctfree 1 pctused 99 as select t1。

* from source_table t1,source_dup_simple t2where t1。field_name1=t2。field_name1 and t1。field_name2=t2。field_name2;第三步: 删去source_dup里的重复记录 --可选择:保留rowid小的记录delete from source_dup a where rowid > (select min(rowid) from source_dup b wherea。

field_name1 = b。field_name1 and a。field_name2=b。field_name2);commit;--可选择:保留rowid大的记录delete from source_dup a where rowid (select min(date_field) from source_dup b wherea。

field_name1 = b。field_name1 and a。field_name2=b。field_name2);

commit;

如果时间字段上有重复,还需要再次根据rowid来删一次

delete from source_dup a where rowid < (

select max(rowid) from source_dup b where

a。

field_name1 = b。field_name1 and a。field_name2=b。field_name2);

commit;

第四步: 删去所有重复组合字段原始表里记录 delete from source_table where field_name1||field_name2 in (select field_name1||field_name2 from source_dup_simple);commit;注意:如果操作一万条以上的记录最好在source_table的field_name1和field_name2字段上建索引。

第五步: 把剩下的没有重复的记录插回原始表 insert into source_table select * from source_dup;commit;。

全部

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值