2015-09-08 33 views
0

我正在使用SQL Server 2012.我已經使用數據透視表編寫了一個選擇查詢。查詢工作正常,除了一個我看不出來的問題。在包含關鍵點的選擇查詢中將null更改爲零

在結果中,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 

回答

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;