Skip to content

Instantly share code, notes, and snippets.

@gmgent
Created March 8, 2011 19:24
Show Gist options
  • Select an option

  • Save gmgent/860832 to your computer and use it in GitHub Desktop.

Select an option

Save gmgent/860832 to your computer and use it in GitHub Desktop.
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