SQL Server 2005 fill pivot table with 0s

后端 未结 1 731
梦谈多话
梦谈多话 2021-01-14 09:38

I have this query in SQL Server 2005 (I used the more convenient 2008 insert method for example only) and I need the (null) to be replaced with 0 in the grid output.

<
相关标签:
1条回答
  • 2021-01-14 10:21

    You would use the ISNULL() function. See SQL Fiddle

    SELECT 'lessonid          response ->'
       , isnull([0], 0) as [0]
      , isnull([1], 0) as [1]
      , isnull([2], 0) as [2]
      , isnull([3], 0) as [3]
      , isnull([4], 0) as [4]
    FROM (
        SELECT lessonid AS 'lessonid          response ->'
            ,ISNULL(response,0) as response
            ,count(response) AS respcnt
        FROM tblRChoices
        GROUP BY lessonid
            ,response
        ) TableResponse
    PIVOT(SUM(respcnt) FOR response IN (
                [0]
                ,[1]
                ,[2]
                ,[3]
                ,[4]
                )) ResponsePivot
    
    0 讨论(0)
提交回复
热议问题