删除重复的records

select inquiry_id,supplier_id,product_id,email_type,count(1)
from AA
group by inquiry_id,supplier_id,product_id,email_type having count(1) >1
order by inquiry_id,supplier_id,product_id

会出现:
inquiry_id, supplier_id,product_id,email_type,count(1)
80000001   600000001  10001           S                 2
80000001   600000001  10002           S                 2
80000001   600000001  10003           S                 2
80000001   600000001  10004           S                 2
80000001   600000001  10005           S                 2

要只是保留非重复的纪录,删除重复的纪录:

SELECT *
--DELETE
from   AA
where  inquiry_id=8000046530
  and supplier_id=6008815291
  AND  rowid   not   in 
  ( select   max(rowid)   from   AA
    where inquiry_id=8000046530
      and supplier_id=6008815291
group   by   product_id
)

蓝色粗体部分:选出按照product_id 分组的最大的rowid, 然后删除非max(rowid)地记录。
=========================

删除重复数据的一种高效的方法
---------------------------------
表demo是重复拷贝自dba_objects,有88万左右,不重复的是27323,没有索引
方法一:delete from demo a where a.rowid <> (select max(rowid) from demo b where
b.object_id=a.object_id);
耗时:几个小时以上


方法二: delete from demo where rowid in
(select rid from
(select rowid rid,row_number() over(partition by object_id order by rowid) rn
from demo)
where rn <> 1 );
耗时:30秒

方法三: create table demo2 as
select object_id,owner... from
(select demo.*,row_number() over(partition by object_id order by rowid) rn from demo)
where rn = 1;
truncate table demo; insert into demo select * from demo2; drop table demo2;
共耗时: 10秒,适合大数据量的情况,产生更少回滚量;





你可能感兴趣的:(object,table,delete,insert,email)