Solved

How to calculate the time since job started or restarted

Posted on 2014-03-12
15
308 Views
Last Modified: 2014-06-08
I have a database with many tables.  One of the tables maintains the history of the job status.  This tables has many columns but the columns of importance are Job Number, Status, and Status Start Time.  I need to calculate the time the job spent from start to completion.

For most jobs this is easy since I have the job request time and the job complete time in the job table.  However there are instances when a job can be marked complete but the reopened for further work.  This can potentially happen many times.

Job Status Data
The users want to see the time between start of the cycle and job complete.  For the first job completion this is simply the job complete time - job request time.  I can do this one easily.  For a job that has multiple completions I need the time calculated as Job Complete Time - Previous Job Complete Time.

  Completion time calculation
I don't know how to select the previous Job Complete Time from the data based on the completion record I have.

I cannot modify existing tables in the database but I can add a view or build a derived table in the universe the reports are based on


mlmcc
Job-Status-History-data.xlsx
0
Comment
Question by:mlmcc
  • 5
  • 4
  • 4
  • +1
15 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39923732
should the 2nd job completion time not be the difference from row 5 - row 6?
because during time row 4 - row 5, nothing was done? just to consider ...


apart from that, the calculation simply needs to take the row which matches, prior to the completion, either being the first one unless another completion is before it.

let me work on the script ...
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 39923769
presuming the ID numbers are indeed ascending per start_date, this would be a working query:
select h.id, h.job_number, p.status_start, h.status_end
from job_history h
join job_history p
  on p.job_number = h.job_number
 and p.id = ( select min(x.id) 
      from job_history x
     where x.job_number = h.job_number
       and x.status <> 99
       and not exists( select null 
                       from job_history c 
                      where c.job_number = h.job_number
                          and c.status = 99
                          and c.id > x.id                                     
                          and c.id < h.id
                      )
        )       
where h.status = 99
;

Open in new window

where you only have to modify the table and column names as needed
0
 
LVL 100

Author Comment

by:mlmcc
ID: 39923804
There are no useful ID numbers in the table.  I added those to help with the explanation.

I didn't think about that when I wrote the question.

mlmcc
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39923855
then just replace the ID by status_start field, it should do the same job for this query
0
 
LVL 32

Expert Comment

by:awking00
ID: 39923928
Two questions. What version of Oracle are you using and is it possible for there to be more than two complete statuses for the same job number?
0
 
LVL 100

Author Comment

by:mlmcc
ID: 39923972
Not sure on the Oracle version.  Yes it is possible for there to be more than 2 complete statuses.  

mlmcc
0
 
LVL 32

Expert Comment

by:awking00
ID: 39923995
select * from v$version  ==> to get Oracle version
0
 
LVL 100

Author Comment

by:mlmcc
ID: 39924216
11g

mlmcc
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 250 total points
ID: 39924251
with cte as
(select jobno, status, statusstart, lead(statusstart) over (partition by jobno order by statusstart) nexttime  from
 (select jobno, status, statusstart
  from status_history
  where statusdesc = 'Complete'
  union
  select jobno, status, statusstart
  from status_history
  where statusdesc = 'Requested'
  order by jobno, statusstart)
)
select sh.jobno, sh.status, sh.statusstart, round(24 * (cte.nexttime - cte.statusstart),2) timesincestart
from status_history sh
left join cte
on sh.jobno = cte.jobno
and sh.statusstart = cte.nexttime
and cte.nexttime is not null
order by sh.jobno, sh.statusstart;
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39925728
awking: I think your assumption on using status "requested" as "starting point" is not correct, when I look at the same input data ...
0
 
LVL 32

Expert Comment

by:awking00
ID: 39926190
mlmcc,
Would the initial "starting point" always be where the status desc is 'Requested' or not?
0
 
LVL 100

Author Comment

by:mlmcc
ID: 39926259
It should be but there are times it is not.

I have the initial starting date in another table so if it is the first Completion NULL or 0 can be returned.  The dates I am using are stored as integers.

This SQL, based on the first comment from Guy, seems to be working but I don't get NULLs or a value when there is no previous

select  h.JOBNUMBER,  h.BEGINDATE, h.BEGINTIME, max(p.BEGINDATE) as previousstartdate,  p.BEGINTIME as previousstarttime
from JOBSTATUSHISTORY h
left outer join JOBSTATUSHISTORY p
  on p.JOBNUMBER = h.JOBNUMBER and
      (p.BEGINDATE < h.BEGINDATE OR (p.BEGINDATE = h.BEGINDATE AND p.BEGINTIME < h.BEGINTIME))   and
       p.STATUS= '99'
where h.STATUS= '99' and h.JOBNUMBER IN ( 29395148, 29397082, 29399202)
group by  h.JOBNUMBER,  h.BEGINDATE, h.BEGINTIME,  p.BEGINTIME

Open in new window


The 3 jobnumbers have 2 completion records.  I get 2 records for each but when I try to convert the dates for display I get an error that indicates it is a bad value - either out of range or 0.  When I just display the values they appear to be good values or nothing is displayed.  If I format the number to show something for an undefined (NULL) value then I still get nothing displayed.

Code for the conversion is

CASE WHEN NVL("Status_History_Previous".PREVIOUSSTARTDATE,0) = 0  THEN NULL ELSE EFSCRID.TO_GREGORIAN("Status_History_Previous".PREVIOUSSTARTDATE)  END

Open in new window


mlmcc
0
 
LVL 32

Expert Comment

by:awking00
ID: 39926351
Can you provide some sample data that covers all of the possible criteria and what you expect as output?
0
 
LVL 22

Expert Comment

by:Steve Wales
ID: 40121186
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

808 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