Oracle去除重復(fù)數(shù)據(jù)
查詢某些字段相同的記錄
如:查詢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方法:
根據(jù)oracle自帶的rowid屬性進(jìn)行判斷是否存在重復(fù)記錄。
rowid偽列用于唯一標(biāo)識(shí)物理位置的表行,當(dāng)用insert插入數(shù)據(jù)時(shí),會(huì)自動(dòng)生成rowid,與數(shù)據(jù)一起存放,形如:AAAL=XAAAEAAAAA。
1、查數(shù)據(jù):
select * from table1 a where rowid!= (select max(rowid) from table1 b where a.col1 = b.col1 and a.col2 = b.col2;
2、刪數(shù)據(jù):
保留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、查數(shù)據(jù):
select * from table1 a where (a.col1,a.col2) in (select col1,col2 from table1 group by col1,col2 having count(*)>1)
2、刪數(shù)據(jù):
刪除表中多余的重復(fù)記錄(多個(gè)字段),只保留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)
到此這篇關(guān)于Oracle去除重復(fù)數(shù)據(jù)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
WINDOWS下使用DOS命令行連接oracle數(shù)據(jù)庫
本文講述了通過windows下的DOS命令連接oracle數(shù)據(jù)庫并進(jìn)行簡單操作的方法2018-03-03ORACLE 常用函數(shù)總結(jié)(80個(gè))
ORACLE 常用函數(shù)總結(jié)(80個(gè)),大家可以參考下。2009-09-09分享ORACLE SEQUENCE跳號(hào)總結(jié)
在ORACLE數(shù)據(jù)庫中,序列(SEQUENCE)是使用非常頻繁的一個(gè)數(shù)據(jù)庫對(duì)象,但是有時(shí)候會(huì)遇到序列(SEQUECNE)跳號(hào)(skip sequence numbers)的情形,那么在哪些情形下會(huì)遇到跳號(hào)呢,下面通過本文給大家詳解介紹,一起看看吧2017-09-09