I’ve got a table in an Oracle 11g database that is similar to the following:
process_times:
--------------------------------------------
machine_id | load_id | start_time | end_time
some example records might look like this:
process_times:
----------------------------------------------------------------
machine_id | load_id | start_time | end_time
----------------------------------------------------------------
220 | 25 | 06/24/2012 04:29:00 | 06/25/2012 04:42:38
187 | 23 | 06/22/2012 14:41:00 | 06/24/2012 00:34:32
187 | 18 | 06/20/2012 11:57:00 | 06/20/2012 23:53:51
I’m trying to write a query that will return a relation with an attribute representing the down time between loads of each machine. By down time I mean the time in between each end_time and the following start_time for a given machine.
So for the above example I’d get a result like:
result:
----------------------
machine_id | down_time
----------------------
220 | 0
187 | (06/22/2012 14:41:00 - 06/20/2012 23:53:51) -- the actual result, I wrote it like this to be more clear
I’ve been tinkering with this for a good chunk of the afternoon and it’s only a pre requisite to the query I’m really trying to write so I figured I would post to see if anyone had any pointers to get me heading in the right direction.
Edit Another constraint that I should mention is that I need to have the result list the individual down times for each machine and not just the total amount of down time over a given period of time.
Interesting question. You need to start by getting the prev/next record and then doing the subtraction. Fortunately, Oracle has the lead/lag functions for this.
So:
This finds the previous record with and end time and then calculates the downtime.