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