简体   繁体   中英

Calculate Running Balance using TSQL

I need some help on running balance. I already got the ending balance. But I want also to produce the beginning balance. My balance is "386240.00". In my screenshot, The "run" column is the Ending Balance. Which is correct, and deducted per row from "Principal". But when I'm trying the Beginning Balance "bal_run" column, the computation should based from the "Principal". The first row from "bal_run" is correct, but the 2nd row should less from the 1st row of the "Principal".

I'm using SQL Server 2014.

在此处输入图片说明

CREATE PROC spAppPaymentSchedRB @RENum as varchar(150), @Amount as decimal(19, 6)
AS

--SET @Amount = @Amount - (-664250.000000 * -1.00)

DECLARE @TotalAmount as money, @ItemCode as varchar(50), @BegBal as money

SELECT @ItemCode = U_App_ItemCode FROM [@APP_OAMS] WHERE U_App_DocNum = @RENum
SELECT @BegBal = U_APPRE_Begbal FROM OITM WHERE ItemCode = @ItemCode

IF @BegBal > 0.00
    BEGIN
        SELECT @Amount = @BegBal
    END 

;WITH schedRB as
(
SELECT
    U_App_RBTerm 'Term'
    , a.U_App_DocNum 'Trans No.' 
    , IsNULL(CASE WHEN b.U_App_InvNo = 0 THEN '' ELSE b.U_App_InvNo END, '') 'Inv#'
    , IsNULL(CASE WHEN b.U_App_PaymentNum = 0 THEN '' ELSE b.U_App_PaymentNum END, '') 'Payment#'
    , U_App_RBDesc 'Description'
    , U_App_RBDate 'Date'
    , IsNULL(B2.SumApplied, U_APP_RBayment) 'Payment' /*BTG 3/16/2017 - Include total amount paid*/ 
    , U_APP_RBInterest 'Interest'
    , U_APP_RBPrincipal 'Principal'
    , U_APP_RBPRB 'Principal Running Bal.'
    , U_APP_RBRRB 'Receivable Running Bal.'
    , U_APP_PrincipalBal 'Principal_Balance' 
    , a.U_APP_UserSign
    , a.Code
    , a.U_App_LineNum
    , a.U_App_VisOrder
        FROM [@APP_TRBS] a
            LEFT JOIN [@APP_AMS3](NOLOCK) b ON a.U_App_DocNum = b.U_App_DocNum AND a.U_App_VisOrder = b.U_App_VisOrder /*BTG 2/27/2017 - Old a.U_App_LineNum = b.U_App_LineNum */
            /*BTG 3/16/2017 - Include total amount paid*/
            LEFT JOIN ORCT B1 on b.U_App_PaymentNum = B1.DocNum
            LEFT JOIN RCT2 B2 on B1.Docnum = b2.DocNum AND b.U_App_InvNo = b2.DocEntry /*BTG 2/27/2017 - added B.U_App_InvNo = b2.DocEntry for Filtering*/    
        WHERE (a.U_App_LineStatus <> 'Y' OR a.U_App_LineStatus IS NULL) AND a.U_App_DocNum = @RENum 

UNION ALL

SELECT 
    b.U_App_RBTerm 'Term'
    , a.U_App_DocNum
    , '' 'Inv#'
    , U_App_PaymentNum 'Payment#'
    , '' 'Description'
    , U_App_PCheckDate 'Date'
    , U_App_PAmount 'Payment' 
    , 0.00 'Interest'
    , 0.00 'Principal'
    , 0.00 'Principal Running Bal.' 
    , 0.00 'Receivable Running Bal.' 
    , 0.00 'Principal_Balance'
    , '' 'U_App_UserSign'
    , '' 'Code' 
    , a.U_App_LineNum    
    , b.U_App_VisOrder 
        FROM [@APP_BMS3] a
        INNER JOIN [@APP_TRBS] b ON a.U_App_DocNum = b.U_App_DocNum AND a.U_App_LineNum = b.U_App_LineNum AND a.U_App_DocNum = @RENum
)

    SELECT 
        Term
        , [Trans No.]
        , [Inv#]
        , [Payment#]
        , Description
        , Date
        , Payment 
        , Interest
        , Principal
        , CASE WHEN @Amount - sum(Principal) over (order by CAST(U_App_VisOrder as decimal(19, 2)) /*Date*/) < -100.00 THEN 0.00 ELSE @Amount - sum(Principal) over (order by CAST(U_App_VisOrder as decimal(19, 2)) /*Date*/) END as 'Principal Running Bal.'
        , [Receivable Running Bal.]
        , U_APP_UserSign
        , Code
        , U_App_LineNum
        , U_App_VisOrder
        , @Amount - CASE WHEN Description = 'Installment 1' THEN 0.00 ELSE sum(Principal_Balance) over (order by CAST(U_App_VisOrder as decimal(19, 2))/*Date*/) END 'bal_run' 
        , @Amount - sum(Principal) over (order by CAST(U_App_VisOrder as decimal(19, 2)) /*Date*/) 'run' 
        INTO #TEMP
        FROM schedRB --WHERE U_App_LineNum <> 0

        SELECT *, (SELECT TOP 1 run FROM #TEMP ORDER BY CAST(U_App_VisOrder as decimal(19, 2)) DESC) 'run2' FROM #TEMP
            ORDER BY CAST(U_App_VisOrder as DECIMAL(19, 4))
        DROP TABLE #TEMP
;

GO

/*How to use*/
EXEC spAppPaymentSchedRB '685', 386240.00

You may need to use a window for the sum like this:

    @Amount - CASE WHEN Description = 'Installment 1' THEN 0.00 ELSE sum(Principal_Balance) over (order by CAST(U_App_VisOrder as decimal(19, 2))  
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING/*Date*/) END 'bal_run' 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM