Skip to content

Instantly share code, notes, and snippets.

@GreatPotato
Last active January 4, 2016 09:09
Show Gist options
  • Select an option

  • Save GreatPotato/8600579 to your computer and use it in GitHub Desktop.

Select an option

Save GreatPotato/8600579 to your computer and use it in GitHub Desktop.
Gets number of sales per category in LemonStand (paid orders only and supports products with multiple categories)
SELECT shop_categories.name, count(shop_order_items.id)*shop_order_items.quantity as 'total_sales'
FROM shop_order_items
LEFT JOIN shop_orders
ON shop_order_items.shop_order_id = shop_orders.id
LEFT JOIN shop_products
ON shop_order_items.shop_product_id = shop_products.id
LEFT JOIN shop_products_categories
ON shop_products.id = shop_products_categories.shop_product_id
LEFT JOIN shop_categories
ON shop_products_categories.shop_category_id = shop_categories.id
WHERE shop_orders.payment_processed IS NOT NULL
GROUP BY shop_products_categories.shop_category_id
ORDER BY total_sales DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment