Hi all. I need to write a function to display the best sellers (most quantity sold) for say the last 3 months.
There's only 2 tables involved.
ORDERS
orders_id
date_purchased
ORDERS_PRODUCTS
orders_products_id
orders_id
products_id
products_quantity
ORDERS and ORDERS_PRODUCTS are the tables and the others are their fields. Some fields were omitted.
This is the SQL statement I came up with. But I'm not sure if it's correct.
SELECT DISTINCT products_id, SUM( products_quantity ) AS num_ordered
FROM orders
INNER JOIN orders_products ON orders.orders_id = orders_products.orders_id
WHERE date_purchased >= '2006-12-21 11:10:06' AND date_purchased <= '2007-12-21 11:10:06'
GROUP BY products_id
ORDER BY num_ordered DESC
Please advise. Thanks.