By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
select type
,sum(quantity) as quantity
from (
select *
,count(chng) over(order by id) as grp
from (
select *
,case when type <> lag(type) over(order by id) then 1 end as chng
from t
) t
) t
group by grp, type
type | quantity |
---|---|
buy | 10 |
sell | 4 |
buy | 3 |
sell | 2 |