oracle去除重複資料的方法:1、針對指定列,查出所有重複的行,並刪除,方法為count having;2、刪除所有重複的行,代碼為【delete from nayi224_180824 t where t.rowid in】。
本文操作環境:Windows7系統,oracle9i版本,Dell G3電腦。
建議建議(免費):oracle資料庫
oracle移除重複數據的方法:
建立測試資料
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_1 | COL_2 | #COL_3 |
---|---|---|
1 | 2 | 3 |
2 | 3 | |
2 | 3 | |
20 | 30 |
#針對指定資料列,查出去重後的結果集
distinct
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 |
#count having
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_2 | COL_3 | |
---|---|---|
2 | #3 | |
2 | 3 | |
3 |
count over
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_3 | RN | ||
---|---|---|---|
3 | #3 | 1 | |
3 | 3 | #5 | |
#3 | 3 | 只需要查一次表,推薦。 |
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);
就是上面的語句稍作修改。
刪除重複資料並保留一條分析函數法
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「這樣的要求。
group bydelete 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如何去除重複數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!