sql >> Databasteknik >  >> RDS >> Sqlserver

SQL Server 2005 fyll pivottabellen med nollor

Du skulle använda ISNULL() fungera. Se 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



  1. Infoga ordbok i listan till databas i python

  2. Hämta data från korsningstabellen i Yii2

  3. PostgreSQL array_agg(INTEGER[])

  4. problem med att få num_rows med PDO-klassen i php