One of the things we love most about PostgreSQL is the ease with which one can define new aggregate functions with even a language as
succinct as SQL. Normally when we have needed a median function, we've just used the built-in median function in PL/R as we briefly demonstrated in
Language Architecture in PostgreSQL.
If all you demand is a simple median aggregate function ever then installing the whole R statistical environment so you can use PL/R is overkill and much less
In this article we will demonstrate how to create a Median function with nothing but the built-in PostgreSQL SQL language, array constructs,