您的位置:首页 > 数据库 > Oracle

【oracle】sql处理重复数据

2015-03-25 10:22 183 查看
查询哪些数据有重复的
select 字段1,字段2,count(*) from 表名 group by 字段1,字段2 having count(*) > 1

查询哪些数据没有重复的
select 字段1,字段2,count(*) from 表名 group by 字段1,字段2 having count(*) = 1

删除重复数据,只保留最新的一条数据(rowid为实际存储数据的物理id,所以越大说明插入的越后,即越新)
delete from 表名 a
where a.rowid !=
(
  select max(b.rowid) from 表名 b
  where a.字段1 = b.字段1 and
  a.字段2 = b.字段2
)

数据量大的请执行:
create table 临时表 as
  select a.字段1,a.字段2,MAX(a.ROWID) dataid from 正式表 a GROUP BY a.字段1,a.字段2;
  delete from 表名 a
  where a.rowid !=
  (
  select b.dataid from 临时表 b
  where a.字段1 = b.字段1 and
  a.字段2 = b.字段2
  );
  commit;

对于完全重复的记录查询去重后的数据:
select distinct * from 表名

删除多余的完全重复的数据
CREATE TABLE 临时表 AS (select distinct * from 表名);
delete from table 正式表;
insert into 正式表 (select * from 临时表);
drop table 临时表;
INSERT INTO t_table_bak
select distinct * from t_table;

本文出自 “HAPPY_CANDY” 博客,谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: