简体   繁体   English

SQL JOIN:存在匹配项时,从LEFT和RIGHT表返回行

[英]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 ALLEXISTS似乎在这里起作用:

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.

相关问题 SQL左联接-右表中的多行 - SQL Left Join - Multiple Rows in Right Table 当右表中有很多匹配的行时,左联接 - Left join when there are lots of matched rows from right table 在SQL连接操作中,如何从左侧联接中获取行,以及如何从右侧表中获取两列的汇总 - In an SQL join operation, how to get the rows from the left join and only the aggregate of two columns from the right table 返回连接的左表的所有结果,以及右表可以填写的缺失行 - Return all results from left table that join, as well as missing rows that the right table can fill in 没有连接关系时,SQL返回左表 - SQL return left table when no join relationship 内部联接到右表后,SQL查询从左表中选择不同的行 - SQL query to select distinct rows from left table after inner join to the right table SQL JOIN从左表获取数据,即使所有右列都匹配 - SQL JOIN get data from left table Only even if all right column match SQL-RIGHT表有多行时如何避免LEFT JOIN重复? - SQL - How to avoid duplicates with LEFT JOIN when RIGHT table has multiple rows? POSTGRES SQL左联接仅返回右表上最大的UID吗? - POSTGRES SQL Left Join return only the largest UID on the right table? sql-left-outer-join-with行,基于右表中的列值 - sql-left-outer-join-with rows-based on column value from right table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM