Running total with in each group using MySQL

こ雲淡風輕ζ 提交于 2021-01-05 12:34:05

问题


I am trying to write a SQL to calculate running total with in each group in the below input. Just wondering how can I do it using MySQL. I am aware of how to do it in regular SQL using analytic functions but not in MySQL. Could you share your thoughts on how to implement it.

SQL Fiddle : http://sqlfiddle.com/#!9/59366d/19

SQL using window function :

SELECT e.Id,
       SUM( e.Salary ) OVER( PARTITION BY e.Id ORDER BY e.Month  ) AS cumm_sal
  FROM Employee e 
LEFT JOIN
       (
          SELECT Id,MAX(Month) AS maxmonth
            FROM Employee
          GROUP BY Id
        ) emax
    ON e.Id = emax.Id
WHERE e.Month != emax.maxmonth
ORDER BY e.Id,e.Month DESC;    

Input :

Create table Employee (Id int, Month int, Salary int);

insert into Employee (Id, Month, Salary) values ('1', '1', '20');
insert into Employee (Id, Month, Salary) values ('2', '1', '20');
insert into Employee (Id, Month, Salary) values ('1', '2', '30');
insert into Employee (Id, Month, Salary) values ('2', '2', '30');
insert into Employee (Id, Month, Salary) values ('3', '2', '40');
insert into Employee (Id, Month, Salary) values ('1', '3', '40');
insert into Employee (Id, Month, Salary) values ('3', '3', '60');
insert into Employee (Id, Month, Salary) values ('1', '4', '60');
insert into Employee (Id, Month, Salary) values ('3', '4', '70');

Output :

| Id | Month | Salary |
|----|-------|--------|
| 1  | 3     | 90     |
| 1  | 2     | 50     |
| 1  | 1     | 20     |
| 2  | 1     | 20     |
| 3  | 3     | 100    |
| 3  | 2     | 40     |

回答1:


In MySQL, the most efficient approach is to use variables:

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;

You can also do this with a correlated subquery:

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;


来源:https://stackoverflow.com/questions/50570479/running-total-with-in-each-group-using-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!