Solved

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

Posted on 2016-08-02
9
103 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
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 Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 
LVL 51

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
Dale Fye 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
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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

623 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