add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE t
(`id` int, `type` varchar(4), `quantity` int)
;
INSERT INTO t
(`id`, `type`, `quantity`)
VALUES
(1, 'buy', 2),
(2, 'buy', 5),
(3, 'buy', 3),
(4, 'sell', 4),
(5, 'buy', 3),
(6, 'sell', 1),
(7, 'sell', 1)
;

Records: 7  Duplicates: 0  Warnings: 0
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