思路:使用标量子查询来进行累计求和
即将主表s1的emp_no确定之后,再到第三个字段中查询出小于等于主表的emp_no的所有salary进行求和。
这里的第三个字段就是进行累计求和。
select s1.emp_no, s1.salary,
(select sum(s2.salary) from salaries as s2 where s2.emp_no <= s1.emp_no and s2.to_date = '9999-01-01') as running_total
from salaries as s1
where s1.to_date = '9999-01-01'
order by s1.emp_no