佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > oracle 刪除重復(fù)數(shù)據(jù)

oracle 刪除重復(fù)數(shù)據(jù)

熱門標簽:百度競價排名 Linux服務(wù)器 網(wǎng)站排名優(yōu)化 地方門戶網(wǎng)站 鐵路電話系統(tǒng) AI電銷 服務(wù)外包 呼叫中心市場需求

重復(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ù)量來說,可能會將數(shù)據(jù)庫吊死。
另一種高效率的方法是先將查詢到的重復(fù)的數(shù)據(jù)插入到一個臨時表中,然后再進行刪除。
CREATE TABLE 臨時表 AS
(
select 字段1,字段2, count(*) as row_num
from 表名
group by 字段1,字段2
having count(*) > 1
);
  上面這句話就是建立了臨時表,并將查詢到的數(shù)據(jù)插入其中。
  下面就可以進行這樣的刪除操作了:
delete from 表名 a
where 字段1,字段2 in (select 字段1,字段2 from 臨時表);   
3.保留重復(fù)數(shù)據(jù)中最新的一條記錄
在Oracle中,rowid是隱藏字段,用來唯一標識每條記錄。所以,只要保留重復(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 )
  使用臨時表實現(xiàn)高效查詢
create table 臨時表 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 臨時表 b
where a.字段1 = b.字段1 and
a.字段2 = b.字段2 );
commit;
  二、對于完全重復(fù)記錄的刪除
  對于表中兩行記錄完全一樣的情況,可以用下面語句獲取到去掉重復(fù)數(shù)據(jù)后的記錄:
select distinct * from 表名
可以將查詢的記錄放到臨時表中,然后再將原來的表記錄刪除,最后將臨時表的數(shù)據(jù)導(dǎo)回原來的表中。如下:
CREATE TABLE 臨時表 AS (select distinct * from 表名);
drop table 正式表;
insert into 正式表 (select * from 臨時表);
drop table 臨時表;   假如想刪除一個表的重復(fù)數(shù)據(jù),可以先建一個臨時表,將去掉重復(fù)數(shù)據(jù)后的數(shù)據(jù)導(dǎo)入到臨時表,然后在從臨時表將數(shù)據(jù)導(dǎo)入正式表中,如下: INSERT INTO t_table_bak
select distinct * from t_table;

以下是補充:

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);

您可能感興趣的文章:
  • oracle查詢重復(fù)數(shù)據(jù)和刪除重復(fù)記錄示例分享
  • 解決Oracle刪除重復(fù)數(shù)據(jù)只留一條的方法詳解
  • Oracle查詢表里的重復(fù)數(shù)據(jù)方法

標簽:湖南 蘭州 黃山 銅川 衡水 崇左 仙桃 湘潭

巨人網(wǎng)絡(luò)通訊聲明:本文標題《oracle 刪除重復(fù)數(shù)據(jù)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    闽侯县| 柳州市| 申扎县| 佛教| 屏东县| 桓仁| 固安县| 云林县| 称多县| 新民市| 怀来县| 大洼县| 江口县| 大同县| 枝江市| 荃湾区| 翁牛特旗| 百色市| 加查县| 东乡| 新绛县| 沛县| 故城县| 馆陶县| 昌平区| 繁昌县| 阿荣旗| 尚义县| 秦安县| 桐梓县| 民县| 托克托县| 阜康市| 广州市| 和林格尔县| 桦南县| 临澧县| 田东县| 彭州市| 丰台区| 任丘市|