Solved

how to make month name dynamic on pivot table

Posted on 2014-04-03
2
553 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
Comment Utility
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
Comment Utility
great solution. thank you very much!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

743 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

17 Experts available now in Live!

Get 1:1 Help Now