Thanks to much help here, I've got a query that works. Now I have to revisit this query, because now I need something different, something that I don't think can be done in a simple query (I suspect it would require a stored procedure), but who knows, here's the challenge:
My current query looks like this, and it works fine for the old problem:
Very nifty, and thanks to everyone.
However, here's my new need: If S.Scheduled is True than I want D.LastDate just the way it is, but if S.Scheduled = False then I want D.LastDate + 28 days.
I can do this in the application layer, but the data doesn't come sorted in the order I need (by the date, modified or not), so it requires an array sort and stuff that I'd like to avoid if SQL can help me.
Can it be done in a select, or am I SOL?
My current query looks like this, and it works fine for the old problem:
Code:
SELECT P.ID, P.LastName, P.FirstName, P.Chart, P.BirthdayMonth, P.BirthdayDay, P.Phone, P.MCStatus, P.Hold, D.LastDate, 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 > #10/01/2004#
ORDER BY D.LastDate ASC, P.LastName ASC
However, here's my new need: If S.Scheduled is True than I want D.LastDate just the way it is, but if S.Scheduled = False then I want D.LastDate + 28 days.
I can do this in the application layer, but the data doesn't come sorted in the order I need (by the date, modified or not), so it requires an array sort and stuff that I'd like to avoid if SQL can help me.
Can it be done in a select, or am I SOL?