VARCHAR?-- Prepare sample dataDECLARE @Sample TABLE ( ID INT, col VARCHAR(9) )INSERT @SampleSELECT 0, '1' UNION ALLSELECT 0, '1' UNION ALLSELECT 0, '2' UNION ALLSELECT 1, '1' UNION ALLSELECT 1, '2' UNION ALLSELECT 1, '3' UNION ALLSELECT 2, '5' UNION ALLSELECT 2, '5' UNION ALLSELECT 2, '5' UNION ALLSELECT 3, '6' UNION ALLSELECT 3, '6' UNION ALLSELECT 5, '8' UNION ALLSELECT 5, '9' UNION ALLSELECT 4, '7'-- Stage the source dataDECLARE @Stage TABLE ( ID INT, rowID INT IDENTITY(0, 1), col VARCHAR(9) )INSERT @Stage ( ID, col )SELECT ID, colFROM @SampleORDER BY ID, col-- Display the dataSELECT ID, MAX(CASE WHEN recID = 0 THEN col ELSE NULL END) AS col1, MAX(CASE WHEN recID = 1 THEN col ELSE NULL END) AS col2, MAX(CASE WHEN recID = 2 THEN col ELSE NULL END) AS col3FROM ( SELECT s.ID, s.rowID - t.rowID AS recID, s.col FROM @Stage AS s INNER JOIN ( SELECT ID, MIN(rowID) AS rowID FROM @Stage GROUP BY ID ) AS t ON t.ID = s.ID ) AS dGROUP BY IDORDER BY ID
E 12°55'05.63"N 56°04'39.26"