Pleasehelp me with my problem in sql.. column to row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arvie05
    New Member
    • Aug 2012
    • 1

    #1

    Pleasehelp me with my problem in sql.. column to row

    Hi everyone,

    Can someone please help me with my problem,

    I really need to solve this...

    I have a table Sales....

    Product | Brand | Sales | Price_1| Price_2 | Price_3
    ---------------------------------------------------------
    Milk | Alaska | 0 | 10 | 20 | 30
    Milk | Alaska | 1 | 80 | 70 | 90
    Milk | Alaska | 2 | 100 | 200 | 300



    I need to get the minimum and maximum sales of each Product...

    sales |
    ------
    0 |
    2 |


    after that I need to put their prices as before and after as a row...

    just like this
    salesfor 0 sales for 2
    bef Price | Aft Price
    -------------------------
    10 | 100 <-- Price 1
    20 | 200 <-- Price 2
    30 | 300 <-- Price 3

    I want to have an output like this

    Produt |Brand |MinSales |bef Price|MaxSales |Aft Price
    -----------------------------------------------------
    Milk |Alaska |0 | 10 |2 |100
    Milk |Alaska |0 | 20 |2 |200
    Milk |Alaska |0 | 30 |2 |300





    Please help me with this problem




    Thnk you so much in advance for anyhelp that you can do...
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Use a min and max aggregate query joined to the table to return the rows you need. Then use a union to normalize the data. Then finally join the result to itself on the min row to the max row.

    Comment

    Working...