Solved

SQL How To DateDiff between a record and the next record

Posted on 2008-06-11
2
322 Views
Last Modified: 2012-08-14
I need the best way to present data for reporting when the raw data is a log of status changes that show the jobID, status, and date the job was set to the status shown in the record. The reports need to show how long the job remained at each status, but the log only contains the date the status was set, so the duration requires that we calculate the date diff from one record with a given jobid to the next (sequentially in time) record for the same jobid (regardless of the status code for the next record).
I realize we can sort by jobid and date, but what I'm not certain about is the best method to create a view that calculates the date diff between 2 records sequential records, or how to handle the last record when no additonal record exists for the jobid. I know I could write a user defined function to lookup the next record, and calculate the datediff but I don't know if there is a better method. Looking for ideas.
0
Comment
Question by:vtechdev
2 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21762513
can you post your table structure and some example data?
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 21762705
Something roughly like this:

SELECT jobID, status,
    DATEDIFF(MINUTE, date,
        (SELECT MIN(date) FROM tablename t2 WHERE t2.jobID = t1.jobID AND t2.date > t1.date AND t2.status <> t1.status))  
--<<-- status check can be dropped if status of next rec will always be different
FROM tablename t1
ORDER BY jobID

You'll likely need an index on (jobID, date [, status]) to get good performance [or equivalent individual indexes] -- status being needed in the index depends on whether it is needed in the query or not.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question