I'm trying to do a fairly symbol pivot. This one runs as expected:
with cte as
(
select Symbol, TradeDate, 'Per' + cast(Period as CHAR(3)) Per, Value
from tblDailySMA
)
select * from cte
pivot
(Max(Value) for Per in (Per5,Per10,Per15,Per20,Per30,Per40,Per50,Per60,Per80,Per100,Per110,Per120,
Per150,Per200)) pvt
and this one gives the error Incorrect syntax near 5
(and highlights the the first value (5) after 'for Period in':
with cte as
(
select Symbol, TradeDate, Period, Value
from tblDailySMA
)
select * from cte
pivot
(Max(Value) for Period in (5,10,15,20,30,40,50,60,80,100,110,120,150,200)) pvt
I got the first one to run after much trial and error... is there a reason, such as perhaps integer values not being allowed as column names? Thanks..
You should always place square brackets around the column names []
:
with cte as
(
select Symbol, TradeDate, Period, Value
from tblDailySMA
)
select * from cte
pivot
(
Max(Value) for Period in ([5],[10],[15],[20],[30],[40],[50],[60],[80],[100],[110],[120],[150],[200])
) pvt
From MSDN:
The first character must be one of the following:
If it is anything else, then you will need to use the square brackets. []
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With