简体   繁体   中英

find top 100 values by each group in bigquery

Hi I have a data like this (Tablename: topcount)

    ID     Value       Count_total
    1      Sam         3008
    1      Sarah       5677
    3      Gil         3455
    2      Gil         4555
...

I want to find the count of top 100 values for each ID.

I have tried this so far -

 with top1000 as (select ID, ARRAY_AGG(STRUCT(count_total) ORDER BY count_total DESC LIMIT 1000) as top from topcount group by app_id) select app_id, sum(top.count_total) from top1000

How to I efficiently write this?

Consider below approach

select * from your_table
qualify row_number() over(partition by ID order by Count_total desc) <= 100   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM