Search Postgresql Archives

example of aggregate function for product

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I needed to write a product aggregate function, and just happened to
find this example in the nodes to the 8.0 manual:

Ansis <ansis_no_ AT inbox.lv>13 Jan 2006 16:41:05
An aggregate multiplication function, an analog of "sum" (the same
should be defined also for other numeric types):

CREATE OR REPLACE FUNCTION mul2(FLOAT,FLOAT)
RETURNS FLOAT AS '
DECLARE
a ALIAS FOR $1;
b ALIAS FOR $2;
BEGIN
RETURN a*b;
END;
' LANGUAGE plpgsql;

CREATE AGGREGATE mul (
    sfunc = mul2,
    basetype = FLOAT,
    stype = FLOAT,
    initcond = '1'
);

but that example looks pretty different than the ones found in the 8.3
manual (avg for instance):

CREATE AGGREGATE avg (float8)
(
    sfunc = float8_accum,
    stype = float8[],
    finalfunc = float8_avg,
    initcond = '{0,0}'
);

and float8_accum is defined like this (in the postgres function list).

CREATE OR REPLACE FUNCTION float8_accum(double precision[], double precision)
  RETURNS double precision[] AS
'float8_accum'
  LANGUAGE 'internal' IMMUTABLE STRICT
  COST 1;
ALTER FUNCTION float8_accum(double precision[], double precision)
OWNER TO postgres;
COMMENT ON FUNCTION float8_accum(double precision[], double precision)
IS 'aggregate transition function';


Are there any experts out there who have defined a product or
cumulative product function that abides best practices who would be
willing to share?

Thanks,
Whit

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux