先找出每个部门当前最高工资作为表1, 然后找出当前部门,员工,工资作为表2, 最后合并, 读取部门,员工,最高工资, 并按部门排序。
select a.dept_no, b.emp_no, a.salary from (select d.dept_no, max(s.salary) as salary from dept_emp 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' group by d.dept_no) a inner join (select d.emp_no, s.salary, d.dept_no from dept_emp d join salaries s on d.emp_no = s.emp_no and d.to_date='9999-01-01' and s.to_date='9999-01-01') b on a.dept_no = b.dept_no and a.salary = b.salary order by a.dept_no;