Search code examples
oracle-databaseoracle12c

Combine 2 queries to get data in same row in Oracle


I have created 2 queries result set is like below:

Query 1:

ID Name Value Choice
1  abc   10    x
2  def   20    x

Query 2:

ID1 Name1 Value1 Choice1
1  eft   10    y
2  bgf   20    y

I have added a full outer join and result set is like

ID   Name Value Choice ID1  Name1 Value1 Choice1
1    abc   10    x     null null   null    null
2    def   20    x     null null   null    null
null null  null  null  1     eft     10    y 
null null  null  null  2     bgf     20    y 

But I need like below:

ID   Name Value Choice ID1  Name1 Value1 Choice1
1    abc   10    x     1     eft     10    y 
2    def   20    x     2     bgf     20    y 

Not sure on what join or logic should I use.


Solution

  • Not sure what your FULL JOIN condition looks like. However, if you want to "get records side by side" (as stated in your comment) you could do something like ...

    select *
    from (
      select 1 as id, 'abc' as name, 10 as value, 'x' as choice from dual union all
      select 2, 'def', 20, 'x' from dual
    ) Q1 full outer join (
      select 1 as id1, 'eft' as name1, 10 as value1, 'y' as choice1 from dual union all
      select 2, 'bgf', 20, 'y' from dual
    ) Q2
      on Q1.id = Q2.id1 
    ;
    
    -- result
    ID  NAME  VALUE  CHOICE  ID1  NAME1  VALUE1  CHOICE1  
    1   abc   10     x       1    eft    10      y        
    2   def   20     x       2    bgf    20      y 
    

    That, of course will give you some NULLs for IDs that don't "match". Eg (same join, data different)

    select *
    from (
      select 1 as id, 'abc' as name, 10 as value, 'x' as choice from dual union all
      select 2, 'def', 20, 'x' from dual union all
      select 4, '_4_', 40, 'g' from dual
    ) Q1 full outer join (
      select 1 as id1, 'eft' as name1, 10 as value1, 'y' as choice1 from dual union all
      select 2, 'bgf', 20, 'y' from dual union all
      select 5, '_5_', 50, 'z' from dual
    ) Q2
      on Q1.id = Q2.id1 
    ;
    
    ID    NAME   VALUE   CHOICE  ID1   NAME1  VALUE1  CHOICE1  
    1     abc    10      x       1     eft    10      y        
    2     def    20      x       2     bgf    20      y        
    NULL  NULL   NULL    NULL    5     _5_    50      z        
    4     _4_    40      g       NULL  NULL   NULL    NULL
    

    Ponder Stibbon's (thank you!) suggested solution, would probably remove some of the NULLs, but you may end up having records with different IDs in some of the resultset's rows.

    select *
    from ( 
      select id, name, value, choice, rownum row_
      from (
        select 1 as id, 'abc' as name, 10 as value, 'x' as choice from dual union all
        select 2, 'def', 20, 'x' from dual union all
        select 4, '_4_', 40, 'g' from dual
      ) 
    ) Q1 full join (
      select id1, name1, value1, choice1, rownum row_
      from (
        select 1 as id1, 'eft' as name1, 10 as value1, 'y' as choice1 from dual union all
        select 2, 'bgf', 20, 'y' from dual union all
        select 5, '_5_', 50, 'z' from dual
      )
    ) Q2 on Q1.row_ = Q2.row_
    ;
    
    -- result
    ID  NAME  VALUE  CHOICE  ROW_  ID1  NAME1  VALUE1  CHOICE1  ROW_  
    1   abc   10     x       1     1    eft    10      y        1     
    2   def   20     x       2     2    bgf    20      y        2     
    4   _4_   40     g       3     5    _5_    50      z        3