Solved

SQL Pivot Table Percent Column

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

I have a large data set and a SSIS package. How can I load this file in multi threading?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

738 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