MySQL—accumulate a column
Shaped like this
Use the variable @sumsalary:=@sumsalary+salary to accumulate, but don’t forget to initialize the variable: select @sumsalary:=0;
Let’s take an example, a MySQL topic on a certain customer network—60
According to the cumulative sum running_total of salary, running_total is the cumulative sum of salary of the first N current employees (to_date = '9999-01-01'), and so on. The specific results are shown in the following Demo. .
select emp_no,max(salary),cast((@sumsalary:=@sumsalary+max(salary)) as decimal(9,0)) as sum from salaries s
join (select @sumsalary:=0) b
where to_date="9999-01-01"
group by emp_no;
[Note: The cast function retains the decimal point]
result: