I have a function that checks how many minutes an event overlaps a certain period like this:
dbo.udf_CountMinutes(event_start, event_end, from, to)
When I have a very long event it can overlap two periods and I want to count the overlapping minutes on both ends, but I do not want to count the event twice!
Test data:
Here I want number of events to say 1 and total time to say 180 minutes. Two hours off the start (04-02) and one at the end (21-20).
My query looks something like this:
The events have a unique row ID that I might use to avoid counting the same event twice. I'm just not sure how to do it.
![[elephant2] [elephant2] [elephant2]](/data/assets/smilies/elephant2.gif)
graabein
dbo.udf_CountMinutes(event_start, event_end, from, to)
When I have a very long event it can overlap two periods and I want to count the overlapping minutes on both ends, but I do not want to count the event twice!
Test data:
Code:
-- One event.
'2008-11-02 02:00', '2008-11-02 21:00'
-- Two periods.
'2008-11-01 20:00', '2008-11-02 04:00'
'2008-11-02 20:00', '2008-11-03 04:00'
Here I want number of events to say 1 and total time to say 180 minutes. Two hours off the start (04-02) and one at the end (21-20).
My query looks something like this:
Code:
select
...,
total = sum(
case when dbo.udf_CountMinutes(e.From, e.To, p.From, p.To) > 0
then 1 else 0 end
),
duration = sum(
dbo.udf_CountMinutes(e.From, e.To, p.From, p.To)
),
...
from
events e, #periods p
group by
...
The events have a unique row ID that I might use to avoid counting the same event twice. I'm just not sure how to do it.
![[elephant2] [elephant2] [elephant2]](/data/assets/smilies/elephant2.gif)
graabein