使用TSQL计算运行余额

时间:2017-07-11 08:05:52

标签: sql sql-server tsql

我需要一些关于运行平衡的帮助。我已经得到了期末余额。但我也希望产生初始平衡。我的余额是“386240.00”。在我的屏幕截图中,“运行”列是结束余额。这是正确的,并从“校长”每行扣除。但是当我尝试初始平衡“bal_run”列时,计算应该基于“Principal”。 “bal_run”的第一行是正确的,但第二行应该少于“Principal”的第一行。

我正在使用SQL Server 2014。

enter image description here

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

1 个答案:

答案 0 :(得分:0)

您可能需要使用一个窗口来获取总和,如下所示:

    @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'