• haddoque2000 (5/11/2014)


    I have spent most of the weekend trying out different solutions for creating running totals. Set based and cursors. None of them worked. T-SQL has always got some gotcha in the end.

    I have a table with monthly sums for assets and liabilities. I need a running total for each of the accounts. What i'd like to do should be quite simple;

    - Take the first account, add the sum if its first month to the running total and update the row.

    - Take the sum of next month for the same account and add it to the running total and update that row.

    - And so on until another account turns up. Then we clear the running total and start over. This continues until the last period of the last account has been read and updated with a running total.

    This means that which ever method I use I will have have to do some kind of sorting. Otherwise it won't work. This is where the suggested methods I have found so far usually fails. Cursors for instance - I cant update if I sort.

    Is there anyone who have done this on a SQL server 2012? And, please, don't bother to answer if the solution has not been verified on SQL server 2012.

    Hi...you have posted in SQL 2008 forum....is this definitely a 2012 issue?

    if it is 2012 then you do have a SQL solution....but it would be easier if you could provide some sample table create / data / expected results scripts.......that way we can all see your problem and give you tested answers based on your specific requirements.

    search for running totals / windowing function in SQL 2012/

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day