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;