0

我需要一种方法来仅显示与每个 emp_num(person) 的最高日期相关的工资金额。

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

在此处输入图像描述

正如您在图片中看到的那样,有几个人出现了不止一次,他们有多个付款日期。任何帮助是极大的赞赏。提前致谢。

4

1 回答 1

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
于 2013-10-31T01:39:49.510 回答