永发信息网

Oracle如何删除一张表内的重复数据,但保留最新的一条?

答案:2  悬赏:0  手机版
解决时间 2021-03-30 00:09
  • 提问者网友:箛茗
  • 2021-03-29 12:11
Oracle如何删除一张表内的重复数据,但保留最新的一条?
最佳答案
  • 五星知识达人网友:执傲
  • 2021-03-29 12:51
参考这个
DELETe from Table t1
WHERe createddate != ( SELECt max(createddate)
FROM Table t2 
where t2.customer_guid=t1.customer_guid)
and exists ( select count(customer_guid)
from Table t3
where t3.customer_guid=t1.customer_guid
having count(customer_guid)>1 );
全部回答
  • 1楼网友:独行浪子会拥风
  • 2021-03-29 14:24
楼主你的这个SQL会将表中所有重复的数据都给删掉的,包括你要保留的最新的时间戳的数据。其实你的这个SQL知识查询出来的那些数据是重复的,至于要删除那条数据,你的这个SQL定位不到。
查询出你想要删除的重复数据用下面的SQL:
select * from table a where a.createdate < (select max(b.createdate) from table b where a.customer_guid=b.customer_guid);
删除的话就用这个了:
delete from table a where a.createdate < (select max(b.createdate) from table b where a.customer_guid=b.customer_guid);
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯