Intenta el siguiente código:
Código SQL:
Ver originalCREATE TABLE TABLE1(id INT, nombre VARCHAR(MAX), valor INT);
GO
INSERT INTO TABLE1 SELECT 1, 'Juan', 12;
INSERT INTO TABLE1 SELECT 2, 'Juan', 32;
INSERT INTO TABLE1 SELECT 3, 'Juan', 51;
INSERT INTO TABLE1 SELECT 4, 'Mario', 58;
INSERT INTO TABLE1 SELECT 5, 'Mario', 19;
INSERT INTO TABLE1 SELECT 6, 'Mario', 74;
INSERT INTO TABLE1 SELECT 7, 'Melisa', 27;
INSERT INTO TABLE1 SELECT 8, 'Melisa', 58;
INSERT INTO TABLE1 SELECT 9, 'Melisa', 36;
INSERT INTO TABLE1 SELECT 10, 'Carlos', 27;
INSERT INTO TABLE1 SELECT 11, 'Carlos', 11;
GO
WITH T AS
(SELECT ROW_NUMBER() OVER(Partition BY nombre ORDER BY id) Nm,
*
FROM TABLE1)
SELECT nombre,
IsNull(MAX(CASE WHEN Nm=1 THEN CAST(Valor AS VARCHAR) END),'-')+','
+IsNull(MAX(CASE WHEN Nm=2 THEN CAST(Valor AS VARCHAR) END),'-')+','
+IsNull(MAX(CASE WHEN Nm=3 THEN CAST(Valor AS VARCHAR) END),'-')
FROM T
GROUP BY nombre;