March 13, 2012 at 12:31 pm
I need to do a pivot in SQL for a client who asked that the column names be a combination of two fields (month_year). I have the data pivoting however I have no idea how to do the comination of the month and year to show as asked.
I thought to do a SSIS package where the data is pulled and in a script task, a new field is created as month_year with the sales for that month_year showing under it per sales person and then inserting into a table so that the pivot can then be done with the new field.
Is my approach wrong? is there an easier way to do this?
please note that this is the first time i am doing pivots in SQL
Thanks in advance for the help
March 13, 2012 at 4:51 pm
Since you don't know exact number of columns in a result, you need dynamic pivot.
Something like this:
http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx
but use QUOTENAME() function.
If you need working example, post a script that creates data environment.
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply