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

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

    You can do It simply by declaring an variable in following, COALESCE is used to avoid NULLS.

    DECLARE @var INT
    
    SELECT @var = Col1 * COALESCE(@var, 1) FROM Tbl
    
    SELECT @var
    

    SQL FIDDLE

提交回复
热议问题