Sno is an autonumber... SQL server version is 2005
Main Topics
Browse All TopicsOne of the tables has a date column in the sql server database. I need to calculate the time difference between all the consecutive rows i.e., difference between row1 and row2,differance between row2 and row3 and so on... I do know that this can be possible using cursors... Please help me in how to do so... My table structure is as follows
sno numeric(18,0)
Description varchar(150)
datentime datetime
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
select a.sno, datediff(MINUTE,(select c.datentime from yourtable c where c.sno = (select max(b.sno) from yourtable b where b.sno < a.sno)), a.datentime) , a.description
from yourtable a
where
datediff(MINUTE,(select c.datentime from yourtable c where c.sno = (select max(b.sno) from yourtable b where b.sno < a.sno)), a.datentime)>15
;with Diffs as (select row_number() over (order by datenTime) rn, sno, datentime,description from YourTable)
select d1.sno, d1.description, d1.datenTime, datediff(ms, d1.datentime, d2.datentime) 'ms from Prev to Curr', d2.datentime as 'Previous DateNTime'
from diffs d1
left join diffs d2
on d1.rn=d2.rn+1
;with Diffs as (select row_number() over (order by datenTime) rn, sno, datentime,description from YourTable)
select d1.sno, d1.description, d1.datenTime, datediff(n, d1.datentime, d2.datentime) 'n from Prev to Curr', d2.datentime as 'Previous DateNTime'
from diffs d1
join diffs d2
on d1.rn=d2.rn+1
where datediff(n, d1.datentime, d2.datentime) > 15
Business Accounts
Answer for Membership
by: BrandonGalderisiPosted on 2009-02-17 at 22:21:30ID: 23667372
What purpose is SNO? is it an autonumber? What version of SQL Server?