How to Multiply all values within a column with SQL like SUM()

前端 未结 5 1867
忘掉有多难
忘掉有多难 2020-12-30 02:34

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
         


        
5条回答
  •  自闭症患者
    2020-12-30 03:17

    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:

    x = a * b -> log(x) = log(a * b) -> log(x) = log(a) + log(b) ->
    exp[log(x)] =  exp[log(a) + log(b)] -> x = exp[log(a) + log(b)].
    

    Therefore:

    a * b = exp[log(a) + log(b)]
    

    This explains Matt's answer:

    SELECT ROUND(EXP(SUM(LOG([Col A]))),1)

    FROM your table

    ROUND is required because of the limited precision of the SQL variables.

提交回复
热议问题