<?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/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</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>PIVOT Query Plans</title><link>http://blogs.msdn.com/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;
</description></item><item><title>PIVOT Query Plans</title><link>http://blogs.msdn.com/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;
</description></item><item><title>The UNPIVOT Operator</title><link>http://blogs.msdn.com/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;
</description></item><item><title>The UNPIVOT Operator</title><link>http://blogs.msdn.com/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;
</description></item><item><title>re: The PIVOT Operator</title><link>http://blogs.msdn.com/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;
</description></item><item><title>re: The PIVOT Operator</title><link>http://blogs.msdn.com/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>craigfr</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;
</description></item></channel></rss>