'MaxOf' Query returning duplicates (1 Viewer)

ljoekelsoey4

Registered User.
Local time
Today, 18:26
Joined
Mar 16, 2017
Messages
49
I've two tables, tblPatients where the primary key is PatientID, and tblAppointments where the primary key is AppointmentID;

I've multiple appointment entries for each patient, as well as a follow up date for when the next appointment is to be booked. When trying to create a 'max of' query for appointment date, the query is returning 2 appointment records for each patient record; one is the max of appointment with the follow up date field empty, and the other is the max appointment date with the follow up date not empty. I want to return the max of appointment regardless of follow up date, but i need to be able to see what the follow up date is in the query result.

Thanks for your help!


SQL:

SELECT tblAppointments.PatientID, Max(tblAppointments.AppointmentDate) AS MaxOfAppointmentDate, tblAppointments.FollowUpDate
FROM tblAppointments
GROUP BY tblAppointments.PatientID, tblAppointments.FollowUpDate
ORDER BY tblAppointments.PatientID;
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    39.6 KB · Views: 85

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 03:26
Joined
Jan 20, 2009
Messages
12,852
You need to do the aggregate query without the Followup date first then join that query back to the table again to pick up the followup date.
 

ljoekelsoey4

Registered User.
Local time
Today, 18:26
Joined
Mar 16, 2017
Messages
49
You need to do the aggregate query without the Followup date first then join that query back to the table again to pick up the followup date.

Thanks for the quick reply. Not really sure how to join the query back to the table though?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:26
Joined
May 7, 2009
Messages
19,237
On your Select statement, use Max(tblAppointments.FollowUpdate)
then ONLY group by PatientID.
 

ljoekelsoey4

Registered User.
Local time
Today, 18:26
Joined
Mar 16, 2017
Messages
49
On your Select statement, use Max(tblAppointments.FollowUpdate)
then ONLY group by PatientID.

Is that instead of Max(tblAppointments.AppointmentDate)? Would I then need to delete tbl.Appointments.FollowUpDate from the SELECT statement?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:26
Joined
May 7, 2009
Messages
19,237
Yes u should
 

ljoekelsoey4

Registered User.
Local time
Today, 18:26
Joined
Mar 16, 2017
Messages
49
Yes u should

So the SQL would then look like this;

SELECT tblAppointments.PatientID, Max(tblAppointments.FollowUpDate) AS MaxOfAppointmentDate
FROM tblAppointments
GROUP BY tblAppointments.PatientID ?

Doesn't quite seem right to me
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:26
Joined
May 7, 2009
Messages
19,237
sorry did not read thoroughly your last post.
you add also Max(tblAppointmets.AppointmentDate) As MaxAppointmentDate on your query.
 

Users who are viewing this thread

Top Bottom