Created
August 19, 2020 01:06
-
-
Save logworthy/5803085d57d486eeacae17124e5bd2af to your computer and use it in GitHub Desktop.
Using width_bucket function to bin in postgres
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 example as ( | |
select 399 as num, width_bucket(399, 400, 990, 59) as buk | |
union all | |
select 400 as num, width_bucket(400, 400, 990, 59) as buk | |
union all | |
select 401 as num, width_bucket(401, 400, 990, 59) as buk | |
union all | |
select 409 as num, width_bucket(409, 400, 990, 59) as buk | |
union all | |
select 410 as num, width_bucket(410, 400, 990, 59) as buk | |
union all | |
select 411 as num, width_bucket(411, 400, 990, 59) as buk | |
union all | |
select 989 as num, width_bucket(989, 400, 990, 59) as buk | |
union all | |
select 990 as num, width_bucket(990, 400, 990, 59) as buk | |
union all | |
select 991 as num, width_bucket(991, 400, 990, 59) as buk | |
union all | |
select 999 as num, width_bucket(999, 400, 990, 59) as buk | |
) | |
select | |
num, | |
case when buk = 0 then '[0-400)' | |
when buk >= 60 then '[990-999]' | |
else '[' || (400 + (10 * buk-1)) || '-' || (400 + (10 * buk-1)) || ')' | |
end | |
from | |
example; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment