根据我在您的问题中添加的标签,这是一个空白和孤岛的问题。
效果最好的解决方案可能是
WITH T AS (SELECt *, ID - ROW_NUMBER() OVER (PARTITION BY [STATUS] ORDER BY [ID]) AS Grp FROM YourTable)SELECt [STATUS], SUM([VALUE]) AS [SUM(VALUE)]FROM TGROUP BY [STATUS], GrpORDER BY MIN(ID)
如果
ID值不能保证如所述连续,那么您将需要使用
ROW_NUMBER() OVER (ORDER BY [ID]) - ROW_NUMBER() OVER (PARTITION BY [STATUS] ORDER BY [ID]) AS Grp
而是在CTE定义中。
SQL小提琴
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)