我认为您可以使用 a 来实现这一点FULL JOIN
,尽管我对您的模式的确切工作方式非常模糊,但按照以下方式应该可以工作:
SELECT COALESCE(ModTab.ItemID, MedTab.ItemID) AS ItemID,
COALESCE(ModTab.TrfCode, MedTab.TrfCode) AS TrfCode,
COALESCE(ModTab.Component, MedTab.Component) AS Component,
COALESCE(ModTab.StateCode, MedTab.StateCode) AS StateCode,
COALESCE(ModTab.OptCode, MedTab.OptCode) AS OptCode,
CASE WHEN ModTab.OptCode IS NULL THEN 'MedTab Only'
WHEN MedTab.OptCode IS NULL THEN 'ModTab Only'
ELSE 'Both Tables'
END AS MatchStatus
FROM ( SELECT l1.ItemID,
l1.TrfCode,
l2.Component,
l2.StateCode,
l2.OptCode
FROM ModTab m
INNER JOIN Lookup1 l1
ON l1.ItemID = m.ItemID
INNER JOIN Lookup2 l2
ON l2.Component = m.Component
) ModTab
FULL JOIN
( SELECT l1.ItemID,
l1.TrfCode,
l2.Component,
l2.StateCode,
l2.OptCode
FROM MedTab m
INNER JOIN Lookup1 l1
ON l1.TrfCode = m.TrfCode
INNER JOIN Lookup2 l2
ON l2.StateCode = m.StateCode
AND l2.OptCode = m.OptCode
) MedTab
ON ModTab.ItemID = MedTab.ItemID
AND ModTab.TrfCode = MedTab.TrfCode
AND ModTab.Component = MedTab.Component
AND ModTab.StateCode = MedTab.StateCode
AND ModTab.OptCode = MedTab.OptCode;
一些连接可能需要修改,但原则是,基本上完全连接数据集,这将返回每个记录的所有记录,其中一个为空你知道记录不在那个集合中,两个都不null 你知道记录在两者中。
编辑
MS-Access 不支持FULL JOIN
多个连接,并且具有不同的 JOIN 语法,因此您必须使用 UNION 来合并记录,然后检查记录源的位置。这样的事情应该这样做:
SELECT ItemID,
TrfCode,
Component,
StateCode,
OptCode,
IIF(SUM(MedTab)=0,'ModTab',IIF(SUM(ModTab)=0,'MedTab','Both')) AS TabStatus
FROM ( SELECT Lookup1.ItemID,
Lookup1.TrfCode,
Lookup2.Component,
Lookup2.StateCode,
Lookup2.OptCode,
1 AS MedTab,
0 AS ModTab
FROM ( MedTab
INNER JOIN Lookup1
ON Lookup1.TrfCode = MedTab.TrfCode
)
INNER JOIN Lookup2
ON Lookup2.StateCode = MedTab.StateCode
AND Lookup2.OptCode = MedTab.OptCode
UNION ALL
SELECT Lookup1.ItemID,
Lookup1.TrfCode,
Lookup2.Component,
Lookup2.StateCode,
Lookup2.OptCode,
0 AS MedTab,
1 AS ModTab
FROM ( ModTab
INNER JOIN Lookup2
ON ModTab.Component = Lookup2.Component
)
INNER JOIN Lookup1
ON ModTab.ItemID = Lookup1.ItemID
)
GROUP BY ItemID, TrfCode, Component, StateCode, OptCode;
我还没有测试过这个,自从我在访问中进行任何查询以来已经有一段时间了,所以手指交叉它第一次工作!