Oracle 尋找與刪除表中重複記錄的步驟方法

來源:互聯網
上載者:User

這時候如果暫存資料表中有重複資料,無論是主鍵欄位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;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.