我需要一种方法来显示与每个emp_num(人)的最高日期相关的工资金额。
select
lge.emp_num,
lge.emp_lname,
lge.emp_fname,
lgs.sal_from,
lgs.sal_amount
from lgemployee lge
join lgsalary_history lgs
on lge.emp_num = lgs.emp_num
where lge.dept_num in ('300')
group by lge.emp_num, lge.emp_lname, lge.emp_fname, lgs.sal_from, lgs.sal_amount
order by lgs.sal_amount desc
正如你在图片中看到的那样,有几个人出现的情况多了一次,因为他们有多个工资日期。任何帮助是极大的赞赏。提前致谢。
答案 0 :(得分:2)
试试这个:
select
lge.emp_num, lge.emp_lname, lge.emp_fname,
lgs1.sal_from, lgs1.sal_amount
from (
select emp_num, max(sal_from) maxDate from lgsalary_history
group by emp_num
) lgs2
join lgsalary_history lgs1
on lgs1.emp_num = lgs2.emp_num and lgs1.sal_from = lgs2.maxDate
join lgemployee lge
on lgs1.emp_num = lge.emp_num
where lge.dept_num in ('300')
order by lgs1.sal_amount desc