It seems like there is no native function for that purpose in Presto SQL. Do you know any way to efficiently aggregate a group and return its median?
1 Answer
approx_percentile()
should be a reasonable approach. Assuming a table like mytable(id, val)
, that you want to aggregate by id
:
select id, approx_percentile(val, 0.5) median_val
from mytable
group by id
-
2Note that this is an approximate solution. Upcoming Presto release will provide more accuracy in general case, follow github.com/prestosql/presto/pull/5158 for details. Sep 24, 2020 at 11:21