select *,
sum(salary) over(partition by id order by month ) sum_salary, # order by记得排序,不然都是总和。
rank() over(partition by id order by `month` desc) rks # 按月份排序,跳跃
from employee
SELECT t.id, t.`month`, t.sum_salary FROM
(
SELECT id, `month`, salary,
SUM(salary) over(PARTITION BY id ORDER BY `month` ROWS 2 PRECEDING) sum_salary, -- 累加的总薪水
rank() over(PARTITION BY id ORDER BY `month` DESC) ranks -- 排名好去掉最近一个月
FROM employee
) t
WHERE t.ranks > 1
order by id ,`month` desc;
LeetCode 【困难】数据库-第579:查询员工的累计薪水
原文:https://www.cnblogs.com/Tdazheng/p/14964373.html