How to replace (null) values with 0 output in PIVOT

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

I tried to convert the (null) values with 0 (zeros) output in PIVOT function but have no sucess.

Below is the table and the syntax i've tried"

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 

I tried to use the ISNULL but did not work.

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

Could someone please look into its syntax error? Thanks a lot!

回答1:

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 


回答2:

You cannot place the IsNull() until after the data is selected so you will place the IsNull() around the final value in the 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:

If you have a situation where you are using dynamic columns in your pivot statement you could use the following:

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 ') 


回答4:

Sometimes it's better to think like a parser, like T-SQL parser. While executing the statement, parser does not have any value in Pivot section and you can't have any check expression in that section. By the way, you can simply use this:

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 


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