cr_histogram.sql

Save this script as cr_histogram.sql.

-- "scale_factor" controls the histogram height.
-- Use 10 for 10,000 rows, 100 for 100,000 rows.

create or replace function histogram(
  no_of_bukets in int,
  scale_factor in numeric)
  returns SETOF text
  language sql
as $body$
  with
    bucket_assignments as (
      select bucket(dp_score, 0::double precision, 100::double precision, no_of_bukets) as bucket
      from t4)
  select
    to_char(count(*), '9999')||' | '||
    rpad('=', (count(*)/scale_factor)::int, '=')
  from bucket_assignments
  group by bucket
  order by bucket;
$body$;