Get only latest date (1 Viewer)

terrytek

Registered User.
Local time
Today, 11:20
Joined
Aug 12, 2016
Messages
75
I have the following query, which I want to return ONLY the most recent test date:
Code:
SELECT qryTestingMostRecentTest.StudentFileName, Last(qryTestingMostRecentTest.TestDate) AS LastOfTestDate, qryTestingMostRecentTest.NextForm, qryTestingMostRecentTest.Class
FROM qryTestingMostRecentTest
GROUP BY qryTestingMostRecentTest.StudentFileName, qryTestingMostRecentTest.NextForm, qryTestingMostRecentTest.Class;

But it is giving me all the test dates for each student when I only want the latest.
 

Minty

AWF VIP
Local time
Today, 16:20
Joined
Jul 26, 2013
Messages
10,368
Try using Max() instead of Last.

Last is a horrible function and doesn't do what you might have thought.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:20
Joined
Oct 29, 2018
Messages
21,456
Hi Terry,


I agree with Minty, but you might also need to use a subquery. For example, what do you get with the following modified query?


Code:
SELECT qryTestingMostRecentTest.StudentFileName, 
    qryTestingMostRecentTest.TestDate, 
    qryTestingMostRecentTest.NextForm, 
    qryTestingMostRecentTest.Class 
FROM qryTestingMostRecentTest 
INNER JOIN SELECT qryTestingMostRecentTest.StudentFileName,
    Max(qryTestingMostRecentTest.TestDate) AS LastTestDate
FROM qryTestingMostRecentTest
GROUP BY qryTestingMostRecentTest.StudentFileName) AS SQ
ON qryTestingMostRecentTest.StudentFileName=SQ.StudentFileName
    AND qryTestingMostRecentTest.TestDate=SQ.LastTestDate;
 

terrytek

Registered User.
Local time
Today, 11:20
Joined
Aug 12, 2016
Messages
75
Hi Terry,


I agree with Minty, but you might also need to use a subquery. For example, what do you get with the following modified query?


Code:
SELECT qryTestingMostRecentTest.StudentFileName, 
    qryTestingMostRecentTest.TestDate, 
    qryTestingMostRecentTest.NextForm, 
    qryTestingMostRecentTest.Class 
FROM qryTestingMostRecentTest 
INNER JOIN SELECT qryTestingMostRecentTest.StudentFileName,
    Max(qryTestingMostRecentTest.TestDate) AS LastTestDate
FROM qryTestingMostRecentTest
GROUP BY qryTestingMostRecentTest.StudentFileName) AS SQ
ON qryTestingMostRecentTest.StudentFileName=SQ.StudentFileName
    AND qryTestingMostRecentTest.TestDate=SQ.LastTestDate;

I get an error "syntax error in FROM clause".
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:20
Joined
Oct 29, 2018
Messages
21,456
Sorry, looks like I forgot to add an opening bracket here...
Code:
SELECT...
FROM...
INNER JOIN (SELECT...
...
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:20
Joined
Oct 29, 2018
Messages
21,456
That worked a charm. Thanks so much!
Hi. You're welcome. Glad to hear you got it to work. Minty and I were happy to assist. Good luck with your project.
 

Users who are viewing this thread

Top Bottom