Is there a way to find the records with the 4 most recent dates? (1 Viewer)

terrytek

Registered User.
Local time
Today, 00:29
Joined
Aug 12, 2016
Messages
75
I need to find instances of students that have three or four tests by their three or four most recent test dates. Is there a way to query with that criterion?

Thanks.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:29
Joined
Oct 29, 2018
Messages
21,467
Hi,

Sounds like you could use a "Top N Per Group" query.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 14:29
Joined
Jan 20, 2009
Messages
12,852
Sounds like you could use a "Top N Per Group" query.

Googling turns up a lot of unsatisfactory attempts to do this in Access.

I think it would have to be done as an aggregate query then a TOP query on each of the groups. I don't think it can written just as a query in Access. The TOP queries would have to be dynamically built in code.

It can be done in SQL Server with a PARTITION. (Partition function in Access is a different animal.)
 

sxschech

Registered User.
Local time
Yesterday, 21:29
Joined
Mar 2, 2010
Messages
792
Here is one I did based on Allen Browne's link example to get the 10 most recent documents by date within a group based on first 11 chars of the document number:

Code:
SELECT DocumentNo, DateModified
FROM (SELECT left(qryLetters.DocumentNo,11) AS DOC11, qryLetters.DateModified, qryLetters.DocumentNo
FROM qryLetters
WHERE qryLetters.DocumentNo IN
   (SELECT TOP 10 DocumentNo                            
   FROM qryLetters AS Dupe                              
   WHERE left(Dupe.DocumentNo,11) = left(qryLetters.DocumentNo,11)        
   ORDER BY Dupe.DateModified DESC, Dupe.DocumentNo DESC) 
ORDER BY left(qryLetters.DocumentNo,11), qryLetters.DateModified, qryLetters.DocumentNo)  AS TOP10
ORDER BY DOC11, getnumber([DocumentNo]);
 

Users who are viewing this thread

Top Bottom