I have a table which I convert lines into columns with PIVOT. 'Chumbando' the column name works very well, however I will need to do this dynamically. Is it possible to use variables to define the name of the columns?
SELECT PRODUTO,
ISNULL([@MES3],0) AS MES3,
ISNULL([@MES2],0) AS MES2,
ISNULL([@MES1],0) AS MES1,
ISNULL([@MES0],0) AS MESATUAL
FROM #VENDASESTM
PIVOT (SUM(QTDVEND) FOR EMISSAO IN ([@MES3],[@MES2],[@MES1],[@MES0]) )P
ORDER BY PRODUTO
These 4 months are dynamic and informed in the Store Procedure call. SP creates the #VENDASESTM table correctly with the products, months chosen and quantities sold. Then I need to transform this with PIVOT as well dynamically.
As above, the result was 0 in all columns. I did the conference and there is sales, so something is wrong.