题解 | #统计salary的累计和running_total#

统计salary的累计和running_total

http://www.nowcoder.com/practice/58824cd644ea47d7b2b670c506a159a6

知识点

  1. 本题有两种写法一种直接使用窗口函数,一种使用子查询
  2. 函数名(column) over(选项),所有聚合函数都可以作为窗口函数
  3. 子查询就是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
全部评论

相关推荐

不愿透露姓名的神秘牛友
03-18 14:29
牛客604067584号:感觉算法卷的人少很多,毕竟只有一部分bg还不错的硕士才会考虑算法,虽然hc不如后端,但是竞争真的少很多。
点赞 评论 收藏
分享
评论
4
1
分享

创作者周榜

更多
牛客网
牛客企业服务