Link to home
Start Free TrialLog in
Avatar of bfuchs
bfuchsFlag for United States of America

asked on

How to get the last two records per employee according to day column?

Hi Experts,

I have a table with following columns
ID
EmployeeID
Day
SupvType

How can I get the last two records of this table per employee according to day column?

Add Note, only where SupvType is "Soc".

PS, Its an Access table.
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

what's the values in the Day field and what's its data type?
Depends on what you want to do with the data.  One method, would be something like:

SELECT T1.ID, T1.Employee, T1.Day, T1.SupvType, Count(T2.ID) as EmpDayOrder
From yourTable as T1
LEFT JOIN yourTable as T2
ON T1.Employee = T2.Employee
AND T1.Day  <= T2.Day
GROUP BY T1.ID, T1.Employee, T1.Day, T1.SubvType
HAVING Count(T2.ID) <=2

Now, this SQL involves a non-equi join which matches each employee record against itself and then matches the day, which allows you to count the number of records in T2, where the Day >= T1.Day.  So for the most recent T1.Day value, there will only be 1 record in T2, which matches to each Employee/Day, for the 2nd most recent day in T1, there will be 2 records in T2 which match, so by setting the HAVING clause to select only the top 2 values

HTH
Dale
Avatar of bfuchs

ASKER

@Rayn,
Day is datetime and contains dates

@Dale,

1- Its giving me attached error
2- I need to see the entire record from this table, not only those 4 columns listed above.

Thanks,
Ben
untitled.png
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bfuchs

ASKER

Hi Experts,

Trying to figure out why Gustav's returns 20% more records than Dale's..

@Dale,
If I use yours, how can I get all columns?

Thanks,
Ben
You cannot with the method I used.

Or you would make what I provided a subquery and then join the table to the ID field of the subquery.
Avatar of bfuchs

ASKER

Thanks Experts!

Interestingly, when I tried on the actual table needed, the diff was minor, less then 00.1%
Avatar of bfuchs

ASKER

Hi Experts,

It turned out that the table in question was a SQL table and therefore I was able to use the following query as pass-through which is extremely effecient.
select * from (
SELECT Patients.ID as pID, Patients.SocialSecurity,patients.FirstName,patients.LastName, PatientsCertificationPeriods_HC.*,
ROW_NUMBER() OVER(PARTITION BY PatientsCertificationPeriods_HC.PatientID 
ORDER BY PatientsCertificationPeriods_HC.FromDate desc, PatientsCertificationPeriods_HC.ToDate DESC) rnum
FROM Patients 
INNER JOIN PatientsCertificationPeriods_HC ON Patients.ID = PatientsCertificationPeriods_HC.PatientID
WHERE Patients.Active=1 
) a
where a.rnum < 3 and subject in ('Soc','WG Recert')

Open in new window


Thanks,
Ben