Tuesday, November 23, 2010

Pivot In SQL Server 2008

PIVOT is a new feature included in SQL Server 2008 which can be used to rotate rows into columns and columns into rows adn create cross tab query.

This is one very interesting thing in SQL Server 2008. If we want to switch the rows and columns in a table, we can use Pivot.

Let us see this with an example..



Now the way I want to see this is a little different.



If you observe the above result set, I am interchanging the rows and columns in the original result set. This can be achieved by using PIVOT.

Below query will do this for me..


The FROM clause create the values that you want ot see in rows of the newly created columns. we used the for operator to list the values that we wanted to pivot in the 'Element' column.

No comments:

Post a Comment