Solved

SQL Pivot Table Percent Column

Posted on 2013-10-24
2
1,143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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…

717 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