Solved

Exlusions in calculating a running balance.

Posted on 2013-02-05
8
348 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

856 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