Greetings. Technically I'm using Jet SQL, but I don't think anything I'm trying to do isn't ANSI compatible, so I thought I'd try my question here.
I have two table, like this:
There's a one-to-many relationship between MedicarePatients and MedicareCheckups.
Here's the select that I currently have. Among other things, it's designed to select all of the patients and the maximum date from checkups (and has an additional join so I can know whether this date is Scheduled or not). It looks like this:
What I need to differently now, though, is rather than just getting the latest checkup date and whether it's scheduled or not, I need to get both the latest scheduled date and the latest non-scheduled date (with the current trick where non-scheduled dates get 28 days added to them, such that they sort the way I need).
If I can have both of those then I won't need that scheduled flag, since I want both (and nulls in those fields where there simply isn't a scheduled or unscheduled checkup for that patient).
I tried adding a different join in myself, but my SQL skills just aren't strong enough. Any tips or ideas?
I have two table, like this:
Code:
[b]MedicarePatients[/b]
ID -- Primary Key
LastName
FirstName
...etc...
Hold -- Boolean
[b]MedicareCheckups[/b]
ID -- Foreign key, with MedicarePatients' ID field
Checkup -- DateTime
Scheduled -- Boolean
Here's the select that I currently have. Among other things, it's designed to select all of the patients and the maximum date from checkups (and has an additional join so I can know whether this date is Scheduled or not). It looks like this:
Code:
SELECT P.ID, P.LastName, P.FirstName, P.Chart, P.BirthdayMonth, P.BirthdayDay, P.Phone, P.MCStatus, P.Hold, D.LastDate + IIF(S.Scheduled, 0, 28) AS ScheduleDate, S.Scheduled, P.Notes
FROM (
MedicarePatients P
INNER JOIN (
SELECT ID, Max(Checkup) As LastDate
FROM MedicareCheckups
GROUP BY ID) D
ON P.ID = D.ID)
INNER JOIN MedicareCheckups S
ON D.ID=S.ID AND D.LastDate=S.Checkup
WHERE P.Hold = False AND Checkup > #8/20/2004#
ORDER BY D.LastDate + IIF(S.Scheduled, 0, 28) ASC, P.LastName ASC
If I can have both of those then I won't need that scheduled flag, since I want both (and nulls in those fields where there simply isn't a scheduled or unscheduled checkup for that patient).
I tried adding a different join in myself, but my SQL skills just aren't strong enough. Any tips or ideas?