MySQLでは、最も効率的なアプローチは変数を使用することです:
select e.*,
(@s := if(@id = e.id, @s + salary,
if(@id := e.id, salary, salary)
)
) as running_salary
from (select e.*
from employee e
order by e.id, e.month
) e cross join
(select @id := -1, @s := 0) params;
相関サブクエリを使用してこれを行うこともできます:
select e.*,
(select sum(e2.salary)
from employee e2
where e2.id = e.id and e2.month <= e.month
) as running_salary
from employee e;