我已经看到了一些关于此的帖子,但没有找到一个满足我的需求。我希望能够在mysql数据库中完成以下功能。我通过mysql workbench连接。
SELECT dept_id,
salary,
month,
LAG(salary,1,0) OVER(PARTITION BY dept_id ORDER BY month) lag_one,
LAG(salary,2,0) OVER(PARTITION BY dept_id ORDER BY month) lag_two,
LAG(salary,3,0) OVER(PARTITION BY dept_id ORDER BY month) lag_three,
LAG(salary,4,0) OVER(PARTITION BY dept_id ORDER BY month) lag_four,
LAG(salary,5,0) OVER(PARTITION BY dept_id ORDER BY month) lag_five
FROM Employees;
我需要做多个滞后而不仅仅是跳过1行,因为我需要抓住最后5个历史记录并为它们分配权重。
示例数据
month, salary, dept_id
1,2000,1
2,2200,1
3,2400,1
4,2000,1
5,2100,1
6,2700,1
7,2000,1
8,2800,1
9,2050,1
10,2020,1
11,2030,1
12,2900,1
预期产出
1,2900,12,2030,2020,2050,2800,2000
非常感谢