简体   繁体   English

如何从一列中找到前 3 个返回值中的每一个,从另一列中查找前 3 个返回值?

[英]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.

相关问题 在 bigquery 中按每个组查找前 100 个值 - find top 100 values by each group in bigquery 如何添加一个列,该列根据另一列的条件对其他列的值进行字符串加法 - How to add a column which does a string addition of values from other column based on condition from another column 从另一个表中为每个 ID 写入数组中的每一列获取值 - Fetch values from another table for each column written in an array for each ID 如何将一个表中的列与 BigQuery 中另一个表中的数组进行比较? - How to compare column in one table with array from another table in BigQuery? 根据条件从一列中提取数据并存储在另一列中 - Extracting data from one column and storing in another based on a condition SQL - 根据其他列的值重命名一列 - SQL - Rename one column based on values from other columns 如何将一个表中的多行插入到另一个表的单行的结构列中? - How do I insert multiple rows from one table into a struct column of a single row of another table? CASE WHEN 错误 - 根据另一列的条件将值设置为列 - CASE WHEN error - setting value to column based on conditions from another column 如何将数据从一个表复制到另一个表中,该表在 GCP Bigquery 中有一个记录重复列 - How to copy data from one table into another table which has a record repeated column in GCP Bigquery 如何计算过去 X 周数据的一列百分比? - How to calculate percentage from one column for past X weeks of data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM