Skip to Main Content
  • Questions
  • Calculate time taken to complete task with start/stops

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Alok.

Asked: January 09, 2020 - 12:46 pm UTC

Last updated: January 10, 2020 - 9:50 am UTC

Version: sql

Viewed 1000+ times

You Asked

Dear Sir,

TASKID | EMPLOYEECODE| SLOT_ACTION | date

1         A             S            08/jan/2020 09:30:32 am
1         A             p            08/jan/2020 02:10:20 pm
1         A             S            09/jan/2020 09:50:50 am
1         A             P            09/jan/2020 12/50:58 pm
1         A             S            10/jan/2020 09:30:32 am
1         A             P            10/jan/2020 12/50:58 pm
1         A             S            11/jan/2020 09:30:32 am
1         A             p            11/jan/2020 02:10:20 pm
1         A             S            12/jan/2020 09:50:50 am
1         A             P            12/jan/2020 12/50:58 pm
1         A             S            13/jan/2020 09:30:32 am
1         A             P            13/jan/2020 12/50:58 pm
1         A             S            14/jan/2020 09:30:32 am
1         A             p            14/jan/2020 02:10:20 pm
1         A             S            15/jan/2020 09:50:50 am
1         A             P            15/jan/2020 12/50:58 pm
1         A             S            16/jan/2020 09:30:32 am
1         A             P            16/jan/2020 12/50:58 pm
1         A             S            17/jan/2020 09:30:32 am
1         A             p            17/jan/2020 02:10:20 pm
1         A             S            18/jan/2020 09:50:50 am
1         A             P            18/jan/2020 12/50:58 pm
1         A             S            19/jan/2020 09:30:32 am
1         A             P            19/jan/2020 12/50:58 pm

. . . .
. . . .
like that 100 records there
2nd last and last record is

1         A              S           10/feb/2020 09:30:58 am
1         A              E           10/feb/2020 05:20:25 pm 


they want calculate time.
they ask how much time taken to complete takid 1 for employee A?

Where
S - Start work
P - Pause work
E - End or completed work
A - Employee code
1 - Taskid

and Chris said...

Assuming that the sequence is always

S -> P -> S -> P -> ... -> S -> E

i.e. every S is followed by a P or E, it's just a matter of

* Pairing up the starts with pauses/ends
* Subtracting the start from the end
* Summing the total

There's many ways you could do this. Here's a pivot approach to get you started:

* Assign a row_number() to each S and P/E, ordered by date
* Map E -> P
* Pivot the date for each emp, task, and rn

with rws as (
  select 1 id, 'A' emp, 'S' state, to_date ( '08/jan/2020 09:30:32 am', 'dd/mon/yyyy hh:mi:ss am' ) dt from dual union all
  select 1 id, 'A' emp, 'P' state, to_date ( '08/jan/2020 02:10:20 pm', 'dd/mon/yyyy hh:mi:ss am' ) from dual union all
  select 1 id, 'A' emp, 'S' state, to_date ( '09/jan/2020 09:50:50 am', 'dd/mon/yyyy hh:mi:ss am' ) from dual union all
  select 1 id, 'A' emp, 'P' state, to_date ( '09/jan/2020 12/50:58 pm', 'dd/mon/yyyy hh:mi:ss am' ) from dual union all
  select 1 id, 'A' emp, 'S' state, to_date ( '10/jan/2020 09:30:32 am', 'dd/mon/yyyy hh:mi:ss am' ) from dual union all
  select 1 id, 'A' emp, 'E' state, to_date ( '10/jan/2020 12/50:58 pm', 'dd/mon/yyyy hh:mi:ss am' ) from dual
), rks as (
  select id, emp, 
         dt,
         case 
           when state in ('E','P') then 'P' 
           else 'S' 
         end state, 
         row_number() over ( 
           partition by case 
             when state in ('E','P') then 'P' 
             else 'S' 
           end 
           order by dt 
         ) rn
  from   rws r
)
  select * from rks
  pivot (
    min ( dt ) for state in ( 'S' st, 'P' en )
  );

ID    EMP    RN    ST                      EN                     
    1 A          1 08-JAN-2020 09:30:32    08-JAN-2020 14:10:20    
    1 A          2 09-JAN-2020 09:50:50    09-JAN-2020 12:50:58    
    1 A          3 10-JAN-2020 09:30:32    10-JAN-2020 12:50:58 


All you need to do from here is group and sum the differences.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Alok Verma, January 09, 2020 - 7:03 pm UTC

Dear Sir,

They want output like
Taskid employee task_start_date task_end_date total_taken_hour

1. A. 08/Jan/2020 09:30:32 am. 10/Feb/2020 05:20:32 pm 100hours
Chris Saxon
January 10, 2020 - 9:50 am UTC

I've given you a starting point. Frankly if you're interviewing for a job using SQL you should be able to get the requested output from the example above.

If you want to brush up your SQL skills, we have several FREE courses to help you learn:

https://devgym.oracle.com/pls/apex/dg/class/databases-for-developers-foundations.html
https://devgym.oracle.com/pls/apex/dg/class/databases-for-developers-next-level.html
https://devgym.oracle.com/pls/apex/dg/class/analytic-sql-for-developers.html

More to Explore

Analytics

Analytic SQL got you confused? Check out Connor McDonald's complete video course.