题解 | #查找在职员工自入职以来的薪水涨幅情况#
查找在职员工自入职以来的薪水涨幅情况
http://www.nowcoder.com/practice/fc7344ece7294b9e98401826b94c6ea5
- 找到所有人的入职工资,连起来之后找员工的hire_dat = 薪资的from_dat的那些,为tb_hire;
- 找到所有人的在职工资,连起来之后找薪资的to_dat = '9999-01-01'的那些,为tb_now,这个表只保留了在职人员;
- 把tb_hire和tb_now按照emp_no连起来,inner join或直接join,取在职工资-入职工资即可;
- 按照薪资涨幅排序。
代码:
select tb_hire.emp_no, (tb_now.now_salary - tb_hire.hire_salary) growth
from
(select e.emp_no, s.salary hire_salary
from employees e
left join salaries s
on e.emp_no = s.emp_no
where e.hire_date = s.from_date) as tb_hire
join
(select e.emp_no, s.salary now_salary
from employees e
left join salaries s
on e.emp_no = s.emp_no
where s.to_date = '9999-01-01') as tb_now
on tb_hire.emp_no = tb_now.emp_no
order by growth