select *,(select sum(a0-a3) from a where cdb=x.cdb and (tyear<x.tyear or tyear=x.tyear and tmonth<=x.tmonth)) as a4
from a x
order by cdb,tyear,tmonth