2017-07-11 3 views
0

Ich brauche etwas Hilfe beim Laufen Balance. Ich habe bereits den Endstand erreicht. Aber ich will auch das beginnende Gleichgewicht herstellen. Mein Kontostand ist "386240.00". In meinem Screenshot ist die Spalte "run" der Endsaldo. Das ist korrekt und wird pro Zeile von "Principal" abgezogen. Aber wenn ich die "bal_run" -Spalte der Anfangsbalance ausprobiere, sollte die Berechnung auf dem "Principal" basieren. Die erste Reihe von "bal_run" ist korrekt, aber die zweite Reihe sollte weniger von der 1. Reihe des "Principal" sein.Berechnen Laufende Balance mit TSQL

Ich bin mit 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 
+0

Welche Datenbank Sie verwenden? –

+0

Ich benutze SQL Server 2014. –

+0

Überprüfen Sie dies: [Beste Ansätze für laufende Summen] (https://sqlperformance.com/2012/07/t-sql-queries/running-totals) –

Antwort

0

Sie können ein Fenster für die Summe wie folgt verwenden müssen:

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