【问题标题】:I get the same value percentile for all rows我为所有行得到相同的百分位数
【发布时间】:2021-10-11 09:09:38
【问题描述】:

对于每个行政区,每个十字路口受伤人数的第 90 个百分位数是多少?

我有 borough 列,其中包含受伤人数的列,我将其汇总为一列。我需要找到受伤人数的 90%。它只给了我一个价值。我需要为每一行获取不同的值(或者我错了吗?)

select distinct borough,count(num_of_injured) as count_all, PERCENTILE_CONT(num_of_injured, 0.9 RESPECT NULLS) OVER() AS percentile90

from`bigquery-public-data.new_york.nypd_mv_collisions` c cross join
  unnest(array[number_of_persons_injured,number_of_pedestrians_injured,number_of_motorist_killed,number_of_cyclist_injured]
    )num_of_injured

where borough!='' 

group by borough,num_of_injured

order by count_all desc

limit 10;

table

感谢您的帮助!

【问题讨论】:

    标签: sql group-by google-bigquery subquery percentile


    【解决方案1】:

    如果您查看每个受伤人数按行政区划分的计数,那么超过 90% 是0

    select  borough, num_of_injured, count(*),
            count(*) / sum(count(*)) over (partition by borough)
    from`bigquery-public-data.new_york.nypd_mv_collisions` c cross join
        unnest(array[number_of_persons_injured,number_of_pedestrians_injured,number_of_motorist_killed,number_of_cyclist_injured]
              ) num_of_injured 
    group by 1, 2
    order by 1, 2;
    

    因此,第 90 个百分位是 0

    【讨论】:

      猜你喜欢
      • 2018-11-08
      • 2020-05-13
      • 2019-02-02
      • 2014-09-06
      • 1970-01-01
      • 2020-08-08
      • 2021-01-14
      • 1970-01-01
      • 2021-05-16
      相关资源
      最近更新 更多