Ik gebruik liever de GROUP BY-oplossing met CASE-expressie.
SELECT
id,
MAX(CASE WHEN emailRank = 1 THEN email END) AS [1],
MAX(CASE WHEN emailRank = 2 THEN email END) AS [2],
MAX(CASE WHEN emailRank = 3 THEN email END) AS [3],
MAX(CASE WHEN emailRank = 4 THEN email END) AS [4]
FROM (
SELECT
id,
email,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY email) AS emailRank
FROM TABLE
)
GROUP BY id;
Origineel Pivot-voorbeeld had type en ontbrak ")". Probeer het volgende om pivot werkend te krijgen:
pivot( max(email) FOR emailRank IN (1,2,3));