By chance, is Client Number a text field? Assuming that it is, let's try
this SQL statement (am delimiting the Client Number value with ' characters,
and have again omitted the # delimiters for the Service date value):
SELECT [Client Number], [Service date],
(DateDiff("d",Nz(DMax("Service date", "TableName",
"[Client Number]='" &
[Client Number] & "' And [Service date]<" &
[Service date]), [Service date]), [Service date])) AS DaysSinceLast
FROM TableName
ORDER BY [Client Number], [Service date];
NOTE: You must replace TableName in the above with the real name of the
table. I am using TableName as a generic name because you didn't post the
real name of the table.
If Client Number is a numeric field, then use this:
SELECT [Client Number], [Service date],
(DateDiff("d",Nz(DMax("Service date", "TableName",
"[Client Number]=" &
[Client Number] & " And [Service date]<" &
[Service date]), [Service date]), [Service date])) AS DaysSinceLast
FROM TableName
ORDER BY [Client Number], [Service date];
NOTE: You must replace TableName in the above with the real name of the
table. I am using TableName as a generic name because you didn't post the
real name of the table.
--
Ken Snell
<MS ACCESS MVP>
--
Ken Snell
<MS ACCESS MVP>
Analyst 1 said:
It is formatted as date/time.
Ken Snell said:
What is the data type for Servicedate field in the table?
--
Ken Snell
<MS ACCESS MVP>
I get an error message that indicates an invalid date format.
:
Let's try this (I had omitted the # delimiter for date, and I'm now
formatting the date into the US format):
SELECT [Client Number], [Service date],
(DateDiff("d",Nz(DMax("Service date", "TableName",
"[Client Number]=" &
[Client Number] & " And [Service date]<#" &
Format([Service date],"m/d/yyyy") & "#"), [Service date]),
[Service date])) AS DaysSinceLast
FROM TableName
ORDER BY [Client Number], [Service date];
--
Ken Snell
<MS ACCESS MVP>
Hi Ken,
The SQL you sent me returned "0" for dayssincelast .
:
Thank you I'll try it.
:
Not sure what you mean by "count", but to get the differences in
days
between each chronological record, you can use a query like this:
SELECT [Client Number], [Service date],
(DateDiff("d",Nz(DMax("Service date", "TableName",
"[Client Number]=" &
[Client Number] & " And [Service date]<" &
[Service date]), [Service date]), [Service date])) AS
DaysSinceLast
FROM TableName
ORDER BY [Client Number], [Service date];
--
Ken Snell
<MS ACCESS MVP>
Here is an example of the data:
Client Number Service date
22 7/1/2002
22 7/2/2002
22 7/3/2002
22 7/4/2002
22 7/5/2002
22 9/9/2003
22 9/10/2003
22 9/11/2003
22 1/1/2005
22 2/10/2005
22 4/1/2005
22 4/2/2005
33 6/1/2003
33 7/1/2003
33 7/2/2003
33 7/3/2003
33 7/4/2003
33 7/5/2003
33 8/5/2005
33 8/6/2005
33 8/7/2005
So, Client #22 would have 5 separate episodes (an episode being
one
day or
a
number of consecutive days of service) the first with 5
consecutive
days,
the second with 3 consecutive days and so on....
The output I would hope to have would count the "episodes" and
consecutive
days of each.
Does this make sense?
:
You'll need to post examples of the data that you're using and
examples
of
the result that you expect from those data. Sounds as if you
want
to
have
the query "look back" in the data records to find a "start
date"?
--
Ken Snell
<MS ACCESS MVP>
message
Thank you for your response. I have used the datediff
function
in
the
past,
however in this querey it will not work because it is
possible
for
clients
to
have a break in service and datediff will count the days
inbetween. I
need a
funtion that only counts consecutive days per each client.
something
like
:
if client number = client number and date =date+1 then
datediff.
I
just
don't know how to do this in access.
Thanks again
:
Check out the DateDiff function.
--
Ken Snell
<MS ACCESS MVP>
message
I am trying to count the number of consecutive days a
client
stays in
serivces. My variables are: client number and a service
date.
Thanks