Oracle Join:从表2中获取表1中重复值的所有值

时间:2017-06-29 18:57:31

标签: oracle join

我希望上面的标题有点合理......

所以我有一张表中有一些副本:

Key

100
100
100
101
102
102
103

第二张表中包含非重复键和相应的指示符:

Key     Indicator

100        Y
101        N
102        Y
103        N

我如何进行连接以便返回表1的重复键和相应的指示符?所以...

Key     Indicator

100         Y
100         Y
100         Y
101         N
102         Y
102         Y
103         N 

无论我如何加入,我都会继续获得重复。

1 个答案:

答案 0 :(得分:0)

好的,如果最后一个表是所需的结果,那么一个简单的JOIN就可以做到这一点:

SELECT tab1.key, tab2.Indicator
FROM table1 tab1
INNER JOIN table2 tab2 ON tab2.key = tab1.key

因此,对于key中匹配table1的{​​{1}}中的每个table2,来自table1的行将显示为匹配的indicator {在key中{1}}。接下来,我将其与您最初发布的数据一起呈现:

table2

结果为:

select tab1.key, tab2.indicator
from (
        select 100 as key from dual
        union all
        select 100 as key from dual
        union all
        select 100 as key from dual
        union all
        select 101 as key from dual
        union all
        select 102 as key from dual
        union all
        select 102 as key from dual
        union all
        select 103 as key from dual ) tab1
inner join (    select 100 as key, 'Y' as indicator from dual
                union all
                select 101 as key, 'N' as indicator from dual
                union all
                select 102 as key, 'Y' as indicator from dual
                union all
                select 103 as key, 'N' as indicator from dual
            ) tab2 on tab2.key = tab1.key;