使用枢轴但获得额外的行

时间:2015-10-20 12:38:38

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

我正在使用sql server 2012。

我在下面写了几乎可以工作的查询。首先请看输出示例。

输出示例

ISIN      NAME     Price     Barc    Bberg   QuoteType
ABC       MadeUp   100.33    101.23  0       Null
ABC       MadeUp   100.33    0       100.65  C

它应该是什么

ISIN      NAME     Price     Barc    Bberg   QuoteType
ABC       MadeUp   100.33    101.23  100.65  C

我无法理解为什么我的数据透视查询将结果放在两行上?我需要它在一条线上,但无法找到我的错误

with PCF as 
(
    select distinct tp.isin, tp.name, price 
    from tblTempPCF tp   
    inner join tblFunds f on tp.FundCode = f.CodeSS
    where f.FundType ='FixedIncome' and tp.CashItem =0
 ),
 OffPrice as 
 (  
    select isin, coalesce(Price, 0) Price, PriceSource, QuoteType 
    from tblTempPrices 
    where SecurityType = 'FixedIncome' and TableCheck = 'PCF'
 ),
 pvt as 
(
    select * 
    from OffPrice 
    source pivot (max(price) for PriceSource in ([BBerg], [Barc])) as pvt
), 
FinalP as 
(
 select isin, coalesce(BBerg, 0) as BBerg, coalesce(Barc, 0) as Barc, QuoteType 
 from pvt
)
select PCF.ISIN, PCF.Name, isnull(PCF.Price,0) Price, isnull(finalP.Barc,0) Barc, isnull(finalP.BBerg,0) Bberg, FinalP.QuoteType 
from PCF 
left join FinalP on PCF.isin = FinalP.isin

0 个答案:

没有答案