這時候如果暫存資料表中有重複資料,無論是主鍵欄位businessid有重複,還是一整行有重複都會報出違反唯一主鍵約束錯誤。
方法:group by XX having count(*)>1,rowid,distinct,temporary table,procedure
1、查詢表中的重複資料
a.重複一個欄位
b.重複多個欄位
c.重複一整行
建立測試表:
複製代碼 代碼如下:create table cfa (businessid number,customer varchar2(50),branchcode varchar2(10),data_date varchar2(10));
insert into cfa values (1,'Albert','SCB','2011-11-11');
insert into cfa values (2,'Andy','DB','2011-11-12');
insert into cfa values (3,'Allen','HSBC','2011-11-13');
---------------以下為重複資料----------------------------------------------
insert into cfa values (1,'Alex','ICBC','2011-11-14');
insert into cfa values (1,'Albert','CTBK','2011-11-15');
insert into cfa values (1,'Albert','SCB','2011-11-11');
對於a的情況,只有businessid重複
複製代碼 代碼如下:select * from cfa where businessid in (select businessid from cfa group by businessid having count(businessid)>1);
如果是b的情況,businessid 和name同時存在重複
複製代碼 代碼如下:select * from cfa where (businessid,customer) in (select businessid,customer from cfa group by businessid,customer having count(*)>1);
對於c的情況,重複一整行
參考b的方法:
複製代碼 代碼如下:select * from cfa where (businessid,customer,branchcode,data_date) in (select * from cfa group by businessid,customer,branchcode,data_date having count(*)>1);
2、刪除表中的重複資料
a情況,刪除表中多餘的重複記錄,重複記錄是根據單個欄位(businessid)來判斷,只留有rowid最小的記錄
也可以只保留rowid不是最小記錄,需要把代碼中的min改為max這裡不再贅述。
複製代碼 代碼如下:delete from cfa
where businessid in (select businessid
from cfa
group by businessid
having count(businessid) > 1)
and rowid not in (select min(rowid)
from cfa
group by businessid
having count(businessid) > 1);
或者,使用下面更簡單高效的語句
複製代碼 代碼如下:DELETE FROM cfa t
WHERE t.ROWID >
(SELECT MIN(X.ROWID) FROM cfa X WHERE X.businessid = t.businessid);
b情況,刪除表中多餘的重複記錄(多個欄位),只留有rowid最小的記錄
複製代碼 代碼如下:delete from cfa
where (businessid,customer) in (select businessid,customer
from cfa
group by businessid,customer
having count(*) > 1)
and rowid not in (select min(rowid)
from cfa
group by businessid,customer
having count(*) > 1);
或者,使用下面更簡單高效的語句
複製代碼 代碼如下:DELETE FROM cfa t
WHERE t.ROWID > (SELECT MIN(X.ROWID)
FROM cfa X
WHERE X.businessid = t.businessid
and x.customer = t.customer);
c情況,這種情況就比較簡單,使用暫存資料表方法
複製代碼 代碼如下:create table cfabak as select distinct * from cfa;
truncate table cfa;--如果是生產最好對該表backup
Insert into cfa select * from cfabak;
commit;