Home Forums SQL Server 2008 T-SQL (SS2K8) Trying to SUM row with Current Date to Row with "Last Month" Date RE: Trying to SUM row with Current Date to Row with "Last Month" Date

  • There's no need for a recursive CTE. If that query gives the correct results you can use it to update your table.

    WITH CTE AS(

    SELECT MTB.*,

    Trail_Balance_Debit_New = MTB.ACTIVITYDEBIT + ISNULL(CTE.Trail_Balance_Debit, 0)

    FROM #MyTrialBalance AS MTB

    LEFT OUTER JOIN #MyTrialBalance CTE ON

    MTB.Calendar_Month = DATEADD(month, 1, CTE.Calendar_Month)

    )

    UPDATE b SET

    Trail_Balance_Debit = c.Trail_Balance_Debit_New

    FROM #MyTrialBalance b

    JOIN CTE c ON b.Trial_Balance_ID = c.Trial_Balance_ID

    However, you could always try a fast method called Quirky Update (QU) which will help you to generate running totals. You can read more about it and other methods on the following article: http://www.sqlservercentral.com/articles/T-SQL/68467/

    If you use the QU, be sure to follow all the rules.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2