2012-07-12 3 views
3

저는 SQL Server 2005에서 this query을 사용하고 있습니다. (예를 들어 좀 더 편리한 2008 삽입 메서드를 사용했습니다.) 그리드 출력에서 ​​0으로 대체해야합니다.SQL Server 2005에서 0으로 채우기 피벗 테이블

어쨌든이 작업을 수행 하시겠습니까?

+2

예, 참조 : [** 1 ** (http://stackoverflow.com/questions/3297132/replace-null-values-in -sql-pivot), [** 2 **] (http://stackoverflow.com/questions/9567807/null-in-sql-server-pivot), [** 3 **] (http : // stackoverflow .com/questions/10325538/how-to-replace-the-null-values-in-pivot-output-with-zeroes), [** 4 **] (http://stackoverflow.com/questions/) 7304081/replacement-null-from-case-query). Google에서 'SQL Server 피벗 대체 nulls 사이트 : stackoverflow.com'을 검색했습니다. – mellamokb

+0

# 1 나는 보았고 심지어 내 피들에 포함되었습니다. 다른 사람들이 내 문제를 해결했을 것입니다. 나는 google> 그래서 searchbox를 추측한다? – gooddadmike

+0

그게 내가하는 경향이있어. 하자 그것을 직면하자, 구글은 거의 항상 웹 사이트 자체 검색 상자보다 웹 사이트를 검색하는 것이 좋습니다 :) – mellamokb

답변

4

ISNULL() 기능을 사용합니다. 사실 여기에 동일한 질문/이미 SO에 대한 답변이 있습니다 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