Quote Originally Posted by numinix View Post
I'm working on a simple module that loads the best sellers from a given category from the previous x months. Here is the SQL query:

PHP Code:
SELECT  DISTINCT (p2c.products_id), SUMop.products_quantity )  AS products_orderedpd.products_namep.products_image
FROM products_to_categories  p2c
LEFT  JOIN orders_products op ON 
op.products_id p2c.products_id 
LEFT  JOIN orders o ON o.orders_id =  op.orders_id 
LEFT  JOIN products_description  pd ON pd.products_id p2c.products_id 
LEFT  JOIN products p ON p.products_id =  p2c.products_id 
WHERE p.products_status =1
AND pd.language_id =   '1'
AND (o.date_purchased >=  '2010-03-12'
AND o.date_purchased <=  '2010-05-13')
AND 
p2c.categories_id
IN 
86101112479135  
GROUP  BY p2c.products_id
ORDER  BY products_ordered DESC 
products_name ASC
The issue I am having is that if a product is linked, it doubles up the products_ordered. Any fresh eyes see the mistake?
If anyone was wondering how to do this, the following query worked:

PHP Code:
SELECT  DISTINCT (p2c.products_id), SUMop.products_quantity )  AS products_orderedpd.products_namep.products_image
FROM products_to_categories  p2c
LEFT  JOIN orders_products op ON 
op.products_id p2c.products_id 
LEFT  JOIN orders o ON o.orders_id =  op.orders_id 
LEFT  JOIN products_description  pd ON pd.products_id p2c.products_id 
LEFT  JOIN products p ON p.products_id =  p2c.products_id 
WHERE p.products_status =1
AND pd.language_id =   '1'
AND (o.date_purchased >=  '2010-03-12'
AND o.date_purchased <=  '2010-05-13')
AND 
p2c.categories_id
IN 
86101112479135  
GROUP  BY p2c.products_idp2c.categories_id
ORDER  BY products_ordered DESC 
products_name ASC