I am trying to calculate the number of hours of operation per week for each facility in a region. The part I am struggling with is that there are multiple programs each day that overlap which contribute to the total hours.
Here is a sample of the table I am working with:
location | program | date | start_time | end_time |
---|---|---|---|---|
a | 1 | 09-22-21 | 14:45:00 | 15:45:00 |
a | 2 | 09-22-21 | 15:30:00 | 16:30:00 |
b | 88 | 09-22-21 | 10:45:00 | 12:45:00 |
b | 89 | 09-22-21 | 10:45:00 | 14:45:00 |
I am hoping to get:
location | hours of operation |
---|---|
a | 1.75 |
b | 4 |
I've tried using SUM DATEDIFF with some WHERE statements but couldn't get them to work. What I have found is how to identify the overlapping ranges(Detect overlapping date ranges from the same table), but not how to sum the difference to get the desired outcome of total non-overlapping hours of operation.