Here is how it works so you can appreciate the genius of this solution.
Use table and data definition from the above
DECLARE @CurrentTimeZoneName nvarchar(128) = 'Central Standard Time'--dbo.fnCLR_getCurrentTimeZoneName()
DECLARE @ocDate date = '2021-05-26' -- input day to evaluate schedule for
SELECT
CONCAT('Job ', ce.jobID) as Label
,geometry::Parse(pol.P) P
--geometry::UnionAggregate(pol.P).STArea()
FROM(VALUES
(CAST(@ocDate as datetime2(0)) AT TIME ZONE @CurrentTimeZoneName
,CAST(DATEADD(day, 1, @ocDate) as datetime2(0)) AT TIME ZONE @CurrentTimeZoneName)
) i(ocDateTimeStart, ocDateTimeFinish)
CROSS JOIN @CalendarEvents ce
CROSS APPLY(SELECT -- if dates overlap day, trim to a day
ceStartDateTime = CASE WHEN ce.ceStartDateTime < i.ocDateTimeStart THEN i.ocDateTimeStart ELSE ce.ceStartDateTime END
,ceFinishDateTime = CASE WHEN ce.ceFinishDateTime > i.ocDateTimeFinish THEN i.ocDateTimeFinish ELSE ce.ceFinishDateTime END
) d
CROSS APPLY(VALUES (DATEDIFF(minute, i.ocDateTimeStart, d.ceStartDateTime), DATEDIFF(minute, i.ocDateTimeStart, d.ceFinishDateTime))) p(s, e) -- durations in minutes for start and end points since the beginning of the day
-- this polygon, a bar, is closed geometry, built with 5 points, specified as 'X Y', with comma separator, clockwise, equivalent to a bar of time interval of 1 unit high, laid flat on X = 0
-- https://learn.microsoft.com/en-us/sql/relational-databases/spatial/polygon?view=sql-server-ver15
CROSS APPLY(VALUES (CONCAT('POLYGON (('
-- X Y Separator
------ ---- ---------
,p.s, ' ' , 0, ',' -- point 1, X Y, distance from the 00:00 hrs marker, in my case the beginning of a day. it can be the beginning of a year or other peg
,p.s, ' ' , 1, ',' -- point 2, X Y, bar height is only 1 unit, second if you will. it does not matter what this is, just it is of 1 unit.
,p.e, ' ' , 1, ',' -- point 3, X Y, top right end point
,p.e, ' ' , 0, ',' -- point 4, X Y, bottom right point
,p.s, ' ' , 0, -- point 5, X Y, the same as point 1, closes geometry
'))'))) pol(P)
-- so, each job gets a bar of a length equal to its duration
-- because they all are layered on X = 0, UnionAggregate combines these, into new polygons. Overlapping polygons become one.
-- https://learn.microsoft.com/en-us/sql/t-sql/spatial-geography/unionaggregate-geography-data-type?view=sql-server-ver15
-- then area method derives area of these polygons. because Y is 1 unit high, it's length that remains, which is total minutes.
-- https://learn.microsoft.com/en-us/sql/t-sql/spatial-geography/starea-geography-data-type?view=sql-server-ver15
WHERE ce.ceStartDateTime <= i.ocDateTimeFinish
AND ce.ceFinishDateTime >= i.ocDateTimeStart
AND ce.wrcID = 1
-- run the above and select 'Spatial Results' tab
-- grid should show you these bars. hover over bars to see job ID representing bars.
@Viorel simply took Excel screenshot, created bars, laid them on X axis, combined them and got area value.
@Viorel please chime in if you can explain more or correct me
This is simply unique solution to this problem, worth understanding how it works. It can be used to solve similar issues.
Thanks
Vladimir