Use google bigquery to build histogram graph

前端 未结 7 1096
再見小時候
再見小時候 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:32

    Write a subquery like this:

    (SELECT '1' AS agegroup, count(*) FROM people WHERE AGE <= 10 AND AGE >= 0)
    

    Then you can do something like this:

    SELECT * FROM
    (SELECT '1' AS agegroup, count(*) FROM people WHERE AGE <= 10 AND AGE >= 0),
    (SELECT '2' AS agegroup, count(*) FROM people WHERE AGE <= 20 AND AGE >= 10),
    (SELECT '3' AS agegroup, count(*) FROM people WHERE AGE <= 120 AND AGE >= 20)
    

    Result will be like:

    Row agegroup count 
    1   1       somenumber
    2   2       somenumber
    3   3       another number
    

    I hope this helps you. Of course in the age group you can write anything like: '0 to 10'

提交回复
热议问题