Let's say that I want to efficiently compute something like a weighted
standard deviation (the actual formula I have in mind is slightly more
complicated). The kind of SQL statement I want to have work is
something like
SELECT weighted_stdev( t.val, t.weight ) FROM target_vals_tbl t
WHERE t.val > 0;
I thought I'd like to write a C function (or more properly a set of two
C functions) into the server side to handle this, and then declare it
as an aggregate using CREATE AGGREGATE. However, aggregate functions
appear to want just a single argument, so I feel like either I am on
the wrong track, or I have run into a limitation of postgresql.
What should I be doing here? Is there a way to do this with array
functions instead?
- Brian K. Boonstra