I've been trying to work out a solution to this problem but so far haven't been able to work it out. I'm using Oracle.
I have a set of data that looks like this:
| USER | ACTIVITY | START_TIME | END_TIME | DURATION |
|--------|------------|-----------------|-----------------|----------|
| jsmith | Front Desk | 2020-08-24 8:00 | 2020-08-24 9:30 | 90 |
| jsmith | Phones | 2020-08-24 8:15 | 2020-08-24 8:45 | 30 |
| jsmith | Phones | 2020-08-24 9:45 | 2020-08-24 9:50 | 5 |
| bjones | Phones | 2020-08-24 9:00 | 2020-08-24 9:10 | 10 |
| bjones | Front Desk | 2020-08-24 9:05 | 2020-08-24 9:15 | 10 |
| bjones | Phones | 2020-08-24 9:15 | 2020-08-24 9:45 | 30 |
The above output can be generated from the following query:
SELECT
USER,
ACTIVITY,
START_TIME,
END_TIME,
DURATION
FROM USER_ACTIVITIES
WHERE USER IN ('jsmith', 'bjones')
AND START_TIME BETWEEN '2020-08-24 00:00:00' AND '2020-08-25 00:00:00'
ORDER BY USER, START_TIME, END_TIME
;
I need to calculate the total "busy" time per user, taking into account that some of the activities overlap each other. Using the existing query I'll get a total duration per user of 125 for jsmith and 50 for bjones, However since some of the activities overlapped this doesn't reflect the total amount of time the users were busy.
The output I'm looking for is the total busy duration per day by user:
| USER | DATE | DURATION |
|--------|------------|----------|
| jsmith | 2020-08-24 | 95 |
| bjones | 2020-08-24 | 45 |
Any help with this would be greatly appreciated.
