INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# RE:calculation using first record and next record

## RE:calculation using first record and next record

(OP)
Hi Everyone,

I have a table that looks like:
LOG Date Start Stop
522747 12/1/15 0:00 12/1/15 7:40 12/1/15 8:40
509655 12/1/15 0:00 12/1/15 8:05 12/1/15 8:25
482436 12/1/15 0:00 12/1/15 8:41 12/1/15 9:11

What I need to do is to create a calculated field that will calculate the difference between the first record in the start field and do a date diff using the 2nd record in the stop field and continue using the 2nd record in the start field and using a date diff using the 3rd row of the stop field and so on. I undertand using the date diff for the calculation but can't figure out how to do the calculation using the next row below.

### RE: RE:calculation using first record and next record

Depends on your version of SQL Server, since 2008 there is LAG and LEAD.

As an example here's a gap finder of a numeric sequence:

#### CODE

declare @sequencewithanomalies as table (num int);
insert into @sequencewithanomalies values (1),(2),(4),(5),(6),(6),(9),(9),(9),(10);

--detecting anomalies
Select num,
(LEAD(num) over (order by num) -num)  as diff
from @sequencewithanomalies

--extracting anomalies
Select * from
(
Select num,
(LEAD(num) over (order by num) -num)  as diff
from @sequencewithanomalies
) tmp
Where diff<>1 

A diff of 1 is normal increment, A diff of 0 means a double number, as is the case with 6 once and 9 twice. A diff>1 means a gap of size diff-1, obviously.
The nature of diff with datetimes is of course either a gap in time or an overlap in time. Does the start and stop time come from getDate(), from server time? Or does it come in from the client application as local system time? Then you can easily get such gaps or overlaps, just to give one reason for them.

Bye, Olaf.

### RE: RE:calculation using first record and next record

(OP)
YAA!!! It works! Thank you so much!!

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!