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

前端 未结 5 1879
忘掉有多难
忘掉有多难 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:19

    This is a complicated matter. If you want to take signs and handle zero, the expression is a bit complicated:

    select (case when sum(case when a = 0 then 1 else 0 end) > 0
                 then 0
                 else exp(sum(log(abs(a)))) *
                      (case when sum(case when a < 0 then 1 else 0 end) % 2 = 1 then -1 else 1 end)
            end) as ProductA
    from table t;
    

    Note: you do not specify a database. In some databases you would use LN() rather than LOG(). Also the function for the modulo operator (to handle negative values) also differs by database.

提交回复
热议问题