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

时间:2016-09-28 09:32:40

标签: sql-server-2008

找到图像1和2,我想使用pivot SQL将结果作为图像2。

Old table

Desired Format

1 个答案:

答案 0 :(得分:0)

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;