I need to rotate the table a little by changing the measurements. I use Pivot , but the problem is that I do not understand how to select all the values ​​in the column for the selector as column names and for processing the pivot . Need instead:

 select [2001], [2002], [2003] from table pivot( MAX(Value) for date in ([2001], [2002], [2003]) )as pvt 

Something like:

 select (select date from table) from table pivot( MAX(value) For date in (select date from table) ) 

    1 answer 1

    PIVOT can only be done for a previously known data set. You cannot dynamically generate columns in it. If you want dynamic generation, write a stored procedure or function.

    • those. without using the pivot itself, rotate the table as needed? Or you can shove in the pivot a ready-made set of column names, collected in advance (but how to do it and in what form if it is real)? - Vincento Law
    • one
      You can make a stored procedure that first defines a set of columns (selects all years), then generates SQL and executes it by issuing a table with the necessary data. Well, for example, like here: hashcode.ru/questions/42644/… - minamoto