Skip to content

Instantly share code, notes, and snippets.

@ebuildy
Created May 26, 2017 15:24
Show Gist options
  • Save ebuildy/6a508d0e4e9d89d26ba199cd587f260d to your computer and use it in GitHub Desktop.
Save ebuildy/6a508d0e4e9d89d26ba199cd587f260d to your computer and use it in GitHub Desktop.
Get top N records of group by, with Facebook PrestoDB against Hive
SELECT * FROM (
SELECT *, RANK() OVER (PARTITION BY day, month ORDER BY hits DESC) AS row_number FROM (
SELECT country AS value, COUNT(*) AS hits, day, month
FROM hits_table
WHERE year = 2017
GROUP BY country, month, day
HAVING COUNT(*) > 1000
ORDER BY month, day, COUNT(*) DESC
)
)
WHERE row_number <= 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment