How to get cummulative sum per column?

  • Hi,

    I have a statement per period. I want the periods to be cumulate.  So column 1 must be 1, column 2 must be 1+2, colum3 must be 1+2+3 etc..

     

    SELECT        reknr, oms25_0, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11]
    FROM dbo.View_balans_XXX_per_periode
    ORDER BY reknr

    Result:

    result

    I want to be the result as this:

    result1

     

  • Use a running aggregate before you pivot?

    SELECT n
    , rt = SUM(t.n) OVER (order by n rows between unbounded preceding and current row)
    FROM Testdb.dbo.tally t;

    tally is just a table of numbers.

  • Thanks but how should I do this?

     

  • With no usable SQL or data to work with, maybe this is what you are looking for

    SELECT   reknr, oms25_0
    , [1] = [1]
    , [2] = [1] + [2]
    , [3] = [1] + [2] + [3]
    , [4] = [1] + [2] + [3] + [4]
    , [5] = [1] + [2] + [3] + [4] + [5]
    , [6] = [1] + [2] + [3] + [4] + [5] + [6]
    , [7] = [1] + [2] + [3] + [4] + [5] + [6] + [7]
    , [8] = [1] + [2] + [3] + [4] + [5] + [6] + [7] + [8]
    , [9] = [1] + [2] + [3] + [4] + [5] + [6] + [7] + [8] + [9]
    , [10] = [1] + [2] + [3] + [4] + [5] + [6] + [7] + [8] + [9] + [10]
    , [11] = [1] + [2] + [3] + [4] + [5] + [6] + [7] + [8] + [9] + [10] + [11]
    FROM dbo.View_balans_XXX_per_periode
    ORDER BY reknr

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply