合并2个查询以在Oracle的同一行中获取数据

时间:2019-04-03 10:41:37

标签: oracle oracle12c

我已经创建了2个查询结果集,如下所示:

查询1:

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

查询2:

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

我添加了完整的外部联接,结果集就像

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 

但是我需要如下所示:

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

不确定应该使用哪种联接或逻辑。

2 个答案:

答案 0 :(得分:1)

您从未向我们显示您最初的两个查询,但是我假设我们可以将它们包装为CTE。鉴于此,您这里实际上需要的是一个内部联接,而不是一个完整的外部联接:

WITH cte1 AS (
    -- first query
),
cte2 AS (
    -- second query
)

SELECT t1.ID, t1.Name, t1."Value", t1.Choice, t2.ID1, t2.Name1, t2.Value1, t2.Choice1
FROM cte2 t1
INNER JOIN cte2 t2
    ON t1.ID = t2.ID1;

答案 1 :(得分:1)

不确定您的FULL JOIN条件是什么样的。但是,如果您想“并排获取记录”(如评论中所述),则可以执行...

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 

那当然会为您提供一些“不匹配” ID的NULL。例如(连接相同,数据不同)

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

考虑Stibbon的(谢谢!)建议的解决方案,可能会删除一些NULL,但最终可能会在结果集的某些行中包含具有不同ID的记录。

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