Using PIVOT in Microsoft SQL

In this post I'm going to show you how to use the PIVOT relational operator to change a table-valued expression into another table, as it follows.

Notice that timetable, Room, Days, pvt, Monday, Tuesday etc. are contextual elements that you (should) replace with your own values (see your table's name, collumns etc.)!

First, let me show you how my table normally looks like:

SELECT * FROM timetable

Table without PIVOT However, this is what I need my table to look like: Table after using PIVOT In order to do this, we'll use the following query:

SELECT * FROM timetable
			FOR timetable.[Days] IN ([Monday], [Tuesday], [Wednesday], [Thursday], [Friday])

		) as pvt;

So as you can see, PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output, and performs aggregations where they are required on any remaining column values that are wanted in the final output: Table after using PIVOT I hope you'll find this post useful and easy to approach!