Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-08-02
9
Medium Priority
?
105 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 53

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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 51

Accepted Solution

by:
Gustav Brock earned 1000 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 1000 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

730 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