Solved

SQL How To DateDiff between a record and the next record

Posted on 2008-06-11
2
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

707 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