Oracle消除重复记录的几种方式
消除重复数据是数据清洗的头等大事,下面介绍比较常用的几种去重方式。
Distinct
删除重复的员工信息
select distinct(empno) from emp
Rowid
利用rowid
结合max
和min
函数快速去重
select e.* from emp e where e.rowid = (select max(e.rowid) from emp e
利用rowid
结合max
和min
函数快速删除重复数据
delete e.* from emp e where e.rowid < (select max(e.rowid) from emp e
Group by
select deptno from emp group by deptno;
Row_number()
row_number是通过标记排号方式去重,如果有2条或以上的重复数据,直接筛选删除即可。
1.查看重复数据
select d.id,d.outer_code from dict_depts_source d order by outer_code
2.标识重复数据
select d.id,d.outer_code,row_number() over(partition by outer_code order by outer_code) row_flag from dict_depts_source d
3.删除重复数据
delete from dict_depts_source where id in(
select id from(select d.id,d.outer_code,row_number() over(partition by outer_code order by outer_code) row_flag from dict_depts_source d)t
where t.row_flag > 1)
4.检查删除效果
select d.id,d.outer_code,row_number() over(partition by outer_code order by outer_code) row_flag from dict_depts_source d