[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Exlusions in calculating a running balance.

Posted on 2013-02-05
8
Medium Priority
?
355 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

649 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