思路:①两表相连,用窗口函数max加一列最后工作的时间,形成表tb1;
②用case when分类,选出入职时间的薪水,最后工作时间的薪水,形成表tb2;
③对tb2子查询,计算即可。
select emp_no, max(end_s)-max(start_s) as growth
from(
select emp_no,
case when from_date=hire_date then salary end as start_s,
case when to_date=a then salary end as end_s
from(
select *, max(to_date)over(partition by emp_no) as a
from salaries left join employees using(emp_no)) as tb1
where a='9999-01-01') as tb2
group by emp_no
order by growth