Skip to content

Instantly share code, notes, and snippets.

@manuthu
Created April 29, 2020 09:36
Show Gist options
  • Save manuthu/96192099a82662186521e57bea9b2f96 to your computer and use it in GitHub Desktop.
Save manuthu/96192099a82662186521e57bea9b2f96 to your computer and use it in GitHub Desktop.
-- db=# \d order_payment
-- Table "public.order_payment"
-- Column | Type | Collation | Nullable | Default
-- -----------------+-----------------------------+-----------+----------+---------
-- id | uuid | | not null |
-- order_id | uuid | | |
-- amount_paid | numeric | | |
-- ...
select
bucket,
range,
freq,
repeat('==', (freq::float /sum(freq) over() *30 )::int ) as bar
from (
select
width_bucket(amount_paid::int, 1, 10000, 20) as bucket,
int4range(min(amount_paid::int), max(amount_paid::int), '[]') as range,
count(*) as freq
from order_payment
group by bucket
) as hist
order by freq desc
----
bucket | range | freq | bar
--------+---------------+------+--------------------------------------------------------------
1 | [1,501) | 47 | ============================================================
2 | [600,1001) | 23 | ==============================
3 | [1050,1501) | 19 | ========================
5 | [2050,2451) | 7 | ========
4 | [1600,2001) | 7 | ========
6 | [2600,3001) | 5 | ======
7 | [3168,3326) | 4 | ======
21 | [10000,64501) | 4 | ======
8 | [3600,4001) | 4 | ======
14 | [6530,6531) | 1 | ==
11 | [5400,5401) | 1 | ==
10 | [4810,4811) | 1 | ==
15 | [7020,7021) | 1 | ==
(13 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment