How to build “Star Rating” report in BigQuery (or sparklines, or color gradients)

后端 未结 5 1166
忘了有多久
忘了有多久 2021-02-03 14:44

Suppose I have the followng sample input:

WITH Ratings AS (
    (SELECT \'A\' name, 2 score) UNION ALL
    (SELECT \'B\' name, 0 score) UNION ALL
    (SELECT \'C         


        
5条回答
  •  再見小時候
    2021-02-03 15:27

    Fitting vertical bar chart into single character is challenging because there are only 8 different heights we could use. But horizontal bar charts don't have this limitation, we can scale horizontal chart by arbitrary length. Example below uses 30, and it shows number of births per day of week as horizontal bar chart. Data is based on public dataset:

    create temp function hbar(value int64, max int64) as (
      repeat('█', cast(30 * value / max as int64))
    );
    select 
      ['sunday', 'monday', 'tuesday', 'wednesday',
       'thursday', 'friday', 'saturday'][ordinal(wday)] wday, bar from (
    select wday, hbar(count(*), max(count(*)) over()) bar
    from `bigquery-public-data.samples.natality`
    where wday is not null
    group by 1
    order by 1 asc)
    

    Results in

    wday      bar
    ---------------------------------------------
    sunday    ███████████████████
    monday    ███████████████████████████
    tuesday   ██████████████████████████████
    wednesday ██████████████████████████████
    thursday  █████████████████████████████
    friday    █████████████████████████████
    saturday  █████████████████████
    

提交回复
热议问题