Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

SQL running totals and subtractions

Last post 05-27-2008 12:05 PM by joe90. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 04-08-2008 10:54 PM

    • sterjo
    • Not Ranked
    • Joined on 04-08-2008
    • Republic of Macedonia
    • New Member
    • Points 10

    SQL running totals and subtractions

    I have a table for which I need a “special” running total. More specifically there should be a close communication between the credit and the debit column. The scenario should be in the following order

    “Credit”

    “Debit”

    “Balance credit”

    “Balance debit”

    6

    0

    6

    0

    5

    0

    11

    0

    0

    4

    7

    0

    0

    9

    0

    2

    3

    0

    1

    0

    0

    5

    0

    4

    0

    2

    0

    6

     

    I have to point out that this is done in a grouped form, where the CustormerID is the grouped clause.  Now I have achieved this to a point where the calculations work only if the credit column is bigger than 0, but when the account starts with debit I get only sums of the column not the needed subtractions. What am I missing? Thanks in advance.

     DECLARE @PrevRunBal MONEY --Overall running total    SET @PrevRunBal = 0DECLARE @PrevGrpBal MONEY --Running total resets when account changes SET @PrevGrpBal = 0 DECLARE @PrevRunCnt INT   --Overall running count (ordinal rank)   SET @PrevRunCnt = 0  DECLARE @PrevGrpCnt INT   --Running count resets when account changes     SET @PrevGrpCnt = 0   DECLARE @PrevAcctID INT   --The "anchor" and "account change detector"    SET @PrevAcctID = 0changes    SET @PrevGrpBalP = 0       update Temp                SET --===== Running Total              @PrevRunBal  = RunBal = @PrevRunBal + dolguva,        --  @iznos =RunBal= dolguva - pobaruva + @sdol -@spob,        --===== Grouped Running Total (Reset when account changes)               @PrevGrpBal  = Bcredit = CASE                                                             WHEN CustomerID = @PrevAcctID                           THEN CASE                         WHEN CREDIt > DEBIT or CREDIT = 0                         THEN (@PrevGrpBal+CREDIT)-(DEBIT)                         WHEN  DEBIT>CREDIT or DEBIT =0                         THEN (@PrevGrpBal+DEBIT)-DEBIt                         END                   ELSE CASE                                 WHEN CREDIT >DEBIT                         THEN (CREDIT)                           WHEN DEBIT > CREDIT                        THEN DEBIT    -- restarts from 0 if only 1 rec.                        END                                                            END,                                   --===== Running Count (Ordinal Rank)                @PrevRunCnt  = RunCnt = @PrevRunCnt + 1, --===== Grouped Running Total (Ordinal Rank, Reset when account changes)                @PrevGrpCnt  = GrpCnt = CASE                                        WHEN CustomerID = @PrevAcctID                                         THEN @PrevGrpCnt + 1                                               ELSE 1 -- Restarts count at "1"                                          END,  --===== "Anchor" and provides for "account change detection"   

                 @PrevAcctID = CustomerID

     

    • Post Points: 10
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 05-27-2008 12:05 PM In reply to

    • joe90
    • Top 50 Contributor
    • Joined on 06-15-2005
    • United Kingdom
    • Guru
    • Points 2,925

    Re: SQL running totals and subtractions

    Is this the actual table?

    ... if so, why not use a trigger to update the "balance credit" and "balance debit" columns appropriately as and when values are entered?

    Joe
     

    • Post Points: 5
Page 1 of 1 (2 items)