So here is a simple solution.
I generate a table with sick or well for each calendar day (s or w)
Then for each day I count the not sick days in the last 30 calendar days, excluding weekends.
If there are 0 well days in that period, then its long term sickness, so for every day we look ahead 29 days to see if we hid long term sickness in the next 30 days.
If its a sick day, and within the next 29 days we will hid a period of no well days, then the sick day is a part of a long term sickness.
In my example the second week is a sick week, which is not marked, and april and may ae sick months, all of the days being marked as long term
WITH dates AS /* Generate som dates */
(
SELECT CAST(DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY (SELECT 1 n))-1, '2020-01-01') AS DATE) dato
FROM (VALUES (0), (1), (2), (3), (4)) a (b)
, (VALUES (0), (1), (2), (3), (4)) b (b)
, (VALUES (0), (1), (2), (3), (4)) c (b)
),
CTE AS /* Set 2nd week and 3,4 month as sick */
(
SELECT IIF(MONTH(dato) IN (3, 4) OR DATEPART(WEEK, dato) = 2, 's', 'w') state
, dato
FROM dates
),
sums AS /* count days of work last 30 days current day included */
(
SELECT dato
, State
, SUM(IIF(State = 'w' AND DATEPART(WEEKDAY, dato) NOT IN (1, 7), 1, 0)) OVER (ORDER BY dato ROWS BETWEEN 29 PRECEDING AND CURRENT ROW) NonSickDays
FROM CTE
),
periods AS /* Check if no well days for any day in the next 30 days, current day included */
(
SELECT dato
, State
, MIN(NonSickDays) OVER (ORDER BY dato ROWS BETWEEN CURRENT ROW AND 29 FOLLOWING) Minperiod
, NonSickDays
FROM sums
)
SELECT dato /* id sick day, and minimum well days is 0 then its a long term day */
, State
, IIF(State = 's' AND Minperiod = 0, 'l', '') longtimesick
, NonSickDays
FROM periods