Top N Per Group in BigQuery
EDIT: After I posted this initially, I got some great feedback, so I wrote a follow-up post here. In this post, we are going to explore a strategy for collecting the Top N results per Group over a mixed dataset, all in a single query. I stumbled onto this solution the other day, mostly driven by the fear that I was re-scanning my BigQuery data too often. At the time, the only way I knew how to look at a Top 10 list of a subset of the data was to add a WHERE clause limiting the whole data set to a single group and combine with ORDER BY and LIMIT clauses. For each group, I would just modify the WHERE clause, rescan all the data, and get new results. I thought there had to be an easier way to get the same ordered subset for any particular group in the data, all at once. It turns out, there is a much more efficient way to solve this problem. ...