[英]How do I return a single record using max function when a single employee-ID has several date values and Need Only Latest Date
[英]MS Access: How do I find an employee with the latest date with "Working" or "Reinstated" status only?
tbl_emp_mast
emp_no emp_last_name emp_first_name
1 smith john
2 doe jane
3 case justin
tbl_emp_st
emp_no st_date emp_st
1 10/01/2020 WORKING
1 10/05/2020 QUIT
1 10/10/2020 REINSTATED
2 10/07/2020 QUIT
3 10/02/2020 WORKING
为了表单中的组合框,我只想要那些目前正在工作的员工。 因此,查询结果中应该有员工 #1 和 3。 我试过这个:
SELECT tbl_emp_mast.emp_no AS tbl_emp_mast_emp_no, tbl_emp_mast.emp_last_name & ", " & tbl_emp_mast.emp_first_name AS Employee, tbl_emp_st.st_date, tbl_emp_st.emp_st
FROM tbl_emp_mast INNER JOIN tbl_emp_st ON tbl_emp_mast.[emp_no] = tbl_emp_st.[emp_no];
谢谢你。
嗯。 . . 我想你想要最新的emp_st
。 如果是这样,那么我认为:
select es.*
from tbl_emp_st as es
where es.st_date = (select max(es2.st_date)
from tbl_emp_st as es2
where es2.emp_no = es.emp_no
) and
es.emp_st in ('WORKING', 'REINSTATED');
您应该添加一个 WHERE 子句来检查每个tbl_emp_st
的最新日期是否是emp_st
不等于'QUIT'
:
SELECT m.emp_no AS tbl_emp_mast_emp_no,
m.emp_last_name & ', ' & m.emp_first_name AS Employee,
s.st_date,
s.emp_st
FROM tbl_emp_mast AS m INNER JOIN tbl_emp_st AS s
ON m.emp_no = s.emp_no
WHERE s.st_date = (SELECT MAX(st_date) FROM tbl_emp_st WHERE emp_no = m.emp_no)
AND s.emp_st <> 'QUIT'
结果:
tbl_emp_mast_emp_no Employee st_date emp_st
1 smith, john 10/10/2020 REINSTATED
3 case, justin 10/02/2020 WORKING
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.