SQL Server 2005中的PIVOT附近的语法不正确
问题描述:
我想在数据透视表中创建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
任何人都可以帮助我找到上述查询中的错误吗?
答
您已在T中的选定列中添加了聚合(总和),但不是在前两列(即SalesChannel和CardType)中添加了聚合(总和)。错误是因为透视的源表T不正确。
+0
我已经删除了聚合函数(总和),但我仍然得到相同的错误 - – Urvashi
您已在T中的选定列中添加了聚合(总和),但不是在前两列(即SalesChannel和CardType)中添加了聚合(总和)。错误是因为透视的源表T不正确。这个查询是否独立工作? – Deepshikha
你得到的错误信息是什么?如果你运行没有PIVOT的子查询,你会得到一个错误吗? – Taryn