weighted_mean

This Release
weighted_mean 1.0.1
Date
Status
Stable
Other Releases
Abstract
A weighted mean aggregate function.
Description
This library contains a single PostgreSQL extension, an aggregate function called weighted_mean.
Released By
rdunklau
License
PostgreSQL
Resources
Special Files
Tags

Extensions

weighted_mean 1.0.0
A weighted mean aggregate function

README

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;