Last active
August 29, 2015 14:12
-
-
Save daveslutzkin/42a32e9b4f08a66961e0 to your computer and use it in GitHub Desktop.
Show a histogram in the Postgres console
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
with | |
num_buckets as (select 10), | |
suburbs as (select unnest(array['Fitzroy North','Northcote','Brunswick East','Clifton Hill'])), | |
rows as ( | |
select | |
price as field | |
from | |
results inner join properties on (property_id=properties.id) | |
where | |
suburb in (select * from suburbs) and property_type='house' and price > 0 and (result like '%sold%' or result like '%sale%') | |
), | |
summary_stats as ( | |
select | |
min(field) as min, max(field) as max, (max(field) - min(field))::float / (select * from num_buckets) as bucket_size | |
from | |
rows | |
), | |
bucketed_rows as ( | |
select | |
width_bucket(field, summary_stats.min, summary_stats.max, (select * from num_buckets)) as bucket, field | |
from | |
summary_stats, rows | |
), | |
histogram as ( | |
select | |
bucket, count(true) as freq | |
from | |
bucketed_rows | |
group by bucket | |
), | |
bucket_ranges as ( | |
select | |
bucket, numrange((min + (bucket-1) * bucket_size)::int, (min + bucket * bucket_size)::int) as range | |
from | |
summary_stats, generate_series(1, (select * from num_buckets)) as bucket | |
) | |
select | |
bucket, range, freq, repeat('*', (freq::float / max(freq) over() * 30)::int) as bar | |
from | |
bucket_ranges left join histogram using (bucket) | |
order by bucket ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment