How to eliminate non-working hours in Oracle

前端 未结 2 425
攒了一身酷
攒了一身酷 2020-12-18 16:34

I have two columns of DateTime type. The first one stores the DateTime when a process it started, the other one stores the DateTime when that process is finished. I want to

2条回答
  •  醉梦人生
    2020-12-18 17:28

    You can take the hour difference between the two dates and reduce the amount of non-working hours times the days between the two days. consider this example :

    create table t1 (
        start_hour date , finish_hour date);
    
    insert into t1 values (
      to_date('01/01/2013 12:00:00','dd/mm/yyyy hh24:mi:ss'),
      to_date('01/01/2013 15:00:00','dd/mm/yyyy hh24:mi:ss'));
    
    insert into t1 values (
      to_date('01/01/2013 12:00:00','dd/mm/yyyy hh24:mi:ss'),
      to_date('02/01/2013 15:00:00','dd/mm/yyyy hh24:mi:ss'));
    
    insert into t1 values (
      to_date('01/01/2013 18:00:00','dd/mm/yyyy hh24:mi:ss'),
      to_date('03/01/2013 11:00:00','dd/mm/yyyy hh24:mi:ss'));
    
    
    with x as (
    select start_hour ,finish_hour , 
          (finish_hour - start_hour) * 24 hour_diff , 
           trunc(finish_hour) - trunc(start_hour) as day_diff
    from t1
    ) 
    select  start_hour , finish_hour , 
            hour_diff - 15 * day_diff as working_hours 
    from x;
    
    |                     START_HOUR |                    FINISH_HOUR | WORKING_HOURS |
    -----------------------------------------------------------------------------------
    | January, 01 2013 12:00:00+0000 | January, 01 2013 15:00:00+0000 |             3 |
    | January, 01 2013 12:00:00+0000 | January, 02 2013 15:00:00+0000 |            12 |
    | January, 01 2013 18:00:00+0000 | January, 03 2013 11:00:00+0000 |            11 |
    

提交回复
热议问题