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 > MySQL work with SUM()

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-24-11, 14:47
DaKlonker DaKlonker is offline
Registered User
 
Join Date: Nov 2011
Posts: 5
MySQL work with SUM()

I have a new question based on this two tables:
1. IN_Order(price, order_amount, order_date, BENR(FK), BANR(FK)
2. Product (Product_name, product_price, BANR(PK)

The question i will answer is: Which product sales best and how much?
For answer this question I need to use 2 rows; Product_name and order_amount.
I think the best way to solve the question is with sum() function and function for max().

IŽd tried this code:
Select product_name, sum(order_amount) from Product p, IN_Order i WHERE order_amount = (select max(order_amount from in_order) AND i.banr=p.banr;

Results: Products sold most at one time. This is not the right answar.
What should I do to answer my question?
Reply With Quote
  #2 (permalink)  
Old 11-24-11, 16:22
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by DaKlonker View Post
I think the best way to solve the question is with sum() function and function for max().
yes, you need SUM, but not MAX

this sure sounds like a homework assignment, the classic "best total sales" scenario

it requires GROUP BY

Quote:
IŽd tried this code:
and surely got a syntax error
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-25-11, 04:22
DaKlonker DaKlonker is offline
Registered User
 
Join Date: Nov 2011
Posts: 5
Thanks

IŽd tried this code with SUM() and Group by():
Select product_name, sum(order_amount) from Product p, IN_Order i WHERE i.banr=p.banr group by product_name;

With this code a get a list of all product with each order amount. I just want to get the product with the highest order amount
Reply With Quote
  #4 (permalink)  
Old 11-25-11, 07:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
add an ORDER BY clause with the LIMIT option
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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