update cu
set cu.bal=t.amt from collect cu inner join
(
select t1.ref,min(t1.amt) as amt from DebtHistory as t1 inner join
(
select ref,max(insert_time) as insert_time
from DebtHistory group by ref
) as t2 on t1.ref=t2.ref and t1.insert_time=t2.insert_time
group by t1.ref
) as t on t.ref=cu.ref