题目:查询员工每个月与前2个月的薪水累加。
对比代码:
1.没有‘row n preceding’
SELECT
id,
month,
salary,
sum(salary) over(PARTITION BY id order by month) sum_salary
FROM test;
1.有‘row n preceding’
SELECT
id,
month,
salary,
sum(salary) over(PARTITION BY id order by month ROWS 2 PRECEDING) sum_salary
FROM test;
我们看id=1,month=4的sum_salary,两者相差2000,也就说说,上面是前四个月的和,下面是前三个月的和,而当上方小于2时,两者没有差别。