Oracle method to remove duplicate data: 1. Find all duplicate rows for the specified column and delete them. The method is count having; 2. Delete all duplicate rows. The code is [delete from nayi224_180824 t where t.rowid in].
#The operating environment of this article: Windows7 system, oracle9i version, Dell G3 computer.
Recommended (free): oracle database
oracle removes duplicate data Method:
Create test data
create table nayi224_180824(col_1 varchar2(10), col_2 varchar2(10), col_3 varchar2(10)); insert into nayi224_180824select 1, 2, 3 from dual union allselect 1, 2, 3 from dual union allselect 5, 2, 3 from dual union allselect 10, 20, 30 from dual ;commit;select*from nayi224_180824;
COL_2 | COL_3 | |
---|---|---|
2 | 3 | |
2 | 3 | |
2 | 3 | |
20 | 30 |
distinct
select distinct t1.* from nayi224_180824 t1;
COL_3 | ||
---|---|---|
30 | ##1 | |
3 | 5 | |
3 | The method has great limitations. Because it can only deduplicate all query columns. If I want to deduplicate col_2 and col3, then my result set can only have col_2 and col_3 columns, but not col_1. |
##2 | |
---|---|
20 | 30 |
But it is also the simplest and easiest way to understand. |
RN | 1 | ||
---|---|---|---|
1 | 10 | 20 | |
1 | ##It’s a lot more troublesome to write, but it has greater flexibility . |
select * from nayi224_180824 t
where (t.col_2, t.col_3) in (select t1.col_2, t1.col_3
from nayi224_180824 t1
group by t1.col_2, t1.col_3
having count(1) > 1)
COL_1
COL_2##1 | 2 | |
---|---|---|
1 | 2 | 3 |
##5 | 2 | 3 |
#The table needs to be checked twice, so the efficiency will be relatively low. Not recommended. | count over |
COL_2
COL_3##1 | 2 | 3 | |
---|---|---|---|
1 | 2 | 3 | 3 |
##5 | 2 | 3 | 3 |
You only need to check the table once, recommended. | Delete all duplicate rows | delete from nayi224_180824 t where t.rowid in ( select rid from (select t1.rowid rid, count(1) over(partition by t1.col_2, t1.col_3) rn from nayi224_180824 t1) t1 where t1.rn > 1); Copy after login | is a slight modification of the above statement.
Analytical function method
delete from nayi224_180824 t where t.rowid in (select rid from (select t1.rowid rid, row_number() over(partition by t1.col_2, t1.col_3 order by 1) rn from nayi224_180824 t1) t1 where t1.rn > 1);
Has the consistent high flexibility of analytical functions . You can do whatever you want with the grouping and change the orderby clause to achieve requirements like "retain the maximum id".
group by
delete from nayi224_180824 t where t.rowid not in (select max(rowid) from nayi224_180824 t1 group by t1.col_2, t1.col_3);
Sacrifice some flexibility in exchange for higher efficiency.
The above is the detailed content of How to remove duplicate data in oracle. For more information, please follow other related articles on the PHP Chinese website!