Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 532143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:22:28+00:00 2026-05-13T09:22:28+00:00

I have a query to subtract current balance from one table with previous balance

  • 0

I have a query to subtract current balance from one table with previous balance from another history table. When a particular product has no current balance but has previous balance, I am able to subtract it correctly…in this case it will be like 0 – 100.

However, when the product has current balance but no previous balance, I am unable to get the result. My query does not even select the current balance even though I have done a full outer join on both tables.

Following is my query:

SELECT  DATEPART(yyyy, @ExecuteDate) * 10000 + DATEPART(mm, @ExecuteDate) * 100 + DATEPART(dd, @ExecuteDate) AS Period_Key,
                CASE WHEN GL.GL_Acct_Key IS NULL THEN 0 ELSE GL.GL_Acct_Key END AS GL_Acct_Key,
                CASE WHEN BANK.Bank_Type_Key IS NULL THEN 0 ELSE BANK.Bank_Type_Key END AS Bank_Type_Key,
                CASE WHEN TSC.TSC_Key IS NULL THEN 0 ELSE TSC.TSC_Key END AS TSC_Key,
                ISNULL(FT.CurrentBalance,0) - ISNULL(HIST.CurrentBalance,0) AS Actual_Income_Daily,
                CASE WHEN BR.Branch_Key IS NULL THEN 0 ELSE BR.Branch_Key END AS Branch_Key
        FROM    WSB_Stage.dbo.Stage_TS_Daily_Income_Hist HIST
                FULL OUTER JOIN WSB_Stage.dbo.Stage_TS_Daily_Income FT
                    ON FT.GLAcctID = HIST.GLAcctID AND
                       FT.BankType = HIST.BankType AND
                       FT.BranchNumber = HIST.BranchNumber
                LEFT OUTER JOIN WSB_Mart.dbo.Dim_Branch BR
                    ON HIST.BranchNumber = BR.Branch_Code
                LEFT OUTER JOIN WSB_Mart.dbo.Dim_GL_Acct GL
                    ON HIST.GLAcctID = GL.Acct_Code
                LEFT OUTER JOIN WSB_Mart.dbo.Dim_Bank_Type BANK
                    ON HIST.BankType = BANK.Bank_Type_Code
                LEFT OUTER JOIN WSB_Stage.dbo.Param_Branch_TSC_Map BRTSC
                    ON HIST.BranchNumber = BRTSC.BranchNumber
                LEFT OUTER JOIN WSB_Mart.dbo.Dim_TSC TSC
                    ON BRTSC.RegionCode = TSC.TSC_Code
        WHERE   HIST.TransactionDate = @PreviousDate
            AND GL.Acct_Type_Code = 'Interest'
            AND BANK.Bank_Type_Key = 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:22:29+00:00Added an answer on May 13, 2026 at 9:22 am

    Thanks for the help but I couldn’t get it to work the way I wanted using the below answers. Finally, I decided to go the long way and declare two temporary tables to hold current and previous balances. I think I want to stay as far away from outer joins as possible ;p

    Code is below:

    INSERT INTO @PreviousGL
        SELECT  GLAcctID,
                BankType,
                BranchNumber,
                ISNULL(CurrentBalance,0) AS Current_Balance
        FROM    WSB_Stage.dbo.Stage_TS_Daily_Income_Hist
        WHERE   TransactionDate = @PreviousDate
    
    INSERT INTO @CurrentGL
    SELECT  GLAcctID,
            BankType,
            BranchNumber,
            ISNULL(CurrentBalance,0) AS Current_Balance
    FROM    WSB_Stage.dbo.Stage_TS_Daily_Income
    
    INSERT INTO @DailyIncomeGL
    SELECT  CASE WHEN CURR.GLAcctID IS NULL THEN PREV.GLAcctID
                 WHEN PREV.GLAcctID IS NULL THEN CURR.GLAcctID
                 WHEN CURR.GLAcctID IS NULL AND PREV.GLAcctID IS NULL THEN 0
                 ELSE CURR.GLAcctID
            END AS GLAcctID,
            CASE WHEN CURR.BankType IS NULL THEN PREV.BankType
                 WHEN PREV.BankType IS NULL THEN CURR.BankType
                 WHEN CURR.BankType IS NULL AND PREV.BankType IS NULL THEN ''
                 ELSE CURR.BankType
            END AS BankType,
            CASE WHEN CURR.BranchNumber IS NULL THEN PREV.BranchNumber
                 WHEN PREV.BranchNumber IS NULL THEN CURR.BranchNumber
                 WHEN CURR.BranchNumber IS NULL AND PREV.BranchNumber IS NULL THEN 0
                 ELSE CURR.BranchNumber
            END AS BranchNumber,
            ISNULL(CURR.CurrentBal,0) - ISNULL(PREV.CurrentBal,0) AS Actual_Income_Daily
    FROM    @CurrentGL CURR
            FULL OUTER JOIN @PreviousGL PREV
                ON CURR.GLAcctID = PREV.GLAcctID AND
                   CURR.BankType = PREV.BankType AND
                   CURR.BranchNumber = PREV.BranchNumber
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.