select date, sum(amount), sum(sum(amount)) over (order by date) from (select ref date,amount, amount amtmx, 0 amtrk from rk where ctg='str' and db='01nrg' and ref is not null union all select date,-amount, 0, amount amtrk from rk where ctg='str' and db='01nrg' and ref is not null) t group by date