Forum Moderators: open

Message Too Old, No Replies

MYSL Query Needed

         

artie2004

5:52 pm on Mar 11, 2006 (gmt 0)

10+ Year Member



hello, i'm in the need of an SQL query with the following tables:

1. customers: list of customers, indexed by an id, fields: usual personal stuff

2. orders: list of all orders made, indexed by a order_id,
fields: key to customer who made the order

i want to do a query on the table orders that gives me a ranking list of the customer who ordered the most and the total number that the customer ordered.

Thanks.

FalseDawn

1:47 am on Mar 12, 2006 (gmt 0)

10+ Year Member



SELECT C.customer_id, C.customer_name, COUNT (O.order_id) AS OrderCount, SUM(O.quantity) AS OrderQuantity FROM customer_table C INNER JOIN order_table O ON C.order_id=O.order_id GROUP BY C.customer_id,C.customer_name ORDER BY SUM(O.quantity) DESC

Not clear whether by "most" you mean number of items, or value - if value, replace SUM(O.quantity) with SUM (O.value) as appropriate.

artie2004

5:13 am on Mar 12, 2006 (gmt 0)

10+ Year Member



That's awsome. Thank you.