bfuchs
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.
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.
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
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
Or you would make what I provided a subquery and then join the table to the ID field of the subquery.
ASKER
Thanks Experts!
Interestingly, when I tried on the actual table needed, the diff was minor, less then 00.1%
Interestingly, when I tried on the actual table needed, the diff was minor, less then 00.1%
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.
Thanks,
Ben
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')
Thanks,
Ben