Skip to content

Instantly share code, notes, and snippets.

@tegansnyder
Created January 9, 2014 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tegansnyder/8336537 to your computer and use it in GitHub Desktop.
Save tegansnyder/8336537 to your computer and use it in GitHub Desktop.
Finding the category that has the most sales in Magento
SELECT cat_id, SUM(row_total) as total_sales_by_cat, cv.value FROM (
SELECT c2.entity_id as cat_id, si.row_total
FROM catalog_category_product c1
INNER JOIN catalog_category_entity_varchar c2 ON (c1.category_id = c2.entity_id)
INNER JOIN catalog_product_entity c3 ON (c1.product_id = c3.entity_id)
INNER JOIN sales_flat_order_item si ON (si.product_id = c3.entity_id)
WHERE c2.attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'name' AND entity_type_id = 3)
ORDER BY si.created_at DESC
) main
INNER JOIN catalog_category_entity_varchar cv ON (cv.entity_id = cat_id)
GROUP BY cat_id
ORDER BY total_sales_by_cat DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment