Full outer join not returning all rows?

后端 未结 2 1002
名媛妹妹
名媛妹妹 2020-12-12 05:06

I have a table that contains multiple records for multiple dates.

I am trying to see the difference between \"date 1\" and \"date 2\" and my full outer join is not r

相关标签:
2条回答
  • 2020-12-12 06:03

    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
    
    0 讨论(0)
  • 2020-12-12 06:07

    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;
    
    0 讨论(0)
提交回复
热议问题