问题
The data I am working on looks like below-
A_ID B_ID count
123 abcd 1000
123 aaaa 2000
123 aaaa 3000
456 null 50
456 bbbb 6000
456 cccc 450
I want to be able to extract the B_id that has the highest count for a given A_id
The result should look like-
A_ID B_ID count
123 aaaa 3000
456 bbbb 6000
How to achieve this result?
回答1:
One option is to filter with a subquery:
select t.*
from mytable t
where t.count = (select max(t1.count) from mytable t1 where t1.a_id = t.a_id)
You can also use window functions:
select t.* except(rn)
from (
select t.*, rank() over(partition by a_id order by count desc) rn
from mytable t
) t
where rn = 1
回答2:
You can use aggregation in BigQuery:
select array_agg(t order by count desc limit 1)[ordinal(1)].*
from t
group by a_id;
回答3:
Below is for BigQuery Standard SQL
#standardSQL
SELECT AS VALUE ARRAY_AGG(t ORDER BY count DESC LIMIT 1)[OFFSET(0)]
FROM `project.dataset.table` t
GROUP BY a_id
if to apply to sample data from your question - the output is
Row a_id b_id count
1 123 aaaa 3000
2 456 bbbb 6000
来源:https://stackoverflow.com/questions/62460163/return-the-highest-count-record