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!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.