If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Help with writing a query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-27-08, 19:37
Braggo Braggo is offline
Registered User
 
Join Date: Nov 2008
Posts: 2
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
Reply With Quote
  #2 (permalink)  
Old 11-27-08, 19:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-27-08, 20:06
Braggo Braggo is offline
Registered User
 
Join Date: Nov 2008
Posts: 2
Yeah I see where I was getting messed up, thanks for your help.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On