Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2008
    Posts
    2

    Unanswered: Help with writing a query

    I have an OrderDetails table with a order_id field that has multiple rows of the order_id field each with a different item and quantity for the item ordered. So say, order_id = 1 and there are two items for that order. So in the OrderDetails table it has two rows for that order_id, one for each item, along with a quantity of the items. I'm not sure how I can add up the quantity of items ordered for each order_id. Everything I can think of with group by's etc. won't work. Is there anyone who can help?

    Thanks
    Braggo

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    this is pretty basic stuff
    Code:
    SELECT order_id
         , COUNT(*) AS number_of_items
         , SUM(quantity) AS total_quantity
      FROM orders
    GROUP
        BY order_id
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Nov 2008
    Posts
    2
    Yeah I see where I was getting messed up, thanks for your help.

Posting Permissions

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