使用相同的键变量连接表但具有不同数量的记录

时间:2017-10-10 23:36:09

标签: sql join

我需要连接两个具有相同ID值但记录数不同的表。 这就是我所拥有的:

enter image description here

enter image description here

enter image description here

enter image description here

这是我的语法:

select  a.id,
    a.event_a,
    a.occur_a,
    a.site_a,
    b.event_b,
    b.site_b

from table_1 a

full outer join table_2 b

on a.id=b.id;

1 个答案:

答案 0 :(得分:0)

您似乎希望对两个表中的大多数列进行独立排序。

这应该做你想要的:

select  a.id, a.event_a, a.occur_a, a.site_a,
        b.event_b, b.site_b
from (select a.*, row_number() over (partition by id order by id) as seqnum
      from table_1 a
     ) a full outer join
     (select b.*, row_number() over (partition by id order by id) as seqnum
      from table_2 b
     ) b
     on a.id = b.id;