Skip to content

Instantly share code, notes, and snippets.

@swrobel
Last active December 21, 2015 07:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swrobel/6274229 to your computer and use it in GitHub Desktop.
Save swrobel/6274229 to your computer and use it in GitHub Desktop.
Number of orders by product in Spree 2.0+
SELECT p.id,
sum(quantity) AS num_sold
FROM spree_orders o
JOIN spree_line_items li ON o.ID = li.order_id
JOIN spree_variants v ON li.variant_id = v.id
JOIN spree_stock_items i ON v.id = i.variant_id
JOIN spree_products p ON v.product_id = p.id
WHERE o.state = 'complete'
GROUP BY p.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment