<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://vishnuprasad.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Vishnu Prasad blog : Pivot</title><link>http://vishnuprasad.com/archive/tags/Pivot/default.aspx</link><description>Tags: Pivot</description><dc:language>en</dc:language><generator>CommunityServer 2.1 (Build: 60804.900)</generator><item><title>PIVOT in SQL Server 2005</title><link>http://vishnuprasad.com/archive/2009/04/13/PIVOT-in-SQL-Server-2005.aspx</link><pubDate>Mon, 13 Apr 2009 00:26:00 GMT</pubDate><guid isPermaLink="false">bf6bce95-1e81-4ef7-965f-45bc55c1466c:56211</guid><dc:creator>Vishnu</dc:creator><slash:comments>0</slash:comments><comments>http://vishnuprasad.com/comments/56211.aspx</comments><wfw:commentRss>http://vishnuprasad.com/commentrss.aspx?PostID=56211</wfw:commentRss><wfw:comment>http://vishnuprasad.com/rsscomments.aspx?PostID=56211</wfw:comment><description>&lt;p&gt;We can use PIVOT relational operator to change a table-valued expression into another table. &lt;strong&gt;PIVOT &lt;/strong&gt;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. Source &lt;a href="http://msdn.microsoft.com/en-us/library/ms177410.aspx" target="_blank"&gt;msdn&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Here is an example I was working on.&lt;/p&gt;&lt;p&gt;I have a table called Student and with 3 columns Subject, Grade and Student_Id. I am looking for output which is aggregated along the Subject. Something shown below.&lt;/p&gt;&lt;p&gt;Subject&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; B&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; C&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; D &lt;br /&gt;----------------------------------- &lt;br /&gt;MATH&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 20&amp;nbsp;&amp;nbsp;&amp;nbsp; 10&amp;nbsp;&amp;nbsp;&amp;nbsp; 30&amp;nbsp;&amp;nbsp;&amp;nbsp; 50 &lt;br /&gt;PHY&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 20&amp;nbsp;&amp;nbsp;&amp;nbsp; 10&amp;nbsp;&amp;nbsp;&amp;nbsp; 50&amp;nbsp;&amp;nbsp;&amp;nbsp; 30 &lt;br /&gt;CHEM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 30&amp;nbsp;&amp;nbsp;&amp;nbsp; 20&amp;nbsp;&amp;nbsp;&amp;nbsp; 50&amp;nbsp;&amp;nbsp;&amp;nbsp; 10 &lt;br /&gt;BIO&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 30&amp;nbsp;&amp;nbsp;&amp;nbsp; 10&amp;nbsp;&amp;nbsp;&amp;nbsp; 20&amp;nbsp;&amp;nbsp;&amp;nbsp; 50&lt;/p&gt;&lt;p&gt;Its not easy to aggregate along the subject using the group by statement and the case statement. (Atleast I dont know of any easy way!!!) But using the Pivot operator makes this very simple.&lt;/p&gt;&lt;p&gt;&lt;img alt="SQL Suery" height="42" src="http://vishnuprasad.com/mypics/SQLQuery.png" style="width:350px;height:42px;" title="SQL Suery" width="350" /&gt;&lt;/p&gt;&lt;p&gt;Running the above query displays the information as shown below. &lt;/p&gt;&lt;p&gt;&lt;img alt="SQL Results" height="131" src="http://vishnuprasad.com/mypics/SQLResults.png" style="width:188px;height:131px;" title="SQL Results" width="188" /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;strong&gt;UNPIVOT &lt;/strong&gt;performs almost the reverse operation of PIVOT, by rotating columns into rows. &lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://vishnuprasad.com/archive/2009/04/13/PIVOT-in-SQL-Server-2005.aspx&amp;amp;;subject=PIVOT+in+SQL+Server+2005" target="_blank" title = "Post http://vishnuprasad.com/archive/2009/04/13/PIVOT-in-SQL-Server-2005.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://vishnuprasad.com/archive/2009/04/13/PIVOT-in-SQL-Server-2005.aspx&amp;amp;;title=PIVOT+in+SQL+Server+2005" target="_blank" title = "Post http://vishnuprasad.com/archive/2009/04/13/PIVOT-in-SQL-Server-2005.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://vishnuprasad.com/archive/2009/04/13/PIVOT-in-SQL-Server-2005.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://vishnuprasad.com/archive/2009/04/13/PIVOT-in-SQL-Server-2005.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://vishnuprasad.com/archive/2009/04/13/PIVOT-in-SQL-Server-2005.aspx&amp;amp;title=PIVOT+in+SQL+Server+2005" target="_blank" title = "Post http://vishnuprasad.com/archive/2009/04/13/PIVOT-in-SQL-Server-2005.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://vishnuprasad.com/archive/2009/04/13/PIVOT-in-SQL-Server-2005.aspx&amp;amp;;title=PIVOT+in+SQL+Server+2005" target="_blank" title = "Post http://vishnuprasad.com/archive/2009/04/13/PIVOT-in-SQL-Server-2005.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://vishnuprasad.com/archive/2009/04/13/PIVOT-in-SQL-Server-2005.aspx&amp;amp;;title=PIVOT+in+SQL+Server+2005&amp;amp;;top=1" target="_blank" title = "Post http://vishnuprasad.com/archive/2009/04/13/PIVOT-in-SQL-Server-2005.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://vishnuprasad.com/aggbug.aspx?PostID=56211" width="1" height="1"&gt;</description><category domain="http://vishnuprasad.com/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://vishnuprasad.com/archive/tags/Pivot/default.aspx">Pivot</category></item></channel></rss>