SQL Server 2005中PIVOT附近的语法不正确

时间:2013-11-28 05:52:52

标签: sql sql-server-2005 pivot-table

我想在数据透视表中创建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

任何人都可以帮我找到上述查询中的错误吗?

1 个答案:

答案 0 :(得分:0)

您已在T中的选定列中添加了聚合(总和),但未在前两列中添加,即SalesChannel和CardType。错误是因为用于数据透视的源表T不正确。