枢轴查询导致"语法错误" PIVOT子句中的错误

时间:2015-04-16 16:00:13

标签: sql sql-server-2008 pivot

我收到错误,"基本'"附近的语法不正确当我尝试执行以下查询时:

WITH BaseQuery AS (

SELECT Region,Essbase_Channel,Product,COUNT(New_reconnects)
FROM NDW.dbo.SOS_Detail SOS
WHERE SOS.EntityID IN ('000310','000700','000815','000854')
  AND Division ='NORTHEAST DIVISION' AND Month_Name ='MAR'
GROUP BY Month_Name,Product,Region,Essbase_Channel,EntityID,Division

)
SELECT * FROM BaseQuery

PIVOT (COUNT(New_reconnects) FOR Product IN ('BASIC','HSI','CDV','H1','X1')) AS PVT
ORDER BY Product,Region,Essbase_Channel

我的目标是让第一列基于Essbase_Channel,数据透视列为Product值:BASIC,HSI,CDV,H1和数据透视中的值为{{1 }}

我应该如何更改查询的语法以避免此错误?

1 个答案:

答案 0 :(得分:1)

更改为:

FOR Product IN ([BASIC],[HSI],[CDV],[H1],[X1])