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.
| Author |
Topic |
|
szabonorbert
Starting Member
2 Posts |
Posted - 2006-06-30 : 04:01:18
|
| Hello!I'm using MS SQL Server 2000.I would like to make a query (not stored procedure) where I can count a record value from the previous(or next) record value./Table Event/EventID StartDate------------------------------2 2006-06-20 13:30:104 2006-06-20 13:30:4611 2006-06-20 13:31:235 2006-06-20 13:32:034 2006-06-20 13:32:14...so, i would like to show three fields, eventid,startdate, and the difference (in seconds) between each event. i cannot use stored procedures. is it possible to reach other records value?thanks for your help! |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-30 : 04:33:26
|
| [code]declare @event table (EventID INT, StartDate datetime)insert @eventselect 2, '2006-06-20 13:30:10' union allselect 4, '2006-06-20 13:30:46' union allselect 11, '2006-06-20 13:31:23' union allselect 5, '2006-06-20 13:32:03' union allselect 4, '2006-06-20 13:32:14'select e.EventID, e.StartDate, DATEDIFF(ss, e.StartDate, (SELECT ISNULL(MIN(StartDate), GETDATE()) FROM @event where StartDate > e.StartDate)) Secsfrom @event eorder by e.StartDate, e.EventID[/code]Output[code]ID StartDate Secs-- ----------------------- ------ 2 2006-06-20 13:30:10.000 36 4 2006-06-20 13:30:46.000 3711 2006-06-20 13:31:23.000 40 5 2006-06-20 13:32:03.000 11 4 2006-06-20 13:32:14.000 853148[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-30 : 04:55:02
|
I am not sure, but it looks to me that szabonorbert wanted the other way round.Modify from Peter's codedeclare @event table (EventID INT, StartDate datetime)insert @eventselect 2, '2006-06-20 13:30:10' union allselect 4, '2006-06-20 13:30:46' union allselect 11, '2006-06-20 13:31:23' union allselect 5, '2006-06-20 13:32:03' union allselect 4, '2006-06-20 13:32:14'select e.EventID, e.StartDate, DATEDIFF(ss, (SELECT ISNULL(MAX(StartDate), e.StartDate) FROM @event x where x.StartDate < e.StartDate), e.StartDate) Secsfrom @event eorder by e.StartDate, e.EventID/* RESULTEventID StartDate Secs ----------- ------------------------------------------------------ ----------- 2 2006-06-20 13:30:10.000 04 2006-06-20 13:30:46.000 3611 2006-06-20 13:31:23.000 375 2006-06-20 13:32:03.000 404 2006-06-20 13:32:14.000 11*/ KH |
 |
|
|
szabonorbert
Starting Member
2 Posts |
Posted - 2006-06-30 : 05:01:30
|
| Perfect!I wanted the first version, thank you very much! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-30 : 05:10:04
|
quote: Originally posted by szabonorbert Perfect!I wanted the first version, thank you very much!
Wow, the SQL Team Mind Reading seminar RyanRandall told me about paid off! Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|