My SQL skills are pretty basic, but thanks to the help of some folks here I managed to get a query that works for my situation. Unfortunately the situation changed, and I really have no idea how to go about rewriting this to make it work.
The basics: Two tables, with a one-to-many relationship, like this (most of the extra fields removed for simplicity):
My previous need was to create a select where I got all of the items in MedicarePatients, along with their latest (in time) checkup (with some ordering). The select (thanks so much to the folks here) I'm using now looks like this:
(some additional order bys removed).
But here's the new requirement: from the MedicareCheckups table I need both the latest record where Scheduled is false and the latest one where Scheduled as true, hopefully all in one select. Obviously it's easy for me to add a WHERE clause to the sub-select to get one or the other, but I can't figure out how to join the MedicarePatients table with the MedicareCheckups table twice (or whatever I need to do).
Any help would be greatly appreciated.
The basics: Two tables, with a one-to-many relationship, like this (most of the extra fields removed for simplicity):
Code:
[b]MedicarePatients[/b]
ID -- Primary Key, autonumber
FirstName
LastName
Hold -- True/False boolean
[b]MedicareCheckups[/b]
ID -- Foreign Key, to MedicarePatients' primary
Checkup -- DateTime
Scheduled -- True/False boolean, scheduled checkup vs. actual checkup
Code:
SELECT P.ID, P.LastName, P.FirstName, P.Hold, D.LastDate, P.Notes
FROM MedicarePatients P LEFT JOIN (
SELECT ID, Max(Checkup) As LastDate FROM MedicareCheckups GROUP BY ID
) D ON P.ID = D.ID ORDER BY P.Hold DESC
But here's the new requirement: from the MedicareCheckups table I need both the latest record where Scheduled is false and the latest one where Scheduled as true, hopefully all in one select. Obviously it's easy for me to add a WHERE clause to the sub-select to get one or the other, but I can't figure out how to join the MedicarePatients table with the MedicareCheckups table twice (or whatever I need to do).
Any help would be greatly appreciated.