Skip to content

weighted_mean

Weighted average aggregate function for PostgreSQL numeric values.

weighted_mean · Weighted average aggregate function for PostgreSQL numeric values.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
100041weighted_meanweighted_mean1.0.1FUNCPostgreSQLC

PostgreSQL · 10, 11, 12, 13, 14, 15, 16, 17, 18

Usage

Sources:

weighted_mean provides a two-argument aggregate over numeric values and weights. It computes the sum of value times weight divided by the sum of weights, returning zero when no rows are processed or the total weight is zero.

CREATE EXTENSION weighted_mean;
SELECT weighted_mean(unitprice, quantity)
FROM sales
WHERE unitprice IS NOT NULL AND quantity IS NOT NULL;

The transition function is not declared strict yet dereferences both inputs, so filter NULL values explicitly; malformed NULL calls may crash a backend. Negative weights and a zero total can also make the result misleading. The repository is archived and the aggregate lacks modern parallel-state support; regression-test exact numeric behavior and prefer an explicit maintained SQL expression when practical.

Table reader