2013-11-28 59 views
0

我想在數據透視表中創建SSRS報告。所以我寫下面的查詢。 解析下面的查詢時,我收到Pivot錯誤附近的錯誤語法。SQL Server 2005中的PIVOT附近的語法不正確

Create PROCEDURE [dbo].[CP_get_transaction_by_card_Type_Summary] 
@StartDate DateTime, 
@EndDate DateTime 

AS 
BEGIN 
select [SalesChannel],[Amex] as Amex,[Maestro] 
from 
(
select 
case isnull(C.Agentid,'') 
     when 'D085' then 'Contact Centre' 
     when 'NXHQ' then 'Public Website' 
     when 'D031' then 'Partner Agent - NXWN' 
     when 'D167' then 'Partner Agent - NXWN' 
     when 'D267' then 'Partner Agent - NXWN' 
     when 'D334' then 'Partner Agent - NXWN' 
     when 'D345' then 'Partner Agent - NXWN' 
     when 'D031' then 'Partner Agent - NXWN' 
     when 'D446' then 'Partner Agent - NXWN' 
     when 'G500' then 'Partner Agent - VCS' 
     else 
      case substring(C.Agentid,1,2) 
       when 'XK' then 'Kiosk'  
       else    
        case C.thirdparty 
         when 0 then 'NEL Travel Shop' 
         else 
          case a.agent_code 
           when 'STAFF' then 'Public Website' 
           else 'Partner Agent' 
          end 
         end 
       end 
     end as SalesChannel 
    ,case CT.card_description 
     when 'Switch' then 'Maestro' 
     else CT.card_description 
    end as CardType 
    ,p.payment_value as Value 


from 
    dbo.tbl_sales S 
    join dbo.tbl_basket_summary BS 
     on s.sale_id = bs.sale_id 
      --and bs.agentid in ('NXHQ','D085') 
      --and bs.transtype = 'R' 
      and bs.transstatus in ('Q','P','Z') 
      and s.sale_date between @StartDate and @EndDate 
    join dbo.tbl_Payments P 
     on s.sale_id = p.sale_id 
    join dbo.tbl_card_details CD 
     on p.card_details_id = CD.card_details_id 
      and p.card_details_id <> 0 
    join dbo.tbl_card_types CT 
     on CD.card_type_id = CT.card_type_id 
    join dbo.tbl_agents A 
     on bs.agentid = a.agent_code 
    left join dbo.Config C 
     on BS.agentid = C.agentid 

)T 


PIVOT 
(
    SUM(Value) 
    FOR [CardType] in ([Amex],[Maestro]) 

)as pvt 



END 

任何人都可以幫助我找到上述查詢中的錯誤嗎?

+0

您已在T中的選定列中添加了聚合(總和),但不是在前兩列(即SalesChannel和CardType)中添加了聚合(總和)。錯誤是因爲透視的源表T不正確。這個查詢是否獨立工作? – Deepshikha

+0

你得到的錯誤信息是什麼?如果你運行沒有PIVOT的子查詢,你會得到一個錯誤嗎? – Taryn

回答

0

您已在T中的選定列中添加了聚合(總和),但不是在前兩列(即SalesChannel和CardType)中添加了聚合(總和)。錯誤是因爲透視的源表T不正確。

+0

我已經刪除了聚合函數(總和),但我仍然得到相同的錯誤 - – Urvashi

相關問題