Created
June 9, 2018 16:59
-
-
Save jessedhillon/9412aae6c6948b50d129349fdb151bfb to your computer and use it in GitHub Desktop.
postgresql histogram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- 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