Solved

SQL Query - Oracle 10g - Subract date from next data row

Posted on 2016-11-03
4
97 Views
Last Modified: 2016-11-07
Experts,
I need some help with a query I need to populate a report.
I have data that looks like this:
JOB        START TIME
TASK1   02/11/2016 09:00
TASK2   02/11/2016 09:06
TASK3   02/11/2016 09:08
END      02/11/2016 09:12

Each row has a JobID and a start time. The end time needs to be the start time of the next Job.
My goal is to add a column that contains the total task time
JOB        START TIME        TOTAL TIME
TASK1   02/11/2016 09:00   6.0
TASK2   02/11/2016 09:06   2.0
TASK3   02/11/2016 09:08   4.0
END      02/11/2016 09:12

Obviously I need to subtract Task1 from Task2 to get the time, but how can I write a query that looks at the next row?
any guidance is greatly appreciated.
0
Comment
Question by:JDCam
[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
  • 2
4 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 41872166
LEAD allows you to peak at the next row.

If you want the results in minutes try this:
select job, start_time, (lead(start_time) over(order by start_time)-start_time) * 1440
from yourtable;

Open in new window

0
 

Author Comment

by:JDCam
ID: 41872230
Sounds great. I will read up on and try it out.
I assume it considers any order by clause to determine what the next row is.
Thanks
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41872235
>>I assume it considers any order by clause to determine what the next row is.

Nope.  It uses the ORDER BY inside the LEAD call.
0
 

Author Closing Comment

by:JDCam
ID: 41877074
Works Great Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
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.

726 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