Solved

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

Posted on 2016-08-02
9
90 Views
Last Modified: 2016-08-03
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.
0
Comment
Question by:bfuchs
9 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41740041
what's the values in the Day field and what's its data type?
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 41740063
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
0
 
LVL 3

Author Comment

by:bfuchs
ID: 41740106
@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
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 250 total points
ID: 41740262
You can use Top 2 for each:
Select * 
From YourTable
Where ID In 
    (Select Top 2 T.ID 
    From YourTable As T 
    Where T.EmployeeID = YourTable.EmployeeID 
    Order By T.[Day] Desc)
Order By EmployeeID, [Day] Desc

Open in new window

/gustav
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 250 total points
ID: 41740392
change:

GROUP BY T1.ID, T1.Employee, T1.Day, T1.SubvType

to:

GROUP BY T1.ID, T1.Employee, T1.Day, T1.SupvType
0
 
LVL 3

Author Comment

by:bfuchs
ID: 41741187
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
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 41741248
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.
0
 
LVL 3

Author Closing Comment

by:bfuchs
ID: 41741335
Thanks Experts!

Interestingly, when I tried on the actual table needed, the diff was minor, less then 00.1%
0
 
LVL 3

Author Comment

by:bfuchs
ID: 41741730
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
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now