Rows to Columns in SQL Server

  • I've a data in the following format....

    VehicleID CarName VendorName Quantity Price

    1 Honda Smith 20 25000

    1 Honda Ross 30 20000

    1 Honda Nick 40 10000

    1 Nissan Steve 25 24000

    1 Nissan Bruce 15 18000

    1 Nissan Sean 20 40000

    2 Honda Kate 30 90000

    2 Honda Ricky 40 20000

    2 Honda Shaun 10 15000

    I'd like to convert the above data into following format....

    VehicleID CarName Vendor1 Quantity1 Price1 Vendor2 Quantity2 Rate2 Vendor3 Quantity3

    1 Honda Smith 20 25000 Ross 30 20000 Nick 10000

    1 Nissan Steve 25 24000 Bruce 15 18000 Sean 40000

    2 Honda Kate 30 90000 Ricky 40 20000 Shaun 15000

    Pls Note: 1. One VehicleID can have any number of CarName.

    2. There can be any number of VehicleID.

    3. But One CarName can have maximum upto only 3 Vendors.

    How can I achieve this???

    Kindly Help..!!!!

    Thanks in advance:-):-)

  • Double post. No posts here, please. Please refer to the other post at the following URL:

    http://www.sqlservercentral.com/Forums/Topic1045849-391-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 1 (of 1 total)

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