I have a table with the following structure
Name Data Month
A 100 2016-01
A 120 2016-03
A 120 2016-04
A 150 2016-05
B 100 2016-03
How can I make the final result become below between starting month and end month e.g. 2016-01 till 2016-05 using SQL if it is possible?
Name 2016-01 2016-02 2016-03 2016-04 2016-05
A 100 0 120 120 150
B 0 0 100 0 0
I did a number experiments but can't make this work. This is the code I have tried
select *
from
(
select id, isnull(price,0), [PERIOD]
from price_table
) a
pivot
(
max(price)
for [PERIOD] in ([2010-01-01],[2010-02-01])
) p
There is error "Msg 8155, Level 16, State 2, Line 10 No column name was specified for column 2 of 'a'. Msg 207, Level 16, State 1, Line 12 Invalid column name 'price'."
Please also note that the starting months and ending months are variable in my query, the months will be dynamically added more and is not really 2016-01 till 2016-05