Oracle 中去除重复数据
2010-07-06 14:32
736 查看
在网上查了一下,去除重复数据有两种情况:
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)
真正实施方案如下:
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;
相关文章推荐
- 【Oracle】去除表中重复的数据
- oracle去除重复数据
- oracle去除重复, 取最新的第一条数据
- Oracle去除表重复数据
- oracle学习----去除表中的重复数据
- oracle,把多列同时转成用逗号分割的一行,并去除重复数据
- oracle 重复数据去除
- oracle去除重复, 取最新的第一条数据
- oracle 去除重复数据
- oracle 的 row_number over 去除重复数据
- oracle查询去除数据重复
- Oracle查询中如何去除查询的重复数据
- Oracle去除重复(某一列的值重复),取最新(日期字段最新)的一条数据
- oracle 批量插入时,如何去除重复数据
- 去除数据表中重复行&oracle 替换数据
- Oracle去除重复数据的方法
- oracle中sql去除重复数据
- Remove Duplicates from Sorted List 去除链表重复数据
- Oracle删除重复数据
- 高效删除Oracle中重复数据的方法