Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate median in postgres [duplicate]

I'm now stuck on calculation median on postgres because there is no function about calculation median in postgres. Following is my query and I want to get median value of that query. Please help me how to do it.

SELECT count, conversion, company_id FROM (SELECT count(ap.id), 
(count(ap.id)/cast(NULLIF(SUM(j.views), 0) as float) * 100) AS conversion, 
j.company_id FROM applications ap RIGHT JOIN jobs j ON ap.job_id = j.id GROUP BY j.company_id order by conversion) with_avg
like image 807
PPShein Avatar asked Jun 30 '17 06:06

PPShein


People also ask

How do you find the median of a big query?

To compute the median, we will use the PERCENTILE_CONT(order_value, 0.5) function. The percentile function will go over each row of our datasets and return the median value (this is why we use the 0.5 parameters, meaning 50% of the values are above or below this point) of the order_value column.

How do I calculate the median?

To find the median: Arrange the data points from smallest to largest. If the number of data points is odd, the median is the middle data point in the list. If the number of data points is even, the median is the average of the two middle data points in the list.

How does mysql calculate median?

SELECT CEIL(COUNT(*)/2) FROM data; Then take the middle value in a sorted subquery: SELECT max(val) FROM (SELECT val FROM data ORDER BY val limit @middlevalue) x; I tested this with a 5x10e6 dataset of random numbers and it will find the median in under 10 seconds.


1 Answers

There is a function for calculating the median. The median is the 50% percentile, you could use percentile_disc, I think: https://www.postgresql.org/docs/9.6/static/functions-aggregate.html

like image 76
Carsten Tolkmit Avatar answered Sep 30 '22 17:09

Carsten Tolkmit