?
Solved

create pivot table, with dynamic columns

Posted on 2006-03-21
7
Medium Priority
?
495 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!
0
Comment
Question by:brokeMyLegBiking
  • 3
  • 3
7 Comments
 
LVL 4

Author Comment

by:brokeMyLegBiking
ID: 16252108
I'm using SqlServer 2005, by the way.
0
 
LVL 15

Expert Comment

by:DonKronos
ID: 16252398
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)
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 16252436
Is the "yearMonth" an int or a (var)char?
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 16252479
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
0
 
LVL 4

Author Comment

by:brokeMyLegBiking
ID: 16252959
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)
...
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 16257920
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 :-) .
0
 
LVL 4

Author Comment

by:brokeMyLegBiking
ID: 16259320
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!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

839 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