Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compute a median in PrestoSQL?

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?

like image 933
Roméo Després Avatar asked Jan 24 '23 17:01

Roméo Després


1 Answers

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
like image 118
GMB Avatar answered Jan 29 '23 23:01

GMB