?
Solved

Exlusions in calculating a running balance.

Posted on 2013-02-05
8
Medium Priority
?
353 Views
Last Modified: 2013-02-05
I'm calculating a running balance on a column with the amounts of transactions.  However, I want the running balance to "skip over" the amounts in the columns with an entry type of savings.  I'm having trouble with that part.  Here's by running balance and the savings information that needs to be skipped.


(SELECT SUM(C2.AMOUNT) AS Balance
      FROM TRANS AS C2
      WHERE C2.FacilityID = T.FacilityID
      AND C2.N <= T.N
) AS Balance


NOTE:  I had this in the first query to get the amount with other statements in a CASE

WHEN BAD.IBS_ENTRY_TYPE BETWEEN '50' AND '99'
      AND BAD.IBS_ENTRY_TYPE IN ('92','95')
THEN BAD.IBS_DETAIL_AMOUNT


Would appreciate any help.  Thanks
0
Comment
Question by:Sherry
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38856197
post your full query

explain how the IBS_entry_table relates to the transaction table

and just so we are clear about what you mean by running balance

could you should us some example data and the expected/desired result
0
 

Author Comment

by:Sherry
ID: 38856232
Here you go.

USE FrozenTAS
GO      



DECLARE @DOC_NB            CHAR(6)
--DECLARE @StartDate      DATE
DECLARE      @EndDate      DATE

SET @DOC_NB = '753545'
--SET @StartDate = '1996-04-03'
SET      @EndDate =       '2008-12-09';

-- Insert statements for procedure here
--====================================================================
WITH TRANS AS
(
      SELECT
                  BAD.IBS_FACILITY_ID AS FacilityID
            ,      BAD.IBS_DOC_NUMBER AS  DOC_NB
            ,      BAD.IBS_BATCH_DATE AS  Batch_Date
            ,      BAD.IBS_BATCH_NUMBER AS Batch_NB
            ,      BAD.IBS_BATCH_ITEM_NUMBER AS Item_NB
            ,      BAD.IBS_ENTRY_TYPE AS Entry_Type
            ,      ET.IBS_ENTRY_TYPE_DESCRIPTION AS Entry_Desc
            ,      BAD.IBS_REFERENCE_FIELD AS Reference
            ,      BAD.IBS_REMITTER AS Remitter
            
            -- Determine the debits and credits
            ,      CASE
                        WHEN BAD.IBS_ENTRY_TYPE < '50' AND BAD.IBS_BATCH_ITEM_NUMBER < '5000'
                              THEN BAD.IBS_DETAIL_AMOUNT * + 1
                        WHEN BAD.IBS_ENTRY_TYPE < '50' AND BAD.IBS_BATCH_ITEM_NUMBER > '4999'
                              THEN BAD.IBS_DETAIL_AMOUNT * - 1
                        WHEN BAD.IBS_ENTRY_TYPE BETWEEN '50' AND '99'
                                    AND BAD.IBS_BATCH_ITEM_NUMBER < '6000'
                              THEN BAD.IBS_DETAIL_AMOUNT * - 1
                        WHEN BAD.IBS_ENTRY_TYPE BETWEEN '50' AND '99'
                                    AND BAD.IBS_BATCH_ITEM_NUMBER !< '6000'
                              THEN BAD.IBS_DETAIL_AMOUNT * + 1
                        WHEN BAD.IBS_ENTRY_TYPE BETWEEN '50' AND '99'
                                    AND BAD.IBS_ENTRY_TYPE IN ('92','95')
                              THEN BAD.IBS_DETAIL_AMOUNT       
                        ELSE NULL
                  END AS Amount
                  
            ,ROW_NUMBER() OVER(PARTITION BY BAD.IBS_FACILITY_ID ORDER BY BAD.IBS_BATCH_DATE ASC,BAD.IBS_BATCH_NUMBER ASC
                  ,BAD.IBS_BATCH_ITEM_NUMBER ASC) AS N


      FROM OMS_OWNER.TB_BANK_ACCT_DET BAD

            INNER JOIN OMS_OWNER.TB_BANK_ACCOUNT BA
                  ON ( BA.IBS_DOC_NUMBER = BAD.IBS_DOC_NUMBER )
                        AND ( BA.IBS_FACILITY_ID = BAD.IBS_FACILITY_ID )

            INNER JOIN OMS_OWNER.TB_ENTRY_TYPES ET
                  ON ( ET.IBS_ENTRY_TYPE = BAD.IBS_ENTRY_TYPE )

      WHERE  BA.IBS_DOC_NUMBER = @DOC_NB
)

--===============================================================
--Final Results
--===============================================================

      SELECT
                  T.FacilityID
            --,      T.DOC_NB
            --,      (RTRIM(I.INMATE_LAST_NAME) + ', ' + RTRIM(I.INMATE_FIRST_NAME) + ' '+ RTRIM(I.INMATE_MID_INIT))
            --            AS Offender_Name
            ,      T.Batch_Date
            ,      T.Batch_NB
            ,      T.Item_NB
            ,      T.Entry_Type
            ,      T.Entry_Desc
            ,      T.Reference
            ,      T.Remitter
            ,      T.Amount
            ,      Balance
            

      FROM TRANS T
            INNER JOIN OMS_OWNER.TB_INMATES I
                  ON ( I.IBS_DOC_NUMBER = T.DOC_NB )
            OUTER APPLY
            (SELECT SUM(C2.AMOUNT) AS Balance
                        FROM TRANS AS C2
                        WHERE C2.FacilityID = T.FacilityID
                              AND C2.N <= T.N
                  ) AS Balance
                  
                        
WHERE T.Batch_Date < @EndDate

ORDER BY  T.Batch_Date,T.Batch_NB, T.Item_NB
--====================================================



Sample data:
See attached file.  I've highlighted the two entries in this sample.  The amounts need to show on the statement, but I don't want them in the running balance calculations.
FacilityID-Batch.docx
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 2000 total points
ID: 38856924
change the outer apply sum to this...

 OUTER APPLY
            (SELECT SUM(case when entry_type in (92,95) then 0.00 else C2.AMOUNT end) AS Balance
                        FROM TRANS AS C2
                        WHERE C2.FacilityID = T.FacilityID
                              AND C2.N <= T.N
                  ) AS Balance
0
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 

Author Comment

by:Sherry
ID: 38857169
This is closer:  
      
FROM TRANS T
    INNER JOIN OMS_OWNER.TB_INMATES I
    ON ( I.IBS_DOC_NUMBER = T.DOC_NB )
OUTER APPLY
     (SELECT CASE WHEN T.Entry_Type IN (92,95) THEN (SUM(C2.Amount)- T.Amount)
            ELSE SUM(C2.AMOUNT) END AS Balance
      FROM TRANS AS C2
      WHERE C2.FacilityID = T.FacilityID
            AND C2.N <= T.N
     ) AS Balance

This shows the balance right now for the savings amount and doesn't calculate it.  But, the next transaction/balance is incorrect.  It subtracts the savings amount and the current amount from the previous balance.
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38857237
can you show the output from mine and your cases and explain what your expected result should be.
0
 

Author Comment

by:Sherry
ID: 38857251
I will, but unfortunately, I have meetings this afternoon and will have to pick it back up in the morning.  If I can, I'll get this out later today.  Thank you
0
 

Author Comment

by:Sherry
ID: 38857630
Ok, I looked at this way too much.  When I came back and started again.  I re-copied your code to my query and it does exactly what I want.  Sorry and thank you so much for your help.
0
 

Author Closing Comment

by:Sherry
ID: 38857631
Really appreciate the help, works just the way I wanted.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question