Created
May 23, 2020 02:14
-
-
Save brito/a6d7727555824d781797c5453df3d25b to your computer and use it in GitHub Desktop.
Column histogram
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
/* | |
Column histogram | |
eg */ select * from _meta ( | |
'{app_public}', | |
'{data_report_session,data_raw_session,data_report}'), | |
_histogram(_schema,_relname,_column,_type);/* | |
*/ | |
create function _histogram(_schema name,_relname name,_column name,_type text) | |
returns setof json as $function$ begin | |
return query execute format(case | |
when _type ~ 'int\d|float|numeric' then $$ | |
select array_to_json(array_agg(row_to_json(b))) as _buckets | |
from ( | |
select | |
width_bucket(%3$I,min,max,num_buckets) bucket, | |
count(*) freq, | |
trunc(count(*)::numeric/"N",3) p, | |
floor(100*count(*)::numeric/"N") "%%", | |
trunc(min(%3$I)::numeric,3) low, | |
trunc(max(%3$I)::numeric,3) high | |
from %1$I.%2$I, | |
(select count(*) "N", min(%3$I), max(%3$I) from %1$I.%2$I) s, | |
(select (case when count(*) >= 15 then 1+3.3*log(count(*)) | |
else (max(%3$I)-min(%3$I))/(2* ||/count(*) * ( | |
percentile_disc(.75) within group (order by %3$I) | |
- percentile_disc(.25) within group (order by %3$I) | |
)) end)::int as num_buckets from %1$I.%2$I) nb | |
group by bucket,"N" order by bucket | |
) b $$ | |
else $$ select null::json $$ | |
end,_schema,_relname,_column,_type); | |
end $function$ language plpgsql stable; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment