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

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

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

/*请你查找在职员工自入职以来的薪水涨幅情况,给出在职员工编号emp_no以及其对应的薪水涨幅growth,并按照growth进行升序,以上例子输出为

在职,自入职以来的薪水涨幅,不是每一年的薪水涨幅(自连接封闭区间)
输出 在职员工emp_no存在于employees与salaries, 薪水涨幅growth


意外情况 1: salaries表中的emp_no对应的from_date没有按从早到晚排列
        2.存在离职员工,需甄别离职员工,即max(to_date)=1

先挑出 在职员工,即从salaries里面按emp_no分组,剔除 max(to_date)≠1的老登
*/

with new_s as
(
select 
*
from salaries
where emp_no in (
select
emp_no
from salaries
where to_date='9999-01-01'))

select
a1.emp_no as emp_no
,sum(a1.growth) as growth
from(
select
s1.emp_no as emp_no
,s2.salary-s1.salary as growth
from new_s s1 left join new_s s2
on s1.to_date=s2.from_date and s1.emp_no=s2.emp_no
where s2.salary is not null 
) as a1
group by a1.emp_no
order by growth

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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