PIVOT doesn't work Incorrect syntax near ')'

T-SQL code:

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

      

Mistake:

Msg 102, Level 15, State 1, Line 3
Invalid syntax near ')'.

Any idea why I am getting this error?

+3


source to share


1 answer


It looks like you are trying to directly select the pivot columns from the table itself and not for the pivot point. You will need to do something like this:



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;

      

+1


source







All Articles