0
votes

I'm new to big query. I need top 3 scores for each group

| Name  | Group | Score |
| A     | 1     | 100   |
| B     | 2     | 80    |
| C     | 3     | 101   |
| D     | 1     | 53    |
| X     | 2     | 8     |
| Y     | 3     | 61    |
| Z     | 1     | 97    |
| W     | 2     | 20    |
1

1 Answers

0
votes

Consider below

select * except(pos)
from (
  select *, row_number() over(partition by `group` order by score desc) pos 
  from `project.dataset.table`
)
where pos <= 3     

Another option (more BigQuery'ish)

select arr.*
from (
  select array_agg(t order by score desc limit 3) arr
  from `project.dataset.table` t
  group by `group`
) a, a.arr