A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Substracting two adjacent rows



 
 
Thread Tools Display Modes
  #1  
Old May 30th, 2004, 10:39 AM
Yakimo
external usenet poster
 
Posts: n/a
Default Substracting two adjacent rows

Hi!
I have a very large table, containing some DateTime data.
Is it possible to run a query that will return a resultset, containing the
difference between my DataTime values in two adjacent rows?
Example:

ID OnOff TID DateTime
-------------------------------------
1 On 1 11:30:30
2 On 2 11:30:35
3 Off 1 11:35:30
4 Off 2 11:45:35

I want to get a recordset, containing the between Off time and On time for
particular TID, like this:
TID Duration
---------------------------
1 0:05:00 (11:35:30-11:30:30)
2 0:15:00 (11:45:35-11:30:35)

Can this be done with SQL query?
I can do this iterating in a loop, but it seems to slow and clumsy...

I 'd appreciate any help.

Thanks,
Yakimo


  #2  
Old May 30th, 2004, 01:46 PM
Gary Walter
external usenet poster
 
Posts: n/a
Default Substracting two adjacent rows


"Yakimo" wrote
I have a very large table, containing some DateTime data.
Is it possible to run a query that will return a resultset, containing the
difference between my DataTime values in two adjacent rows?
Example:

ID OnOff TID DateTime
-------------------------------------
1 On 1 11:30:30
2 On 2 11:30:35
3 Off 1 11:35:30
4 Off 2 11:45:35

I want to get a recordset, containing the between Off time and On time for
particular TID, like this:
TID Duration
---------------------------
1 0:05:00 (11:35:30-11:30:30)
2 0:15:00 (11:45:35-11:30:35)

Hi Yakimo,

The general method for "adjacent rows" is to
bring the table into your query twice (joining
in this case on TID).

Alias one table as "t1" and other as "t2"

Set criteria for t1.OnOff = "On"
Set criteria for t2.OnOff = "Off"

"DateTime" is a reserved word. If possible,
change the name of the field to something
else (say "OnOffTime"). In following, I am
going to use "OnOffTime" in its place, but if
you are not able to change it, at least be sure
to put brackets around the fieldname in your
queries. It really is a "gotcha" waiting to happen.

Anyway...in this general method, include "OnOffTime"
from both tables in your query, plus subtraction
(t2.OnOffTime - t1.OnOffTime).

Then use a correlated subquery to set criteria
for t1.OnOffTime.

If I am looking at a specific Off time, what On time
do I want to use for subtraction?

-- same TID
-- OnOff = "On"
-- very latest (Max) On time less than Off time
(if there can be many entries for a TID, then
you may be able to use ID instead:
"latest" (Max) On time ID less than this
Off time ID)

SELECT
t2.TID,
t1.OnOffTime As OnTime,
t2.OnOffTime As OffTime,
(t2.OnOffTime - t1.OnOffTime) As Difference
FROM
yourtable AS t1
INNER JOIN
yourtable AS t2
ON t1.TID = t2.TID
WHERE
t1.OnOff = "ON"
AND
t2.OnOff = "OFF"
AND
t1.OnOffTime =
(SELECT MAX(t3.OnOffTime)
FROM yourtable AS t3
WHERE t3.TID = t1.TID
AND
t3.OnOff = "ON"
AND
t3.OnOffTime t2.OnOffTime);

or

SELECT
t2.TID,
t1.OnOffTime As OnTime,
t2.OnOffTime As OffTime,
(t2.OnOffTime - t1.OnOffTime) As Difference
FROM
yourtable AS t1
INNER JOIN
yourtable AS t2
ON t1.TID = t2.TID
WHERE
t1.OnOff = "ON"
AND
t2.OnOff = "OFF"
AND
t1.ID =
(SELECT MAX(t3.ID)
FROM yourtable AS t3
WHERE t3.TID = t1.TID
AND
t3.OnOff = "ON"
AND
t3.ID t2.ID);

Without testing I may have easily gotten something
wrong above, but hopefully you will be able to adapt
the general method.

Good luck,

Gary Walter




  #3  
Old May 30th, 2004, 06:01 PM
Yakimo
external usenet poster
 
Posts: n/a
Default Substracting two adjacent rows

Thanks Gary
That's exactly what I need

../yakimo

"Gary Walter" wrote in message
...

"Yakimo" wrote
I have a very large table, containing some DateTime data.
Is it possible to run a query that will return a resultset, containing

the
difference between my DataTime values in two adjacent rows?
Example:

ID OnOff TID DateTime
-------------------------------------
1 On 1 11:30:30
2 On 2 11:30:35
3 Off 1 11:35:30
4 Off 2 11:45:35

I want to get a recordset, containing the between Off time and On time

for
particular TID, like this:
TID Duration
---------------------------
1 0:05:00 (11:35:30-11:30:30)
2 0:15:00 (11:45:35-11:30:35)

Hi Yakimo,

The general method for "adjacent rows" is to
bring the table into your query twice (joining
in this case on TID).

Alias one table as "t1" and other as "t2"

Set criteria for t1.OnOff = "On"
Set criteria for t2.OnOff = "Off"

"DateTime" is a reserved word. If possible,
change the name of the field to something
else (say "OnOffTime"). In following, I am
going to use "OnOffTime" in its place, but if
you are not able to change it, at least be sure
to put brackets around the fieldname in your
queries. It really is a "gotcha" waiting to happen.

Anyway...in this general method, include "OnOffTime"
from both tables in your query, plus subtraction
(t2.OnOffTime - t1.OnOffTime).

Then use a correlated subquery to set criteria
for t1.OnOffTime.

If I am looking at a specific Off time, what On time
do I want to use for subtraction?

-- same TID
-- OnOff = "On"
-- very latest (Max) On time less than Off time
(if there can be many entries for a TID, then
you may be able to use ID instead:
"latest" (Max) On time ID less than this
Off time ID)

SELECT
t2.TID,
t1.OnOffTime As OnTime,
t2.OnOffTime As OffTime,
(t2.OnOffTime - t1.OnOffTime) As Difference
FROM
yourtable AS t1
INNER JOIN
yourtable AS t2
ON t1.TID = t2.TID
WHERE
t1.OnOff = "ON"
AND
t2.OnOff = "OFF"
AND
t1.OnOffTime =
(SELECT MAX(t3.OnOffTime)
FROM yourtable AS t3
WHERE t3.TID = t1.TID
AND
t3.OnOff = "ON"
AND
t3.OnOffTime t2.OnOffTime);

or

SELECT
t2.TID,
t1.OnOffTime As OnTime,
t2.OnOffTime As OffTime,
(t2.OnOffTime - t1.OnOffTime) As Difference
FROM
yourtable AS t1
INNER JOIN
yourtable AS t2
ON t1.TID = t2.TID
WHERE
t1.OnOff = "ON"
AND
t2.OnOff = "OFF"
AND
t1.ID =
(SELECT MAX(t3.ID)
FROM yourtable AS t3
WHERE t3.TID = t1.TID
AND
t3.OnOff = "ON"
AND
t3.ID t2.ID);

Without testing I may have easily gotten something
wrong above, but hopefully you will be able to adapt
the general method.

Good luck,

Gary Walter







 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 02:38 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.