Lets say I have table with 1 column like this:
Col A
1
2
3
4
If I SUM
it, then I will get this:
Col A
10
My question is: how do I multiply Col A so I get the following?
Col A
24
Lets say I have table with 1 column like this:
Col A
1
2
3
4
If I SUM
it, then I will get this:
Col A
10
My question is: how do I multiply Col A so I get the following?
Col A
24
This is a complicated matter. If you want to take signs and handle zero, the expression is a bit complicated:
Note: you do not specify a database. In some databases you would use
LN()
rather thanLOG()
. Also the function for the modulo operator (to handle negative values) also differs by database.You can do It simply by declaring an variable in following, COALESCE is used to avoid
NULLS
.SQL FIDDLE
A quick example, supposing that the column contains only two values: a and b, both different than zero.
We are interested
in x = a*b
. Then, applying some math, we have:Therefore:
This explains Matt's answer:
ROUND is required because of the limited precision of the SQL variables.
Using a combination of
ROUND
,EXP
,SUM
andLOG
SQL Fiddle: http://sqlfiddle.com/#!3/d43c8/2/0
Explanation
LOG
returns the logarithm of col a ex.LOG([Col A])
which returnsThen you use
SUM
to Add them all togetherSUM(LOG([Col A]))
which returnsThen the exponential of that result is calculated using
EXP(SUM(LOG(['3.1780538303479453'])))
which returnsThen this is finally rounded using
ROUND
ROUND(EXP(SUM(LOG('23.999999999999993'))),1)
to get24
Extra Answers
Simple resolution to:
When you have a
0
in your dataIf you only have
0
Then the above would give a result ofNULL
.Example Input:
Output:
SQL Fiddle: http://sqlfiddle.com/#!3/01ddc/3/0
In MySQL you could use
SQLFiddle demo