I MySQL är den mest effektiva metoden att använda variabler:
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;
Du kan också göra detta med en korrelerad underfråga:
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;