Oracle去除表中重复记录
在一個表中,某一字段為重復(fù)字段。需要去除重復(fù)字段。同時將所有字段顯示出來。
SELECT * FROM (select a1,a2,a3,
Row_number() OVER (PARTITION BY a1 ORDER BY a1) rn
from a
) where RN = 1
?
--刪除重復(fù)列 ?
? a.如果有ID字段,就是具有唯一性的字段 ?
? ?
? delect ? table ? where ? id ? not ? in ? ( ?
? ?
? select ? max(id) ? from ? table ? group ? by ? col1,col2,col3... ?
? ) ?
? group ? by ? 子句后跟的字段就是你用到判斷重復(fù)的字段 ?
? ?
? ?
? ?
? b.,如果是判斷所有字段 ?
? select ? * ? into ? #aa ? from ? table ? group ? by ? id1,id2,.... ?
? delete ? table ? table ? ?
? insert ? into ? table ? ?
? select ? * ? from ? #aa ?
? ?
? ?
? ?
? c.如果表中有ID的情況 ?
? ?
? select ? identity(int,1,1) ? as ? id,* ? into ? #temp ? from ? tabel ?
? delect ? # ? where ? id ? not ? in ? ( ?
? select ? max(id) ? from ? # ? group ? by ? col1,col2,col3...) ?
? delect ? table ?
? inset ? into ? table(...) ?
? select ? ..... ? from ? #temp ?
? ?
? ?
? col1+','+col2+','...col5 ? 組合主鍵 ?
? ?
? ?
? select ? * ? from ? table ? where ? col1+','+col2+','...col5 ? in ? ( ?
? ?
? select ? max(col1+','+col2+','...col5) ? from ? table ? ?
? where ? having ? count(*)>1 ?
? group ? by ? col1,col2,col3,col4 ? ?
? ) ?
? group ? by ? 子句后跟的字段就是你用到判斷重復(fù)的字段 ?
? ?
? d. ?
? select ? identity(int,1,1) ? as ? id,* ? into ? #temp ? from ? tabel ?
? select ? * ? from ? #temp ? where ? id ? in ? ( ?
? select ? max(id) ? from ? #emp ? where ? having ? count(*)>1 ? group ? by ? col1,col2,col3...) ?
? ?
? e. ?
? alter ? table ? yourtable ? add ? rownum ? int ? identity(1,1) ?
? go ?
? delete ? from ? yourtable ? where ? rownum ? not ? in ? (select ? min(rownum ? ) ? from ? yourtable ? group ? by ? 你重復(fù)的字段名) ?
? go ?
? alter ? table ? yourtable ? drop ? column ? rownum ?
? go ?
? ?
? f. ?
? alter ? table ? 表 ? add ? newfield ? int ? identity(1,1) ?
? delete ? 表 ?
? where ? newfield ? not ? in( ?
? select ? min(newfield) ? from ? 表 ? group ? by ? 除newfield外的所有字段 ?
? ) ?
? ?
? alter ? table ? 表 ? drop ? column ? newfield ?
? ?
? ?
? g. ?
? -- ? 刪除表中重復(fù)的記錄 ?
? DELETE ? delete1 ?
? FROM ? tabTest ? delete1 ?
? JOIN ? tabTest ? delete2 ? ?
? ON ? delete1.student_id=delete2.student_id ? AND ? delete1.course_id=delete2.course_id ? AND ? delete1.id>delete2.id???
???
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
?
?
?
平時工作中可能會遇到當(dāng)試圖對庫表中的某一列或幾列創(chuàng)建唯一索引時,系統(tǒng)提示 ORA-01452 :不能創(chuàng)建唯一索引,發(fā)現(xiàn)重復(fù)記錄。
下面總結(jié)一下幾種查找和刪除重復(fù)記錄的方法(以表CZ為例):
表CZ的結(jié)構(gòu)如下:
SQL> desc cz
Name Null? Type
C1 NUMBER(10)
C10 NUMBER(5)
C20 VARCHAR2(3)
刪除重復(fù)記錄的方法原理:
(1).在Oracle中,每一條記錄都有一個rowid,rowid在整個數(shù)據(jù)庫中是唯一的,rowid確定了每條記錄是在Oracle中的哪一個數(shù)據(jù)文件、塊、行上。
(2).在重復(fù)的記錄中,可能所有列的內(nèi)容都相同,但rowid不會相同,所以只要確定出重復(fù)記錄中那些具有最大rowid的就可以了,其余全部刪除。
重復(fù)記錄判斷的標(biāo)準(zhǔn)是:
C1,C10和C20這三列的值都相同才算是重復(fù)記錄。
經(jīng)查看表CZ總共有16條記錄:
SQL>set pagesize 100
SQL>select * from cz;
C1 C10 C20
1 2 dsf
1 2 dsf
1 2 dsf
1 2 dsf
2 3 che
1 2 dsf
1 2 dsf
1 2 dsf
1 2 dsf
2 3 che
2 3 che
2 3 che
2 3 che
3 4 dff
3 4 dff
3 4 dff
4 5 err
5 3 dar
6 1 wee
7 2 zxc
20 rows selected.
1.查找重復(fù)記錄的幾種方法:
(1).SQL>select * from cz group by c1,c10,c20 having count(*) >1;
C1 C10 C20
1 2 dsf
2 3 che
3 4 dff
(2).SQL>select distinct * from cz;
C1 C10 C20
1 2 dsf
2 3 che
3 4 dff
(3).SQL>select * from cz a where rowid=(select max(rowid) from cz where c1=a.c1 and c10=a.c10 and c20=a.c20);
C1 C10 C20
1 2 dsf
2 3 che
3 4 dff
2.刪除重復(fù)記錄的幾種方法:
(1).適用于有大量重復(fù)記錄的情況(在C1,C10和C20列上建有索引的時候,用以下語句效率會很高):
SQL>delete cz where (c1,c10,c20) in (select c1,c10,c20 from cz group by c1,c10,c20 having count(*)>1) and rowid not in
(select min(rowid) from cz group by c1,c10,c20 having count(*)>1);
SQL>delete cz where rowid not in(select min(rowid) from cz group by c1,c10,c20);
(2).適用于有少量重復(fù)記錄的情況(注意,對于有大量重復(fù)記錄的情況,用以下語句效率會很低):
SQL>delete from cz a where a.rowid!=(select max(rowid) from cz b where a.c1=b.c1 and a.c10=b.c10 and a.c20=b.c20);
SQL>delete from cz a where a.rowid<(select max(rowid) from cz b where a.c1=b.c1 and a.c10=b.c10 and a.c20=b.c20);
SQL>delete from cz a where rowid <(select max(rowid) from cz where c1=a.c1 and c10=a.c10 and c20=a.c20);
(3).適用于有少量重復(fù)記錄的情況(臨時表法):
SQL>create table test as select distinct * from cz; (建一個臨時表test用來存放重復(fù)的記錄)
SQL>truncate table cz; (清空cz表的數(shù)據(jù),但保留cz表的結(jié)構(gòu))
SQL>insert into cz select * from test; (再將臨時表test里的內(nèi)容反插回來)
(4).適用于有大量重復(fù)記錄的情況(Exception into 子句法):
采用alter table 命令中的 Exception into 子句也可以確定出庫表中重復(fù)的記錄。這種方法稍微麻煩一些,為了使用“excepeion into ”子句,必須首先創(chuàng)建 EXCEPTIONS 表。創(chuàng)建該表的 SQL 腳本文件為 utlexcpt.sql 。對于win2000系統(tǒng)和 UNIX 系統(tǒng), Oracle 存放該文件的位置稍有不同,在win2000系統(tǒng)下,該腳本文件存放在$ORACLE_HOMEOra90rdbmsadmin 目錄下;而對于 UNIX 系統(tǒng),該腳本文件存放在$ORACLE_HOME/rdbms/admin 目錄下。
具體步驟如下:
SQL>@?/rdbms/admin/utlexcpt.sql
Table created.
SQL>desc exceptions
Name Null? Type
ROW_ID ROWID
OWNER VARCHAR2(30)
TABLE_NAME VARCHAR2(30)
CONSTRAINT VARCHAR2(30)
SQL>alter table cz add constraint cz_unique unique(c1,c10,c20) exceptions into exceptions;
*
ERROR at line 1:
ORA-02299: cannot validate (TEST.CZ_UNIQUE) - duplicate keys found
SQL>create table dups as select * from cz where rowid in (select row_id from exceptions);
Table created.
SQL>select * from dups;
C1 C10 C20
1 2 dsf
1 2 dsf
1 2 dsf
1 2 dsf
2 3 che
1 2 dsf
1 2 dsf
1 2 dsf
1 2 dsf
2 3 che
2 3 che
2 3 che
2 3 che
3 4 dff
3 4 dff
3 4 dff
16 rows selected.
SQL>select row_id from exceptions;
ROW_ID
AAAHD/AAIAAAADSAAA
AAAHD/AAIAAAADSAAB
AAAHD/AAIAAAADSAAC
AAAHD/AAIAAAADSAAF
AAAHD/AAIAAAADSAAH
AAAHD/AAIAAAADSAAI
AAAHD/AAIAAAADSAAG
AAAHD/AAIAAAADSAAD
AAAHD/AAIAAAADSAAE
AAAHD/AAIAAAADSAAJ
AAAHD/AAIAAAADSAAK
AAAHD/AAIAAAADSAAL
AAAHD/AAIAAAADSAAM
AAAHD/AAIAAAADSAAN
AAAHD/AAIAAAADSAAO
AAAHD/AAIAAAADSAAP
16 rows selected.
SQL>delete from cz where rowid in ( select row_id from exceptions);
16 rows deleted.
SQL>insert into cz select distinct * from dups;
3 rows created.
SQL>select *from cz;
C1 C10 C20
1 2 dsf
2 3 che
3 4 dff
4 5 err
5 3 dar
6 1 wee
7 2 zxc
7 rows selected.
從結(jié)果里可以看到重復(fù)記錄已經(jīng)刪除。
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的Oracle去除表中重复记录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle-sql汇总
- 下一篇: Linux负载均衡