题解 | #查找在职员工自入职以来的薪水涨幅情况#

查找在职员工自入职以来的薪水涨幅情况

https://www.nowcoder.com/practice/fc7344ece7294b9e98401826b94c6ea5

#思路,我们可以求出入职的时候的工资,也就是按照emp_no分组,时间升序第一个的工资,在求一个emp_no分组的时间降序的工资,作为最近工资,相减就是答案,当然,我们要考虑,to_date="9999-01-01"的要求,所有,我们增加子查询,找到那些符合to_date="9999-01-01"的emp_no即可。
select
t.emp_no,
sum(case when t.m=1 then t.salary when t.d=1 then -t.salary end) as growth
from(
select
emp_no,
salary,
dense_rank()over(partition by emp_no order by to_date asc) as d,
dense_rank()over(partition by emp_no order by to_date desc) as m,
to_date
from salaries
) as t 
where 
t.emp_no in (select emp_no from salaries where to_date="9999-01-01")
group by t.emp_no
order by growth asc

全部评论

相关推荐

Gaynes:查看图片
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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