Question about Pivots in SQL Server 2008

  • 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

  • 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.

    _____________________________________________________
    Microsoft Certified Master: SQL Server 2008
    XDetails Addin - for SQL Developers
    blog.sqlxdetails.com - Transaction log myths

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply