Full outer join not returning all rows?

限于喜欢 提交于 2019-12-02 08:27:01

The where is killing the outer join.
A column cannot be both null and = to a value.
Put the conditions in the join.

SELECT COALESCE(c.AccountBranch, p.AccountBranch)
     , COALESCE(c.AccountNumber, p.AccountNumber)
     , COALESCE(c.AccountSuffix, p.AccountSuffix)
     , c.PrincipleAmount, p.PrincipleAmount
FROM            cb_account_extension_principle_dpd c
FULL OUTER JOIN cb_account_extension_principle_dpd p
  ON p.AccountBranch = c.AccountBranch
 AND p.AccountNumber = c.AccountNumber 
 AND p.AccountSuffix = c.AccountSuffix
 AND c.BusinessDataDate = @CurrentBusinessDataDate
 AND p.BusinessDataDate = @PreviousBusinessDataDate

I think your problem is that in the first query the date isn't part of the match. In your second query the two sets you are joining are filtered on date already, so it mimics what would happen if you matched on date. Say you have the same accountbranch,number,suffix twice but for different dates! That would mean that the full outer join finds a match since you don't have the date in the ON-clause.

Try this out:

use ADStaging;
--your dates go here:
declare @CurrentBusinessDataDate date = '2013-04-21'
, @PreviousBusinessDataDate date = '2013-04-23';
SELECT 
COALESCE(c.AccountBranch, p.AccountBranch)
, COALESCE(c.AccountNumber, p.AccountNumber)
, COALESCE(c.AccountSuffix, p.AccountSuffix)
, c.PrincipleAmount
, p.PrincipleAmount

FROM cb_account_extension_principle_dpd AS c
FULL OUTER JOIN cb_account_extension_principle_dpd AS p
    ON p.AccountBranch = c.AccountBranch
    AND p.AccountNumber = c.AccountNumber
    AND p.AccountSuffix = c.AccountSuffix
    AND p.BusinessDataDate = @PreviousBusinessDataDate
    AND c.BusinessDataDate = @CurrentBusinessDataDate;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!