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
 determine the time diff

Author  Topic 

AdamWest
Constraint Violating Yak Guru

360 Posts

Posted - 2010-04-12 : 11:42:47
I have a table that is in chronological order. The row contains
a timestamp field.
How can I use SQL to determine the time difference (in seconds and parts of
seconds) between one record and the next

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-04-12 : 11:50:19
what's the data type of the "timestamp field" ?

also define what do you mean by "between one record and the next". ?
Is the based on the primary key in ascending order ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-12 : 11:55:06
HOLY HOW-TOs Batman. :)

You could use a ROW_NUMBER() function to number each row. Then you could join that onto itself using the result of ROW_NUMBER() such that each record joins to the previous record. Then call datediff() to give you the value you need.

Does the table have a primary key? What's its data type? Is the timestamp unique? Perhaps you could post a table def and some sample data.

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

AdamWest
Constraint Violating Yak Guru

360 Posts

Posted - 2010-04-12 : 11:59:13
there is no key
the field is like this

2010-04-12 10:07:01.523
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-12 : 12:05:48
Suppose you had 2 records with the exact same timestamp? How do you know what order they were entered in? Or doesn't it matter?

It the timestamp field a datetime datatype?

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page
   

- Advertisement -