Created
May 23, 2020 02:13
-
-
Save brito/9265c019e770bb43853dea280d60318a to your computer and use it in GitHub Desktop.
Column interquartile range and box-and-whisker plot
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 interquartile range and box-and-whisker plot | |
eg */ select * from _meta ( | |
'{app_public}', | |
'{data_report_session,data_raw_session,data_report}'), | |
_iqt(_schema,_relname,_column,_type);/* | |
*/ | |
create function _iqt(_schema name, _relname name, _column name, _type text) | |
returns table (_quartiles json, _whiskers text) as $function$ | |
select xmltable.* from (select query_to_xml(format(case | |
when _type ~ 'int\d|float|numeric' then $$ | |
select | |
array_to_json(array_agg(row_to_json(r))) as _quartiles, | |
repeat(' ',ceil(low/high*30)::int) | |
|| repeat('-',ceil((q1-low)/high*30)::int) | |
|| repeat('■', ceil((median-q1)/high*30)::int) | |
|| '|' | |
|| repeat('■', ceil((q3-median)/high*30)::int) | |
|| repeat('-', ceil((high-q3)/high*30)::int) as _whiskers | |
from ( | |
select | |
min(%3$I) low, | |
percentile_cont(.25) within group (order by %3$I) q1, | |
percentile_cont(.5) within group (order by %3$I) median, | |
percentile_cont(.75) within group (order by %3$I) q3, | |
max(%3$I) high | |
from %1$I.%2$I) r | |
group by low,q1,median,q3,high $$ | |
else 'select null' | |
end,_schema,_relname,_column),false,false,'')) q, | |
xmltable('//table/row' passing query_to_xml columns _quartiles json, _whiskers text) | |
$function$ language sql stable; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment