Solved

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

Posted on 2016-08-02
9
101 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 52

Expert Comment

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

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 4

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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
LVL 50

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
 
LVL 48

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 4

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 48

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 4

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 4

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

752 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