cr_histogram.sql

This page documents the preview version (v2.21). Preview includes features under active development and is for development and testing only. For production, use the stable version (v2024.1). To learn more, see Versioning.

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$;