?
Solved

How to calculate the time since job started or restarted

Posted on 2014-03-12
15
Medium Priority
?
315 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
14 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 1000 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 101

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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 101

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 101

Author Comment

by:mlmcc
ID: 39924216
11g

mlmcc
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 1000 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 101

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 23

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

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
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.

569 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