A simple weighted mean aggregate function for postgresql
C Makefile
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
doc
sql
src
test
LICENSE
META.json
Makefile
README.md
weighted_mean.control

README.md

Weighted Mean extension for PostgreSQL

This extension provides a "weighted mean" aggregate function for postgreql.

The weighted mean is defined as a two columns aggregation function, one containing the measure to be averaged, the second containing the weight of each measure.

This extensions is implemented as a C library.

Installation

You can either install it with the pgxn client:

pgxn install weighted_mean

Or from source:

git clone git://github.com/Kozea/weighted_mean.git make && sudo make install

Then, create the extension in the specific database:

CREATE extension weighted_mean;

Usage

select weighted_mean(unitprice, quantity) from sales;

This is equivalent to:

select case when sum(quantity) = 0 then 0 else sum(unitprice * quantity) / sum(quantity) end from sales;