1. 程式人生 > 實用技巧 >刪除site_planning_id 和version 重複的,如果有多個版本,留下版本號最高的

刪除site_planning_id 和version 重複的,如果有多個版本,留下版本號最高的

--查詢哪些重複
select site_planning_id ,version from pl_macro_station_copy2 where site_planning_id in (  

select site_planning_id from (select site_planning_id, count(*) as num from pl_macro_station_copy2  group by site_planning_id  ) a where a.num >1 ) order by site_planning_id

--刪除site_planning_id,version完全重複的
delete from pl_macro_station_copy2 where detail_id not in ( SELECT MAX(detail_id) from pl_macro_station_copy2 GROUP BY site_planning_id,version ) --刪除最小版本的 delete from pl_macro_station_copy2 where detail_id in( select detail_id from ( select c.detail_id,min(c.version) from
( select detail_id,site_planning_id ,version from pl_macro_station_copy2 where site_planning_id in ( select site_planning_id from pl_macro_station_copy2 where site_planning_id in ( select site_planning_id from (select site_planning_id, count(*) as num from pl_macro_station_copy2 group
by site_planning_id ) a where a.num >1 ) order by site_planning_id ) group by detail_id,site_planning_id order by site_planning_id ) c group by c.detail_id ) d )