Pivot in MS SQL


How to make pivot in MS SQL?

In the table with cars model and model year we have the following data.


And I’d like to create a pivot with the names of car models as columns and grouped by car model year and see how many such cars we have from each year.

Like this:


To get this result at first we need to write query with the creation of columns (car models) separated by a comma.

Now we can create a query with pivot.

With that way we can create any pivot with any columns.

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *