Created
April 8, 2016 10:38
-
-
Save anonymous/7c7556a85ec357cd476947bbe37d32bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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