Q&A for people studying math at any level and professionals in related fields
WITH a AS ( SELECT IIF(DATEDIFF(DD, LAG(CreationDate) OVER (ORDER BY CreationDate), CreationDate) > 20, 1, 0) AS [GroupNo], * FROM Posts p WHERE PostTypeId = 6 ) SELECT SUM(GroupNo) OVER (ORDER BY CreationDate), * FROM a ORDER BY CreationDate;