Mr_Difficult
asked on
Calculate the difference between two timestamps
I am really new to SQL so I am asking for some assistance.
I would like to have a query that would calculate the difference between two timestamps.
The timestamp data is unsorted and stored in two columns as shown in TABLE RandomData.
Given the following data in TABLE RandomData it should produce the results shown.
The timestamp is calculated as (Weeks * 604800) + Seconds (probably promote to a BIGINT)
to account for seconds rolling over at the beginning of a new week. So I will need to sort
by the timestamp then calculate the TimeDiff as next row timestamp minus current row timestamp,
the last row timestamp ends up null.
This should be doable without producing any temporary tables but I don't know enough yet
how to structure the query. It would also be nice to have an all-in-one query that identifies
only the differences that exceed a threshold value.
Any assistance is greatly appreciated.
CREATE TABLE RandomData
(
ID INT -- ID
Weeks INT, -- Weeks since beginning of epoch
Seconds INT -- Time since start of week
);
--Data
ID Weeks Seconds
1 161 161983
2 161 161998
3 161 161985
4 161 161999
5 161 161987
6 161 162001
7 161 161988
8 161 162002
9 161 161990
10 161 162004
11 161 161991
12 161 162005
13 161 161993
14 161 162007
15 161 161996
16 161 162009
17 161 161997
18 161 162010
-- Expected results
ID Week Time TimeDiff
1 161 161983 2
3 161 161985 2
5 161 161987 1
7 161 161988 2
9 161 161990 1
11 161 161991 2
13 161 161993 3
15 161 161996 1
17 161 161997 1
2 161 161998 1
4 161 161999 2
6 161 162001 1
8 161 162002 2
10 161 162004 1
12 161 162005 2
14 161 162007 2
16 161 162009 1
18 161 162010 NULL
I would like to have a query that would calculate the difference between two timestamps.
The timestamp data is unsorted and stored in two columns as shown in TABLE RandomData.
Given the following data in TABLE RandomData it should produce the results shown.
The timestamp is calculated as (Weeks * 604800) + Seconds (probably promote to a BIGINT)
to account for seconds rolling over at the beginning of a new week. So I will need to sort
by the timestamp then calculate the TimeDiff as next row timestamp minus current row timestamp,
the last row timestamp ends up null.
This should be doable without producing any temporary tables but I don't know enough yet
how to structure the query. It would also be nice to have an all-in-one query that identifies
only the differences that exceed a threshold value.
Any assistance is greatly appreciated.
CREATE TABLE RandomData
(
ID INT -- ID
Weeks INT, -- Weeks since beginning of epoch
Seconds INT -- Time since start of week
);
--Data
ID Weeks Seconds
1 161 161983
2 161 161998
3 161 161985
4 161 161999
5 161 161987
6 161 162001
7 161 161988
8 161 162002
9 161 161990
10 161 162004
11 161 161991
12 161 162005
13 161 161993
14 161 162007
15 161 161996
16 161 162009
17 161 161997
18 161 162010
-- Expected results
ID Week Time TimeDiff
1 161 161983 2
3 161 161985 2
5 161 161987 1
7 161 161988 2
9 161 161990 1
11 161 161991 2
13 161 161993 3
15 161 161996 1
17 161 161997 1
2 161 161998 1
4 161 161999 2
6 161 162001 1
8 161 162002 2
10 161 162004 1
12 161 162005 2
14 161 162007 2
16 161 162009 1
18 161 162010 NULL
Can you explain how did you calculate the TimeDiff?
ASKER
...then calculate the TimeDiff as next row timestamp minus current row timestamp,
the last row timestamp ends up null.
TimeDiff is calculated by subtracting the timestamp in one row from the timestamp in the next row and so on until the end of the table is reached. (The table needs to be sorted for any of the TimeDiff values to make sense.)
So the sorted table (based on the calculated timestamp) for the first two rows is ID 1 and ID 3.
Take the calculated timestamp of the second row in the sorted table (ID 3) and subtract the calculated timestamp of the first row in the sorted table (ID 1) to get the TimeDiff.
so we have ((161*604800)+161985) - ((161*604800)+161983) = 2 which is the TimeDiff for the first row (ID = 1)
timestamp is not a column in the table, it is a calculated value based on Week and Time per the formula
timestamp = (Week*604800)+Time
ID Week Time TimeDiff
1 161 161983 2
3 161 161985 2
Hopefully this is a little more clear now, if not I will try to rephrase it.
the last row timestamp ends up null.
TimeDiff is calculated by subtracting the timestamp in one row from the timestamp in the next row and so on until the end of the table is reached. (The table needs to be sorted for any of the TimeDiff values to make sense.)
So the sorted table (based on the calculated timestamp) for the first two rows is ID 1 and ID 3.
Take the calculated timestamp of the second row in the sorted table (ID 3) and subtract the calculated timestamp of the first row in the sorted table (ID 1) to get the TimeDiff.
so we have ((161*604800)+161985) - ((161*604800)+161983) = 2 which is the TimeDiff for the first row (ID = 1)
timestamp is not a column in the table, it is a calculated value based on Week and Time per the formula
timestamp = (Week*604800)+Time
ID Week Time TimeDiff
1 161 161983 2
3 161 161985 2
Hopefully this is a little more clear now, if not I will try to rephrase it.
which SQL version you are running?
ASKER
Using Microsoft SQL Server 2008 R2 64-bit
;WITH Sorting AS (
SELECT ID, Weeks, Seconds, ROW_NUMBER() OVER (ORDER BY Weeks, Seconds) AS RN
FROM RandomData
--ORDER BY Weeks, Seconds
)
SELECT A.*, ((B.Weeks - A.Weeks) * 604800) + B.Seconds - A.Seconds
FROM Sorting A
LEFT JOIN Sorting B ON A.RN = B.RN - 1
ORDER BY A.RN
ASKER
Works like a charm, I will bump up the points a bit more if you can modify the query to only return the TimeDiff over a threshold.
simply put WHERE condition inside the "Sorting" query
for ex
for ex
;WITH Sorting AS (
SELECT ID, Weeks, Seconds, ROW_NUMBER() OVER (ORDER BY Weeks, Seconds) AS RN
FROM RandomData
WHERE Weeks = 161
-- or WHERE Weeks BETWEEN 153 AND 154 etc.
) SELECT ....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Last Question: The ORDER BY A.RN is commented out, is that by design or an accident?
It's depend, since to compare by Threshold, order might not important.
However, you can remain the ORDER BY to have the resultset sorting by Weeks, Seconds
However, you can remain the ORDER BY to have the resultset sorting by Weeks, Seconds
ASKER
Cool, I will retain it then. Awesome job by the way. The code exactly what I want with no goofing around with temporary tables.
Thanks.
Thanks.
ASKER
Awesome job, quick, short and to the point solution.