Any set-based solutions to this problem?

  • Given a dataset like this:

    [font="Arial"]Name Quarter Amount1Date1 Amount2 Date2 Amount3 Date3

    n1 q1 100 1/3/2012

    n1 q1 200 2/2/2012 300 1/3/2012

    n1 q1 400 2/7/2012 500 8/1/2012

    n1 q1 600 7/1/2012[/font]

    is there any set-based solution to transform it in something like this:

    Name Quarter Amount1Date1 Amount2Date2Amount3 Date3

    n1q1100 1/3/20123001/3/20125008/1/2012

    n1q1200 2/2/2012400 2/7/20126007/1/2012

    It is easy to write a cursor-based solution, I am curious if set-based is possible in this case

  • You have been around here long enough to know that you should post ddl, sample data and desired output. Read the article at the first link in my signature. Post some details and you will get some help.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • virgilrucsandescu (7/17/2012)


    Given a dataset like this:

    [font="Arial"]Name Quarter Amount1Date1 Amount2 Date2 Amount3 Date3

    n1 q1 100 1/3/2012

    n1 q1 200 2/2/2012 300 1/3/2012

    n1 q1 400 2/7/2012 500 8/1/2012

    n1 q1 600 7/1/2012[/font]

    is there any set-based solution to transform it in something like this:

    Name Quarter Amount1Date1 Amount2Date2Amount3 Date3

    n1q1100 1/3/20123001/3/20125008/1/2012

    n1q1200 2/2/2012400 2/7/20126007/1/2012

    It is easy to write a cursor-based solution, I am curious if set-based is possible in this case

    It looks like you tried to do a PIVOT and didn't quite make it. I recommmend you post the code that put the data into this condition and fix that.

    --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 3 posts - 1 through 2 (of 2 total)

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