如何刪除oracle庫中相同的記錄

xypincle發表於2017-02-19

  1. 如何刪除oracle庫中相同的記錄,但是保留重複記錄中的一條記錄:
  2. 解決方法:可以利用oracle中的rowid偽列來達到這個目的:

  3. 1.建立臨時表,並將查詢到的重複資料插入其中(是否可以建一個檢視?)
  4. create table temp_woods as
  5. (select item_id,count(*) as rowcount from wooods group by item_id having count(*) > 1 );

  6. 2.查詢相同的紀錄:
  7. select a.*,a.rowid from woods a where a.rowid <> (select max(b.rowid) from woods b where b.item_id in (select item_id from temp_woods) where b.item_id = a.item_id) ;

  8. 3.刪除重複的記錄並保留其中rowid列最大的紀錄:
  9. delete from woods a where a.rowid <> (select max(b.rowid) from woods b where b.item_id in (select item_id from temp_woods) where b.item_id = a.item_id) ;

  10. 4.刪除臨時表:
  11. drop table temp_woods cascade constraints ;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28878983/viewspace-2133898/,如需轉載,請註明出處,否則將追究法律責任。

相關文章