从oracle中的表的多行中检索行

时间:2018-07-27 06:53:34

标签: sql oracle

我想从三个表中检索数据

例如

表_1:NAME_A

PD_ID 
 A
 B
 C

表_2:NAME_B主键PD_ID,EV_N

PD_ID   EV_N   EV_DEC
 A      1       one
 A      2       Two
 B      1       one
 B      2       Two
 B      3       THREE
 C      1       one
 C      2       Two

表3:NAME_C主键PD_ID

PD_ID, FFT_NAME, FFT_DESC
A        XY       XY_DESC
B        ZY       ZY_DESC
B        XY       XY_DESC
C        ZY       ZY_DESC
C        XY       XY_DESC
C        PY       PY_DESC

必需的输出

PD_ID    EV_N   EV_DEC  FFT_NAME  FFT_DESC
 A        1      ONE      XY        XY_DESC 
 A        2      TWO       
 B        1      ONE      ZY        ZY_DESC 
 B        2      TWO      XY        XY_DESC 
 B        3      THREE    
 C        1      ONE      ZY        ZY_DESC
 C        2      Two      XY        XY_DESC
                          PY        PY_DESC 

2 个答案:

答案 0 :(得分:1)

想法是对两个表中的记录进行范围划分,然后在完全外部联接中使用此范围编号:

with
t1 as (
  select 'A' pd_id from dual union all
  select 'B' pd_id from dual union all
  select 'C' pd_id from dual
),
t2 as (
  select 'A' pd_id, 1 EV_N, 'one' EV_DEC from dual union all
  select 'A' pd_id, 2 EV_N, 'two' EV_DEC from dual union all
  select 'B' pd_id, 1 EV_N, 'one' EV_DEC from dual union all
  select 'B' pd_id, 2 EV_N, 'two' EV_DEC from dual union all
  select 'B' pd_id, 3 EV_N, 'three' EV_DEC from dual union all
  select 'C' pd_id, 1 EV_N, 'one' EV_DEC from dual union all
  select 'C' pd_id, 2 EV_N, 'two' EV_DEC from dual
),
t3 as (
  select 'A' pd_id, 'XY' FFT_NAME, 'XY_DESC' FFT_DESC from dual union all
  select 'B' pd_id, 'ZY' FFT_NAME, 'ZY_DESC' FFT_DESC from dual union all
  select 'B' pd_id, 'XY' FFT_NAME, 'XY_DESC' FFT_DESC from dual union all
  select 'C' pd_id, 'ZY' FFT_NAME, 'ZY_DESC' FFT_DESC from dual union all
  select 'C' pd_id, 'XY' FFT_NAME, 'XY_DESC' FFT_DESC from dual union all
  select 'C' pd_id, 'PY' FFT_NAME, 'PY_DESC' FFT_DESC from dual
)
select coalesce(t22.pd_id,t33.pd_id) pd_id, 
       t22.ev_dec, 
       t33.FFT_NAME, 
       t33.FFT_DESC
from   (
        select pd_id, ev_n, ev_dec, row_number() over (partition by pd_id order by ev_n, ev_dec) rn
        from  t2
       ) t22 
         full join (
           select pd_id, FFT_NAME, FFT_DESC, row_number() over (partition by pd_id order by FFT_NAME, FFT_DESC) rn
           from   t3
         ) t33 
           on t22.pd_id = t33.pd_id
           and t22.rn = t33.rn  

答案 1 :(得分:1)

这不会产生您指定的确切输出,但是会产生一致且可预测的输出:

select t1.PD_ID 
       ,   t2.EV_N
       ,   t2.EV_DEC
       ,   t2.FFT_NAME
       ,   t2.FFT_DESC
from  name_a t1
     cross join ( select coalesce(b.p_id, c.p_id) as p_id
                         , b.ev_n
                         , upper(b.ev_dec) as ev_dec
                         , c.fft_name
                         , c.fft_desc
                  from  ( select * from name_b ) b
                  full outer join 
                        ( select c.*
                                 , row_number() over (partition by c.p_id 
                                                      order by c.fft_name) as rn
                           from name_c c) c
                  on c.p_id = b._pid  
                  and c.rn = b.ev_n) t2
where t1.p_id = t2.p_id                     
order by t1.p_id
         , t2.ev_n nulls last