insert into Rin_cdr_call_cleaned (f1 , f2 , f3 , f4 , ENDING_BAL , COST,INFO,inbtime1) select t1.card,t1.phone, t1.inbtime,length,ending_bal, cost,info, coalesce((select min(inbtime) from rin_cdr_call_history t2 where t2.inbtime>t1.inbtime and t2.card=t1.card), t1.inbtime) from rin_cdr_call_history t1 order by 1,2