Results 1 to 2 of 2

Thread: Query Help

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    50

    Query Help

    How would I get the following table:


    FRUIT SHOP1 SHOP2 SHOP3
    RedApple 66
    GrnApple 74 444 5
    Pear 21 12
    Peach 321
    Orange 321 1

    Into this.....

    FRUIT SHOP QTY
    RedApple Shop3 66
    GrnApple Shop1 74
    GrnApple Shop2 444
    GrnApple Shop3 5
    ...

    Thanks,

    Adam.

  2. #2
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    You could do something like this:
    Code:
    SELECT FRUIT, 'Shop1' AS SHOP, SHOP1 AS QTY
        FROM FruitTable
        WHERE SHOP1 IS NOT NULL
    UNION 
    SELECT FRUIT, 'Shop2', SHOP2
        FROM FruitTable
        WHERE SHOP2 IS NOT NULL
    UNION 
    SELECT FRUIT, 'Shop3', SHOP3
        FROM FruitTable
        WHERE SHOP3 IS NOT NULL
    ORDER BY 1, 2
    If you wanted to put this data into a new table, you would make the new table first, then you could run this INSERT statement:
    Code:
    INSERT INTO NewTable (FRUIT, SHOP, QTY)
    SELECT FRUIT, 'Shop1' AS SHOP, SHOP1 AS QTY
        FROM FruitTable
        WHERE SHOP1 IS NOT NULL
    UNION 
    SELECT FRUIT, 'Shop2', SHOP2
        FROM FruitTable
        WHERE SHOP2 IS NOT NULL
    UNION 
    SELECT FRUIT, 'Shop3', SHOP3
        FROM FruitTable
        WHERE SHOP3 IS NOT NULL
    Note: If your existing "SHOP1/SHOP2/SHOP3" fields have zero instead of NULL, change the WHERE clauses in my example to:
    WHERE SHOPx > 0
    "It's cold gin time again ..."

    Check out my website here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width