2

我有一个包含多个日期的多条记录的表。

我试图查看“日期 1”和“日期 2”之间的区别,并且我的完整外部连接没有返回我期望的数据。

我知道日期 1 有 13278 行,日期 2 有 13282 - 因此我希望看到至少 13282 行,但我得到 13195 ......这是一个INNER JOIN(我测试过)。

我希望得到如下结果:

001     000123    009    NULL    1000
001     000124    009    1000    1000
001     000125    009    1000    1000
001     000126    009    1000    NULL

但这不会从任何一方获得任何空行?

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

这行得通 - 将连接的“键”组合在两个单独的选择语句中?

SELECT
      COALESCE(C.Account, P.Account) AS Account
    , COALESCE(C.AccountBranch, P.AccountBranch) as AccountBranch
    , COALESCE(C.AccountNumber, P.AccountNumber) as AccountNumber
    , COALESCE(C.AccountSuffix, P.AccountSuffix) as AccountSuffix
    , P.PrincipleAmount AS PreviousAmount
    , C.PrincipleAmount AS CurrentAmount
    , ISNULL(C.PrincipleAmount, P.PrincipleAmount) - ISNULL(P.PrincipleAmount,0)
FROM 
(SELECT 
    (pd.AccountBranch + pd.AccountNumber + pd.AccountSuffix) AS Account
    , pd.AccountBranch
    , pd.AccountNumber
    , pd.AccountSuffix
    , pd.PrincipleAmount
FROM ADStaging..cb_account_extension_principle_dpd pd
WHERE pd.BusinessDataDate = @CurrentBusinessDataDate) C
FULL OUTER JOIN 
(SELECT 
    (pd.AccountBranch + pd.AccountNumber + pd.AccountSuffix) AS Account
    , pd.AccountBranch
    , pd.AccountNumber
    , pd.AccountSuffix
    , pd.PrincipleAmount
FROM ADStaging..cb_account_extension_principle_dpd pd
WHERE pd.BusinessDataDate = @PreviousBusinessDataDate) P
    ON P.Account = C.Account
WHERE 
    (P.PrincipleAmount IS NULL OR C.PrincipleAmount IS NULL)
    OR
    P.PrincipleAmount <> C.PrincipleAmount

但这不 - 加入组合值 - 只有当它们是单独的表时?

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

2 回答 2

12

where 正在杀死外部连接。
一列不能同时为 null 和 = 到一个值。
将条件放入联接中。

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
于 2013-04-23T13:33:49.740 回答
0

我认为您的问题是,在第一个查询中,日期不是匹配的一部分。在您的第二个查询中,您加入的两个集合已经按日期过滤,因此它模拟了您在日期匹配时会发生的情况。假设您有两次相同的帐户分支,编号,后缀,但日期不同!这意味着完整的外部联接会找到匹配项,因为您在 ON 子句中没有日期。

试试这个:

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;
于 2013-04-23T12:14:25.110 回答