在涉及数据透视的选择查询中将null更改为零

时间:2015-09-08 15:00:28

标签: sql sql-server sql-server-2012

我正在使用SQL Server 2012.我使用数据透视表编写了一个选择查询。除了我无法弄清楚的一个问题之外,该查询工作正常。

在结果中,POLT的部分价格为空。我想将空值更改为零。我知道在正常的选择查询中我可以使用isnull函数但是在使用数据透视时不能使用它吗?

;with offPrice as
(select ISIN, Price, PriceSource from tblTempPrices
where SecurityType = 'FP'
and TableCheck = 'PB') select * from offPrice
source pivot (max (price) for PriceSource in ([FSB], [POLT])) as pvt

1 个答案:

答案 0 :(得分:2)

如果问题是数据中的值 NULL,则在枢轴之前在CTE中修复此问题:

with offPrice as (
      select ISIN, coalesce(Price, 0) as Price, PriceSource
      from tblTempPrices
      where SecurityType = 'FP' and TableCheck = 'PB'
     )
. . .

如果在枢轴之前问题缺少值,请在枢轴之后修复此问题:

with offPrice as (
      select ISIN, Price, PriceSource
      from tblTempPrices
      where SecurityType = 'FP' and TableCheck = 'PB'
     ),
     pvt as (
      select * 
      from offPrice
      source pivot (max (price) for PriceSource in ([FSB], [POLT])) as pvt
     )
select ISIN, coalesce(fsb, 0) as fsb, coalesce(polt, 0) as polt
from pvt;