By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE EventLog
(
EventID INT
, EventName VARCHAR(50) NOT NULL
, EventStartDateTime DATETIME NOT NULL
, EventEndDateTime DATETIME NULL
)
INSERT INTO EventLog(EventID, EventName, EventStartDateTime, EventEndDateTime)
VALUES(100, 'Planting', '20210620 10:34:09 AM','20211018 10:54:49 PM')
,(200, 'Foundation', '20200420 10:34:09 AM','20211018 10:54:49 PM')
,(300, 'Seeding', '20210410 10:27:19 AM','')
,(400, 'Spreading', '20220310 10:24:09 PM','');
CREATE TABLE EventSummary
(
EventID INT
, EventName VARCHAR(50) NOT NULL
, [Year] INT
, [MonthName] VARCHAR(25)
, [Hours] DECIMAL(12,2)
)
4 rows affected
;WITH CTE AS (
SELECT EventID,EventName,EventStartDateTime,IIF(EventEndDateTime = '',GETUTCDATE(),EventEndDateTime) EventEndDateTime
FROM EventLog
UNION ALL
SELECT EventID,EventName, DATEADD(month, DATEDIFF(month, 0, DATEADD(month , 1 , EventStartDateTime)), 0) , EventEndDateTime
FROM CTE
WHERE DATEADD(month, DATEDIFF(month, 0, DATEADD(month , 1 , EventStartDateTime)), 0) <= EventEndDateTime
)
INSERT INTO EventSummary(EventID,EventName,Year,MonthName,Hours)
SELECT EventID,EventName,YEAR(EventStartDateTime),DATENAME(MONTH,EventStartDateTime),DATEDIFF(second, EventStartDateTime, n_EventStartDateTime) / 3600.0
FROM (
SELECT EventID,EventName,EventStartDateTime,LEAD(EventStartDateTime,1,EventEndDateTime) OVER(PARTITION BY EventID,EventName ORDER BY EventStartDateTime) n_EventStartDateTime
FROM CTE
) t1
option (maxrecursion 0)
47 rows affected
SELECT * FROM EventSummary
EventID | EventName | Year | MonthName | Hours |
---|---|---|---|---|
100 | Planting | 2021 | June | 253.43 |
100 | Planting | 2021 | July | 744.00 |
100 | Planting | 2021 | August | 744.00 |
100 | Planting | 2021 | September | 720.00 |
100 | Planting | 2021 | October | 430.91 |
200 | Foundation | 2020 | April | 253.43 |
200 | Foundation | 2020 | May | 744.00 |
200 | Foundation | 2020 | June | 720.00 |
200 | Foundation | 2020 | July | 744.00 |
200 | Foundation | 2020 | August | 744.00 |
200 | Foundation | 2020 | September | 720.00 |
200 | Foundation | 2020 | October | 744.00 |
200 | Foundation | 2020 | November | 720.00 |
200 | Foundation | 2020 | December | 744.00 |
200 | Foundation | 2021 | January | 744.00 |
200 | Foundation | 2021 | February | 672.00 |
200 | Foundation | 2021 | March | 744.00 |
200 | Foundation | 2021 | April | 720.00 |
200 | Foundation | 2021 | May | 744.00 |
200 | Foundation | 2021 | June | 720.00 |
200 | Foundation | 2021 | July | 744.00 |
200 | Foundation | 2021 | August | 744.00 |
200 | Foundation | 2021 | September | 720.00 |
200 | Foundation | 2021 | October | 430.91 |
300 | Seeding | 2021 | April | 493.54 |
300 | Seeding | 2021 | May | 744.00 |
300 | Seeding | 2021 | June | 720.00 |
300 | Seeding | 2021 | July | 744.00 |
300 | Seeding | 2021 | August | 744.00 |
300 | Seeding | 2021 | September | 720.00 |
300 | Seeding | 2021 | October | 744.00 |
300 | Seeding | 2021 | November | 720.00 |
300 | Seeding | 2021 | December | 744.00 |
300 | Seeding | 2022 | January | 744.00 |
300 | Seeding | 2022 | February | 672.00 |
300 | Seeding | 2022 | March | 744.00 |
300 | Seeding | 2022 | April | 720.00 |
300 | Seeding | 2022 | May | 744.00 |
300 | Seeding | 2022 | June | 720.00 |
300 | Seeding | 2022 | July | 744.00 |
300 | Seeding | 2022 | August | 221.69 |
400 | Spreading | 2022 | March | 505.60 |
400 | Spreading | 2022 | April | 720.00 |
400 | Spreading | 2022 | May | 744.00 |
400 | Spreading | 2022 | June | 720.00 |
400 | Spreading | 2022 | July | 744.00 |
400 | Spreading | 2022 | August | 221.69 |