<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.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>The PIVOT Operator</title><link>http://blogs.msdn.com/b/craigfr/archive/2007/07/03/the-pivot-operator.aspx</link><description>In my next few posts, I'm going to look at how SQL Server implements the PIVOT and UNPIVOT operators. Let's begin with the PIVOT operator. The PIVOT operator takes a normalized table and transforms it into a new table where the columns of the new table</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: The PIVOT Operator</title><link>http://blogs.msdn.com/b/craigfr/archive/2007/07/03/the-pivot-operator.aspx#10231667</link><pubDate>Mon, 31 Oct 2011 14:15:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10231667</guid><dc:creator>Craig Freedman</dc:creator><description>&lt;p&gt;Yes, joins are possible in the source query which can be any legal SQL query.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10231667" width="1" height="1"&gt;</description></item><item><title>re: The PIVOT Operator</title><link>http://blogs.msdn.com/b/craigfr/archive/2007/07/03/the-pivot-operator.aspx#10231303</link><pubDate>Sat, 29 Oct 2011 11:24:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10231303</guid><dc:creator>karthick</dc:creator><description>&lt;p&gt;is it possible to use join statements in pivot operator ?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10231303" width="1" height="1"&gt;</description></item><item><title>re: The PIVOT Operator</title><link>http://blogs.msdn.com/b/craigfr/archive/2007/07/03/the-pivot-operator.aspx#10028703</link><pubDate>Tue, 22 Jun 2010 20:33:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10028703</guid><dc:creator>Craig Freedman</dc:creator><description>&lt;p&gt;Use ROLLUP or GROUPING SETS. &amp;nbsp;See &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/b/craigfr/archive/2007/09/21/aggregation-with-rollup.aspx"&gt;blogs.msdn.com/.../aggregation-with-rollup.aspx&lt;/a&gt; or &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/b/craigfr/archive/2007/10/11/grouping-sets-in-sql-server-2008.aspx"&gt;blogs.msdn.com/.../grouping-sets-in-sql-server-2008.aspx&lt;/a&gt;.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10028703" width="1" height="1"&gt;</description></item><item><title>re: The PIVOT Operator</title><link>http://blogs.msdn.com/b/craigfr/archive/2007/07/03/the-pivot-operator.aspx#10026946</link><pubDate>Fri, 18 Jun 2010 10:02:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10026946</guid><dc:creator>Roni</dc:creator><description>&lt;p&gt;it&amp;#39;s a great topic, &lt;/p&gt;
&lt;p&gt;hm..I want to make table like this using pivot, is that posible? thx b4&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;StudentName &amp;nbsp;| AssignmentName | &amp;nbsp;Grade&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;---------------------------------------&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;StudentA &amp;nbsp; &amp;nbsp; | Assignment 1 &amp;nbsp; | 100&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;StudentA &amp;nbsp; &amp;nbsp; | Assignment 2 &amp;nbsp; | 80&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;StudentA &amp;nbsp; &amp;nbsp; | Total &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| 180&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;StudentB &amp;nbsp; &amp;nbsp; | Assignment 1 &amp;nbsp; | 100&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;StudentB &amp;nbsp; &amp;nbsp; | Assignment 2 &amp;nbsp; | 80&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;StudentB &amp;nbsp; &amp;nbsp; | Assignment 3 &amp;nbsp; | 100&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;StudentB &amp;nbsp; &amp;nbsp; | Total &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| 280&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10026946" width="1" height="1"&gt;</description></item><item><title>re: The PIVOT Operator</title><link>http://blogs.msdn.com/b/craigfr/archive/2007/07/03/the-pivot-operator.aspx#4172998</link><pubDate>Wed, 01 Aug 2007 19:42:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4172998</guid><dc:creator>Craig Freedman</dc:creator><description>&lt;p&gt;For your first question, the best I can do with PIVOT is:&lt;/p&gt;
&lt;p&gt;SELECT EmpId, [2005], [2006], [2007], [0] AS [Total]&lt;/p&gt;
&lt;p&gt;FROM (&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;SELECT EmpId, Yr, Sales FROM Sales&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;UNION ALL&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;SELECT EmpId, 0, Sum(Sales) FROM Sales GROUP BY EmpId&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;) AS s&lt;/p&gt;
&lt;p&gt;PIVOT (SUM(Sales) FOR Yr IN ([2005], [2006], [2007], [0])) AS p&lt;/p&gt;
&lt;p&gt;We can get a better plan by rewriting it without PIVOT as:&lt;/p&gt;
&lt;p&gt;SELECT EmpId,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;SUM(CASE WHEN Yr = 2005 THEN Sales END) AS [2005],&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;SUM(CASE WHEN Yr = 2006 THEN Sales END) AS [2006],&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;SUM(CASE WHEN Yr = 2007 THEN Sales END) AS [2007],&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;SUM(Sales) AS [Total]&lt;/p&gt;
&lt;p&gt;FROM Sales&lt;/p&gt;
&lt;p&gt;GROUP BY EmpId&lt;/p&gt;
&lt;p&gt;For your second question, we can use GROUP BY WITH ROLLUP:&lt;/p&gt;
&lt;p&gt;SELECT EmpId, SUM([2005]), SUM([2006]), SUM([2007])&lt;/p&gt;
&lt;p&gt;FROM (SELECT EmpId, Yr, Sales FROM Sales) AS s&lt;/p&gt;
&lt;p&gt;PIVOT (SUM(Sales) FOR Yr IN ([2005], [2006], [2007])) AS p&lt;/p&gt;
&lt;p&gt;GROUP BY EmpId WITH ROLLUP&lt;/p&gt;
&lt;p&gt;Perhaps there is a better solution (to either question) that another reader might suggest?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4172998" width="1" height="1"&gt;</description></item><item><title>re: The PIVOT Operator</title><link>http://blogs.msdn.com/b/craigfr/archive/2007/07/03/the-pivot-operator.aspx#4104343</link><pubDate>Sat, 28 Jul 2007 21:08:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4104343</guid><dc:creator>mmurrell</dc:creator><description>&lt;p&gt;Great Post...&lt;/p&gt;
&lt;p&gt;Is there any way to add row-level aggregations to the end? &amp;nbsp;Following your example, I would like to add another column for Total Employee Sales that sums the years by employee. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;And the parallel question would be can I add a row with column-level aggregations? &amp;nbsp;For example, a new row with null employee id that sums the sales by year.&lt;/p&gt;
&lt;p&gt;Any help would be appreciated.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4104343" width="1" height="1"&gt;</description></item><item><title>The UNPIVOT Operator</title><link>http://blogs.msdn.com/b/craigfr/archive/2007/07/03/the-pivot-operator.aspx#3920952</link><pubDate>Tue, 17 Jul 2007 21:09:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3920952</guid><dc:creator>Noticias externas</dc:creator><description>&lt;p&gt;The UNPIVOT operator is the opposite of the PIVOT operator. As I explained in my earlier post , the PIVOT&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3920952" width="1" height="1"&gt;</description></item><item><title>The UNPIVOT Operator</title><link>http://blogs.msdn.com/b/craigfr/archive/2007/07/03/the-pivot-operator.aspx#3920522</link><pubDate>Tue, 17 Jul 2007 20:44:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3920522</guid><dc:creator>Craig Freedman's WebLog</dc:creator><description>&lt;p&gt;The UNPIVOT operator is the opposite of the PIVOT operator. As I explained in my earlier post , the PIVOT&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3920522" width="1" height="1"&gt;</description></item><item><title>PIVOT Query Plans</title><link>http://blogs.msdn.com/b/craigfr/archive/2007/07/03/the-pivot-operator.aspx#3785480</link><pubDate>Mon, 09 Jul 2007 22:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3785480</guid><dc:creator>Noticias externas</dc:creator><description>&lt;p&gt;In my last post , I gave an overview of the PIVOT operator. In this post, I&amp;amp;#39;m going to take a look&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3785480" width="1" height="1"&gt;</description></item><item><title>PIVOT Query Plans</title><link>http://blogs.msdn.com/b/craigfr/archive/2007/07/03/the-pivot-operator.aspx#3784412</link><pubDate>Mon, 09 Jul 2007 21:23:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3784412</guid><dc:creator>Craig Freedman's WebLog</dc:creator><description>&lt;p&gt;In my last post , I gave an overview of the PIVOT operator. In this post, I'm going to take a look at&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3784412" width="1" height="1"&gt;</description></item></channel></rss>