Last active
August 18, 2020 15:42
-
-
Save MatMoore/0ebc5c0a2cdc9adc7e9be93e1e1d5d0e to your computer and use it in GitHub Desktop.
Creating a histogram in postgreSQL
This file contains hidden or 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 course_buckets as ( | |
select | |
course_slug, | |
extract(days from open_for_enrolment_at - proposal_submitted_at) as days, | |
width_bucket(extract(days from open_for_enrolment_at - proposal_submitted_at), 0, 200,20) as bucket | |
from course_creation_milestones | |
where open_for_enrolment_at > current_timestamp - interval '3 months' | |
), | |
bucket_totals as ( | |
select | |
count(*) as total, | |
bucket | |
from course_buckets | |
group by bucket | |
) | |
select | |
all_the_buckets.bucket, | |
case | |
when all_the_buckets.bucket <= 20 | |
then (all_the_buckets.bucket-1) * 10 || '-' || (all_the_buckets.bucket * 10) | |
else '200+' | |
end as bucket_range, | |
COALESCE(bucket_totals.total, 0) as "Number of courses" | |
from | |
generate_series(1,21) as all_the_buckets(bucket) | |
left outer join bucket_totals on (all_the_buckets.bucket = bucket_totals.bucket) | |
order by all_the_buckets.bucket |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment