Solved

Exlusions in calculating a running balance.

Posted on 2013-02-05
8
343 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
  • 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 500 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now