Solved

how to make month name dynamic on pivot table

Posted on 2014-04-03
2
604 Views
Last Modified: 2014-04-08
Hello I have a stored procedure that at the end of it, produces a pivot output by month_quantity

The 12 months are static and I would like to learn if it is possible to, using the data, and code I have, to make the column names dynamic instead that always returning twelve.

I will use this code to later feed a SSRS report, that is if the column month names can be created dynamically.

Any information and/ or examples would be really appreciated.


Thank you very much.

p.s.

I attached some data and the code that produces the prepivot table and the output pivoted data set
pre-pivot-and-pivoted-output.txt
pivot-data-sample.xlsx
0
Comment
Question by:metropia
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39976683
To make the pivot column list dynamic you will need to use a dynamic SQL. In SQL Server you can assign your SQL statement into a nvarchar variable and then execute it using EXEC statement. Also, I recommend to stick with [Jan], [Feb], [Mar], etc instead of [Jan] AS [Jan_Qty] , [Feb] AS [Feb_Qty], etc. This way it will be easier to work with. Try this:
DECLARE @sql nvarchar(MAX)
DECLARE @cols nvarchar(200)
SET @cols = '[Apr], [May], [Jun], [Jul]'
SET @sql = 'SELECT  [TypeGroup], [Type], [Location] , ' + @cols +
'FROM  (SELECT [TypeGroup] ,[Type] ,[FiscalMonthName] ,[Location] ,[RemainingDailyQuantity] FROM  @Prepivot ) ' + 
'AS SourceTable PIVOT (SUM([RemainingDailyQuantity]) FOR [FiscalMonthName] IN (' +
@cols + ') ) AS PivotTable;'
EXEC @sql

SET @sql = 'SELECT  [TypeGroup] ,[Type] ,[Location] ,' + @cols + ', ISNULL(' +
REPLACE(@cols, ',', ', 0) + ISNULL(') + ', 0) AS [TotalQty] FROM  @OutTable;'
EXEC @sql

Open in new window

0
 

Author Closing Comment

by:metropia
ID: 39986598
great solution. thank you very much!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

860 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