Solved

SQL Pivot Table Percent Column

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now