with tmp as
(
select orderid,max(ctid) as ctid
from erp_order.erp_order
group by orderid
having count(1)>1
)
delete from erp_order.erp_order
/*
where exists(select 1 from tmp a
where a.orderid=b.orderid
and a.ctid<>b.ctid)*/
using erp_order.erp_order a inner join erp_order.erp_order b
on a.orderid=b.orderid
where a.ctid<>b.ctid
(
select orderid,max(ctid) as ctid
from erp_order.erp_order
group by orderid
having count(1)>1
)
delete from erp_order.erp_order
/*
where exists(select 1 from tmp a
where a.orderid=b.orderid
and a.ctid<>b.ctid)*/
using erp_order.erp_order a inner join erp_order.erp_order b
on a.orderid=b.orderid
where a.ctid<>b.ctid