日期:2014-05-17 浏览次数:20854 次
select * from cfg_ag_iad a where (a.agip,a.agid,a.relationtype) in (select agip,agid,relationtype from cfg_ag_iad group by agip,agid,relationtype having count(*)>1) and updatetime not in (select max(updatetime) from cfg_ag_iad group by agip,agid having count(*) >1)
select agip,agid,relationtype,updatetime from (select agip, agid, relationtype, updatetime, row_number()over(partition by agip,agid,relationtype order by updatetime desc) rn from cfg_ag_iad) where rn>1
------解决方案--------------------
试试这个吧,可以滴 delete from --select * from cfg_ag_iad t where (agip,agid,relationtype,updatetime) not in ( select a.agip,a.agid,a.relationtype,max(updatetime) updatetime from cfg_ag_iad a group by a.agip,a.agid,a.relationtype )
------解决方案--------------------
--查询出你要的数据
select *
from cfg_ag_iad a
where (a.agip, a.agid, a.relationtype) in
(select agip, agid, relationtype
from cfg_ag_iad
group by agip, agid, relationtype
having count(*) > 1)
and updatetime in (select max(updatetime) as updatetime, agip, agid, relationtype
from cfg_ag_iad
group by agip, agid, relationtype
having count(*) > 1)
--在数据库保留你要的数据
delete from from cfg_ag_iad a
where (a.agip, a.agid, a.relationtype) in
(select agip, agid, relationtype
from cfg_ag_iad
group by agip, agid, relationtype
having count(*) > 1)
and updatetime not in (select max(updatetime) as updatetime, agip, agid, relationtype
from cfg_ag_iad
group by agip, agid, relationtype
having count(*) > 1)