2012-09-07 355 views
22

我試圖在PIVOT函數中用0(零)輸出轉換(空)值,但沒有成功。如何用PIVOT中的0輸出替換(null)值

下面是表和語法我已經試過」

SELECT 
CLASS, 
[AZ], 
[CA], 
[TX] 
FROM #TEMP 
PIVOT (SUM(DATA) 
FOR STATE IN ([AZ], [CA], [TX])) AS PVT 
ORDER BY CLASS 

CLASS AZ CA  TX 
RICE 10 4  (null) 
COIN 30 3  2 
VEGIE (null) (null) 9 

我試圖用ISNULL,但沒有奏效。

PIVOT SUM(ISNULL(DATA,0)) AS QTY 

可能有人請看看它的語法錯誤?非常感謝!

回答

30
SELECT CLASS, 
isnull([AZ],0), 
isnull([CA],0), 
isnull([TX],0) 
FROM #TEMP 
PIVOT (SUM(DATA) 
FOR STATE IN ([AZ], [CA], [TX])) AS PVT 
ORDER BY CLASS 
+6

這並不總是工作。當記錄數量有差異時,數據透視表創建新的「單元格」,可以爲NULL。解決方案應該迎合這種情況。 – greenafrican

+0

你能爲此答案添加一些解釋嗎? –

11

您不能放置IsNull(),直到數據被選中後,您將把IsNull()放置在最終值E在SELECT

SELECT CLASS, 
    IsNull([AZ], 0) as [AZ], 
    IsNull([CA], 0) as [CA], 
    IsNull([TX], 0) as [TX] 
FROM #TEMP 
PIVOT 
(
    SUM(DATA) 
    FOR STATE IN ([AZ], [CA], [TX]) 
) AS PVT 
ORDER BY CLASS 
3

有時,最好看待一個解析器,像T-SQL語法分析程序。執行語句時,解析器在「透視」部分中沒有任何值,並且該部分中不能有任何檢查表達式。順便說一句,你可以簡單地使用:

SELECT CLASS 
, IsNull([AZ], 0) 
, IsNull([CA], 0) 
, IsNull([TX], 0) 
    FROM #TEMP 
    PIVOT (
     SUM(DATA) 
     FOR STATE IN (
      [AZ] 
     , [CA] 
     , [TX] 
     ) 
    ) AS PVT 
    ORDER BY CLASS 
11

如果你有,你使用的是在數據透視聲明動態列的情況下,你可以使用如下:

DECLARE @cols    NVARCHAR(MAX) 
DECLARE @colsWithNoNulls NVARCHAR(MAX) 
DECLARE @query    NVARCHAR(MAX) 

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(Name) 
      FROM Hospital 
      WHERE Active = 1 AND StateId IS NOT NULL 
      FOR XML PATH(''), TYPE 
      ).value('.', 'NVARCHAR(MAX)') 
     ,1,1,'') 

SET @colsWithNoNulls = STUFF(
      (
       SELECT distinct ',ISNULL(' + QUOTENAME(Name) + ', ''No'') ' + QUOTENAME(Name) 
       FROM Hospital 
       WHERE Active = 1 AND StateId IS NOT NULL 
       FOR XML PATH(''), TYPE 
      ).value('.', 'NVARCHAR(MAX)') 
     ,1,1,'') 

EXEC (' 
     SELECT Clinician, ' + @colsWithNoNulls + ' 
     FROM 
     (
      SELECT DISTINCT p.FullName AS Clinician, h.Name, CASE WHEN phl.personhospitalloginid IS NOT NULL THEN ''Yes'' ELSE ''No'' END AS HasLogin 
      FROM Person p 
      INNER JOIN personlicense pl ON pl.personid = p.personid 
      INNER JOIN LicenseType lt on lt.licensetypeid = pl.licensetypeid 
      INNER JOIN licensetypegroup ltg ON ltg.licensetypegroupid = lt.licensetypegroupid 
      INNER JOIN Hospital h ON h.StateId = pl.StateId 
      LEFT JOIN PersonHospitalLogin phl ON phl.personid = p.personid AND phl.HospitalId = h.hospitalid 
      WHERE ltg.Name = ''RN'' AND 
       pl.licenseactivestatusid = 2 AND 
       h.Active = 1 AND 
       h.StateId IS NOT NULL 
     ) AS Results 
     PIVOT 
     (
      MAX(HasLogin) 
      FOR Name IN (' + @cols + ') 
     ) p 
') 
+0

不漂亮,但工程,我也不得不添加一個列別名! *不寒而慄* – Shaun