Oracle去除重復數據
更新時間:2022年05月05日 15:39:20 作者:springsnow
這篇文章介紹了Oracle去除重復數據的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
查詢某些字段相同的記錄
如:查詢col1與col2值相同的記錄:
select a.* from table1 a, table1 b where a.id <> b.id and a.col1 = b.col1 and a.col2 = b.col2;
一、用rowid方法:
根據oracle自帶的rowid屬性進行判斷是否存在重復記錄。
rowid偽列用于唯一標識物理位置的表行,當用insert插入數據時,會自動生成rowid,與數據一起存放,形如:AAAL=XAAAEAAAAA。
1、查數據:
select * from table1 a where rowid!= (select max(rowid) from table1 b where a.col1 = b.col1 and a.col2 = b.col2;
2、刪數據:
保留rowid最大的記錄:
delete from table1 a where rowid!= (select max(rowid) from table1 b where a.col1 = b.col1 and a.col2 = b.col2;
二、group by 方法:
1、查數據:
select * from table1 a where (a.col1,a.col2) in (select col1,col2 from table1 group by col1,col2 having count(*)>1)
2、刪數據:
刪除表中多余的重復記錄(多個字段),只保留rowid最小的記錄。
delete from table1 a where (a.col1,a.col2) in (select col1,col2 from table1 group by col1,col2 having count(*)>1) and rowid not in (select min(rowid) from table1 group by col1,col2 having count(*)>1)
到此這篇關于Oracle去除重復數據的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

