Returning the previous enrollment date for a client?

loopa1

Board Regular
Joined
Sep 3, 2006
Messages
156
Hi all

I have a table which contains over 5000 records. One student can be present many times and they have a unique ID. Each row relates to a student and contains the date of when they enrolled on a specific course.

How do I query the table so I return the second most recent enrollment date? So if a client has enrolled 5 times, with the 5th being the most recent, I want the date of the 4th. Similarly, if the client has enrolled 30 times, I'd want the date of the 29th time.

The enrollment date field is sorted in ascending order (if that matters).

I'm thinking of some combination of 'Large', 'Index' and 'Rank' but I have no idea where to start or if these are in fact Excel only functions.

Thanks.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You need two queries, one to get the max (most recent) of each student's dates, and another one which uses the first one's results in the criteria that will say "less than the max date".

Assume your table is called "StudentsDates", and the relevant fields are called "Student" and "Date".

Here's the SQL for the max-date query, called "StudentsMaxDates":
Code:
SELECT StudentsDates.Student, Max(StudentsDates.Date) AS MaxOfDate
FROM StudentsDates
GROUP BY StudentsDates.Student;

Here's the SQL for the 2nd-max query:
Code:
SELECT StudentsDates.Student, Max(StudentsDates.Date) AS MaxOfDate1
FROM StudentsDates
INNER JOIN StudentsMaxDates
ON StudentsDates.Student = StudentsMaxDates.Student
WHERE (((StudentsDates.Date)<[StudentsMaxDates].[MaxOfDate]))
GROUP BY StudentsDates.Student;

Once you put in the SQL (in View/SQL), you should view the Design mode to see how each one was done.
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,820
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top