Solved

how to make month name dynamic on pivot table

Posted on 2014-04-03
2
622 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
[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 25

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

733 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