Created
March 8, 2011 19:24
-
-
Save gmgent/860832 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| SELECT sum(total * 1), r.merchant | |
| FROM summaries s | |
| JOIN retailers r | |
| ON s.retailer_id = r.id | |
| JOIN ( | |
| SELECT sum(total) top_5_total, r.merchant AS top_5_merchant | |
| FROM summaries s | |
| JOIN retailers r | |
| ON s.retailer_id = r.id | |
| WHERE item_date >= '2007-01-01' | |
| AND item_date <= '2009-12-31' | |
| AND CASE 0 | |
| WHEN 0 THEN 1=1 | |
| ELSE account_id IN (SELECT id FROM accounts WHERE partner_id = 0) END | |
| GROUP BY merchant | |
| ORDER BY sum(total) DESC | |
| LIMIT 5 ) top_5 | |
| ON r.merchant = top_5.top_5_merchant | |
| WHERE item_date >= '2007-01-01' | |
| AND item_date <= '2009-12-31' | |
| AND CASE 0 | |
| WHEN 0 THEN 1=1 | |
| ELSE account_id IN (SELECT id FROM accounts WHERE partner_id = 0) END | |
| GROUP BY merchant | |
| ORDER BY merchant, total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment