I don't have SQL Server on my box, but why won't the following answer return 360 on Postgres?
select exp(sum(log(val)))
from (values(4),(5),(3),(6)) as tbl(val)
returns 12.888075
I don't have SQL Server on my box, but why won't the following answer return 360 on Postgres?
select exp(sum(log(val)))
from (values(4),(5),(3),(6)) as tbl(val)
returns 12.888075
You have to use natural logarithm (ln function), not base-10 logarithm (log function):
select exp(sum(ln(val)))
from (values(4),(5),(3),(6)) as tbl(val)
exp
-----
360
(1 row)
But this is not a good way to multiply rows - it is slow and error prone due to rounding. You should declare an aggregate:
create function multiply(int,int) returns int as $$
select $1*$2;
$$ language sql immutable strict;
create aggregate multiply(int) (
sfunc=multiply,
stype=int,
initcond=1
);
select multiply(val)
from (values(4),(5),(3),(6)) as tbl(val)
multiply
----------
360
(1 row)