题解 | #统计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
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 12:05
俺不中了,BOSS遇到了一个hr,我觉得我咨询的问题都很正常吧,然后直接就被拒绝了???
恶龙战士:你问的太多了,要不就整理成一段话直接问他,一个一个问不太好
点赞 评论 收藏
分享
评论
4
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务