サブクエリで合計を計算できます:
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
読みやすさを速度と引き換えに、MySQL変数を使用して現在の合計を保持できます:
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
この構成が機能するには、順序付けが必要です。別の順序が必要な場合は、クエリをサブクエリでラップします。
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