Som jag nämner i kommentaren, använd bara en WHERE
. Detta är naturligtvis en blind gissning på grund av brist på provdata (provet har inga arbetstidsdata):
--I prefer CTEs over subqueries
WITH CTE AS(
SELECT s.date_sick,
s.employee_number,
ROW_NUMBER() OVER (PARTITION BY employee_number ORDER BY date_sick) AS rn
FROM dbo.sickness s)
SELECT C.date_sick,
C.employee_number,
DENSE_RANK() OVER (ORDER BY C.employee_number, DATEADD(DAY, -C.rn, C.date_sick)) AS sickness_id,
wh.workinghours
FROM CTE C
JOIN dbo.workinghours wh ON C.employee_number = wh.employee_number
WHERE wh.working_hours > 0
ORDER BY C.employee_number,
C.date_sick;