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

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

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


#在职,自入职以来涨薪幅度=当前工资-入职工资

with useful_e as
(
select
*
from employees
where emp_no in (select emp_no from salaries where to_date='9999-01-01')
)#只对employees保留在职的员工记录
, useful_s as
(
select
*
from salaries
#where (emp_no,to_date) in (select emp_no, hire_date from useful_e )。二刷这里s要用入职日期来比对,用成from_date了
where (emp_no,from_date) in (select emp_no, hire_date from useful_e )
or to_date='9999-01-01' 
)#只保留入职工资记录和当前工资记录

#放在同行运算,偏移函数
,compare as
(
select
*
,lag(salary,1)over() as t1 #取上一行是因为此时表中只剩2行,而取上之后能用9999-01-01来筛选
from useful_s
)

select
emp_no
,salary-t1 as growth
from compare 
where to_date='9999-01-01'
order by growth asc


全部评论

相关推荐

安静的垂耳兔在泡澡:ks已经第八次投递了,它起码挂了还让你再投,不错了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务