We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

create pivot table, with dynamic columns

Medium Priority
512 Views
Last Modified: 2006-11-18

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:
centerNumber
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.


select
   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
from
   (select Center_Num,Fund_Mth,sumExp from pri_pmpmNew_01_main ) t
PIVOT
(
   sum(sumExp)
   for Fund_Mth in ([200501],[200502],[200503],[200504],[200505],[200506],[200507],[200508],[200509],
   [200510],[200511],[200512])
) as pvt


Thanks really appreciate the help!
Comment
Watch Question

Author

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
begin
    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
End

     .
     .
     .

exec(@sql)
Scott PletcherSenior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Is the "yearMonth" an int or a (var)char?
Scott PletcherSenior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
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:


select  
   Center_num,
   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
   center_num

Author

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.

declare
   @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)
...
Senior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
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 :-) .

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.