[英]How to find for each of the top 3 returned value from one column, the 3 top values from another column?
I want to return for each of the 3 most common values in one column, the 3 most common values from a different column.我想为一列中的 3 个最常见值中的每一个返回另一列中的 3 个最常见值。
I've managed to return the 3 most common values from the first column:我设法从第一列返回了 3 个最常见的值:
SELECT vehicle_type_code1 as vehicle_type
FROM `bigquery-public-data.new_york.nypd_mv_collisions`
GROUP BY vehicle_type
ORDER BY count(vehicle_type_code1) desc
LIMIT 3
But now I want to find for each of them, the most common 'cross_street_name'.但现在我想为他们每个人找到最常见的“cross_street_name”。 I can do it manually and group by 'cross_street_name' where 'vehicle_type_code1' is equal to each of them, but I guess there is a better way.我可以手动完成并按 'cross_street_name' 分组,其中 'vehicle_type_code1' 等于它们中的每一个,但我想有更好的方法。
Consider below approach考虑以下方法
select vehicle_type,
string_agg(cross_street_name, ', ' order by cnt desc limit 5) as cross_streets
from (
select vehicle_type_code1 as vehicle_type, cross_street_name, count(*) as cnt
from `bigquery-public-data.new_york.nypd_mv_collisions`
where cross_street_name != ''
and vehicle_type_code1 != ''
group by vehicle_type, cross_street_name
)
group by vehicle_type
order by sum(cnt) desc
limit 5
with output output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.