Oracle 中去除重复数据

在网上查了一下,去除重复数据有两种情况:

1、部分字段重复,对于这种情况可这样做:

 

(1)create table 临时表 as select a.字段1,a.字段2,MAX(a.ROWID) dataid from 正式表 a GROUP BY a.字段1,a.字段2;
(2) delete from 表名 a
    where a.rowid !=
    (
    select b.dataid from 临时表 b
    where a.字段1 = b.字段1 and
    a.字段2 = b.字段2
    );

(3)commit;

 

我不想删除原有数据,就另建了一个表:

(1)create table 临时表 as select a.字段1,a.字段2,MAX(a.ROWID) dataid from 正式表 a GROUP BY a.字段1,a.字段2;

(2)create table 去重表名 as select  a.*  from 正式表  a, 临时表 b  where a.rowid = b.dataid;

 

 

 

2.对于完全重复的数据,网上说可以这样:

 (1) CREATE TABLE 临时表 AS (select distinct * from 表名);

 (2) truncate table 正式表;

 (3)   insert into 正式表 (select * from 临时表);

 (4)   drop table 临时表;

从SQL语句来看,应该是可以实现的。

 

 

 

更新:

对于不完全重复数据还找到了以下方法,利用ORACLE中的ROWID:

假设student表中的stunum字段中有重复数据,现在要找出哪些数据重复,并删除。

 

查看哪些数据重复:

 

select * from student where stunum in (select stunum(select stunum ,count(*) from student group by stunum having count(*) >1))  --这句太复杂,要好好想想怎么简化。

 

select * from student a,student b where a.stunum = b.stunum and a.rowid < b.rowid

 

 

 

删除重复数据:

 

delete from student a where a.rowid<(select max(rowid) from student b where a.stunum = b.stunum)



真正实施方案如下:

create table mytable_uni as select distinct * from mytable;

truncate table mytable;

alter table mytable disable all triggers;

insert into mytable select * from mytablebak_QUCH;
commit;

alter table mytable enable all triggers;


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值