Solved

SQL Pivot Table Percent Column

Posted on 2013-10-24
2
1,101 Views
Last Modified: 2013-10-24
I want to add a column to the end of my pivot table that shows Location % by Year

sample pivot table
I created a pivot table using the following code, but I cant figure out how to add the "Location %" Column.  

Location % = Location Total for Year 2012 / Total for the Year 2012 (i.e. 400/700) :

WITH BaseQuery AS(            
SELECT            
Location,InvoiceAmount as Amount,            
Year(InvoiceDate) as CalendarYear,            
Datename(month,InvoiceDate) as MonthName              
from Invoice            
)            
SELECT
Location, CalendarYear,ISNULL(January,0)as January, ISNULL(February,0)as February, ISNULL(March,0)as March, ISNULL(April,0)as April, ISNULL(May,0)as May,             
--add row totals            
ISNULL(January,0)+ ISNULL(February,0)+ ISNULL(March,0)+ ISNULL(April,0)+ ISNULL(May,0)as [Total]             
FROM BaseQuery            
            
PIVOT (SUM (Amount) FOR MonthName            
IN(January,February,March,April,May)) AS PVT

Original Data Table Sample

Location      InvoiceDate      InvoiceAmount
Chicago      1/1/2012              100
Chicago      5/12/2012      200
Chicago      2/13/2013      150
Chicago      4/12/2013      250
Detroit      1/1/2012              125
Detroit      5/12/2012      175
Detroit      2/13/2013      100
Detroit      4/12/2013      200
0
Comment
Question by:dmk550
2 Comments
 
LVL 3

Accepted Solution

by:
coreconcepts earned 500 total points
ID: 39598938
Hi dmk550,

     Sounds like you need a window function.  

Here's what I suggest: in your base query, use a Window Aggregate to get your total by (a) location and year and then select it in the outer query.

I used your code and added 3 columns to your inner select.  Ought to do the trick

WITH BaseQuery AS(            
SELECT            
Location,InvoiceAmount as Amount,            
Year(InvoiceDate) as CalendarYear,            
Datename(month,InvoiceDate) as MonthName, 
SUM(InvoiceAmount) OVER(PARTITION BY Location, Year(InvoiceDate)) as Total_by_Location_by_Year, 
SUM(InvoiceAmount) OVER(PARTITION BY Year(InvoiceDate)) as Total_by_Year, 
CAST(
		(SUM(InvoiceAmount) OVER(PARTITION BY Location, Year(InvoiceDate)) /
		SUM(InvoiceAmount) OVER(PARTITION BY Year(InvoiceDate)))
 AS NUMERIC(8, 2) ) as Location_Pct 
from Invoice            
)            
SELECT 
Location, CalendarYear,ISNULL(January,0)as January, ISNULL(February,0)as February, ISNULL(March,0)as March, ISNULL(April,0)as April, ISNULL(May,0)as May,             
--add row totals            
ISNULL(January,0)+ ISNULL(February,0)+ ISNULL(March,0)+ ISNULL(April,0)+ ISNULL(May,0)as [Total], 
Location_Pct             
FROM BaseQuery            
            
PIVOT (SUM (Amount) FOR MonthName            
IN(January,February,March,April,May)) AS PVT

Open in new window


If you are looking for more information on Window Functions here's an article you might find useful:  http://the-unofficial-dba.blogspot.com/2013/10/sql-server-2012-why-you-should-be-using.html
0
 

Author Closing Comment

by:dmk550
ID: 39599336
Thank you.  I really appreciate the solution and the link.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question