DROP TABLE IF EXISTS employee;
CREATE TABLE employee
(
Id INT,
MONTH INT,
Salary DECIMAL
);
INSERT INTO employee VALUES(1, 1, 20);
INSERT INTO employee VALUES(2, 1, 20);
INSERT INTO employee VALUES(1, 2, 30);
INSERT INTO employee VALUES(2, 2, 30);
INSERT INTO employee VALUES(3, 2, 40);
INSERT INTO employee VALUES(1, 3, 40);
INSERT INTO employee VALUES(3, 3, 60);
INSERT INTO employee VALUES(1, 4, 60);
INSERT INTO employee VALUES(3, 4, 70);
SELECT t.id, t.`month`, t.salary FROM
(
SELECT id, `month`,
SUM(salary) over(PARTITION BY id ORDER BY `month` ROWS 2 PRECEDING) salary, -- 累加的总薪水
rank() over(PARTITION BY id ORDER BY `month` DESC) ranks -- 排名好去掉最近一个月
FROM employee
) t
WHERE t.ranks > 1