How to count consecutive values across rows?

Wheat, Samuel G (Contractor) 21 Reputation points
2022-01-27T18:12:55.143+00:00

1 . ) How to count consecutive values across rows? Start from zero and count cumulatively each time the value repeats. Restart from zero each time the value changes.
Example:

	SomeValue	ConsecutiveCount  
	1			0  
	1			1  
	1			2  
	0			0  
	1			0  
	1			1  
	  

Attached script creates a db with two tables that shows an attempt to accomplish the above.

2 . ) Query in attached script is obviously incorrect with respect to accomplishing the task stated above. That said, the results returned by the query appear to be inconsistent and unmatchable to any pattern demonstated in the data. For example the following row returned by the query against TableB shows a value of 2 in the SameConsecutiveResponseCount column:

	2018-03-14 00:00:00.000	67115991	1	0	2		  

For the query as written in the attached script, how is the value 2 calculated? What does the value 2 represent?
169192-capture.png

3 . ) The same query as the attached run against the same data on our production server yields different results. I had to truncate some data to keep the example small, however as shown in the screen cap below the SameConsecutiveResponseCount starts at 3 and increments by 3 for each row. The first three rows on the prod server are exactly the same as what is provided in the example. At a minimum I would expect those rows to return identical results. Why does this occur?

169039-capture2.png

169098-consecutive-values-problem.txt

select @@version  

Microsoft SQL Server 2019 (RTM-GDR) (KB4583458) - 15.0.2080.9 (X64) Nov 6 2020 16:50:01 Copyright (C) 2019 Microsoft Corporation Developer Edition (64-bit) on Windows 10 Pro 10.0 <X64> (Build 18363: )

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,558 questions
0 comments No comments
{count} votes

Accepted answer
  1. LiHong-MSFT 10,046 Reputation points
    2022-01-28T01:36:18.76+00:00

    Hi @Wheat, Samuel G (Contractor)
    For questions 2.) Because you SUM with partition by Response,which means: " first SUM partition :Response=0" , "second SUM partition :Response=1"

    sum(case when Response = LastResponse then 1 else 0 end) over(partition by Response order by InterrogationDate)

    Change order by clause to this:order by SerialNumber, Response,InterrogationDate and you will find the reason.
    169261-image.png


5 additional answers

Sort by: Most helpful
  1. Anonymous
    2022-02-05T19:01:26.31+00:00

    Another example: with cte1 as ( select * , row_number() over (partition by Response order by InterrogationDate) as Rn from TableB ) , cte2 as ( select * , ( case when (rn-lag(Rn,1,Rn) over (order by InterrogationDate)) <> 1 then Rn else 0 end ) as Delta from cte1 ) select InterrogationDate , SerialNumber , Response , ( case when Delta <> 0 then 0 else Rn - (select max(c.Rn) from cte2 as c where c.Response = cte2.Response and c.Rn = c.Delta and c.Rn < cte2.Rn) end ) as ConsecutiveResponseCount from cte2 order by InterrogationDate ![171627-immagine-2022-02-06-122408.png][1] [1]: /api/attachments/171627-immagine-2022-02-06-122408.png?platform=QnA

    0 comments No comments