有条件地加入两个或多个表 [英] Conditional joining on two or more tables

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

问题描述

我创建了一个视图来连接三个表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?

推荐答案





由于表中的NULL值而导致出现此问题,并且您正在对此进行计算。 br />
所以请按照以下方式尝试你的查询。



它可能对你有帮助。



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


此查询取决于场景。如果这是银行应用,这是我的想法。

您有三个存款,取款和贷款的视图

在正常情况下,您无法存款而无需存款(如果我错了,请纠正我) 。

两者的账号(存款和取款)相同。

表示加入DEPOSITS和WITHDRAWALS的条款是 LEFT OUTER JOIN

但对于贷款,账户,账号不一样。你必须使用加入UNION



这只是一个想法。如果我错了,请纠正我。
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.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆