• Here's a much simpler way to calculating the CloseBalance. No recursion or temp tables. A temp table for the daily total would definitely be faster especially in future dates, just keep adding to it daily since the GL should be immutable.

    If validated the SQL but haven't tested it against their DB. I based it off one for order totals in a commerce app and worked great.

    WITH tvDailyTotal AS

    (

    SELECT Company_Key, Account_Key, SUM(ISNULL(Amount,0)) DailyTotal, [Date]

    FROM Fact_GL

    GROUP BY [Date]

    HAVING DailyTotal != 0

    )

    SELECT Company_Key, Account_Key, TD2.[Date], TD2.DailyTotal, SUM(TD1.DailyTotal) Amount_CB

    FROM

    tvDailyTotal TD1

    RIGHT JOIN tvDailyTotal TD2 ON TD1.[Date] <= TD2.[Date]

    GROUP BY TD2.[Date], TD2.DailyTotal

    ORDER BY TD2.[Date]