本文介绍了有条件地加入两个或多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个视图来连接三个表SUMDEPOSITES,SUMWITHDRAWALS和SUMLOANS

在SUMDEPOSITES中我有

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




在SUMWITHDRAWS中的
我有



in SUMWITHDRAWALS i have

SELECT DISTINCT AccountNumber, SUM(Amount) AS withdrawals
FROM         dbo.WithdrawalDetails
GROUP BY AccountNumber




SUMLOANS的
我有



in SUMLOANS i have

SELECT DISTINCT AccountNumber, SUM(TotRepayment) AS Loans
FROM         dbo.Loans
GROUP BY AccountNumber





现在我已经创建了第三个视图BALANCE来做这个计算



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





但问题是当SUMDEPOSITES或SUMLOANS中没有数据并且有数据时在SUMDEPOSITES中,它rturns null而不是SUMDEPOSITES中的数据。

当桌面为空时我没有得到正确答案。

有人能帮助我吗?



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?

推荐答案

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



这篇关于有条件地加入两个或多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-22 03:23