Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Product Aggregate in PostgreSQL

I try to create an aggregate for product (*) in PostgreSQL. The field type of my row is "double precision"

So, I tried :

CREATE AGGREGATE nmul(numeric)
(
   sfunc = numeric_mul,
   stype = numeric
);

When I launch my query, the result :

ERROR:  function nmul(double precision) does not exist
LINE 4: CAST(nmul("cote") AS INT),

Thank you

like image 576
Thomas K Avatar asked Dec 02 '22 21:12

Thomas K


1 Answers

I found a solution from a very smart guy, who realized you can use logarithms to achieve this (credit goes to him):

select exp(sum(ln(x))) from generate_series(1,5) x;
 exp 
-----
 120
(1 row)
like image 161
clapas Avatar answered Dec 21 '22 14:12

clapas