Title: Finding overlapping time periods - suggestions please
Easy, this should do it:
 
------------------------------------------
-- Create a time dimensions
------------------------------------------
drop table test_date_dim;
create table test_date_dim (time_dt date);
------------------------------------------
-- Fill the dimension for one day only
------------------------------------------
begin
for i in 1..24*60 loop
 insert into test_date_dim values (trunc(sysdate)+i/ (24 * 60));
end loop;
commit;
end;
------------------------------------------
-- Check the dimension contents
------------------------------------------
select to_char(time_dt,'mm/dd/yy hh24:mi:ss') mtimestamp from test_date_dim;
------------------------------------------
-- Create the activity table
------------------------------------------
create table test_activity ( activity_id number, start_dt date, end_dt date);
insert into test_activity values (1, to_date('10:00','hh24:mi'), to_date('12:00','hh24:mi'));
insert into test_activity values (3, to_date('11:00','hh24:mi'), to_date('13:00','hh24:mi'));
insert into test_activity values (4, to_date('11:30','hh24:mi'), to_date('13:30','hh24:mi'));
insert into test_activity values (7, to_date('13:30','hh24:mi'), to_date('16:00','hh24:mi'));
commit;
------------------------------------------
-- Check the activity table
------------------------------------------
select * from test_activity;
------------------------------------------
-- Easy Solution
------------------------------------------
select activity_id                                           activity_id,
       count(*)                                              elapsed,
       count(decode(activity_cnt,1,null,time_dt))            elapsed_multitask,
       count(decode(activity_cnt,1,time_dt,null))            elapsed_single,
       round(sum(decode(activity_cnt,1,0,1/activity_cnt)))   prorated_multi_minutes,
       count(decode(activity_cnt,1,time_dt,null)) +
       round(sum(decode(activity_cnt,1,0,1/activity_cnt)))   prorated_minutes
from (
select time_dt,
       b.activity_id,
       count(distinct b.activity_id) over (partition by time_dt) activity_cnt
 from test_date_dim a, test_activity b
 where a.time_dt >= b.start_dt and a.time_dt < b.end_dt)
group by activity_id
---------------------------------------------------------------------------------------------------------
ACTIVITY_ID     ELAPSED ELAPSED_MULTITASK       ELAPSED_SINGLE  PRORATED_MULTI_MINUTES  PRORATED_MINUTES
1               120     60                      60              25                      85
3               120     120                     0               55                      55
4               120     90                      30              40                      70
7               150     0                       150             0                       150
 
 
Regards,
 
Waleed
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Friday, October 31, 2003 1:25 PM
To: Multiple recipients of list ORACLE-L
Subject: Finding overlapping time periods - suggestions please

I was wondering if anyone had the need to find overlapping time periods and how to identify them efficiently.

Here is the scenario:

      Elapsed minutes refer to the actual "clock" time either spent on a given task.  Thus an activity that started at 9:00 am and finished at 11:00 am on the same day is said to have 120 elapsed minutes.

      If one task overlaps another (either completely or partially with another task), then the tasks are said to be "multitasked".  In that case the system will store the portion of the elapsed time that was multitasked as "elapsed multitask minutes" and the portion of the time that was not overlapped as "elapsed single minutes".  In addition, for the portion of time that two or more activities were simultaneously taking place; their time will be divided by the number of simultaneous activities and stored as "prorated multi minutes".  The sum of Elapsed Single Minutes and Prorated Minutes will equal the actual clock time that a vehicle was active.

      The following example should help to illustrate these concepts.  In the table below a list of fictitious activities for a vehicle are shown in addition to how the time is allocated to the various measures:

Activity        Start Time      End Time        Elapsed Minutes Elapsed Multitask Minutes       Elapsed Single Minutes  Prorated Multi Minutes  Prorated Minutes       
1       10:00   12:00   120     60      60      25      85     
3       11:00   13:00   120     120     0       55      55     
4       11:30   13:30   120     90      30      40      70     
7       13:30   16:00   150     0       150     0       150    
Totals                  510     270     240     120     360    
The vehicle was active from 10:00 to 16:00, a total of 6 hours (360 minutes) which is equal to the total of Prorated Minutes.

      The vehicle performed 8 � hours (510 minutes) of work during that 6-hour time span.  This can be arrived at by adding the total of Elapsed Multitask Minutes (270) + the total of Elapsed Single Minutes (240).

Reply via email to