Skip to content

Instantly share code, notes, and snippets.

@jessedhillon
Created June 9, 2018 16:59
Show Gist options
  • Save jessedhillon/9412aae6c6948b50d129349fdb151bfb to your computer and use it in GitHub Desktop.
Save jessedhillon/9412aae6c6948b50d129349fdb151bfb to your computer and use it in GitHub Desktop.
postgresql histogram
--- credit: https://tapoueh.org/blog/2014/02/postgresql-aggregates-and-histograms/
with drb_stats as (
select min(drb) as min,
max(drb) as max
from team_stats
),
histogram as (
select width_bucket(drb, min, max, 9) as bucket,
int4range(min(drb), max(drb), '[]') as range,
count(*) as freq
from team_stats, drb_stats
group by bucket
order by bucket
)
select bucket, range, freq,
repeat('■',
( freq::float
/ max(freq) over()
* 30
)::int
) as bar
from histogram;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment