1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
With Rnum as (
select
Row_number() over( Order by Date) as rNum,
[ID] as ID
,[Etat] as etat
from TA_TABLE),
rec as
(
Select ID, etat , 1 as Etape , rNum
From rnum
Where rNum = 1
union All
Select rnum.ID , rNum.etat ,
Case
When rec.Etat != Rnum.etat then Etape + 1
Else Etape
End as Etape
,Rnum.rNum
From Rnum
INNER Join rec
on (rnum.rNum = rec.rNum+1)
)
SELECT DISTINCT Etat,Etape,
STUFF(( SELECT ',' + cast (d.ID AS nvarchar(255))
FROM rec d
WHERE
g.Etat = d.Etat and g.Etape = d.Etape
FOR XML PATH('')
), 1, 1, '' )
AS IDS
FROM rec g |
Partager