题目:
查找当前薪水详情以及部门编号dept_no_牛客题霸_牛客网
有一个全部员工的薪水表salaries简况如下:
emp_no | salary | from_date | to_date |
10001 | 88958 | 2002-06-22 | 9999-01-01 |
10002 | 72527 | 2001-08-02 | 9999-01-01 |
10003 | 43311 | 2001-12-01 | 9999-01-01 |
有一个各个部门的领导表dept_manager简况如下:
dept_no | emp_no | to_date |
d001 | 10001 | 9999-01-01 |
d002 | 10003 | 9999-01-01 |
请你查找各个部门当前领导的薪水详情以及其对应部门编号dept_no,输出结果以salaries.emp_no升序排序,并且请注意输出结果里面dept_no列是最后一列,以上例子输出如下:
emp_no | salary | from_date | to_date | dept_no |
10001 | 88958 | 2002-06-22 | 9999-01-01 | d001 |
10003 | 43311 | 2001-12-01 | 9999-01-01 | d002 |
题解:
关于为什么一定要两个表格的时间都限制成规定时间(9999-01-01)呢?
-----因为薪水表是按年发的,而题目要查找的是当前的薪水,所以要过滤掉以前,而dept_manager是因为有领导会离职,to_date时间不一定是9999-01-01,所以要过滤过离职的领导
select
s.emp_no,
s.salary,
s.from_date,
s.to_date,
d.dept_no
from
dept_manager d
join
salaries s
on
d.emp_no = s.emp_no
where
d.to_date = '9999-01-01'
and s.to_date = '9999-01-01'
order by
d.emp_no asc