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

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

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

/*入职时候的工资表
SELECT e.emp_no,salary from salaries s
left join employees e 
on s.emp_no=e.emp_no
WHERE s.from_date = e.hire_date;
*/

/*离职或最后一次工资变动时的工资表
SELECT e.emp_no,salary from salaries s
left join employees e
on s.emp_no=e.emp_no
where s.to_date='9999-01-01';
*/

SELECT a.emp_no,(b.salary-a.salary) as growth
from (
    SELECT e.emp_no,salary from salaries s
    left join employees e 
    on s.emp_no=e.emp_no
    WHERE s.from_date = e.hire_date
) as a
inner join (      #a表中所有员工工资都有,b表中只有在职员工的工资,所以用内连接,用左外连接离职员工工资涨幅会显示为null
    SELECT e.emp_no,salary from salaries s
    left join employees e
    on s.emp_no=e.emp_no
    where s.to_date='9999-01-01'
) as b
on a.emp_no=b.emp_no
order by growth asc;
全部评论

相关推荐

11-15 19:28
已编辑
蚌埠坦克学院 硬件开发
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务