How to add the total balances of previous year into the next year?

I'm working with a table called SALFLDGAMS where it has two columns that I need to work with. One of the columns is Period which has years from 2000 to 2005 and the other column is called amount which has the balance for that year. Let me explain in a little more detail. There are account codes associated with the years as well so there will be many places where for example the year 2000 will show up with a given value. What I'm trying to do is to create a view which has a BeginBalance column which adds lets say all the values for 2000 and sticks them to 2001. So what I'm saying is all the values from the previous year I want them in the current year. All in one column. Thanks for the help guys.


Answer this question

How to add the total balances of previous year into the next year?

  • MikeFair

    Thanks for the help guys!

  • Old_newb

    can you post your table structure, some sample data and the result you want


  • Rob Hammond

    Hello there:

    SELECT Acc_code, CONVERT(int, Period) + 1 AS PeriodNext, SUM(Account) AS PreBalance
    FROM dbo.myAccount
    GROUP BY Acc_code, Period


  • Gerald_Schinagl

    Ok, I will post the structure of the table and the structure of the view that I want.

  • Carlillos

    SALFLDGAMS Table:

    Acc_Code Period Amount
    11500 2000 234
    11511 2000 345
    11500 2000 223

    NewView:

    Acc_Code Period BeginBalance
    11500 2001 457

    I want a new view based on the SALFLDGAMS table that shows the begin balance for example 2001 that comes from the total amount Sum of 2000.



  • HelloHello

    what happens if I have to keep Period as Varchar How could it do it then

  • David Sedman

    From reading your other questions about this, you can modify your query to reach your goal.

    Suppose your period column is Varchar column in this format: 20020101

    SELECT Acc_code, (CONVERT(int, Left(Period,4)) + 1) AS PeriodNext, SUM(Account) AS PreBalance
    FROM dbo.myAccount
    GROUP BY Acc_code, Period


  • How to add the total balances of previous year into the next year?