I have the following table :
| PID | FullName | Position | Salary | Status | DateHired
| 11 | Dave | Clerk | 100 | Extended | 2014-01-30
| 11 | Dave | Clerk | 100 | Hired | 2014-01-02
| 22 | Chris | Guard | 80 | Extended | 2014-01-30
| 22 | Chris | DJ | 100 | Hired | 2014-01-02
| 33 | Dud | Clerk | 200 | Terminated| 2014-01-30
| 33 | Dud | Clerk | 200 | Hired | 2014-01-03
| 44 | Trish | Clerk | 200 | Hired | 2014-01-25
I need to be able to output each record grouped by PID, and their latest status. If their latest status is terminated, it should be ignored. The output should be like this:
| PID | FullName | Position | Salary | Status | DateHired
| 11 | Dave | Clerk | 100 | Extended | 2014-01-30
| 22 | Chris | Guard | 80 | Extended | 2014-01-30
| 44 | Trish | Clerk | 200 | Hired | 2014-01-25
Dud has been terminated, so he isn't shown on the table.
Is there a query for this? or should I just link the table to .net, and loop from there?
What I did that also worked is the ff:
- I queried the entire table (with select ) into a datatable ordered by PID then DateHired (asc)
- looped for each record in the datatable
- and copied the last record detected win the loop for that PID into another datatable as long as the Status != "Terminated"
EDIT Corrected the sample table given
EDIT Included the solution I've tried.