Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date difference 1

Status
Not open for further replies.

xxing

Programmer
Feb 17, 2004
61
NZ
Hi

I have the following table

ID date
1 1 Jan 2008
2 2 Jan 2008
3 5 Jan 2008

I want to see the following:

ID date difference
1 1 Jan 2008 Null
2 2 Jan 2008 1
3 4 Jan 2008 2

i.e it finds the day difference from the row above.

Thank you for your help

Regards
Mark

Regards
Mark
 
Hi,

You could do the following...

Code:
select t1.*,
(select top 1 datediff(d, t2.date, t1.date) 
 from [Table] t2 where t2.id < t1.id 
 order by t2.id desc) as Diff
from [Table] t1

Ry
 
Thank you RyanEk for your valuable post. You have put me on another track.

I initially thought cursors was the only way to do this. A star for you!!!

Regards
Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top