I have a "status history" table that contains a list of employee's employment statuses. What I would like to do is grab a list of each employees latest record if and only if their status value is "Up To Date".
So far, this is the query I have with pseudo code
SELECT *
FROM EMP.EmployeeRecords
WHERE (THE LATEST STATUS RECORD) StatusValue = 'Up To Date'
I think you are looking for the MAX() function John. Take a look at the following for the definition:
https://msdn.microsoft.com/en-us/library/ms187751.aspx
Taking some assumptions into account:
Lets say that you have a EmployeeRecordsID
on the EMP.EmployeeRecords
table which is an auto-incrementing identity column as well as an EmployeeName
to differentiate the different employees; to get the latest record where the StatusValue = 'Up To Date'
you would do something like the following
select
LatestID = MAX(EmployeeRecordsID)
, EmployeeName
from EMP.EmployeeRecords
where StatusValue = 'Up To Date'
group by EmployeeName
Your question is quite poorly formed. You should have sample data and desired layouts, as well as tagging the question with the database you are using.
Guessing at the structure of data, here is with a correlated subquery:
SELECT e.*
FROM EMP.EmployeeRecords e
WHERE e.statusdate = (select max(e2.statusdate)
from emp.EmployeeRecords e2
where e2.employeeid = e.employeeid
) and
e.StatusValue = 'Up To Date';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.