R
Rémi
Hello.
I've got a client table and an appointment table. I'd like to display
a list of clients and, for each, the date and reason of the *last*
appointment the client's had. (A read-only form, of course.) I've
been thinking about how to best query for this data.
I thought that First() or Last() aggregate functions - which I've never
used - might do the trick. My Jet SQL reference states that "they
return the value of a specified field in the first or last record,
respectively, of the result set returned by a query." This would work
great if First() and/or Last() take into consideration the ORDER BY
clause I apply to the subquery.
It was pretty easy to uncover the docs aren't quite accurate. (Or, I
suppose, the subquery used isn't considered to be the "result set
returned".) After loading a few test records, I tried this:
qrySortedAppointments:
SELECT clientId, appointmentId, appointmentDate
FROM tblAppointment
ORDER BY clientId, appointmentDate DESC;
qryTestFetchFirstApp:
SELECT clientId, First(qrySortedAppointments.appointmentId) As
MostRecentAppointment
FROM qrySortedAppointments
GROUP BY clientId
When changing qrySortedAppointment's sorting order from DESC to ASC, I
still get the same results in the second query.
My SQL is pretty good, but I'm scratching my head a bit with this
one... Easy enough to determine the earliest date with Min(), but I'll
have problems if a client had more than one appointment on a given day.
(The date field won't have a time component.) I also can't fetch the
Max() autoincrement, since they won't necessarily be entered in
chronological order.
Anyone offer suggestions?
Regards,
Remi.
I've got a client table and an appointment table. I'd like to display
a list of clients and, for each, the date and reason of the *last*
appointment the client's had. (A read-only form, of course.) I've
been thinking about how to best query for this data.
I thought that First() or Last() aggregate functions - which I've never
used - might do the trick. My Jet SQL reference states that "they
return the value of a specified field in the first or last record,
respectively, of the result set returned by a query." This would work
great if First() and/or Last() take into consideration the ORDER BY
clause I apply to the subquery.
It was pretty easy to uncover the docs aren't quite accurate. (Or, I
suppose, the subquery used isn't considered to be the "result set
returned".) After loading a few test records, I tried this:
qrySortedAppointments:
SELECT clientId, appointmentId, appointmentDate
FROM tblAppointment
ORDER BY clientId, appointmentDate DESC;
qryTestFetchFirstApp:
SELECT clientId, First(qrySortedAppointments.appointmentId) As
MostRecentAppointment
FROM qrySortedAppointments
GROUP BY clientId
When changing qrySortedAppointment's sorting order from DESC to ASC, I
still get the same results in the second query.
My SQL is pretty good, but I'm scratching my head a bit with this
one... Easy enough to determine the earliest date with Min(), but I'll
have problems if a client had more than one appointment on a given day.
(The date field won't have a time component.) I also can't fetch the
Max() autoincrement, since they won't necessarily be entered in
chronological order.
Anyone offer suggestions?
Regards,
Remi.