Solved

Exlusions in calculating a running balance.

Posted on 2013-02-05
8
349 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

749 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