GrandMasterTuck
In need of medication
- Local time
- Today, 07:23
Hi folks. I have a query that pulls a list of employees that have at least one call-in-sick date from tblEmployeeSchedules. The trouble I'm having is that this query shows me ALL call-in-sick dates for all employees, and I just want the MOST RECENT call-in-sick date for each employee.
I tried a SELECT TOP 1 type query, but then I only get the one employee that called in sick most recently. I'm pretty sure I have to do this with some kind of subquery, but my programming chops are minimal at best.
tblEmployeeSchedules
When I use SELECT TOP 1, I only get Cindy's record from 8/3. If I do it without the TOP 1 part, I get two records for Cindy, because she called in twice, but I only want ONE of them, the one with the most recent date. I also tried putting TOP(SchDate) in the SELECT statement but I got an error about not recognizing TOP.
What I want to see is ONE record for Bill (the 8/2 record) and ONE record for Cindy (the 8/3 record). Cindy's 8/1 record should not be on there (because the 8/3 record is) and Jason should not be on it at all (because he never called in).
I know this is probably simple, and I'm just not getting the syntax right or something like that. Any ideas?
I tried a SELECT TOP 1 type query, but then I only get the one employee that called in sick most recently. I'm pretty sure I have to do this with some kind of subquery, but my programming chops are minimal at best.
tblEmployeeSchedules
Code:
EmpName SchDate Worked? Loca
-------------------------------------------------
Jason 8/1/16 Y Yard
Jason 8/2/16 Y Cube
Jason 8/3/16 Y Trans
Bill 8/1/16 Y Cube
Bill 8/2/16 N Trans
Bill 8/3/16 Y Yard
Cindy 8/1/16 N Trans
Cindy 8/2/16 Y Yard
Cindy 8/3/16 N Cube
When I use SELECT TOP 1, I only get Cindy's record from 8/3. If I do it without the TOP 1 part, I get two records for Cindy, because she called in twice, but I only want ONE of them, the one with the most recent date. I also tried putting TOP(SchDate) in the SELECT statement but I got an error about not recognizing TOP.
What I want to see is ONE record for Bill (the 8/2 record) and ONE record for Cindy (the 8/3 record). Cindy's 8/1 record should not be on there (because the 8/3 record is) and Jason should not be on it at all (because he never called in).
I know this is probably simple, and I'm just not getting the syntax right or something like that. Any ideas?
Last edited: