with data as (
select 1 as id, 500 as cost from dual union all
select 2 as id, 100 as cost from dual union all
select 3 as id, 300 as cost from dual union all
select 4 as id, 200 as cost from dual union all
select 5 as id, 100 as cost from dual union all
select 6 as id, -800 as cost from dual
)
select batch, sum(cost) as total
form (
select ceil(rownum/3) batch,
( case when rownum < max(rownum) over (partition by ceil(rownum/3)) then cost else -cost end ) as cost
from data
order by id
)
group by batch