[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL How To DateDiff between a record and the next record

Posted on 2008-06-11
2
Medium Priority
?
340 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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

656 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