I have this table like this
| RowID | Model | Request No | Resource | Date | Time | Duration |
|---|---|---|---|---|---|---|
| 12 | W221A0 | RE-1716 | RG | 14-Jan-22 | 9:50:40 | 4 |
| 14 | X210132 | RE-1341 | CL | 15-Dec-21 | 10:45:09 | 2 |
| 20 | X210132 | RE-1341 | ZA | 17-Dec-21 | 8:57:09 | 5 |
| 30 | X210132 | RE-1341 | SA | 21-Dec-21 | 8:10:02 | 1 |
| 44 | W221A0 | RE-1716 | RG | 10-Jan-22 | 10:14:40 | 8 |
| 59 | X210132 | RE-1341 | ZA | 16-Dec-21 | 9:56:46 | 3 |
| 60 | X210132 | RE-1341 | SA | 20-Dec-21 | 8:30:50 | 0 |
| 95 | X210132 | RE-1341 | ZA | 21-Dec-21 | 10:12:51 | 6 |
| 99 | W221A0 | RE-1716 | RG | 11-Jan-22 | 8:06:46 | 2 |
| 100 | X210132 | RE-1341 | ZA | 15-Dec-21 | 10:20:26 | 12 |
| 102 | W221A0 | RE-1716 | CL | 14-Jan-22 | 8:10:27 | 3 |
| 105 | W221A0 | RE-1716 | TE | 10-Jan-22 | 8:36:33 | 7 |
| 106 | W221A0 | RE-1716 | RG | 6-Jan-22 | 10:29:44 | 5 |
| 300 | W221A0 | RE-1716 | RG | 14-Jan-22 | 9:01:18 | 6 |
After sorting by Date, Time field. I expect to have new table with Like this:

The sequence of the table is important to determine the grouping by Resource value, the Resources that are sequentially next to each other should be collapsed together to build the STEP and STEP TIME, where the STEP TIME value for each resource is the SUM of the Duration values for that Resource.
These two images should illustrate how the computed values are constructed:


I tried use
SELECT Main.[Request No.],LEFT(Main.STORAGEDATA,Len(Main.STORAGEDATA)-1) As "Process" FROM(
SELECT DISTINCT ST2.[Request No.],
(
SELECT ST1.Resource + ',' AS [text()]
FROM dbo.STORAGEDATA ST1
WHERE ST1.[Request No.] = ST2.[Request No.]
ORDER BY ST1.[Request No.]
FOR XML PATH (''), TYPE
).value('text()[1]','nvarchar(max)') STORAGEDATA
FROM dbo.STORAGEDATA ST2
) [Main]
However, It doesn't return what I expect, this is the output:
| Request No. | Process |
|---|---|
| RE-1341 | CL,ZA,SA,ZA,SA,ZA,ZA |
| RE-1716 | RG,RG,RG,CL,TE,RG,RG |
This CREATE TABLE expression can be used for this dataset:
CREATE TABLE STORAGEDATA (
RowID int,
Model varchar(10),
[Request No.] varchar(10),
[Resource] char(2),
[Date] Date,
[Time] Time,
Duration int
)
INSERT INTO STORAGEDATA
VALUES
( 12 ,'W221A0 ','RE-1716','RG','14-Jan-22','9:50:40 ',4 )
,( 14 ,'X210132','RE-1341','CL','15-Dec-21','10:45:09 ',2 )
,( 20 ,'X210132','RE-1341','ZA','17-Dec-21','8:57:09 ',5 )
,( 30 ,'X210132','RE-1341','SA','21-Dec-21','8:10:02 ',1 )
,( 44 ,'W221A0 ','RE-1716','RG','10-Jan-22','10:14:40 ',8 )
,( 59 ,'X210132','RE-1341','ZA','16-Dec-21','9:56:46 ',3 )
,( 60 ,'X210132','RE-1341','SA','20-Dec-21','8:30:50 ',0 )
,( 95 ,'X210132','RE-1341','ZA','21-Dec-21','10:12:51 ',6 )
,( 99 ,'W221A0 ','RE-1716','RG','11-Jan-22','8:06:46 ',2 )
,( 100 ,'X210132','RE-1341','ZA','15-Dec-21','10:20:26 ',12 )
,( 102 ,'W221A0 ','RE-1716','CL','14-Jan-22','8:10:27 ',3 )
,( 105 ,'W221A0 ','RE-1716','TE','10-Jan-22','8:36:33 ',7 )
,( 106 ,'W221A0 ','RE-1716','RG','6-Jan-22 ','10:29:44 ',5 )
,( 300 ,'W221A0 ','RE-1716','RG','14-Jan-22','9:01:18 ',6 );