Skip to content

Instantly share code, notes, and snippets.

@gordonje
Created October 22, 2015 21:37
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 gordonje/eb16a1939da33c07c610 to your computer and use it in GitHub Desktop.
Save gordonje/eb16a1939da33c07c610 to your computer and use it in GitHub Desktop.
max lottery wins by a person at each store
-- what we want our distinct stores and the max number of wins for a person that played there
SELECT "AGENT NAME", max(count_person_wins), max(sum_person_winnings)
FROM (
-- gets us the number of wins per person for each store
SELECT
"AGENT NAME"
, "FIRSTNAME"
, "LASTNAME"
, "CITY"
, "STATE"
, COUNT(*) as count_person_wins
, SUM("PRIZE VALUE") as sum_person_winnings
FROM data
WHERE "AGENT NAME" IS NOT NULL
GROUP BY 1, 2, 3, 4, 5
ORDER BY "AGENT NAME", COUNT(*) DESC
) as sub_q
GROUP BY 1
ORDER BY max(count_person_wins) DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment