0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

full outer join を使わずに完全外部結合を行う場合

Last updated at Posted at 2018-11-11

完全外部結合を行う場合、「full outer join」をキーワードにクエリを発行すればよいが、
DBサーバによってはサポートされていないことが多い。

そういった場合は「union all」を使って完全外部結合を行う。
以下クエリでその例を示す。

select distinct * from (select e1.seirino,e1.time,e2.buyer_no,e2.buyer_name from daityou e1 inner join a0160 e2 on e1.seirino = e2.seirino where e1.time ='2018-10-27 00:00:00.0') 
 j1 left join (select distinct a1.seirino,a1.buyer_no,a1.a0162_no,a1.A0162_NAME,a1.a0164_no ,a1.a0164_name,a2.a0168_no ,a2.a0168_name       

from (select distinct q1.seirino,q1.buyer_no,q1.a0162_no,q1.A0162_NAME,q2.a0164_no ,q2.a0164_name    
  from A0162 q1 left join  A0164  q2 on q1.seirino=q2.seirino and q1.buyer_no=q2.buyer_no and q1.a0162_no=q2.a0164_no     
union all     
select  q2.seirino,q2.buyer_no,null as a0164_no,null as A0164_NAME,q2.a0164_no ,q2.a0164_name   
  from A0164 q2      
 where not exists(select * from A0162 q1     
where q2.seiriNO = q1.seiriNO and q1.buyer_no=q2.buyer_no and q1.a0162_no=q2.a0164_no   ) )   

a1 left join  A0168  a2 on a1.seirino=a2.無題.png
seirino and a1.buyer_no=a2.buyer_no and (a1.a0162_no=a2.a0168_no or a1.a0164_no=a2.a0168_no)   
union all   

select  d2.seirino,d2.buyer_no,null as a0162_no,null as A0162_NAME,null as a0164_no,null as A0164_NAME,d2.a0168_no ,d2.a0168_name   
  from A0168 d2     
 where not exists(select * from    
(select distinct c1.seirino,c1.buyer_no,c1.a0162_no,c1.A0162_NAME,c2.a0164_no ,c2.a0164_name       
  from A0162 c1 left join  A0164  c2 on c1.seirino=c2.seirino and c1.buyer_no=c2.buyer_no and c1.a0162_no=c2.a0164_no   
union all     
select  c2.seirino,c2.buyer_no,null as a0164_no,null as A0164_NAME,c2.a0164_no ,c2.a0164_name      
from A0164 c2      
 where not exists  
(select * from A0162 c1  where c2.seiriNO = c1.seiriNO and c1.buyer_no=c2.buyer_no and c1.a0162_no=c2.a0164_no   ) ) d1    
where d2.seiriNO = d1.seiriNO and d1.buyer_no=d2.buyer_no and (d1.a0162_no=d2.a0168_no or d1.a0164_no=d2.a0168_no))) j2 on j1.seirino=j2.seirino and j1.buyer_no=j2.buyer_no  

order by j1.seirino,j1.buyer_no ,j2.a0162_no ASC NULLS LAST,j2.a0164_no ASC NULLS LAST,j2.a0168_no ASC NULLS LAST;

◆テーブル状態

SEIRINO TIME BUYER_NO BUYER_NAME SEIRINO BUYER_NO A0162_NO A0162_NAME A0164_NO A0164_NAME A0168_NO A0168_NAME
1 2018-10-27 00:00:00.0 1 A 1 1 1 N1 1 A4 1 1
1 2018-10-27 00:00:00.0 1 A 1 1 2 N2 2 B4 2 2
1 2018-10-27 00:00:00.0 1 A 1 1 3 N3 3 C4 3 3
1 2018-10-27 00:00:00.0 1 A 1 1 4 N4 4 4 4 4
1 2018-10-27 00:00:00.0 1 A 1 1 null null 5 5 5 5
1 2018-10-27 00:00:00.0 1 A 1 1 null null null null 6 6
1 2018-10-27 00:00:00.0 2 B 1 2 1 M1 1 D4 1 1
1 2018-10-27 00:00:00.0 2 B 1 2 2 M2 2 D4 null null
1 2018-10-27 00:00:00.0 3 C 1 3 1 L1 1 D4 null null
1 2018-10-27 00:00:00.0 4 D null null null null null null null null

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?