# 条件:在职员工
# 薪水涨幅,根据表可知,有一个入职薪水,也就是 from_date=hire_date 的薪水记录,有一个最新的薪水记录,也就是 to_date='9999-01-01' 的薪水记录,涨幅由后者减去前者可得。由于,一个人可能多次调整薪水,则根据员工编号分组,由窗口函数求一个 to_date 的最大值,这条记录才是最新薪水,再然后,员工分组,根据 case when 行转列,将入职薪水和最新薪水,放到一行中去,便于相减得到涨幅


select emp_no
      ,min(case when to_date=finall_date then salary end)-min(case when from_date=first_date then salary end) as growth
from (
    select emp.emp_no
          ,salary
          ,from_date
          ,to_date
          ,hire_date
          ,min(from_date) over (partition by emp_no) as first_date # 入职薪水记录
          ,max(to_date) over(partition by emp_no) as finall_date   # 最新薪水记录
    from employees as emp
    left join salaries as sal
    on emp.emp_no=sal.emp_no
) as a
where finall_date='9999-01-01'
group by emp_no
order by growth