這篇文章為大家帶來了關於Oracle的相關知識,其中主要介紹了關於資料清理的時候常常會清除表中的重複的資料,那麼在oracle中怎麼處理呢?下面一起來看一下,希望對大家有幫助。
推薦教學:《Oracle影片教學》
create table nayi224_180824(col_1 varchar2(10), col_2 varchar2(10), col_3 varchar2(10)); insert into nayi224_180824 select 1, 2, 3 from dual union all select 1, 2, 3 from dual union all select 5, 2, 3 from dual union all select 10, 20, 30 from dual ; commit; select*from nayi224_180824;
COL_1 | COL_2 | COL_3 |
---|---|---|
1 | ##2#3 | |
2 | 3 | |
2 | 3 | |
20 | #30 |
select distinct t1.* from nayi224_180824 t1;
COL_2 | #COL_3 | |
---|---|---|
20 | #30 | |
2 | 3 | |
2 | 3 |
select distinct t1.col_2, col_3 from nayi224_180824 t1
COL_3 | |
---|---|
#3 | |
30 |
row_number()
select * from (select t1.*, row_number() over(partition by t1.col_2, t1.col_3 order by 1) rn from nayi224_180824 t1) t1 where t1.rn = 1 ;
COL_2 | COL_3 | #RN | |
---|---|---|---|
2 | 3 | 1 | |
20 | 30 | 1 |
針對指定列,找出所有重複的行
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_3 | ||
---|---|---|
3 | ## 1 | |
3 | 5 | |
3 | #要查兩次表,效率會比較低。不推薦。 |
select * from (select t1.*, count(1) over(partition by t1.col_2, t1.col_3) rn from nayi224_180824 t1) t1 where t1.rn > 1 ;COL_1
#RN | 1 | ||
---|---|---|---|
3 | #1 | 2 | |
3 | #5 | 2 | |
#3 | 只需要查一次表,推薦。 | 刪除所有重複的行 |
刪除重複資料並保留一條
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);
擁有分析函數一貫的彈性高的特徵。可以為所欲為的分組,並透過改變orderby從句來達到像」保留最大id「這樣的要求。
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);
推薦教學:《
Oracle影片教學以上是歸納整理oracle資料庫去除重複資料常用的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!