Skip to content
Advertisement

How to create a pivot tablefor this problem:

This is my original table:

Original table

And I would like it to be as:

Here is my code, but I get a syntax error near FOR.

Advertisement

Answer

Use ROW_NUMBER here:

This assumes that your original source table would always have 7 arrival days per city. If not, then we might have to use a calendar table to bring in the missing data. Also, I am avoiding the PIVOT operator, because often the above approach performs better (and I also find it much easier to read).

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement