Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

an aggregation function for PostgreSQL

tree: d00f3126da

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 sql
Octocat-spinner-32 src
Octocat-spinner-32 LICENSE
Octocat-spinner-32 META.json
Octocat-spinner-32 Makefile
Octocat-spinner-32 README
Octocat-spinner-32 quantile.control
README
Quantile aggregates
===================
This extension provides three simple aggregate functions to compute
quantiles (http://en.wikipedia.org/wiki/Quantile). There are three
aggregate functions available.


1) median(p_value numeric)
--------------------------
Computes 2-quantile, i.e. the 'middle' value. For example this

   SELECT median(i) FROM generate_series(1,1000) s(i);

should return 500.


2) quantile(p_value numeric, p_quantile float)
----------------------------------------------
Computes arbitrary quantile of the values - the quantile has to be
between 0 and 1. For example this should return 500 just like the
previous example

   SELECT quantile(i, 0.5) FROM generate_series(1,1000) s(i);

but you can choose arbitrary quantile.


3) quantile(p_value numeric, p_quantiles float[])
-------------------------------------------------
If you need multiple quantiles at the same time, you can use this
function instead of the one described above. This version allows
you to pass an array of quantiles and returns an array of values.

So if you need all three quartiles, you may do this

   SELECT quantile(i, ARRAY[0.25, 0.5, 0.75])
     FROM generate_series(1,1000) s(i);

and it should return ARRAY[250, 500, 750]. Compared to calling
the simple quantile function like this

   SELECT quantile(i, 0.25), quantile(i, 0.5), quantile(i, 0.75)
     FROM generate_series(1,1000) s(i);

the advantage is that the internal array is built only once (and
not for each expression separately). If you're working with large
data sets, this may be a significant advantage.


Installation
------------
Installing this is very simple - if you're on 9.1 you can install
it like any other extension, i.e.

  $ make install
  $ psql dbname -c "CREATE EXTENSION quantile"

and if you're on an older version, you have to run the SQL script
manually

  $ psql dbname < `pg_config --sharedir`/contrib/quantile--1.0.sql

That's all.


License
-------
This software is distributed under the terms of GPL v3 license. See
LICENSE or http://www.gnu.org/copyleft/gpl.html for more details.
Something went wrong with that request. Please try again.