Solved

how to make month name dynamic on pivot table

Posted on 2014-04-03
2
571 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

920 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

16 Experts available now in Live!

Get 1:1 Help Now