SQL server 2005中PIVOT附近的语法不正确
我想在透视表中创建SSRS报告。所以我写了下面的问题。 在分析下面的查询时,我发现Pivot错误附近的语法不正确SQL server 2005中PIVOT附近的语法不正确,sql,sql-server-2005,pivot-table,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.Agen
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。错误是因为pivot的源表T不正确。您在T中的选定列中添加了聚合和,但在前两列(即SalesChannel和CardType)中没有添加。错误是因为pivot的源表T不正确。此查询是否独立工作?您收到的错误消息是什么?如果在不使用PIVOT的情况下运行子查询,是否会出现错误?我已删除聚合函数sum,但仍然会出现相同的错误-