SQL Server 2005 fill pivot table with 0s

↘锁芯ラ 提交于 2019-12-19 09:44:43

问题


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.

Anyway to do this?


回答1:


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


来源:https://stackoverflow.com/questions/11459355/sql-server-2005-fill-pivot-table-with-0s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!