I've seen this question, which is almost exactly what I want. But I can't get it working on Bigquery with standard SQL because BQ doesn't allow user defined variables.
Note - I have an arbitrary number of groups, so UNION
ing all of them as per the first answer in the linked question is not feasible.
The following is the simplest possible example, though any solution should be able to scale to however many n top results are needed:
Given a table like that below, with person, group, and age columns, how would you get the 2 oldest people in each group? (Ties within groups should not yield more results, but give the first 2 in any order)
+--------+-------+-----+
| Person | Group | Age |
+--------+-------+-----+
| Bob | 1 | 32 |
| Jill | 1 | 34 |
| Shawn | 1 | 42 |
| Jake | 2 | 29 |
| Paul | 2 | 36 |
| Laura | 2 | 39 |
+--------+-------+-----+
Desired result set:
+--------+-------+-----+
| Shawn | 1 | 42 |
| Jill | 1 | 34 |
| Laura | 2 | 39 |
| Paul | 2 | 36 |
+--------+-------+-----+
One answer in the linked question mentions using ROW_NUMBER
which does exist, but I can't work out how to restart the number for each group.