• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 499
  • Last Modified:

create pivot table, with dynamic columns

I want to create a stored procedure that takes a start month and returns a crosstab
pivot table.

My source table contains columnss:
centerNumber,YearMonth, and expenseAmount

The crosstab result should have 13 columns:
and yearMonth for 12 conscutive months.

The trick is that I want to be able to dynamically determine the start month based on the yearMonth value passed into the stored procedure. The new PIVOT TSQL statement makes this task easier, but I still don't know how to dynamically determine the month columns. Below I have a pivot table statement which always starts with Jan05, I'd like to be able to start with any month.

   Center_num,[200501] mth01,[200502] mth02, [200503] mth03,[200504] mth04,
   [200505] mth05, [200506] mth06,[200507] mth07,[200508] mth08,[200509] mth09,
   [200510] mth10,[200511] mth11,[200512] mth12
   (select Center_Num,Fund_Mth,sumExp from pri_pmpmNew_01_main ) t
   for Fund_Mth in ([200501],[200502],[200503],[200504],[200505],[200506],[200507],[200508],[200509],
) as pvt

Thanks really appreciate the help!
  • 3
  • 3
1 Solution
brokeMyLegBikingAuthor Commented:
I'm using SqlServer 2005, by the way.
You could use dynamic SQL and build the select something like this.  This is just a sample of the beginning of the code to get you started.

declare @sql varchar(1000)

set @sql = 'select Center_num,['

set @month = 1
while @month <= 12
    Set @sql = @sql + @year + right('0' + cast(@month as varchar(2)),2) + '] mth' + right('0' + cast(@month as varchar(2)),2) + ','
    Set @month = @month + 1


Scott PletcherSenior DBACommented:
Is the "yearMonth" an int or a (var)char?
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Scott PletcherSenior DBACommented:
To avoid dynamic SQL, I suggest creating 11 local variables, loading them with the values of the following 11 months, and then using code like this:

   SUM(CASE WHEN Fund_Mth = @StartMth THEN expenseAmount ELSE 0 END) AS Mth01,
   SUM(CASE WHEN Fund_Mth = @Mth02 THEN expenseAmount ELSE 0 END) AS Mth02,
   SUM(CASE WHEN Fund_Mth = @Mth12 THEN expenseAmount ELSE 0 END) AS Mth12
from ri_pmpmNew_01_main
where fund_mth between @StartMth and @Mth12
group by
brokeMyLegBikingAuthor Commented:
ok Scott,  I was able to populate my 12 month varables based on the startMonth, and they I guess I can just use the case statement like you suggested.

   @startMonth8 char(8),
   @mth01 char(6),@mth02 char(6),@mth03 char(6),@mth04 char(6),@mth05 char(6),@mth06 char(6),@mth07 char(6),@mth08 char(6),@mth09 char(6),@mth10 char(6),@mth11 char(6),@mth12 char(6)
   set @startMonth8 = @startMonth + '01'
   set @mth01 = convert(char(6),dateadd(mm, 0,@startMonth8),112)
   set @mth02 = convert(char(6),dateadd(mm, 1,@startMonth8),112)
Scott PletcherSenior DBACommented:
Yep.  Since the entries in the "PIVOT" command cannot be dynamic/variable, to me that's the best method to avoid dynamic SQL -- it's good to know that at least one classic 2000 technique is still useful in 2K5 :-) .
brokeMyLegBikingAuthor Commented:
Wow, I upgraded all my servers for the PIVOT table statement, and now I'm using the classic case statement technique.. ironic.

But there have been plenty of other benefits to upgrading to 2K5, so I don't regret it.

thx again!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now