Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server SQL
i have created a view to join three tables SUMDEPOSITES,SUMWITHDRAWALS and SUMLOANS
In SUMDEPOSITES i have
SELECT DISTINCT AccountNumber, SUM(Amount) AS deposites
FROM         dbo.DepositeDetails
GROUP BY AccountNumber
 
in SUMWITHDRAWALS i have
SELECT DISTINCT AccountNumber, SUM(Amount) AS withdrawals
FROM         dbo.WithdrawalDetails
GROUP BY AccountNumber
 
in SUMLOANS i have
SELECT DISTINCT AccountNumber, SUM(TotRepayment) AS Loans
FROM         dbo.Loans
GROUP BY AccountNumber
 
now i have created a third view BALANCE which is to do this calculation
SELECT     dbo.SUMDEPOSITES.AccountNumber, dbo.SUMDEPOSITES.deposites - dbo.SUMWITHDRAWALS.withdrawals - dbo.SUMLOANS.Loans AS Balance
FROM         dbo.SUMDEPOSITES LEFT OUTER JOIN
                      dbo.SUMWITHDRAWALS ON dbo.SUMWITHDRAWALS.AccountNumber = dbo.SUMDEPOSITES.AccountNumber LEFT OUTER JOIN
                      dbo.SUMLOANS ON dbo.SUMLOANS.AccountNumber = dbo.SUMWITHDRAWALS.AccountNumber
 
But the problem is when there is no data in SUMDEPOSITES or SUMLOANS and there is data in SUMDEPOSITES, it rturns null instead of the data in SUMDEPOSITES.
When on table is empty i do not get a correct answer.
Can someone help me?
Posted 18-Jan-13 22:34pm
Edited 19-Jan-13 2:41am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
The problem occurs due to NULL values in Table and you are doing calculation on that.
so try your query following way.
 
It might be help you.
 
SELECT     dbo.SUMDEPOSITES.AccountNumber, isnull(dbo.SUMDEPOSITES.deposites,0) -
    isnull(dbo.SUMWITHDRAWALS.withdrawals,0) - isnull(dbo.SUMLOANS.Loans,0) AS Balance
FROM         dbo.SUMDEPOSITES LEFT OUTER JOIN
                      dbo.SUMWITHDRAWALS ON dbo.SUMWITHDRAWALS.AccountNumber = dbo.SUMDEPOSITES.AccountNumber LEFT OUTER JOIN
                      dbo.SUMLOANS ON dbo.SUMLOANS.AccountNumber = dbo.SUMWITHDRAWALS.AccountNumber
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This query depend on the scenario. If this is banking application here is my idea.
You have three view for DEPOSITS, WITHDRAWALS, and LOANS
In normal scenario, you can't withdraw money without deposit (correct me if I'm wrong).
account number for both (Deposits and withdrawals) are same.
that mean for joining clause for DEPOSITS and WITHDRAWALS is LEFT OUTER JOIN.
But for the LOAN, accounts, account numbers are not same. you have to JOIN using UNION
 
This is just for a idea. correct me if i'm wrong.
  Permalink  
v2
Comments
rosoftghana at 19-Jan-13 17:53pm
   
You are right just that LOAN account numbers are also the same as deposites and withdrawals
rosoftghana at 19-Jan-13 17:53pm
   
so what do i do then? Tharaka MTR
Tharaka MTR at 19-Jan-13 21:33pm
   
If you are using same account no for DEPOSITS and LOANS. then above answer is correct. you have to use ISNULL to validate the fields.
 
That's why we need some background information of the question before answering. In real world, banking application if you open the Deposit account and Loan account you will get the two different account numbers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Marcin Kozub 330
1 OriginalGriff 256
2 Sergey Alexandrovich Kryukov 215
3 Praneet Nadkar 197
4 Richard MacCutchan 182
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 19 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100