Answers for "Find the count of a group of consecutive records"
https://ask.sqlservercentral.com/questions/135679/find-the-count-of-a-group-of-consecutive-records.html
The latest answers for the question "Find the count of a group of consecutive records"Answer by Gazz
https://ask.sqlservercentral.com/answers/135693/view.html
There is something similar posted here:
[http://blog.jooq.org/2015/11/07/how-to-find-the-longest-consecutive-series-of-events-in-sql/][1]
This does it, although it may not be the prettiest way:
SELECT MIN(id) [Start] ,
MAX(id) [End] ,
COUNT(id) [Count]
FROM ( SELECT * ,
( id - RowNum ) [Grp]
FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY MEMNO ) [RowNum] ,
id ,
flag
FROM [TableName]
) X
WHERE flag = 1
) Y
GROUP BY Grp
ORDER BY MIN(id)
[1]: http://blog.jooq.org/2015/11/07/how-to-find-the-longest-consecutive-series-of-events-in-sql/Mon, 14 Dec 2015 09:53:05 GMTGazz