Oracle SQL将2个选择组合到一列

时间:2015-09-28 08:26:32

标签: sql oracle

很抱歉,如果这听起来含糊不清......我已经找到了答案,但我发现很难解释 - 因此难以搜索。

我有一个非常简单的脚本......

select pr1.polypart_no match_from, pr1.part_no match_to
from oes_polybox_replace pr1
where pr1.plant = 'W'
and pr1.part_no = 
      (select max(pr2.part_no) 
          from oes_polybox_replace pr2 
          where pr2.plant = 'W' 
          and pr2.polypart_no = 'YPOLYGREY')

...在第1列中显示了部件号,在第2列中显示了可用于代替第1列中部件号的通用部件。

enter image description here

我的问题是我需要将第2列中的部分添加到第1列,即

enter image description here

有没有办法将2个选择添加到一列?

1 个答案:

答案 0 :(得分:1)

这是一个联合示例

select pr1.polypart_no match_from
from oes_polybox_replace pr1
where pr1.plant = 'W'
and pr1.part_no = 
      (select max(pr2.part_no) 
          from oes_polybox_replace pr2 
          where pr2.plant = 'W' 
          and pr2.polypart_no = 'YPOLYGREY')
UNION
select pr1.part_no match_from
from oes_polybox_replace pr1
where pr1.plant = 'W'
and pr1.part_no = 
      (select max(pr2.part_no) 
          from oes_polybox_replace pr2 
          where pr2.plant = 'W' 
          and pr2.polypart_no = 'YPOLYGREY')

UNION只选择不同的值,UNION ALL不会。

如果您需要重新订购,可以进一步包装。

SELECT match_from from (
select pr1.polypart_no match_from
    from oes_polybox_replace pr1
    where pr1.plant = 'W'
    and pr1.part_no = 
          (select max(pr2.part_no) 
              from oes_polybox_replace pr2 
              where pr2.plant = 'W' 
              and pr2.polypart_no = 'YPOLYGREY')
    UNION
    select pr1.part_no match_from
    from oes_polybox_replace pr1
    where pr1.plant = 'W'
    and pr1.part_no = 
          (select max(pr2.part_no) 
              from oes_polybox_replace pr2 
              where pr2.plant = 'W' 
              and pr2.polypart_no = 'YPOLYGREY')) tab
order by match_from asc