Skip to content

Instantly share code, notes, and snippets.

Created April 8, 2016 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7c7556a85ec357cd476947bbe37d32bc to your computer and use it in GitHub Desktop.
Save anonymous/7c7556a85ec357cd476947bbe37d32bc to your computer and use it in GitHub Desktop.
def self.best_selling
self.find_by_sql 'SELECT COALESCE(SUM(variants.num), 0) AS num, sellor_products.*
FROM sellor_variants
LEFT JOIN sellor_products ON sellor_products.id = sellor_variants.sellor_product_id
LEFT JOIN
(
SELECT sellor_variant_id AS id, COALESCE(SUM(quantity), 0) AS num
FROM sellor_order_items
GROUP BY sellor_variant_id
ORDER BY num DESC
) variants
ON sellor_variants.id = variants.id
GROUP BY sellor_products.id
ORDER BY num DESC
LIMIT 5'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment