簡體   English   中英

SQL Server 2005中PIVOT附近的語法不正確

[英]Incorrect syntax near PIVOT in SQL server 2005

我想在數據透視表中創建SSRS報告。 所以我在下面寫了查詢。 解析以下查詢時,我在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

誰能幫我找到上面查詢中的問題?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM