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

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

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

不使用employees表的方法:

select s.emp_no,sum(T1.ms-T2.sa) as growth
from salaries s
left join
(select emp_no,salary as ms from salaries 
where to_date = '9999-01-01'
group by emp_no) T1         --求出当前在职人员工资
on T1.emp_no = s.emp_no
left join 
(select emp_no,salary as sa,
row_number() over(partition by emp_no order by to_date desc) as rn
from salaries             --用窗口函数,按离职时间倒序排序
where to_date <> '9999-01-01' --当时间不为9999-01-01时,倒序取第一个,就是上次的调薪时间,但这样会把不在职的也算进来
group by emp_no) T2
on T2.emp_no = s.emp_no
and T2.rn = 1                --取第一个
where s.to_date = '9999-01-01'  --由于会把不在职的也算进来,这里再用9999-01-01进行一下过滤
group by s.emp_no
order by growth
#sql#
全部评论

相关推荐

07-10 14:08
已编辑
江西农业大学 Java
拒绝无效加班的小学生...:期望3k吗?java这辈子有了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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