题解 | #统计salary的累计和running_total#
统计salary的累计和running_total
http://www.nowcoder.com/practice/58824cd644ea47d7b2b670c506a159a6
知识点
- 本题有两种写法一种直接使用窗口函数,一种使用子查询
- 函数名(column) over(选项),所有聚合函数都可以作为窗口函数
- 子查询就是sum使用的表员工编号小于等于外层表员工编号
代码
窗口函数
select emp_no, salary,
sum(salary) over (order by emp_no) as running_total
from salaries
where to_date = '9999-01-01'
子查询
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