以0 SQL Server 2005中填充数据透视表(SQL Server 2005 fill pi

2019-06-26 05:41发布

我有此查询在SQL Server 2005(我只用了更方便的插入2008的方法为例),我需要的(空)将在电网输出与0代替。

反正这样做呢?

Answer 1:

你会使用ISNULL()函数。 请参阅SQL小提琴

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


文章来源: SQL Server 2005 fill pivot table with 0s