如果您的 ID 是连续的并且没有间隙,那么我认为最快的方法是:
;with cte as (
select
T.id, T.sog, T.zoneId,
1 as grpId
from Table1 as T
where T.id = 1
union all
select
T.id, T.sog, T.zoneId,
c.grpId + case when T.zoneId = c.zoneId then 0 else 1 end as grpId
from cte as c
inner join Table1 as T on T.id = c.id + 1
)
select c.id, c.sog, c.zoneId, c.grpId
from cte as c
sql 小提琴演示
如果 Id 不是连续的或确实有间隙,您可以执行以下操作:
;with cte1 as (
select
T.id, T.sog, T.zoneId,
row_number() over (order by T.id) as row_num
from Table1 as T
), cte2 as (
select
T.id, T.sog, T.zoneId, T.row_num,
1 as grpId
from cte1 as T
where T.row_num = 1
union all
select
T.id, T.sog, T.zoneId, T.row_num,
c.grpId + case when T.zoneId = c.zoneId then 0 else 1 end as grpId
from cte2 as c
inner join cte1 as T on T.row_num = c.row_num + 1
)
select c.id, c.sog, c.zoneId, c.grpId
from cte2 as c
sql 小提琴演示
但我不确定性能是否对此有好处