PIVOT不工作')'附近的语法不正确

时间:2015-07-08 14:22:23

标签: sql sql-server pivot

T-SQL代码:

SELECT iCarrierInvoiceDetailsID, [1],[2],[3]
FROM [GroundEDI].[dbo].[tblCarrierInvoiceDetails]
PIVOT(MAX(dTotalCharge) FOR iCarrierInvoiceHeaderID IN ([1],[2],[3]))AS P

错误:

  

Msg 102,Level 15,State 1,Line 3
  ')'附近的语法不正确。

我知道为什么会收到此错误?

1 个答案:

答案 0 :(得分:3)

看起来您正在尝试直接从表本身而不是枢轴中选择枢轴列。你需要做这样的事情:

SELECT p.[1],p.[2],p.[3] 
FROM 
(SELECT iCarrierInvoiceHeaderID
       ,dTotalCharge
FROM [GroundEDI].[dbo].[tblCarrierInvoiceDetails]) t
PIVOT(MAX(dTotalCharge) FOR iCarrierInvoiceHeaderID IN ([1],[2],[3])
)AS P;