Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Calculating interval

Author  Topic 

Frank V
Starting Member

5 Posts

Posted - 2006-03-16 : 16:42:36
If two records containing datetime stamps are SELECTed, how can the interval between them be quickly calculated?

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-03-16 : 16:59:21
when you say "two records" do you mean that the datetime column values are in different rows? Are the rows associated in any JOINable way?

EDIT: maybe you should post the table DDL and some sample data...

datediff function will efficiently give you the interval in various scales (miliseconds, seconds, minutes,... etc, years)

Be One with the Optimizer
TG
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-03-17 : 00:01:26
Post some sample data and the result you want

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Frank V
Starting Member

5 Posts

Posted - 2006-03-17 : 11:39:49
Apologies for the lack of specifics.

From the table below, SELECT the last record and the most recent record with the same stack_ID as the last record and then calculate the interval between them. The column headers are identity_key, sample_datetime and stack_ID.

1 2001-06-02 00:00:00.000 NULL
7 2006-01-02 10:50:22.000 H505-2006-12-001
8 2006-01-02 10:50:25.000 H505-2006-12-003
9 2006-01-02 10:50:30.000 H505-2006-12-004
10 2006-03-02 10:50:30.000 H505-2006-12-004
11 2006-03-03 10:50:30.000 H505-2006-12-004
16 2006-03-04 13:21:58.000 H505-2006-12-001
17 2006-03-04 15:33:12.000 H505-2006-12-003
18 2006-03-04 15:34:15.000 H505-2006-12-003
21 2006-03-05 10:45:25.000 H505-2006-12-005
38 2006-03-06 11:05:25.000 H505-2006-12-007
41 2006-03-16 13:27:21.000 H505-2006-12-004
42 2006-03-16 13:27:21.500 H505-2006-12-004
43 2006-03-16 13:27:21.100 H505-2006-12-004

So far I have:

SELECT TOP 2 sample_datetime AS TwoMostRecent
FROM scratch_table
WHERE stack_ID = (SELECT stack_ID FROM scratch_table
WHERE identity_key = (SELECT MAX(identity_key) FROM scratch_table))
ORDER BY sample_datetime DESC;

This provides the last record and the most recent record with the same stack_ID as the last record. What I'm stuck on is how to take this resultset and calculate the interval (difference between the two sample_datetime).

Any help is much appreciated!
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-03-17 : 12:29:53
Here is one way (i think)

set nocount on
create table #scratch_table (identity_key int, sample_datetime datetime, stack_ID varchar(16))
insert #scratch_table
select 1, '2001-06-02 00:00:00.000', NULL union all
select 7, '2006-01-02 10:50:22.000', 'H505-2006-12-001' union all
select 8, '2006-01-02 10:50:25.000', 'H505-2006-12-003' union all
select 9, '2006-01-02 10:50:30.000', 'H505-2006-12-004' union all
select 10, '2006-03-02 10:50:30.000', 'H505-2006-12-004' union all
select 11, '2006-03-03 10:50:30.000', 'H505-2006-12-004' union all
select 16, '2006-03-04 13:21:58.000', 'H505-2006-12-001' union all
select 17, '2006-03-04 15:33:12.000', 'H505-2006-12-003' union all
select 18, '2006-03-04 15:34:15.000', 'H505-2006-12-003' union all
select 21, '2006-03-05 10:45:25.000', 'H505-2006-12-005' union all
select 38, '2006-03-06 11:05:25.000', 'H505-2006-12-007' union all
select 41, '2006-03-16 13:27:21.000', 'H505-2006-12-004' union all
select 42, '2006-03-16 13:27:21.500', 'H505-2006-12-004' union all
select 43, '2006-03-16 13:27:21.100', 'H505-2006-12-004'


select interval_milliseconds = abs(datediff(millisecond, last, most_Recent))
,[last]
,most_recent
from (
select sample_datetime [last]
,most_recent = (select top 1 sample_datetime
from #scratch_table
where stack_id = a.stack_id
order by sample_datetime desc)
from #Scratch_table a
where identity_key = (select max(identity_key) identity_key from #scratch_table)
) a


drop table #scratch_table


output:

interval_milliseconds last most_recent
--------------------- ------------------------------------------------------ ------------------------
400 2006-03-16 13:27:21.100 2006-03-16 13:27:21.500


Be One with the Optimizer
TG
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-03-17 : 15:00:25
And here's another way...

select datediff(ms, b.sample_datetime, max(a.sample_datetime)) as interval_milliseconds
, b.sample_datetime as last
, max(a.sample_datetime) as most_recent
from #scratch_table a
inner join
(select stack_id, sample_datetime from #scratch_table
where identity_key = (select max(identity_key) from #scratch_table)) b
on a.stack_id = b.stack_id
group by b.sample_datetime

This performs slightly better on this small data set.


Ryan Randall
www.monsoonmalabar.com

Ideas are easy. Choosing between them is the hard part.
Go to Top of Page

Frank V
Starting Member

5 Posts

Posted - 2006-03-20 : 12:33:10
TG, RyanRandall:

Thank you both for your replies. Both of you used a and b as a shorthand for resultset (tables). What is the proper term for this (in line view?) and where can I get more info? How can I hold a resultset for a subsequent query?

Frank
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-03-20 : 12:52:43
Thanks

quote:
What is the proper term for this (in line view?) and where can I get more info?

They're 'derived tables'. Just look that up in BOL, google, this site etc. e.g. http://www.sqlteam.com/item.asp?ItemID=6692

quote:
How can I hold a resultset for a subsequent query?

There are a few methods, including...

1. You can use this query as a further derived table in a subsequent query.
2. Insert the data into a table variable
3. Insert the data into a temporary table

Just look the terms up in BOL for examples and documentation.


Ryan Randall
www.monsoonmalabar.com

Ideas are easy. Choosing between them is the hard part.
Go to Top of Page

Frank V
Starting Member

5 Posts

Posted - 2006-03-20 : 13:58:23
RyanRandall:

Very helpful! Thanks again.

Frank
Go to Top of Page
   

- Advertisement -