Home Forums SQL Server 2008 T-SQL (SS2K8) Update row Beginning Balance with Prev Row Trial_Balance RE: Update row Beginning Balance with Prev Row Trial_Balance

  • I'm still not able to understand what you want to do here.

    If you had one record per Calendar_Month, this would make more sense to me. As it is, you have a lot of them, with no apparent way to sort them. So I'm guessing you want to somehow SUM your columns and group by Calendar_Month or something? If so, you would want to use that as your starting point and then do something like this:

    -- This would have to have one record per Calendar_Month to work:

    WITH A AS

    (

    SELECT *, RN = ROW_NUMBER() OVER(ORDER BY Calendar_Month) FROM #TrialBalance

    )

    SELECT

    A.Trial_Balance_ID,

    A.FISCALYEAR,

    A.Calendar_Month,

    A.actindx,

    A.CATEGORY,

    A.POSTINGTYPE,

    A.ACTIVITYDEBIT,

    A.ACTIVITYCREDIT,

    A.NETAMOUNT,

    A.Trial_Balance_Debit,

    A.Trial_Balance_Credit,

    A.Trial_Balance_Net,

    ISNULL(B.Trial_Balance_Debit, 0) AS Beginning_Balance_Debit,

    A.Beginning_Balance_Credit,

    A.Beginning_Balance_Net

    FROM A

    LEFT OUTER JOIN A AS B ON

    A.RN - 1 = B.RN

    ORDER BY A.Calendar_Month


    "If I had been drinking out of that toilet, I might have been killed." -Ace Ventura