You can calculate the amount in a subquery:
select Category , Time , Qty , ( select sum(Qty) from YourTable t2 where t1.Category = t2.Category and t1.Time >= t2.Time ) as CatTotal from YourTable t1
For readability, for speed, you can use the MySQL variable to store the current amount:
select Category , Time , Qty , @sum := if(@cat = Category,@sum,0) + Qty as CatTotal , @cat := Category from YourTable cross join (select @cat := '', @sum := 0) as InitVarsAlias order by Category , Time
This design requires ordering; if you need a different order, wrap the request in a subquery:
select Category , Time , Qty , CatTotal from ( select Category , Time , Qty , @sum := if(@cat = Category,@sum,0) + Qty as CatTotal , @cat := Category from YourTable cross join (select @cat := '', @sum := 0) as InitVarsAlias order by Category , Time ) as SubQueryAlias order by Time
source share