Jeg foretrækker at bruge GROUP BY-løsningen med CASE-udtryk.
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;
Det originale Pivot-eksempel havde typen og manglede ")". Prøv følgende for at få pivot til at virke:
pivot( max(email) FOR emailRank IN (1,2,3));