weighted_stats

This Release
weighted_stats 1.0.0
Date
Status
Stable
Abstract
Weighted stats aggregate functions.
Description
This library contains a single PostgreSQL extension, aggregate functions which include weighting.
Released By
davidfetter
License
PostgreSQL
Resources
Special Files
Tags

Extensions

weighted_stats 1.0.0
Weighted aggregate functions

README

Weighted Stats extension for PostgreSQL

This extension provides weighted statistical aggregate functions for PostgreSQL. It is based on the work of Ronan Dunklau and Peter Eisentraut in the weighted_mean package. Thanks as usual to Andrew Gierth, who showed how to do these things efficiently.

The weighted stats are defined as two-column aggregation functions. The first column contains the measure to be used. The second contains the weight of each measure.

This extension is implemented as a C library.

Installation

You can either install it with the pgxn client:

pgxn install weighted_stats

Or from source:

git clone git://github.com/davidfetter/weighted_stats.git make && sudo make install

Then, create the extension in the specific database:

CREATE extension weighted_stats;

Usage

select weighted_stats(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;