-- create a table to store our results-- create a dummy unique identifier on the tableCREATE TABLE #Counter2( RowNumber INT IDENTITY(1,1) ,StartDate DATETIME ,[Value] FLOAT ,Code CHAR(1) ,Counter INT)--insert all existing data from original table into our results tableINSERT INTO #Counter2(StartDate, [Value], Code)SELECT StartDate , [Value] , Code FROM #Counter-- create a counter variable-- this will be used to count our rowsDECLARE @Counter INTSET @Counter = 0-- this is a dummy variable-- we need it as an anchor to keep our running totalDECLARE @RowNumber INT-- now we will set the counter-- we need to start the counter when the value is between 10 and 15-- accumulate the counter until we get to 10-- start again when we get to the next value between 10 and 15UPDATE #Counter2SET @Counter = Counter = CASE WHEN (@Counter >= 1 AND @Counter < 10) OR ([Value] >= 10 AND [Value] <=15) THEN @Counter + 1 ELSE 0 -- restart the counter END ,@RowNumber = RowNumberFROM #Counter2-- take a look at our resultsSELECT * FROM #Counter2-- clean upDROP TABLE #Counter2