Use google bigquery to build histogram graph

前端 未结 7 1099
再見小時候
再見小時候 2020-12-17 22:11

How can write a query that makes histogram graph rendering easier?

For example, we have 100 million people with ages, we want to draw the histogram/buckets for age 0

7条回答
  •  执笔经年
    2020-12-17 22:46

    Using a cross join to get your min and max values (not that expensive on a single tuple) you can get a normalized bucket list of any given bucket count:

    select
      min(data.VAL) as min,
      max(data.VAL) as max,
      count(data.VAL) as num,
      integer((data.VAL-value.min)/(value.max-value.min)*8) as group
    from [table] data
    CROSS JOIN (SELECT MAX(VAL) as max, MIN(VAL) as min, from [table]) value
    GROUP BY group
    ORDER BY group 
    

    in this example we're getting 8 buckets (pretty self explanatory) plus one for null VAL

提交回复
热议问题