(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.重複資料刪除記錄的幾種方法:
(1).適用於有大量重複記錄的情況(在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).適用於有少量重複記錄的情況(注意,對於有大量重複記錄的情況,用以下語句效率會很低):
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).適用於有少量重複記錄的情況(暫存資料表法):
SQL>create table test as select distinct * from cz; (建一個暫存資料表test用來存放重複的記錄)
SQL>truncate table cz; (清空cz表的資料,但保留cz表的結構)
SQL>insert into cz select * from test; (再將暫存資料表test裡的內容反插回來)
(4).適用於有大量重複記錄的情況(Exception into 子句法):
採用alter table 命令中的 Exception into 子句也可以確定出庫表中重複的記錄。這種方法稍微麻煩一些,為了使用“excepeion into ”子句,必須首先建立 EXCEPTIONS 表。建立該表的 SQL 指令檔為 utlexcpt.sql 。對於win2000系統和 UNIX 系統, Oracle 存放該檔案的位置稍有不同,在win2000系統下,該指令檔存放在$ORACLE_HOME\Ora90\rdbms\admin 目錄下;而對於 UNIX 系統,該指令檔存放在$ORACLE_HOME/rdbms/admin 目錄下。
具體步驟如下:
SQL>@?/rdbms/admin/utlexcpt.sql
Table created.
SQL>desc exceptions
Name Null? Type
----------------------------------------- -------- --------------