ID Flag
1000 0
1000 1
1000 1
1000 1
1000 1
1000 0
2000 0
2000 1
2000 0
2000 1
2000 0
我想添加一个标记ID的列,如果它们在SQL Server的“Flag”列中至少有3个连续的“1”。
答案 0 :(得分:0)
这可能会有所帮助。它看起来与您的要求类似,可能会让您走上正轨。 Find records with 3 or more consecutive records with same value
所以类似的东西可能
CREATE TABLE Table1 (
id int,
flag bit)
INSERT into Table1 (id,flag)
VALUES (1000,0),(1000,1),(1000,1),(1000,1)
SELECT t.*
FROM (SELECT t.*, count(*) over (partition by ID) as cnt
FROM (SELECT table1.*,
(row_number() over (partition by id order by flag)
) as grp
from table1
) t
WHERE flag = 1
) t
WHERE cnt >= 3;