Solved

SQL Pivot Table Percent Column

Posted on 2013-10-24
2
1,073 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

770 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