Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone tell me error in this query


SQL
SELECT  TxnAttributesID,TxnAttributesName,AccountabilityTypeID,TxnAttributesValue,[DAY],[GRAVE],[SWING],
CreatedDate,ModifiedDate
    FROM    ( SELECT   t1.TxnAttributesID,t1.TxnAttributesName,t1.AccountabilityTypeID,t2.TxnAttributesValue,t3.ShiftTypeID,
    t3.ShiftTypeName,t2.CreatedDate,t2.ModifiedDate
         from Code.tblTxnAttributes as t1 inner join
         App.tblDailyRevDetail as t2 on t1.TxnAttributesID = t2.TxnAttributesID
         inner join App.tblShiftType as t3 on t2.ShiftTypeID = t3.ShiftTypeID) as P

             PIVOT (SUM(cast(TxnAttributesValue as int)) FOR ShiftTypeName IN ([DAY],[GRAVE],[SWING])) AS pvt
    ORDER BY CreatedDate



Its giving Msg 102, Level 15, State 1, Line 9
Incorrect syntax near '('.


but i have checked and all are closed properly
Posted

The problem was that i was selecting the field also in select list for which i was creating pivot . I removed it from the list and got my answer.
 
Share this answer
 
Comments
Maciej Los 23-May-12 12:25pm    
Good work! My 5
I am not sure but as per the Syntax of PIVOT given here
http://msdn.microsoft.com/en-us/library/ms177410(v=sql.90).aspx[^] in the first Select statement the non pivoted columns shall come first and then the pivoted columns shall be specified.

Here in the question the non pivoted columns
SQL
CreatedDate,ModifiedDate
are given after the pivoted columns
SQL
[DAY],[GRAVE],[SWING]

Shift the non pivoted columns before pivoted columns and try.
 
Share this answer
 
Comments
Sharib Ahmad 23-May-12 6:38am    
I did the same but it didnt worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900