PIVOT将所有NULL更改为0

时间:2014-08-18 19:06:00

标签: sql sql-server null pivot

我在下面的PIVOT中有一些NULL值。我必须粘贴到excel并将所有空值更改为0,这非常耗时。 如何为枢轴输出将所有NULL更改为0:

select * from 
(
     SELECT
         Calender.YrPer, [Period DATA 6b].PRODUCT_NO,
         isnull(sum([Period DATA     6b].LI_QTY+[Period DATA 6b].RG_QTY*1.0),0) as "TotalUnits"
     FROM Calender INNER JOIN
         [Period DATA 6b] ON Calender.[Starting Period] = [Period DATA     6b].STARTING_PERIOD LEFT OUTER JOIN
         Allproducts ON [Period DATA 6b].PRODUCT_NO = Allproducts.PRODUCT_NO
     where Calender.YrPer >= '2012P05'
     group by Calender.YrPer, [Period DATA 6b].PRODUCT_NO

     UNION

     SELECT
         'TotalPerProduct' YrPer, 
         [Period DATA 6b].
         PRODUCT_NO, 
         isnull(sum([Period     DATA 6b].LI_QTY+[Period DATA 6b].RG_QTY*1.0),0) as "TotalUnits"
     FROM
          Calender INNER JOIN
          [Period DATA 6b] ON Calender.[Starting Period] = [Period DATA     6b].STARTING_PERIOD LEFT OUTER JOIN
          Allproducts ON [Period DATA 6b].PRODUCT_NO = Allproducts.PRODUCT_NO
      where Calender.YrPer >= '2012P05'
      group by [Period DATA 6b].PRODUCT_NO

)
as test1
pivot
(
    sum(TotalUnits)
    for YrPer
    in ([2012P05],[2012P06],[2012P07],[2012P08],[2012P09],[2012P10],[2012P11],[2012P12],    [2012P13],[2013P01],[2013P02],[2013P03],[2013P04],[2013P05],[2013P06],[2013P07],[2013P08],    [2013P09],[2013P10],[2013P11],[2013P12],[2013P13],[2014P01],[2014P02],[2014P03],[2014P04],     [TotalPerProduct])
)
PivotTable
order by PRODUCT_NO

1 个答案:

答案 0 :(得分:1)

替换"从....中选择*" to"选择isnull(column1,0)作为column1 ...... 不确定这是你想要的吗?