我这里有两张表: A , B表A:只有一列a1:表B:只有一列b1表,A,B的数据分别为:
a1:       b1:
12        12
12        12
12        14
13        14
14        14
14如何能够连接成一张表 c(c1,c2)
其中表c的c1取自a1,c2取自b1
表c的数据为:c1   c2
12   12
12   12
12   null
13   null
14   14
14   14
null 14 ( 相当于将 表a 和 表b 进行连表,相等的行连在一起,但是总行数并不增加。
例如 表a有 三个12 则和表 b连表的时候 和表b的两个12连接,第三个12就只能和null连成一行   )

解决方案 »

  1.   

    先内部建个分组序号字段
    再外连接
    select a.a1,b.b1
    from 
    (select a1,row_number() over(partition by a1 order by 1)rn  from a )a
    full join (select b1,row_number() over(partition by b1 order by 1)rn  from b )b 
    on a.a1=b.b1 and a.rn=b.rn
    ;
      

  2.   

    select m.a1 , n.b1 from
    (
      select a1 , row_number() over(partiton by a1 order by a1) px from a
    ) m 
    full join
    (
      select b1 , row_number() over(partiton by b1 order by b1) px from b
    ) n
    on m.a1 = n.b1 and m.px = n.px