Time Between the Time of the Current Record and the Next Earliest

S

Steve

I am trying to put together the SQL statement to get me the time (in
minutes)between the time entry for the current record and the time of the
most recent entry just before it. For instance, in my query results, I get
entry number 10 with a time stamp of 11:00:00 AM and in the same recordset I
have entry 11 with a time stamp of 11:15:00 AM. I want the row with entry 11
to show 00:15:00. And row 10 to show the difference between it and the most
recent entry before it, etc, etc. I've been toying with the DateDiff function
and the Max function to get the most recent time before the record under
consideration, but I can't get it to work. Any ideas?


Thanks,
Steve
 
S

Steve

Here it is:

SELECT tblCompletedMessages.CMSID, tblCompletedMessages.DateCompleted,
tblCompletedMessages.TimeCompleted
FROM tblCompletedMessages;

I need the WHERE condition to include the criteria of DateCompleted equals
the date selected on a calendar control on my form. CMSID is the employee ID
of the person that the entry refers to. The TimeCompleted field is what I am
trying to calculate the difference upon.

Thanks,
Steve
 
K

KARL DEWEY

Try this --
SELECT tblCompletedMessages.CMSID, tblCompletedMessages.DateCompleted,
tblCompletedMessages.TimeCompleted, (SELECT TOP 1
DateDiff("n",[XX].DateCompleted, tblCompletedMessages.TimeCompleted) FROM
[tblCompletedMessages] AS [XX] INNER JOIN [tblCompletedMessages] ON
tblCompletedMessages.CMSID = [XX].CMSID WHERE [XX].DateCompleted <
tblCompletedMessages.DateCompleted ORDER BY [XX].DateCompleted DESC) AS
Time_Diff
FROM tblCompletedMessages;
 
K

KenSheridan via AccessMonster.com

Or this:

PARAMETERS
Forms!YourForm!YourCalendarControl DATETIME;
SELECT CMSID, DateCompleted, TimeCompleted,
DATEDIFF("n",
(SELECT MAX(TimeCompleted)
FROM tblCompletedMessages AS CM2
WHERE CM2.TimeCompleted
< CM1.TimeCompleted),
TimeCompleted) AS TimeDifference
FROM tblCompletedMessages AS CM1
WHERE DateCompleted = Forms!YourForm!YourCalendarControl;

or if you want the time difference between rows with the same CMSID value, i.
e. since the last entry by the CMSID rather than the last entry per se:

PARAMETERS
Forms!YourForm!YourCalendarControl DATETIME;
SELECT CMSID, DateCompleted, TimeCompleted,
DATEDIFF("n",
(SELECT MAX(TimeCompleted)
FROM tblCompletedMessages AS CM2
WHERE C2.CMSID = C1.CMSID
AND CM2.TimeCompleted
< CM1.TimeCompleted),
TimeCompleted) AS TimeDifference
FROM tblCompletedMessages AS CM1
WHERE DateCompleted = Forms!YourForm!YourCalendarControl;

I've assumed that you are using the Date() and Now() functions to
automatically enter the DateCompleted and TimeCompleted values, in which case
the TimeCompleted column will also include the date, even if you are
formatting it to show the time only. If on the other hand you are either
manually entering the TimeCompleted value or are 'stripping off' the date
with TimeValue(Now()) (which in fact does not remove the date element from
the value but sets it to Access's day-zero of 30 December 1899) then you'll
need to combine both values like so when computing the difference in minutes:

PARAMETERS
Forms!YourForm!YourCalendarControl DATETIME;
SELECT CMSID, DateCompleted, TimeCompleted,
DATEDIFF("n",
(SELECT MAX(DateCompleted+TimeCompleted)
FROM tblCompletedMessages AS CM2
WHERE CM2.DateCompleted+CM2.TimeCompleted
< CM1.DateCompleted+CM1.TimeCompleted),
DateCompleted+TimeCompleted) AS TimeDifference
FROM tblCompletedMessages AS CM1
WHERE DateCompleted = Forms!YourForm!YourCalendarControl;

or:

PARAMETERS
Forms!YourForm!YourCalendarControl DATETIME;
SELECT CMSID, DateCompleted, TimeCompleted,
DATEDIFF("n",
(SELECT MAX(DateCompleted+TimeCompleted)
FROM tblCompletedMessages AS CM2
WHERE C2.CMSID = C1.CMSID
AND CM2.DateCompleted+CM2.TimeCompleted
< CM1.DateCompleted+CM1.TimeCompleted),
DateCompleted+TimeCompleted) AS TimeDifference
FROM tblCompletedMessages AS CM1
WHERE DateCompleted = Forms!YourForm!YourCalendarControl;

Ken Sheridan
Stafford, England
Here it is:

SELECT tblCompletedMessages.CMSID, tblCompletedMessages.DateCompleted,
tblCompletedMessages.TimeCompleted
FROM tblCompletedMessages;

I need the WHERE condition to include the criteria of DateCompleted equals
the date selected on a calendar control on my form. CMSID is the employee ID
of the person that the entry refers to. The TimeCompleted field is what I am
trying to calculate the difference upon.

Thanks,
Steve
Post a SQL of a select query containing all the fields to use.
[quoted text clipped - 10 lines]
 
S

Steve

Ken

Your first example worked fine. The date is from the date function but time
is from the Time function (Long Time format).

Karl

Your's didn't work. The calculated fields for the minutes between completion
were blank.

Thanks to you both. This group has been a life saver for me many times.


Steve

KenSheridan via AccessMonster.com said:
Or this:

PARAMETERS
Forms!YourForm!YourCalendarControl DATETIME;
SELECT CMSID, DateCompleted, TimeCompleted,
DATEDIFF("n",
(SELECT MAX(TimeCompleted)
FROM tblCompletedMessages AS CM2
WHERE CM2.TimeCompleted
< CM1.TimeCompleted),
TimeCompleted) AS TimeDifference
FROM tblCompletedMessages AS CM1
WHERE DateCompleted = Forms!YourForm!YourCalendarControl;

or if you want the time difference between rows with the same CMSID value, i.
e. since the last entry by the CMSID rather than the last entry per se:

PARAMETERS
Forms!YourForm!YourCalendarControl DATETIME;
SELECT CMSID, DateCompleted, TimeCompleted,
DATEDIFF("n",
(SELECT MAX(TimeCompleted)
FROM tblCompletedMessages AS CM2
WHERE C2.CMSID = C1.CMSID
AND CM2.TimeCompleted
< CM1.TimeCompleted),
TimeCompleted) AS TimeDifference
FROM tblCompletedMessages AS CM1
WHERE DateCompleted = Forms!YourForm!YourCalendarControl;

I've assumed that you are using the Date() and Now() functions to
automatically enter the DateCompleted and TimeCompleted values, in which case
the TimeCompleted column will also include the date, even if you are
formatting it to show the time only. If on the other hand you are either
manually entering the TimeCompleted value or are 'stripping off' the date
with TimeValue(Now()) (which in fact does not remove the date element from
the value but sets it to Access's day-zero of 30 December 1899) then you'll
need to combine both values like so when computing the difference in minutes:

PARAMETERS
Forms!YourForm!YourCalendarControl DATETIME;
SELECT CMSID, DateCompleted, TimeCompleted,
DATEDIFF("n",
(SELECT MAX(DateCompleted+TimeCompleted)
FROM tblCompletedMessages AS CM2
WHERE CM2.DateCompleted+CM2.TimeCompleted
< CM1.DateCompleted+CM1.TimeCompleted),
DateCompleted+TimeCompleted) AS TimeDifference
FROM tblCompletedMessages AS CM1
WHERE DateCompleted = Forms!YourForm!YourCalendarControl;

or:

PARAMETERS
Forms!YourForm!YourCalendarControl DATETIME;
SELECT CMSID, DateCompleted, TimeCompleted,
DATEDIFF("n",
(SELECT MAX(DateCompleted+TimeCompleted)
FROM tblCompletedMessages AS CM2
WHERE C2.CMSID = C1.CMSID
AND CM2.DateCompleted+CM2.TimeCompleted
< CM1.DateCompleted+CM1.TimeCompleted),
DateCompleted+TimeCompleted) AS TimeDifference
FROM tblCompletedMessages AS CM1
WHERE DateCompleted = Forms!YourForm!YourCalendarControl;

Ken Sheridan
Stafford, England
Here it is:

SELECT tblCompletedMessages.CMSID, tblCompletedMessages.DateCompleted,
tblCompletedMessages.TimeCompleted
FROM tblCompletedMessages;

I need the WHERE condition to include the criteria of DateCompleted equals
the date selected on a calendar control on my form. CMSID is the employee ID
of the person that the entry refers to. The TimeCompleted field is what I am
trying to calculate the difference upon.

Thanks,
Steve
Post a SQL of a select query containing all the fields to use.
[quoted text clipped - 10 lines]
Thanks,
Steve

--



.
 
K

KenSheridan via AccessMonster.com

Good.

I'd forgotten about the Time function, though like the TimeValue function
that doesn't do quite what it says on the tin in fact, as there is no such
thing in Access as a time value per se, only a date/time value. To see this
enter the following in the debug window (press Ctrl-G to open it)

? Format(Time(),"dd mmmm yyyy hh:nn:ss")

and hit Enter. See what you get.

Ken Sheridan
Stafford, England
Ken

Your first example worked fine. The date is from the date function but time
is from the Time function (Long Time format).

Karl

Your's didn't work. The calculated fields for the minutes between completion
were blank.

Thanks to you both. This group has been a life saver for me many times.

Steve
[quoted text clipped - 84 lines]
 
S

Steve

I see. It pulls the null date of 12-30-1899 and the current system time. But
I usually format the time function to "long time" or "medium time" before
appending to my table. But I do see what your saying about not taking too
much for granted.

Steve

KenSheridan via AccessMonster.com said:
Good.

I'd forgotten about the Time function, though like the TimeValue function
that doesn't do quite what it says on the tin in fact, as there is no such
thing in Access as a time value per se, only a date/time value. To see this
enter the following in the debug window (press Ctrl-G to open it)

? Format(Time(),"dd mmmm yyyy hh:nn:ss")

and hit Enter. See what you get.

Ken Sheridan
Stafford, England
Ken

Your first example worked fine. The date is from the date function but time
is from the Time function (Long Time format).

Karl

Your's didn't work. The calculated fields for the minutes between completion
were blank.

Thanks to you both. This group has been a life saver for me many times.

Steve
[quoted text clipped - 84 lines]
Thanks,
Steve
 
K

KenSheridan via AccessMonster.com

In fact its not a Null date, but a zero date; not the same thing as Null is
the absence of a value, zero is a value. The date/time data type in Access
is implemented as a 64 bit floating point number in which the integer part
represents the days and the fractional part the times of day. Zero is 30
December 1899 00:00:00.

So when you enter a time value manually, call the Time function or call the
TimeValue function with a date/time value as its argument, the value is zero
point something. At the time I'm writing this the value of the time function
as a number is:

0.867060185185185

This can be seen by entering the following in the debug window:

? CDec(Time())

This represents a date/time of:

30 December 1899 20:48:34

as its now 48 minutes past eight in the evening.

The Date() function's return value converted to a decimal number is:

40139

i.e. its 40139 days from 30 December 1899. There is no fractional element
to this value of course as the Date function returns the date/time value at
midnight at the start of the day, i.e. 22 November 2009 00:00:00.

So you can see that when you add the date time values #22 November 2009 00:
00:00# and #30 December 1899 20:48:34# you are in fact adding 40139 to 0.
867060185185185 to give 40139.867060185185185, which as a date/time is:

22 November 2009 20:48:34

You can see this by entering the following in the debug window:

? Format(40139 + 0.867060185185185, "dd mmmm yyyy hh:nn:ss")

which is the same as entering:

? Format(#22 November 2009 00:00:00# + #30 December 1899 20:48:34#, "dd
mmmm yyyy hh:nn:ss")

which, at the time now, is the same as entering:

? Format(Date() + Time(), "dd mmmm yyyy hh:nn:ss")

Once you understand that this is how date/time values are implemented in
Access the implications become clear:

In your table you are using the Time() function to insert the TimeCompleted
value, so in each case the underlying value in the column is actually a time
on 30 December 1899. The first query I posted:

PARAMETERS
Forms!YourForm!YourCalendarControl DATETIME;
SELECT CMSID, DateCompleted, TimeCompleted,
DATEDIFF("n",
(SELECT MAX(TimeCompleted)
FROM tblCompletedMessages AS CM2
WHERE CM2.TimeCompleted
< CM1.TimeCompleted),
TimeCompleted) AS TimeDifference
FROM tblCompletedMessages AS CM1
WHERE DateCompleted = Forms!YourForm!YourCalendarControl;

compares the TimeCompleted value in each row to the highest TimeCompleted
value in other rows where it is less than the current one, i.e. the preceding
time. But lets say a value 15 minutes before the current one was inserted in
a row today, but one only 10 minutes before was inserted yesterday. The
subquery will return the one from yesterday not today as it is later and
consequently the MAX time, because both are on the same day, 30 December
1899, so the time difference will be incorrectly returned as 10 minutes not
15.

If we take my second version of the query, however:

PARAMETERS
Forms!YourForm!YourCalendarControl DATETIME;
SELECT CMSID, DateCompleted, TimeCompleted,
DATEDIFF("n",
(SELECT MAX(DateCompleted+TimeCompleted)
FROM tblCompletedMessages AS CM2
WHERE CM2.DateCompleted+CM2.TimeCompleted
< CM1.DateCompleted+CM1.TimeCompleted),
DateCompleted+TimeCompleted) AS TimeDifference
FROM tblCompletedMessages AS CM1
WHERE DateCompleted = Forms!YourForm!YourCalendarControl;

This adds the DateCompleted and TimeCompleted values to give the date/time on
the day in question and compares the resulting values, not the times on 30
December 1899, so any date/time values entered yesterday will be earlier
than any today even if entered later in the day. Consequently the MAX
date/time value in the above example would be the correct one of 15 minutes
before the current row. So its this second version of the query you need to
use.

Ken Sheridan
Stafford, England
I see. It pulls the null date of 12-30-1899 and the current system time. But
I usually format the time function to "long time" or "medium time" before
appending to my table. But I do see what your saying about not taking too
much for granted.

Steve
[quoted text clipped - 29 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top