6

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
PPShein
  • 10,951
  • 34
  • 129
  • 188
  • I think this is also a duplicate of this other question, which has the best answer in my opinion, if you're using Postgres 9.4+: https://stackoverflow.com/a/40317116/2441263 – lucas Jul 02 '20 at 15:06

1 Answers1

11

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