Solved

SQL How To DateDiff between a record and the next record

Posted on 2008-06-11
2
325 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

792 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