[英]SQL JOIN: Return rows from LEFT and RIGHT table when there is a match
I have a #tmp table, and I need to find all records from the LEFT table and from the RIGHT table that have a matching Name and Coverage. 我有一个#tmp表,我需要从LEFT表和RIGHT表中找到具有匹配的Name和Coverage的所有记录。
-- select * from #tmp
--#tmp table
ID Name IsConverted Coverage EarliestPolicyEffectiveDate
1 abc 1 Test1 9/1/2017
2 abc 0 Test1 9/2/2017
3 abc 0 Auto 9/3/2017
4 xyz 0 Home 9/3/2017
-- select * from #tmp where IsConverted = 0
--LEFT TABLE
ID Name IsConverted Coverage
2 abc 0 Test1
3 abc 0 Auto
4 xyz 0 Home
-- select * from #tmp where IsConverted = 1
--RIGHT TABLE
ID Name IsConverted Coverage
1 abc 1 Test1
-- DESIRED RESULTS
ID Name IsConverted Coverage
1 abc 1 Test1
2 abc 0 Test1
-- CURRENT RESULTS
ID Name IsConverted Coverage
2 abc 0 Test1
select *
from
(SELECT * FROM #tmp tmp WHERE tmp.IsConverted = 0) nc
join
(SELECT * FROM #tmp tmp WHERE tmp.IsConverted = 1) ic on ic.Name = nc.Name and ic.Coverage = nc.Coverage
I need to be able to get the matching records from both tables, left and right. 我需要能够从左右两个表中获取匹配的记录。 The reason for this is very complicated and does not add any additional information for this post.
这样做的原因非常复杂,因此不会为此帖子添加任何其他信息。
I have tried FULL OUTER JOIN, CROSS APPLY, OUTER APPLY. 我尝试了完全外部联接,交叉应用,外部应用。 Nothing is working.
什么都没用。
EDIT: 编辑:
Preferably I would like to use JOIN because once I find the matching results, I still need access to the left table and the right table because I need to make sure the LEFT.EarliestPolicyEffectiveDate within 15 days of RIGHT.EarliestPolicyEffectiveDate . 最好是我想使用JOIN,因为一旦找到匹配的结果,我仍然需要访问左表和右表,因为我需要确保在RIGHT.EarliestPolicyEffectiveDate 15天之内获得LEFT.EarliestPolicyEffectiveDate。
I'm not sure I can do that if I do UNION ALL. 如果我执行UNION ALL,我不确定是否可以这样做。
Like below example: 如下例所示:
select *
from
(SELECT * FROM #tmp tmp WHERE tmp.IsConverted = 0) nc
join
(SELECT * FROM #tmp tmp WHERE tmp.IsConverted = 1) ic on ic.Name = nc.Name and ic.Coverage = nc.Coverage
where
nc.EarliestPolicyEffectiveDate between DATEADD(d, -15, ic.EarliestPolicyEffectiveDate) and DATEADD(d, 15, ic.EarliestPolicyEffectiveDate)
I think there are better ways to do this, but UNION ALL
and EXISTS
seems to work here: 我认为有更好的方法可以执行此操作,但是
UNION ALL
和EXISTS
似乎在这里起作用:
SELECT *
FROM #tmp t
WHERE IsConverted = 1
AND EXISTS(SELECT 1 FROM #tmp
WHERE IsConverted = 0
AND Name = t.Name
AND Coverage = t.Coverage)
UNION ALL
SELECT *
FROM #tmp t
WHERE IsConverted = 0
AND EXISTS(SELECT 1 FROM #tmp
WHERE IsConverted = 1
AND Name = t.Name
AND Coverage = t.Coverage);
This works for me. 这对我有用。 have a subquery to get all the matching names between LEFT and RIGHT.
有一个子查询来获取LEFT和RIGHT之间的所有匹配名称。 and a subquery to get all matching coverage between LEFT AND RIGHT.
和子查询以获取LEFT和RIGHT之间的所有匹配覆盖率。 then use 'AND' to link them together in the
WHERE
clause 然后在
WHERE
子句中使用“与”将它们链接在一起
select * from #tmp
where name in (Select LEF.Name from LEF
INNER JOIN RIG ON LEF.NAME = RIG.NAME)
and coverage in (Select LEF.coverage from LEF
INNER JOIN RIG ON LEF.coverage = RIG.coverage)
result: 结果:
ID NAME IsConverted Coverage EarliestPolicyEffectiveDate
----------- ---------- ----------- ---------- ---------------------------
1 abc 1 Test1 2017-09-01
2 abc 0 Test1 2017-09-02
I think I see now that you are trying to join the LEFT TABLE directly to the RIGHT TABLE, not using #tmp as a middle join. 我想我现在知道您正在尝试将LEFT TABLE直接连接到RIGHT TABLE,而不是使用#tmp作为中间连接。
You need to UNION the two where they intersect. 您需要将两个相交的位置合并。
Psuedocode: 伪代码:
SELECT * FROM LEFT TABLE WHERE EXISTS(matching row in RIGHT TABLE)
UNION
SELECT * FROM RIGHT TABLE WHERE EXISTS(matching row in LEFT TABLE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.