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去除重復數據的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
oracle11g管理員密碼忘記怎么辦 sqlplus解決忘記密碼問題
oracle11g管理員密碼忘記了怎么辦?這篇文章主要介紹了oracle 11g管理員密碼忘記問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06Oracle Database Server ''TNS Listener''遠程數據投毒漏洞(CVE-2012-167
這篇文章主要介紹了Oracle Database Server 'TNS Listener'遠程數據投毒漏洞(CVE-2012-1675的完美解決方法的相關資料,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下2016-09-09分享Oracle 11G Client 客戶端安裝步驟(圖文詳解)
這篇文章主要介紹了分享Oracle 11G Client 客戶端安裝步驟(圖文詳解),非常具有實用價值,需要的朋友可以參考下。2016-12-12PL/SQL Dev連接Oracle彈出空白提示框的解決方法分享
第一次安裝Oracle,裝在虛擬機中,用PL/SQL Dev連接遠程數據庫的時候老是彈出空白提示框,網上找了很久,解決方法也很多,可是就是沒法解決我這種情況的。2014-08-08
最新評論