Hi Jeff and Dale,
PMFBI
In the "speed-me-up" world,
I believe this is a good method,
but I get the idea we don't want
to *limit* CarePlans to "most recent."
I might suggest removing
"AND CarePlans.SOCStart = T1.MostRecent"
so preliminary query might be better named like
"qryCarePlanWithMostRecent"
SELECT CarePlans.*
FROM CarePlans
INNER JOIN
(SELECT T.ClientID, MAX(T.SOCStart) as MostRecent
FROM CarePlans T
GROUP BY T.ClientID) as T1
ON CarePlans.ClientID = T1.ClientID;
so your final query might be:
PARAMETERS [MonthStartDate] DateTime, [MonthEndDate] DateTime;
SELECT
[ClientLastName] & ", " & [ClientFirstName] AS ClientLastandFirst,
qryCarePlanWithMostRecent.SOCStart,
qryCarePlanWithMostRecent.NOAEnd,
nz([noaend],#12/31/9999#) AS ActualNOAEnd
FROM
Clients
INNER JOIN
qryCarePlanWithMostRecent
ON Clients.ClientID = qryCarePlanWithMostRecent.ClientID
WHERE
(((qryCarePlanWithMostRecent.SOCStart)<=[MonthStartDate])
AND
((nz([noaend],#12/31/9999#))>=[MonthStartDate])
AND ((Clients.Inactive)=0))
OR
(((qryCarePlanWithMostRecent.SOCStart)>[MonthStartDate]
And
(qryCarePlanWithMostRecent.SOCStart)<=[MonthEndDate]))
ORDER BY [ClientLastName] & ", " & [ClientFirstName];
or just include in original as subquery (not necessarily "better" in my view)
PARAMETERS [MonthStartDate] DateTime, [MonthEndDate] DateTime;
SELECT
[ClientLastName] & ", " & [ClientFirstName] AS ClientLastandFirst,
CarePlans.SOCStart,
CarePlans.NOAEnd,
nz([noaend],#12/31/9999#) AS ActualNOAEnd,
T1.MostRecent
FROM
(Clients
INNER JOIN
CarePlans
ON Clients.ClientID = CarePlans.ClientID)
INNER JOIN
(SELECT T.ClientID, MAX(T.SOCStart) as MostRecent
FROM CarePlans T
GROUP BY T.ClientID) as T1
ON CarePlans.ClientID = T1.ClientID
WHERE
(((CarePlans.SOCStart)<=[MonthStartDate])
AND
((nz([noaend],#12/31/9999#))>=[MonthStartDate])
AND ((Clients.Inactive)=0))
OR
(((CarePlans.SOCStart)>[MonthStartDate]
And
(CarePlans.SOCStart)<=[MonthEndDate]))
ORDER BY [ClientLastName] & ", " & [ClientFirstName];
Apologies again for butting in.
Gary Walter
Jeff,
Does your CarePlans table contain an ID field?
Alright, Create a new query (call it qry_MostRecentCarePlan)
SELECT CarePlans.*
FROM CarePlans
INNER JOIN
(SELECT T.ClientID, MAX(T.SOCStart) as MostRecent
FROM CarePlans T
GROUP BY T.ClientID) as T1
ON CarePlans.ClientID = T1.ClientID
AND CarePlans.SOCStart = T1.MostRecent
This query should give you a single record for each client, and all the
information will be from the most recent careplan for that individual.
(assuming that SOCStart is the field that indicates the most recent plan.
Now, replace references to the Careplan table in your query with
qry_MostRecentCarePlan, and you should have what you are looking for.
HTH
Dale
Jeff Klein said:
OK...The code I have sent is an example that I created to keep things simple
as the real code is much more complicated. You clearly know enough to solve
my problem although I am having trouble using the answer you have given and
relating back to the original code. Below is the original code. This shows
many records for each client. I just want the latest (or last) for each
client. Sorry about the confusion but I try not to put too much into the
message board as it might not be answered. Also, for me, this is hard to
explain thoroughly. Any help is greatly appreciated and if you choose not
to spend any more time on this I will understand. Thanks Jeff.
PARAMETERS [MonthStartDate] DateTime, [MonthEndDate] DateTime;
SELECT [ClientLastName] & ", " & [ClientFirstName] AS ClientLastandFirst,
CarePlans.SOCStart, CarePlans.NOAEnd, nz([noaend],#12/31/9999#) AS
ActualNOAEnd
FROM Clients INNER JOIN CarePlans ON Clients.ClientID = CarePlans.ClientID
WHERE (((CarePlans.SOCStart)<=[MonthStartDate]) AND
((nz([noaend],#12/31/9999#))>=[MonthStartDate]) AND ((Clients.Inactive)=0))
OR
(((CarePlans.SOCStart)>[MonthStartDate] And
(CarePlans.SOCStart)<=[MonthEndDate]))
ORDER BY [ClientLastName] & ", " & [ClientFirstName];
Ken Snell said:
T is an alias for the table Orders. You need to differentiate between the
two copies of Orders table when you use a subquery.
I also note that I mistyped ClientID in the last line...left out the "t" by
accident.
--
Ken Snell
<MS ACCESS MVP>
Thanks for the reply. I am trying to make this work What is "T" ?
You can replace it with a subquery:
(SELECT Max(T.Orders) FROM
Orders AS T WHERE
T.ClientID = Orders.ClienID)
--
Ken Snell
<MS ACCESS MVP>
I am using DMax to show the last "SOCStart" for all customers. This
function makes the query run very slow. Is there another way to do
this
that might run faster? Below is the code example. Thank you.
DMax("[SOCStart]","[Orders]","[Orders].[ClientID] = " &
[Orders].[ClientID])