Horizontal to vertical array?

  • Hello,

    Not sure if there is any basic info you need to know...I'm running SQL Server 2008 Standard.

    I need to create a query that has data from multiple columns (Columns 1-6 with coresponding Date started and Date Completed data) displayed vertically, but also has the column name the preeceeding column to identify it...along with other data (Record number, status).

    Record Number, Status, Column Name, Date Started, DateCompleted

    1, Open, Column 1, 1/1/2012, 2/1/2012,

    2, Hold, Column 2, 1/3/2012, 3/1/2012,

    1, Open, Column 3, 2/5/2012, 4/6/2012,

    3, Closed, Column 4, 5/10/2012, 7/25/2012,

    2, Hold, Column 5, 3/9/2012, 4/1/2012,

    1, open, Column 6, 10/10/2012, 12/12/2012,

    Please reply with any question you may have. Sorry, I did the best I could to describe what I need, the formating wasn't helping either. I went with CSV, if it helps.

    As Always, any help would be greatly apprechated.

    Thank you,

    David92595

  • You need a little help getting help. 🙂

    Below is a consumable version of your source data:

    ;WITH a ([Record Number], [Status], [Column Name], [Date Started], DateCompleted) AS (

    SELECT 1, 'Open', 'Column 1', '1/1/2012', '2/1/2012'

    UNION ALL SELECT 2, 'Hold', 'Column 2', '1/3/2012', '3/1/2012'

    UNION ALL SELECT 1, 'Open', 'Column 3', '2/5/2012', '4/6/2012'

    UNION ALL SELECT 3, 'Closed', 'Column 4', '5/10/2012', '7/25/2012'

    UNION ALL SELECT 2, 'Hold', 'Column 5', '3/9/2012', '4/1/2012'

    UNION ALL SELECT 1, 'open', 'Column 6', '10/10/2012', '12/12/2012')

    SELECT *

    FROM a

    Now without trying to transform the source data, can you provide us a similar query that shows exactly how the expected results should look for this source data?


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

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

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