0

我想将我的行合并到 symbolID 相同的单行中。符号 id 可以在列中出现两次。我在谷歌上学到了很多东西,我得到的东西Pivot可以用于这种类型的活动。但我很困惑如何使用它。我尝试了很多方法,return null 我正在使用 sql server 2008 以下是我的查询

select Sec.Description,Sec.SecurityTypeID,  t.SymbolID,Sec.Symbol, SUM(Quantity)as Quantity, round(sum(CostBasisAdjustment),2) as CostBasis,
 t.PortfolioID,Sec.AccrualMethodTypeID,Sec.AnnualIncomeRate,Sec.IncomeFrequencyID,sec.DisplayAccruedFlag, Pr.price as CurPrice
 ,case
   when sec.SecurityTypeID =  2 then round(SUM(t.quantity*pr.price/100),2)           -- Fixed   =  2
   when sec.SecurityTypeID =  5 then round(SUM(t.quantity*pr.price/100),2)           -- CD      =  5
   when sec.SecurityTypeID =  6 then round(SUM(t.quantity*pr.Factor*pr.price/100),2) -- Mortgage=  6
   when sec.SecurityTypeID =  8 then round(SUM(t.quantity*100*pr.price),2)           -- Options =  8
   when sec.SecurityTypeID = 11 then round(Sum(CostBasisAdjustment),2)               -- Cash    = 11
   when sec.SecurityTypeID =  9 then round(SUM(t.quantity*pr.price/100),2)           -- T-Bill  =  9    
--  Calculation for Paper  unknownen AT this time.                                   -- Paper   = 10
--  Calculation for Index   ** Not Used ** AT this time.                             -- Index   = 12    
    else round(SUM(t.quantity*pr.price),2)                                     -- Equity  =  1,3,4,7 
  end as EndValue
  --
  -- Case Unrealized gain or loss
  ,case
    -- Individual securityTypeID check so the correct multipler can be used.
    -- Checking the SecurityTypeID give us the unit amount for the quantity. 
    -- Checking the LongPositonsflag lets us know if we own the security or we are short(negative) the security.
         --
         -- Price does not exist in price file 
         when pr.Price IS null and sec.SecurityTypeID <> 11  then sum(t.CostBasisAdjustment *-1)

         -- Option Unrealized
         when sec.SecurityTypeID = 8 and t.LongPositionFlag = 1 then round(SUM(t.Quantity*100*pr.price)-sum(t.CostBasisAdjustment),2) 
         when sec.SecurityTypeID = 8 and t.LongPositionFlag = 0 then round(SUM(t.Quantity*-100*pr.price)+sum(t.CostBasisAdjustment),2) 
         --
         -- Fixed  Unrealized
         when sec.SecurityTypeID = 2 and t.LongPositionFlag = 1 then round(SUM(t.Quantity*pr.price/100)-sum(t.CostBasisAdjustment),2)
         when sec.SecurityTypeID = 2 and t.LongPositionFlag = 0 then round(SUM(t.Quantity*pr.price/100)+sum(t.CostBasisAdjustment),2)   
         -- T-Bill  Unrealized
         when sec.SecurityTypeID = 9 and t.LongPositionFlag = 1 then round(SUM(t.Quantity*pr.price/100)-sum(t.CostBasisAdjustment),2)
         when sec.SecurityTypeID = 9 and t.LongPositionFlag = 0 then round(SUM(t.Quantity*pr.price/100)+sum(t.CostBasisAdjustment),2)   

         -- CD  Unrealized
         when sec.SecurityTypeID = 5 and t.LongPositionFlag = 1 then round(SUM(t.Quantity*pr.price/100)-sum(t.CostBasisAdjustment),2)  
         when sec.SecurityTypeID = 5 and t.LongPositionFlag = 0 then round(SUM(t.Quantity*pr.price/100)+sum(t.CostBasisAdjustment),2)   
         -- Mortgage Unrealized
         when sec.SecurityTypeID = 6 and t.LongPositionFlag = 1 then round(SUM(t.quantity*pr.Factor*pr.price/100)-sum(t.CostBasisAdjustment),2)  
         when sec.SecurityTypeID = 6 and t.LongPositionFlag = 0 then round(SUM(t.quantity*pr.Factor*pr.price/100)+sum(t.CostBasisAdjustment),2)   

       else round(SUM(t.Quantity*pr.price)-sum(t.CostBasisAdjustment),2)
   end as Unrealized
   --, dbo.Fcm_fc_GetAccrued(t.SymbolID,SUM(Quantity),@FromDate) as accurd -- Calculate Accrued Interest

from Transactions as t
 inner join securities     as sec on t.SymbolID = sec.SecurityID
 left outer join SecurityPrices as pr  on symbolid = pr.SecurityID AND pr.PriceDate = @PriceDateFrom -- sets the price file date
where PortfolioID = @PortfolioID
 and TradeDate <= @FromDate
 and t.StatusTypeID <> 110 
group by portfolioid,t.LongPositionFlag, symbolid,sec.Symbol, sec.Description,sec.SecurityTypeID, pr.price
,Sec.AccrualMethodTypeID,Sec.AnnualIncomeRate,Sec.IncomeFrequencyID,sec.DisplayAccruedFlag,sec.MaturityDate having SUM(quantity) <> 0 

union all

select Sec.Description,Sec.SecurityTypeID,  t.SymbolID,Sec.Symbol, SUM(Quantity)as Quantity, round(sum(CostBasisAdjustment),2) as CostBasis,
 t.PortfolioID,Sec.AccrualMethodTypeID,Sec.AnnualIncomeRate,Sec.IncomeFrequencyID,sec.DisplayAccruedFlag, Pr.price as CurPrice
 ,case
   when sec.SecurityTypeID =  2 then round(SUM(t.quantity*pr.price/100),2)           -- Fixed   =  2
   when sec.SecurityTypeID =  5 then round(SUM(t.quantity*pr.price/100),2)           -- CD      =  5
   when sec.SecurityTypeID =  6 then round(SUM(t.quantity*pr.Factor*pr.price/100),2) -- Mortgage=  6
   when sec.SecurityTypeID =  8 then round(SUM(t.quantity*100*pr.price),2)           -- Options =  8
   when sec.SecurityTypeID = 11 then round(Sum(CostBasisAdjustment),2)               -- Cash    = 11
   when sec.SecurityTypeID =  9 then round(SUM(t.quantity*pr.price/100),2)           -- T-Bill  =  9    
--  Calculation for Paper  unknownen AT this time.                                   -- Paper   = 10
--  Calculation for Index   ** Not Used ** AT this time.                             -- Index   = 12    
    else round(SUM(t.quantity*pr.price),2)                                     -- Equity  =  1,3,4,7 
  end as BeginningVal
  --
  -- Case Unrealized gain or loss
  ,case
    -- Individual securityTypeID check so the correct multipler can be used.
    -- Checking the SecurityTypeID give us the unit amount for the quantity. 
    -- Checking the LongPositonsflag lets us know if we own the security or we are short(negative) the security.
         --
         -- Price does not exist in price file 
         when pr.Price IS null and sec.SecurityTypeID <> 11  then sum(t.CostBasisAdjustment *-1)

         -- Option Unrealized
         when sec.SecurityTypeID = 8 and t.LongPositionFlag = 1 then round(SUM(t.Quantity*100*pr.price)-sum(t.CostBasisAdjustment),2) 
         when sec.SecurityTypeID = 8 and t.LongPositionFlag = 0 then round(SUM(t.Quantity*-100*pr.price)+sum(t.CostBasisAdjustment),2) 
         --
         -- Fixed  Unrealized
         when sec.SecurityTypeID = 2 and t.LongPositionFlag = 1 then round(SUM(t.Quantity*pr.price/100)-sum(t.CostBasisAdjustment),2)
         when sec.SecurityTypeID = 2 and t.LongPositionFlag = 0 then round(SUM(t.Quantity*pr.price/100)+sum(t.CostBasisAdjustment),2)   
         -- T-Bill  Unrealized
         when sec.SecurityTypeID = 9 and t.LongPositionFlag = 1 then round(SUM(t.Quantity*pr.price/100)-sum(t.CostBasisAdjustment),2)
         when sec.SecurityTypeID = 9 and t.LongPositionFlag = 0 then round(SUM(t.Quantity*pr.price/100)+sum(t.CostBasisAdjustment),2)   

         -- CD  Unrealized
         when sec.SecurityTypeID = 5 and t.LongPositionFlag = 1 then round(SUM(t.Quantity*pr.price/100)-sum(t.CostBasisAdjustment),2)  
         when sec.SecurityTypeID = 5 and t.LongPositionFlag = 0 then round(SUM(t.Quantity*pr.price/100)+sum(t.CostBasisAdjustment),2)   
         -- Mortgage Unrealized
         when sec.SecurityTypeID = 6 and t.LongPositionFlag = 1 then round(SUM(t.quantity*pr.Factor*pr.price/100)-sum(t.CostBasisAdjustment),2)  
         when sec.SecurityTypeID = 6 and t.LongPositionFlag = 0 then round(SUM(t.quantity*pr.Factor*pr.price/100)+sum(t.CostBasisAdjustment),2)   

       else round(SUM(t.Quantity*pr.price)-sum(t.CostBasisAdjustment),2)
   end as Unrealized
   --,dbo.Fcm_fc_GetAccrued(t.SymbolID,SUM(Quantity),@ToDate) as accurd -- Calculate Accrued Interest


 --into #mfbegpos     
from Transactions as t
 inner join securities     as sec on t.SymbolID = sec.SecurityID
 left outer join SecurityPrices as pr  on symbolid = pr.SecurityID AND pr.PriceDate = @PriceDateTo -- sets the price file date

 -- Two variables are need one for the Price Date and one for the Position Date  they can be different.
where PortfolioID = @PortfolioID
 --and TradeDate <= '2012/12/31' -- set the position date
  and TradeDate <= @ToDate
 and t.StatusTypeID <> 110 

group by portfolioid,t.LongPositionFlag, symbolid,sec.Symbol, sec.Description,sec.SecurityTypeID, pr.price
,Sec.AccrualMethodTypeID,Sec.AnnualIncomeRate,Sec.IncomeFrequencyID,sec.DisplayAccruedFlag,sec.MaturityDate having SUM(quantity) <> 0

以下是数据输出样本

在此处输入图像描述 请指导我,我真的被卡住了。

4

0 回答 0