K
Ken Snell
Let's try this approach first:
SELECT CarePlans.*
FROM CarePlans
WHERE CarePlans.SOCStart IN
(SELECT MAX(T.SOCStart)
FROM CarePlans AS T
WHERE T.ClientID=CarePlans.ClientID);
--
Ken Snell
<MS ACCESS MVP>
SELECT CarePlans.*
FROM CarePlans
WHERE CarePlans.SOCStart IN
(SELECT MAX(T.SOCStart)
FROM CarePlans AS T
WHERE T.ClientID=CarePlans.ClientID);
--
Ken Snell
<MS ACCESS MVP>
Jeff Klein said:access 97
Ken Snell said:As asked elsethread, which ACCESS version are you using? The suggestion that
is in that other thread (using the [ ]. syntax) is something that ACCESS
does automatically in some queries when you save and close them. Can be a
real problem to maintain, as once saved, ACCESS won't let you edit them
easily. In those case, then I use a separate query that is the subquery, and
then join that query as a source table in the original query.
Post back with info and we'll see where we can go with this.
--
Ken Snell
<MS ACCESS MVP>
theJeff Klein said:Syntax error is in the FROM clause
Looks ok to me...what syntax error does it give? and, assuming that you're
doing this in SQL view, where does the cursor "go" to show you where the
error is?
--
Ken Snell
<MS ACCESS MVP>
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;
Post the actual SQL that is giving you the syntax error.
--
Ken Snell
<MS ACCESS MVP>
I am getting a syntax error in the qry_MostRecentCarePlan. I have
been
trying to figure it out but I am stumped on the subquery and
usetooof
the
"T".
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
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
muchifinto
the
message board as it might not be answered. Also, for me, this
is
hard
to
explain thoroughly. Any help is greatly appreciated and
youWhatchoose
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];
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>
message
Thanks for the reply. I am trying to make this workis
"T"
?
message
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>
=inmessage
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])