我有一个包含多个日期的多个记录的表。
我试图看到“日期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)
答案 0 :(得分:10)
杀死外部联接的位置 列不能同时为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
答案 1 :(得分:0)
我认为您的问题是,在第一个查询中,日期不是匹配的一部分。在您的第二个查询中,您加入的两个集合已经过日期过滤,因此它模仿了您在日期匹配时会发生什么。假设您拥有相同的accountbranch,数字,后缀两次但不同的日期!这意味着完整的外部联接找到匹配项,因为您在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;