在Postgresql中计算中位数等各百分位数。
-- simple but slow method
-- percentile_disc will return a value from the input set closest to the percentile you request
-- percentile_cont will return an interpolated value between multiple values based on the distribution
select k, percentile_disc
within group(order by colume_of_table)
from table_name, generate_series(start_num, end_num, step) as k
group by k
-- fast method using window function
select max(buckets.colume_name), ntile as percentile
from(
select colume_of_table, ntile(percentile_num_you_want)
over (order by colume_of_table)
from table_name
) as buckets
group by 2
order by 2