Convert rows to columns in SQL - HELP!

  • I have excel data structured in the following way for each month:

    06/04/07 06/11/07 06/18/07 06/25/07

    Active21234534

    On HOLD1223

    Cancelled 1032

    Completed2251

    Discovery2444

    after I brought the data to SQL using transpose Excel feature I have (I did this to normalize data) and now in SQL table I have :

    Active On Hold Cancelled Completed Discovery

    06/04/07 21 1 1 2 2

    06/11/07 23 2 0 2 4

    06/18/07 45 2 3 3 4

    :

    :

    I would like to display data in its original way in GridView showing dates as columns. My challenge is that I have approx 300 dates so I would like to display only one month or first 4 dates in columns. I do not want to hardcode column names I would prefer to populate column names from sql rows, so I don't have to update weekly or monthly reports every week. Every week user enteres weekly numbers based on specific date (always increments of 7). I looked into SQL 2005 PIVOT feature but got stuck when I found out that column names need to be manually defined. I read about UNION ALL and CROSS tab quries, but never got it to work. Can you please shed some light on this. I am new to SQL this is really challenging problem for me.

  • Dynamic cross-tab...

    http://www.sqlservercentral.com/Forums/Topic242919-8-1.aspx

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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