Вы можете сделать это без курсора, используя windows функции:
select [TIMESTAMP], USERNAME, VALUE, ID, IsDupe,
case
when IsDupe = 1 then null
else DENSE_RANK()over(order by GroupID)
end as SetID
from(
select
*,
case when value like 'ORDER%' then ID
when value like 'ITEM%' then lag(ID,1)over (order by ID)
when value like 'FINISH BUILD%' then lag(ID,2)over (order by ID)
end as GroupID
from #tmp where IsDupe = 0
)a
union
select
[TIMESTAMP], USERNAME, VALUE, ID, IsDupe, null as SetID
from #tmp where IsDupe = 1
order by ID
Вот мой полный пример:
drop table #tmp
select '2020-02-12 07:00:03.000' as TIMESTAMP, 'LINA' as USERNAME , 'ORDER1' as VALUE , 1 as ID , 0 as IsDupe into #tmp
union select '2020-02-12 07:00:03.000' , 'LINA' , 'ITEM1' , 2 , 0
union select '2020-02-12 07:09:09.000' , 'LINA' , 'FINISH BUILD' , 3 , 0
union select '2020-02-12 07:09:10.000' , 'LINA' , 'ORDER1' , 4 , 0
union select '2020-02-12 07:09:11.000' , 'LINA' , 'ITEM2' , 5 , 0
union select '2020-02-12 07:24:07.000' , 'LINA' , 'FINISH BUILD' , 6 , 0
union select '2020-02-12 07:24:08.000' , 'NAGA' , 'ORDER2' , 7 , 0
union select '2020-02-12 07:24:10.000' , 'NAGA' , 'ITEM3' , 8 , 0
union select '2020-02-12 07:45:06.000' , 'NAGA' , 'FINISH BUILD' , 9 , 0
union select '2020-02-12 07:45:12.000' , 'NAGA' , 'FINISH BUILD' , 10 , 1
union select '2020-02-12 07:45:13.000' , 'XELLOS' , 'ORDER3' , 11 , 0
union select '2020-02-12 07:45:14.000' , 'XELLOS' , 'ITEM4' , 12 , 0
union select '2020-02-12 07:56:36.000' , 'XELLOS' , 'FINISH BUILD' , 13 , 0
union select '2020-02-12 07:56:39.000' , 'GOURRY' , 'ORDER4' , 14 , 0
union select '2020-02-12 07:56:40.000' , 'GOURRY' , 'ITEM5' , 15 , 0
union select '2020-02-12 08:30:11.000' , 'GOURRY' , 'FINISH BUILD' , 17 , 0
order by ID
select [TIMESTAMP], USERNAME, VALUE, ID, IsDupe,
case
when IsDupe = 1 then null
else DENSE_RANK()over(order by GroupID)
end as SetID
from(
select
*,
case when value like 'ORDER%' then ID
when value like 'ITEM%' then lag(ID,1)over (order by ID)
when value like 'FINISH BUILD%' then lag(ID,2)over (order by ID)
end as GroupID
from #tmp where IsDupe = 0
)a
union
select
[TIMESTAMP], USERNAME, VALUE, ID, IsDupe, null as SetID
from #tmp where IsDupe = 1
order by ID
Вывод:
TIMESTAMP USERNAME VALUE IDCol IsDupe SetID
2020-02-12 07:00:03.000 LINA ORDER1 1 0 1
2020-02-12 07:00:03.000 LINA ITEM1 2 0 1
2020-02-12 07:09:09.000 LINA FINISH BUILD 3 0 1
2020-02-12 07:09:10.000 LINA ORDER1 4 0 2
2020-02-12 07:09:11.000 LINA ITEM2 5 0 2
2020-02-12 07:24:07.000 LINA FINISH BUILD 6 0 2
2020-02-12 07:24:08.000 NAGA ORDER2 7 0 3
2020-02-12 07:24:10.000 NAGA ITEM3 8 0 3
2020-02-12 07:45:06.000 NAGA FINISH BUILD 9 0 3
2020-02-12 07:45:12.000 NAGA FINISH BUILD 10 1 NULL
2020-02-12 07:45:13.000 XELLOS ORDER3 11 0 4
2020-02-12 07:45:14.000 XELLOS ITEM4 12 0 4
2020-02-12 07:56:36.000 XELLOS FINISH BUILD 13 0 4
2020-02-12 07:56:39.000 GOURRY ORDER4 14 0 5
2020-02-12 07:56:40.000 GOURRY ITEM5 15 0 5
2020-02-12 08:30:11.000 GOURRY FINISH BUILD 17 0 5