﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Ryan Clare  / PIVOT working incorrectly? / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 03:46:16 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: PIVOT working incorrectly?</title><link>http://www.sqlservercentral.com/Forums/Topic499276-1279-1.aspx</link><description>Nice and structured article....... :)</description><pubDate>Tue, 13 May 2008 22:45:34 GMT</pubDate><dc:creator>Anipaul</dc:creator></item><item><title>RE: PIVOT working incorrectly?</title><link>http://www.sqlservercentral.com/Forums/Topic499276-1279-1.aspx</link><description>Good documentation and examples of the problem, Ryan!</description><pubDate>Tue, 13 May 2008 16:58:48 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: PIVOT working incorrectly?</title><link>http://www.sqlservercentral.com/Forums/Topic499276-1279-1.aspx</link><description>[quote][b]Matt Marston (5/13/2008)[/b][hr]The PIVOT statement can be useful at times, but it is handy to know that anything that can be done with a PIVOT statement can be converted to an equivalent query without the PIVOT statement.[/quote]For those that don't know about Matt's fine example, it's called a "Cross Tab" query and examples can be found in Books Online.</description><pubDate>Tue, 13 May 2008 16:58:03 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: PIVOT working incorrectly?</title><link>http://www.sqlservercentral.com/Forums/Topic499276-1279-1.aspx</link><description>[quote][b]benjamin.nelson06 (5/13/2008)[/b][hr]Very interesting as I have been working on doing a similar thing now for a few days.  However I needed a Grand Total Column and a Grand Total Row for reporting purposes.  I also did not know what the PivotValues would be therefore this all had to be done with dynamic SQL.  I too created a temporary table in order to store the data before I pivoted the data.  Here is how I did it (Please excuse the smiley's as they automatically were added.  where you see a smily it's just a close paren.)--Get the Pivot Columns by querying the distinct [Month]	&amp; Year values from the queryDeclare @PivotValues as VARCHAR(8000)Declare @PivotSum as VARCHAR(8000)Declare @PivotTotal as VARCHAR(8000)Declare @PivotTypes as VARCHAR(8000)Declare @MonthYear as VARCHAR(25)Declare @EDATEMM as INTDeclare @Year as INT--Loop over the @YearMonth CURSOR table to build our PivotField ValuesDECLARE YearMonth_cursor CURSOR FOR SELECT DISTINCT dbo.MonthMst.[Month] + '-' + CAST(Year([EDATE]) as varchar(4)), EDATEMM, Year([EDATE])   FROM dbo.MonthMst INNER JOIN 					@MasterSubset AS MS ON dbo.MonthMst.cLngMonthCode = MS.EDATEMM  Order By Year([EDATE]), EDATEMM    OPEN YearMonth_cursorFETCH NEXT FROM YearMonth_cursor INTO @MonthYear, @EDATEMM, @YearWHILE @@FETCH_STATUS = 0BEGIN    --Build the PivotValues to be used in forming the Pivot Statement    If COALESCE(@PivotValues, '') = ''      SET @PivotValues = '[' + @MonthYear + ']'    ELSE      SET @PivotValues = @PivotValues + ',[' + @MonthYear + ']'    --Build the PivotTotal for use in forming the Grand Total Column    If COALESCE(@PivotTotal, '') = ''      SET @PivotTotal = 'COALESCE([' + @MonthYear + '],0)'    ELSE      SET @PivotTotal = @PivotTotal + ' + COALESCE([' + @MonthYear + '],0)'    --Build the PivotSum for use in forming the Grand Total Row    If COALESCE(@PivotSum, '') = ''      SET @PivotSum = 'SUM(COALESCE([' + @MonthYear + '],0))'    ELSE      SET @PivotSum = @PivotSum + ', SUM(COALESCE([' + @MonthYear + '],0))'    --Build the PivotTypes for use in declaring a Table Variable    If COALESCE(@PivotTypes, '') = ''      SET @PivotTypes = '[' + @MonthYear + '] INT NULL'    ELSE      SET @PivotTypes = @PivotTypes + ', [' + @MonthYear + '] INT NULL'    -- Get the next YearMonth.    FETCH NEXT FROM YearMonth_cursor     INTO @MonthYear, @EDATEMM, @YearEND--Add the Grand Total Column to the PivotValues--This keeps us from having to modify the Structure in order to add--The Grand Total columnset @PivotValues = @PivotValues + ',[Grand Total]'--We will need to SUM the Grand Total Column in order to build the last value Grand Total--Rowset @PivotSum = @PivotSum + ', SUM([Grand Total])'--Add the Grand Total column to our PivotTypes as this needs to be a column in our Table Variableset @PivotTypes = @PivotTypes + ',[Grand Total] INT NULL'CLOSE YearMonth_cursorDEALLOCATE YearMonth_cursorDECLARE @SQL as NVARCHAR(4000)--Stuff our Data into a Temporary table--to be used in the dynamic SQL belowSelect [Month] + '-' + CAST([Year] as varchar(4)) AS MonthYear, GroupCol1, GroupCol2, GroupCol3, AggregatColINTO #PivotDatafrom  (Select Month, Year, GroupCol1, GroupCol2, GroupCol3, AggregatCol From DataTable) as DT--Now here was the tricky part.  Since I didn't know what my pivot columns would be I needed--to build dynamic SQL however I couldn't figure out how to get the data to return properly.  So--I basically created a dynamic stored proc without actually creating a stored proc.  SET @SQL = 'DECLARE @PivotTable1 TABLE( GroupCol1 varchar(50) NOT NULL,                                         GroupCol2 varchar(20) NOT NULL,                                        GroupCol3 varchar(20) NOT NULL,                                         ' + @PivotTypes + ');      INSERT INTO @PivotTable1 (GroupCol1, GroupCol2, GroupCol3, ' + @PivotValues + ' )        Select GroupCol1, GroupCol2, GroupCol3, ' + @PivotValues + '         FROM        (Select GroupCol1, GroupCol2, GroupCol3, ' + @PivotValues + '        FROM        (          Select MonthYear,GroupCol1,GroupCol2,            GroupCol3, AggregateCol          from #PivotData          ) As Source        PIVOT        (          SUM(Source.AggregateCol)          FOR MONTHYEAR IN (' + @PivotValues + ')        ) As PivotTable) As PT;   Update @PivotTable1 Set [Grand Total] = ' + @PivotTotal +   '; Insert Into @PivotTable1 (GroupCol1, GroupCol2, GroupCol3, ' + @PivotValues + ')        Select ''Grand Total'', '''', '''', ' + @PivotSum + ' From @PivotTable1;  Select * From @PivotTable1;'Exec sp_executesql @sqlDrop Table #PivotDataAnd here are the results:GroupCol1            GroupCol2             GroupCol3            January-2007        February-2007      Grand Total-------------------------------------------------------------------------------------------------GroupValue1         Group2Value1        Group3Value1                    391                      356          747GroupValue2         Group2Value2        Group3Value1                    356                      419          775GroupValue3         Group2Value3        Group3Value1                    181                      181          362Grand Total                                                                          928                      956         1884[/quote]Post the table CREATE statements and some sample data according to the URL in my signature... you'll be surprised at how many will give you a working code example in return... ;)</description><pubDate>Tue, 13 May 2008 16:54:56 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: PIVOT working incorrectly?</title><link>http://www.sqlservercentral.com/Forums/Topic499276-1279-1.aspx</link><description>The PIVOT statement can be useful at times, but it is handy to know that anything that can be done with a PIVOT statement can be converted to an equivalent query without the PIVOT statement.Here is how: 1. All non-pivoted columns from the source query are put in a GROUP BY clause (e.g. GROUP BY salesPersonsName) 2. Create a case statement for each pivoted column value (e.g. Jan, Feb) that returns the column to be aggregated when the column to be pivoted matches the value. For example[code]CASE WHEN monthAbv = 'Jan' THEN numberOfSales END[/code] 3. Wrap each case statement in the desired aggregate function and add the column alias. Note that this also gives you flexibility to name the columns whatever you want. For example[code]SUM(CASE WHEN monthAbv = 'Jan' THEN numberOfSales END) AS [Jan][/code]For more details and more examples see the comments in the script below.[code]-- The initial PIVOT query:select salesPersonsName, [Jan], [Feb] from (select salesPersonsName, numberOfSales, monthAbv from #tmpPivotTest) as SourceTable --------^ salesPersonsName is the only non-pivoted column from the source;--------  so we GROUP BY salesPersonsNamePIVOT (sum(numberOfSales) for monthAbv in ([Jan],[Feb]) )as PivotTable-------^ sum is the Aggregate function ----^ Jan and Feb are the values to use in the case statements-- The equivalent CASE/GROUP BY query:SELECT	salesPersonsName,	SUM(CASE WHEN monthAbv = 'Jan' THEN numberOfSales END) AS Jan,	SUM(CASE WHEN monthAbv = 'Feb' THEN numberOfSales END) AS FebFROM #tmpPivotTestGROUP BY salesPersonsName-- Pivot table query trying to get saver points as well: select salesPersonsName, [Jan], [Feb], saverPoints from(select * from #tmpPivotTest) as SourceTable --------^ salesPersonsName and saverPoints are both non-pivoted columns returned by the source query;--------  so we GROUP BY salesPersonsName, saverPoints PIVOT (sum(numberOfSales) for monthAbv in ([Jan], [Feb]) )as PivotTable-------^ sum is the Aggregate function ----^ Jan and Feb are the values to use in the case statements-- The equivalent CASE/GROUP BY query:SELECT	salesPersonsName,	SUM(CASE WHEN monthAbv = 'Jan' THEN numberOfSales END) AS Jan,	SUM(CASE WHEN monthAbv = 'Feb' THEN numberOfSales END) AS Feb,	saverPointsFROM #tmpPivotTestGROUP BY salesPersonsName, saverPoints-- The desired results with the sum of saverPoints using CASE/GROUP BYSELECT	salesPersonsName,	SUM(CASE WHEN monthAbv = 'Jan' THEN numberOfSales END) AS Jan,	SUM(CASE WHEN monthAbv = 'Feb' THEN numberOfSales END) AS Feb,	SUM(saverPoints) AS saverPointsFROM #tmpPivotTestGROUP BY salesPersonsName-- Note that CASE/GROUP BY is more flexible.-- PIVOT only allows one source column to be aggregated.-- For example, to aggregate both sales and points by month:SELECT	salesPersonsName,	SUM(CASE WHEN monthAbv = 'Jan' THEN numberOfSales END) AS [Jan Sales],	SUM(CASE WHEN monthAbv = 'Jan' THEN saverPoints END) AS [Jan Points],	SUM(CASE WHEN monthAbv = 'Feb' THEN numberOfSales END) AS [Feb Sales],	SUM(CASE WHEN monthAbv = 'Feb' THEN saverPoints END) AS [Feb Points]FROM #tmpPivotTestGROUP BY salesPersonsName-- In order to do the equivalent of the above query you would need to join two PIVOT-- statements, which is less efficient because it queries the source table twice.;with SalesPivotResults as(	select salesPersonsName, [Jan] as [Jan Sales], [Feb] as [Feb Sales]	from (select salesPersonsName, numberOfSales, monthAbv from #tmpPivotTest)as SalesSourceTable	PIVOT (sum(numberOfSales) for monthAbv in ([Jan],[Feb]) )as SalesPivotTable), PointsPivotResults as(	select salesPersonsName, [Jan] as [Jan Points], [Feb] as [Feb Points]	from (select salesPersonsName, saverPoints, monthAbv from #tmpPivotTest)as PointsSourceTable	PIVOT (sum(saverPoints) for monthAbv in ([Jan],[Feb]) )as PointsPivotTable)select SalesPivotResults.salesPersonsName, [Jan Sales], [Jan Points], [Feb Sales], [Feb Points]from SalesPivotResults	INNER JOIN PointsPivotResults ON PointsPivotResults.salesPersonsName = SalesPivotResults.salesPersonsName[/code]</description><pubDate>Tue, 13 May 2008 10:18:54 GMT</pubDate><dc:creator>Matt Marston</dc:creator></item><item><title>RE: PIVOT working incorrectly?</title><link>http://www.sqlservercentral.com/Forums/Topic499276-1279-1.aspx</link><description>Interesting article, as usual, but that class="code" style trashes the page, making it unreadable, please correct it! thanks</description><pubDate>Tue, 13 May 2008 09:26:04 GMT</pubDate><dc:creator>mpadierna1</dc:creator></item><item><title>RE: PIVOT working incorrectly?</title><link>http://www.sqlservercentral.com/Forums/Topic499276-1279-1.aspx</link><description>The working of pivot depends upon the columns we specify in the select list. The pivot has one column on which aggregate function is applied (like sum(),count(), etc), the second column from which the data is rotated and pivot implicitly applies GROUP BY clause on rest of columns.For E.g :Select objid, attr1, attr2, Col_data1, Col_data2from (      Select num, objid, attr1, attr2, attr3 from Table_Name) as Raw_datapivot(       sum(num) for attr3 in (Col_data1, Col_data2) ) as pvtIn this case, the aggregate function sum() is applied on column "num". Col_data1 and Col_data2 are the row values which will form new columns in the output table. Now the pivot implicitly applies GROUP BY to all those columns in Select list which do not take part in PIVOT, in this case objid, attr1, attr2. So we need to be careful in selecting the columns. So use a derived query to select the required columns instead of using "Select * from table_name". Since it applies GROUP BY in this fashion we might end up applying GROUP BY to all those columns which we dont want resulting in wrong results as mentioned in this post .</description><pubDate>Tue, 13 May 2008 08:09:16 GMT</pubDate><dc:creator>samir-424465</dc:creator></item><item><title>RE: PIVOT working incorrectly?</title><link>http://www.sqlservercentral.com/Forums/Topic499276-1279-1.aspx</link><description>Very interesting as I have been working on doing a similar thing now for a few days.  However I needed a Grand Total Column and a Grand Total Row for reporting purposes.  I also did not know what the PivotValues would be therefore this all had to be done with dynamic SQL.  I too created a temporary table in order to store the data before I pivoted the data.  Here is how I did it (Please excuse the smiley's as they automatically were added.  where you see a smily it's just a close paren.)--Get the Pivot Columns by querying the distinct [Month]	&amp; Year values from the queryDeclare @PivotValues as VARCHAR(8000)Declare @PivotSum as VARCHAR(8000)Declare @PivotTotal as VARCHAR(8000)Declare @PivotTypes as VARCHAR(8000)Declare @MonthYear as VARCHAR(25)Declare @EDATEMM as INTDeclare @Year as INT--Loop over the @YearMonth CURSOR table to build our PivotField ValuesDECLARE YearMonth_cursor CURSOR FOR SELECT DISTINCT dbo.MonthMst.[Month] + '-' + CAST(Year([EDATE]) as varchar(4)), EDATEMM, Year([EDATE])   FROM dbo.MonthMst INNER JOIN 					@MasterSubset AS MS ON dbo.MonthMst.cLngMonthCode = MS.EDATEMM  Order By Year([EDATE]), EDATEMM    OPEN YearMonth_cursorFETCH NEXT FROM YearMonth_cursor INTO @MonthYear, @EDATEMM, @YearWHILE @@FETCH_STATUS = 0BEGIN    --Build the PivotValues to be used in forming the Pivot Statement    If COALESCE(@PivotValues, '') = ''      SET @PivotValues = '[' + @MonthYear + ']'    ELSE      SET @PivotValues = @PivotValues + ',[' + @MonthYear + ']'    --Build the PivotTotal for use in forming the Grand Total Column    If COALESCE(@PivotTotal, '') = ''      SET @PivotTotal = 'COALESCE([' + @MonthYear + '],0)'    ELSE      SET @PivotTotal = @PivotTotal + ' + COALESCE([' + @MonthYear + '],0)'    --Build the PivotSum for use in forming the Grand Total Row    If COALESCE(@PivotSum, '') = ''      SET @PivotSum = 'SUM(COALESCE([' + @MonthYear + '],0))'    ELSE      SET @PivotSum = @PivotSum + ', SUM(COALESCE([' + @MonthYear + '],0))'    --Build the PivotTypes for use in declaring a Table Variable    If COALESCE(@PivotTypes, '') = ''      SET @PivotTypes = '[' + @MonthYear + '] INT NULL'    ELSE      SET @PivotTypes = @PivotTypes + ', [' + @MonthYear + '] INT NULL'    -- Get the next YearMonth.    FETCH NEXT FROM YearMonth_cursor     INTO @MonthYear, @EDATEMM, @YearEND--Add the Grand Total Column to the PivotValues--This keeps us from having to modify the Structure in order to add--The Grand Total columnset @PivotValues = @PivotValues + ',[Grand Total]'--We will need to SUM the Grand Total Column in order to build the last value Grand Total--Rowset @PivotSum = @PivotSum + ', SUM([Grand Total])'--Add the Grand Total column to our PivotTypes as this needs to be a column in our Table Variableset @PivotTypes = @PivotTypes + ',[Grand Total] INT NULL'CLOSE YearMonth_cursorDEALLOCATE YearMonth_cursorDECLARE @SQL as NVARCHAR(4000)--Stuff our Data into a Temporary table--to be used in the dynamic SQL belowSelect [Month] + '-' + CAST([Year] as varchar(4)) AS MonthYear, GroupCol1, GroupCol2, GroupCol3, AggregatColINTO #PivotDatafrom  (Select Month, Year, GroupCol1, GroupCol2, GroupCol3, AggregatCol From DataTable) as DT--Now here was the tricky part.  Since I didn't know what my pivot columns would be I needed--to build dynamic SQL however I couldn't figure out how to get the data to return properly.  So--I basically created a dynamic stored proc without actually creating a stored proc.  SET @SQL = 'DECLARE @PivotTable1 TABLE( GroupCol1 varchar(50) NOT NULL,                                         GroupCol2 varchar(20) NOT NULL,                                        GroupCol3 varchar(20) NOT NULL,                                         ' + @PivotTypes + ');      INSERT INTO @PivotTable1 (GroupCol1, GroupCol2, GroupCol3, ' + @PivotValues + ' )        Select GroupCol1, GroupCol2, GroupCol3, ' + @PivotValues + '         FROM        (Select GroupCol1, GroupCol2, GroupCol3, ' + @PivotValues + '        FROM        (          Select MonthYear,GroupCol1,GroupCol2,            GroupCol3, AggregateCol          from #PivotData          ) As Source        PIVOT        (          SUM(Source.AggregateCol)          FOR MONTHYEAR IN (' + @PivotValues + ')        ) As PivotTable) As PT;   Update @PivotTable1 Set [Grand Total] = ' + @PivotTotal +   '; Insert Into @PivotTable1 (GroupCol1, GroupCol2, GroupCol3, ' + @PivotValues + ')        Select ''Grand Total'', '''', '''', ' + @PivotSum + ' From @PivotTable1;  Select * From @PivotTable1;'Exec sp_executesql @sqlDrop Table #PivotDataAnd here are the results:GroupCol1            GroupCol2             GroupCol3            January-2007        February-2007      Grand Total-------------------------------------------------------------------------------------------------GroupValue1         Group2Value1        Group3Value1                    391                      356          747GroupValue2         Group2Value2        Group3Value1                    356                      419          775GroupValue3         Group2Value3        Group3Value1                    181                      181          362Grand Total                                                                          928                      956         1884</description><pubDate>Tue, 13 May 2008 05:45:09 GMT</pubDate><dc:creator>benjamin.nelson06</dc:creator></item><item><title>RE: PIVOT working incorrectly?</title><link>http://www.sqlservercentral.com/Forums/Topic499276-1279-1.aspx</link><description>I am having difficulty deciphering the example given.  Would you mind doing another Pivot Table query using Northwind Traders?  That structure may prove easier to understand the syntax of this strategy.  Jamie</description><pubDate>Tue, 13 May 2008 05:19:12 GMT</pubDate><dc:creator>JamieBates</dc:creator></item><item><title>RE: PIVOT working incorrectly?</title><link>http://www.sqlservercentral.com/Forums/Topic499276-1279-1.aspx</link><description>Interesting, thanks for the tip; I'll have to watch out for that.  So it seems to result in a sum(distinct value) instead of sum(value), right?It does seem like pre-aggregating the data is the key, but I wouldn't use a temp table and an update, I'd just use a derived table or view, then aggregate the results as well:[code]select salesPersonsName	 , sum([Jan]) as [Jan]	 , sum([Feb]) as [Feb]	 , sum(SaverPoints) as SaverPointsfrom     ( select 	  salesPersonsName	, sum(numberOfSales) as NumberOfSales	, monthAbv	, sum(saverPoints) as saverPoints	from #tmpPivotTest	group by	  salesPersonsName	, numberOfSales	, monthAbv	) as SourceTable PIVOT (sum(numberOfSales) for monthAbv in ([Jan],[Feb]) )as PivotTablegroup by salesPersonsName[/code]</description><pubDate>Tue, 13 May 2008 02:11:44 GMT</pubDate><dc:creator>Calvin Lawson</dc:creator></item><item><title>PIVOT working incorrectly?</title><link>http://www.sqlservercentral.com/Forums/Topic499276-1279-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/articles/pivot/62808/"&gt;PIVOT working incorrectly?&lt;/A&gt;[/B]</description><pubDate>Mon, 12 May 2008 21:31:24 GMT</pubDate><dc:creator>Ryan Clare</dc:creator></item></channel></rss>