Saya lebih suka menggunakan solusi GROUP BY dengan ekspresi CASE.
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;
Contoh Pivot asli memiliki tipe dan ")" hilang. Coba yang berikut ini agar pivot berfungsi:
pivot( max(email) FOR emailRank IN (1,2,3));