If you google for "Islands and Gaps in SQL", you will find examples of this type of problem. There are a few different ways to do it - one of the more efficient ones is as follows:---------------------------------------------------------------------------CREATE TABLE #tmp(dayid INT, person VARCHAR(32), attendance VARCHAR(32));INSERT INTO #tmp VALUES(1,'NOZ2k','Absent'),(2,'NOZ2k','Absent'),(5,'NOZ2k','Attended'),(6,'NOZ2k','Absent'),(10,'NOZ2k','Attended'),(11,'NOZ2k','Absent'),(12,'NOZ2k','Absent'),(18,'NOZ2k','Absent');---------------------------------------------------------------------------;WITH cte1 AS( SELECT *, ROW_NUMBER() OVER (PARTITION BY person ORDER BY dayId) AS RN FROM #tmp),cte2 AS( SELECT *, RN - ROW_NUMBER() OVER(PARTITION BY person,Attendance ORDER BY dayId) AS grp FROM cte1)SELECT person, COUNT(*) AS Consecutive_AbsenceFROM cte2WHERE Attendance = 'Absent'GROUP BY person, grp; ---------------------------------------------------------------------------DROP TABLE #tmp;---------------------------------------------------------------------------