Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

How to calculate the time since job started or restarted

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
mlmcc
Asked:
mlmcc
  • 5
  • 4
  • 4
  • +1
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
mlmccAuthor Commented:
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
then just replace the ID by status_start field, it should do the same job for this query
0
 
awking00Commented:
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
 
mlmccAuthor Commented:
Not sure on the Oracle version.  Yes it is possible for there to be more than 2 complete statuses.  

mlmcc
0
 
awking00Commented:
select * from v$version  ==> to get Oracle version
0
 
mlmccAuthor Commented:
11g

mlmcc
0
 
awking00Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
awking: I think your assumption on using status "requested" as "starting point" is not correct, when I look at the same input data ...
0
 
awking00Commented:
mlmcc,
Would the initial "starting point" always be where the status desc is 'Requested' or not?
0
 
mlmccAuthor Commented:
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
 
awking00Commented:
Can you provide some sample data that covers all of the possible criteria and what you expect as output?
0
 
Steve WalesSenior Database AdministratorCommented:
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now