如果 id 连续 n 行,如何标记一组重复项?

How to tag a group of repeating items if the ids are consecutive for n rows?

事实证明,如果不将其分解成多个部分或使用我想避免的游标,构建连续 ID 的测试很困难。

伪查询-

SELECT all 
FROM table with the same description on multiple adjacent rows for >= 4 rows 
and set tag = 'y' and order by id 

(id,description, tag),
(1, 'xxx', 'n'),
(2, 'xxx', 'n'),
(3, 'xxx', 'n'),
(7, 'xxx', 'n'),
(5, 'xxx', 'n'),
(8, 'xxx', 'n'), 
(4, 'xxx', 'n'), 
(6, 'zzz', 'n') 

想要的结果

(1, 'xxx', 'y') 
(2, 'xxx', 'y') 
(3, 'xxx', 'y') 
(4, 'xxx', 'y') 
(5, 'xxx', 'y') 

这被称为差距和孤岛问题。像这样的东西应该有效

;with cte as
(SELECT id, 
       description, 
       tag = 'y' ,
       cnt = Count(*)over(partition by description, grp)
FROM  (SELECT *, 
              grp = Sum(CASE WHEN prev_description = description THEN 0 ELSE 1 END)Over(Order by id)
       FROM   (SELECT *, 
                      prev_description = Lag(description) OVER(ORDER BY id) 
               FROM   Yourtable) a) b 
GROUP  BY id, description, grp 
)
Select * from cte 
Where cnt >= 4

另一种方法使用Row_Number

;with cte as
(SELECT id, 
       description, 
       tag = 'y' ,
       cnt = Count(*)over(partition by description, grp)
FROM  (select Grp = row_number()over(order by id) - 
             row_number()over(partition by description order by id), *
       from Yourtable) b 
GROUP  BY id, description, grp)
Select * from cte 
Where cnt >= 4

我想这样就可以了

select *, 'y' as 'newTag' 
from ( select *
           , count(*) over (partition by [description], grp) as 'grpSize' 
       from ( select * 
                   , ( [id] - row_number() over (partition by [description] order by [id]) ) as grp
              from [consecutive] 
            ) tt
     ) ttt 
where grpSize >= 4
order by [description], grp, [id]