Max Query issue getting more than 1 record

psycoperl

Active Member
Joined
Oct 23, 2007
Messages
339
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
  3. Web
I am trying to use the following query to get the most recent comment record for a student. I am expecting either 0 or 1 records, however when I am running it I am sometimes getting more than one.

SELECT Max(CommentDate) AS MaxDate,
ReceivedOnATBEligible, NotATBeligible
FROM tblComments
GROUP BY StudentID, ReceivedOnATBEligible, NotATBeligible
HAVING (((StudentID)="000000000'));


Can anyone please advise?

Thanks

tblComments:
ATBCommentsID StudentID CommentDate ReturnedToSender Comment ReceivedOnATBEligible NotATBeligible
1 000000000 6/30/2011 1:50:55 PM True Test Record False True
2 000000000 6/30/2011 2:41:14 PM False True False
4 000000000 6/30/2011 2:41:41 PM False False True
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
It's because you are including other fields where the values are different for each record.

It appears that the values for fields ReceivedOnATBEligible and NotATBeligible are different.
 
Upvote 0
There's more to it, including some trickery. When you group by some fields and then aggregate others, you'll get the aggregations for each unique value of the grouped fields. In this case you'll get the max date for each unique StudentID/ReceivedOnATBEligible/NotATBeligible triple, so if student #1 has two records where NotATBeligible is true in one and false in another, you'll get two maximum dates.

It seems what you want is one record per student, namely the one with the maximum date, plus whatever the ReceivedOnATBEligible and NotATBeligible values are on that record. For that you need two queries.

The first query -- call it StudentsMaxDates -- just gets each student plus their max dates:
Code:
SELECT tblComments.StudentID, Max(tblComments.CommentDate) AS MaxOfCommentDate
FROM tblComments
GROUP BY tblComments.StudentID;
The second query joins with StudentsMaxDates to get the other fields:
Code:
SELECT tblComments.*
FROM tblComments INNER JOIN [B]StudentsMaxDates[/B]
ON (tblComments.CommentDate = [B]StudentsMaxDates[/B].MaxOfCommentDate)
     AND (tblComments.StudentID = [B]StudentsMaxDates[/B].StudentID
     );
 
Upvote 0

Forum statistics

Threads
1,224,537
Messages
6,179,405
Members
452,911
Latest member
a_barila

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