T-SQL incrementing counter based on change in a column value
In SQL Server 2012 and later, you have the luxury of 1) analytic functions, and 2) running totals: declare @t table ( Id int primary key, Value int not null ); insert into @t (Id, Value) values (1, 3), (2, 3), (3, 2), (4, 2), (5, 2), (6, 3), (7, 0), (8, 0); select sq.Id, … Read more