Sudahkah Anda membaca dokumentasi di PIVOT di Microsoft SQL Server 2005?
SELECT CustId,
[1] AS Admin,
[2] AS Manager,
[3] AS Support,
[4] AS Assistant
FROM (SELECT c.CustId, r.RoleId
FROM CustomerRoles c JOIN Roles r USING (RoleId)) AS s
PIVOT (
COUNT(CustId)
FOR RoleId IN ([1], [2], [3], [4])
) AS pvt
ORDER BY CustId;
Saya belum menguji hal di atas, tetapi hanya berdasarkan dokumen. Ini mungkin membantu Anda memulai.
Sepertinya tidak ada cara untuk menghasilkan kolom secara dinamis. Anda harus membuat hard-code mereka.