A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
Create the following procedure
Create Procedure GetSequenceValue @SeqValue int As
;With cteIsland As
(Select TS_Date, TS_Person, TS_Value,
Row_Number() Over(Partition By TS_Person Order By TS_Date) - DateDiff(day, '20000101', TS_Date) As Island
From #Sample
Where TS_Value <> 0),
cteCounts As
(Select TS_Person, Island, Count(*) As Cnt
From cteIsland
Group By TS_Person, Island),
cteMaxCounts As
(Select TS_Person, Max(Cnt) As MaxCount
From cteCounts
Group By TS_Person)
Select TS_Person, Case When MaxCount >= @SeqValue Then 1 Else 0 End As TS_Value
From cteMaxCounts;
go
Then just pass the number of consecutive days you are interested in to the procedure. For example
Exec GetSequenceValue 3
will return 1 for all people with a consecutive sequence of 3 or more days.
Tom