如何在SQL中使用Pivot格式化SQL查询?

问题描述:

找到图像1和2,我想要结果作为图像2使用枢轴SQL。如何在SQL中使用Pivot格式化SQL查询?

Old table

Desired Format

+0

你可以在这里看看:https://msdn.microsoft.com/en-us/library/ms177410.aspx – Jayvee

SELECT PermID, 
ISNULL([New_User],0)as [New_User],ISNULL([Edit_User],0)as [Edit_User] ,ISNULL([Permission],0) as [Permission] ,ISNULL([Store-Credit],0)as [Store-Credit],ISNULL([Refund],0) as [Refund] ,ISNULL([Gift_Certificate],0)as [Gift_Certificate] 
FROM 
(SELECT PermID, Fname,'0' AS Active 
    FROM F3_BC_Users_Permissions_Master) AS SourceTable 
PIVOT 
(
max(Active) 
FOR fname IN ([New_User],[Edit_User],[Permission],[Store-Credit],[Refund],[Gift_Certificate]) 
) AS PivotTable;