oracle 刪除重復(fù)數(shù)據(jù)
重復(fù)的數(shù)據(jù)可能有這樣兩種情況,第一種: 表中只有某些字段一樣,第二種是兩行記錄完全一樣。
一、對于部分字段重復(fù)數(shù)據(jù)的刪除
1.查詢重復(fù)的數(shù)據(jù)
select 字段1,字段2, count(*) from 表名 group by 字段1,字段2 having count(*) > 1
例:Select owner from dba_tables group by owner having count(*)>1;
Select owner from dba_tables group by owner having count(*)=1; //查詢出沒有重復(fù)的數(shù)據(jù)
2.刪除重復(fù)的數(shù)據(jù)
delete from 表名 a where 字段1,字段2 in (select 字段1,字段2,count(*) from 表名 group by 字段1,字段2 having count(*) > 1)
這種刪除執(zhí)行的效率非常低,對于大數(shù)據(jù)量來說,可能會(huì)將數(shù)據(jù)庫吊死。
另一種高效率的方法是先將查詢到的重復(fù)的數(shù)據(jù)插入到一個(gè)臨時(shí)表中,然后再進(jìn)行刪除。
CREATE TABLE 臨時(shí)表 AS
(
select 字段1,字段2, count(*) as row_num
from 表名
group by 字段1,字段2
having count(*) > 1
);
上面這句話就是建立了臨時(shí)表,并將查詢到的數(shù)據(jù)插入其中。
下面就可以進(jìn)行這樣的刪除操作了:
delete from 表名 a
where 字段1,字段2 in (select 字段1,字段2 from 臨時(shí)表);
3.保留重復(fù)數(shù)據(jù)中最新的一條記錄
在Oracle中,rowid是隱藏字段,用來唯一標(biāo)識每條記錄。所以,只要保留重復(fù)數(shù)據(jù)中rowid最大的一條記錄就可以了?! ?
查詢重復(fù)數(shù)據(jù):
select a.rowid,a.* from 表名 a
where a.rowid != (
select max(b.rowid) from 表名 b
where a.字段1 = b.字段1 and a.字段2 = b.字段2 );
例:selete from dba_tables a
where a.rowid!=(
select max(rowid) from test b
where a.owner=b.owner);
刪除重復(fù)數(shù)據(jù),只保留最新的一條數(shù)據(jù):
delete from 表名 a
where a.rowid != (
select max(b.rowid) from 表名 b
where a.字段1 = b.字段1 and a.字段2 = b.字段2 )
使用臨時(shí)表實(shí)現(xiàn)高效查詢
create table 臨時(shí)表 as
(select a.字段1, a.字段2, MAX(a.ROWID) as dataid from 正式表 a
GROUP BY a.字段1,a.字段2);
delete from 表名 a
where a.rowid !=
( select b.dataid from 臨時(shí)表 b
where a.字段1 = b.字段1 and
a.字段2 = b.字段2 );
commit;
二、對于完全重復(fù)記錄的刪除
對于表中兩行記錄完全一樣的情況,可以用下面語句獲取到去掉重復(fù)數(shù)據(jù)后的記錄:
select distinct * from 表名
可以將查詢的記錄放到臨時(shí)表中,然后再將原來的表記錄刪除,最后將臨時(shí)表的數(shù)據(jù)導(dǎo)回原來的表中。如下:
CREATE TABLE 臨時(shí)表 AS (select distinct * from 表名);
drop table 正式表;
insert into 正式表 (select * from 臨時(shí)表);
drop table 臨時(shí)表; 假如想刪除一個(gè)表的重復(fù)數(shù)據(jù),可以先建一個(gè)臨時(shí)表,將去掉重復(fù)數(shù)據(jù)后的數(shù)據(jù)導(dǎo)入到臨時(shí)表,然后在從臨時(shí)表將數(shù)據(jù)導(dǎo)入正式表中,如下: INSERT INTO t_table_bak
select distinct * from t_table;
以下是補(bǔ)充:
Oracle 數(shù)據(jù)庫中查詢重復(fù)數(shù)據(jù):
select * from employee group by emp_name having count (*)>1;
Oracle 查詢可以刪除的重復(fù)數(shù)據(jù)
select t1.* from employee t1 where (t1.emp_name) in (SELECT t2.emp_name from employee t2 group by emp_name having count (*)>1) and t1.emp_id not in (select min(t3.emp_id) from employee t3 group by emp_name having count (*)>1);
Oracle 刪除重復(fù)數(shù)據(jù)
delete from employee t1 where (t1.emp_name) in (SELECT t2.emp_name from employee t2 group by emp_name having count (*)>1) and t1.emp_id not in (select min(t3.emp_id) from employee t3 group by emp_name having count (*)>1);
相關(guān)文章
Oracle ASM故障數(shù)據(jù)恢復(fù)解決方案
在本篇文章里小編給大家整理的是關(guān)于Oracle ASM故障數(shù)據(jù)恢復(fù)解決方案以及相關(guān)知識點(diǎn),有需要的朋友們參考下。2019-11-11Oracle 啟動(dòng)例程 STARTUP參數(shù)說明
Oracle啟動(dòng)例程:STARTUP參數(shù)說明(2008-07-28 20:51:20)標(biāo)簽:startup shutdown 事例 it 分類:Oracle數(shù)據(jù)管理2009-07-07Windows系統(tǒng)下Oracle數(shù)據(jù)庫每天自動(dòng)備份
linux和unix下面使用shell可以很方便實(shí)現(xiàn),如果windows環(huán)境下可以結(jié)合計(jì)劃任務(wù)實(shí)現(xiàn)自動(dòng)備份,下面通過本文給大家介紹實(shí)現(xiàn)方法,需要的朋友參考下吧2016-12-12Oracle生成隨機(jī)數(shù)字、字符串、日期、驗(yàn)證碼及 UUID的方法
這篇文章主要介紹了Oracle生成隨機(jī)數(shù)字、字符串、日期、驗(yàn)證碼及 UUID的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02Oracle生成單據(jù)編號存儲(chǔ)過程的實(shí)例代碼
Oracle生成單據(jù)編號存儲(chǔ)過程,在做訂單類似的系統(tǒng)都可能會(huì)存在訂單編號不重復(fù),或是流水號按日,按年,按月進(jìn)行重新編號。下面給大家分享oracle生成單據(jù)編號存儲(chǔ)過程,需要的的朋友參考下吧2017-04-04安裝Oracle時(shí)出現(xiàn)環(huán)境變量Path的值大于1023的解決辦法
這篇文章主要介紹了安裝Oracle時(shí)出現(xiàn)環(huán)境變量Path的值大于1023的解決辦法,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-12-12