CREATE TABLE #t
(
RdvId int NOT NULL
,WorkLineNumber int NOT NULL
,WorkDescription nvarchar(250)
);
INSERT INTO #t
VALUES (1177, 0, 'Distribution - Remplcement kit Diesel')
,(1177, 1, 'Pneus - Forfait montage + équilibrage x 2')
,(1177, 2, 'Freinage - Remplacement plaquettes AV')
,(1181, 0, 'Entretien des 180 000 km Long Life Classique')
,(1181, 1, 'Pneus - Réparation x 1');
There are two common approaches:
Option 1
SELECT RdvId
,MAX(CASE WHEN WorkLineNumber = 0 THEN WorkDescription END) AS Q1
,MAX(CASE WHEN WorkLineNumber = 1 THEN WorkDescription END) AS Q2
,MAX(CASE WHEN WorkLineNumber = 2 THEN WorkDescription END) AS Q3
FROM #t
GROUP BY RdvId;
Option 2
SELECT RdvId, [0] AS Q1, [1] AS Q2, [2] AS Q3
FROM
(
SELECT RdvId, WorkLineNumber, WorkDescription
FROM #t
) S
PIVOT
(
MAX(WorkDescription)
FOR WorkLineNumber IN ([0], [1], [2])
) AS P;