你所做的类似于:
with d1 as (select 1 col1, 'A' col2 from dual union select 2, 'B' from dual),
d2 as (select 1 col1, 'Q' col2, 'W' col3, 'E' col4 from dual),
d3 as (select 2 col1, 'R' col2, 'T' col3, 'Y' col4 from dual)
select d1.col1, d1.col2,
case d1.col2
when 'A' then
(select d2.col2, d2.col3, d2.col4 from d2 where d2.col1 = d1.col1)
else
(select d3.col2, d4.col3, d5.col4 from d2 where d2.col1 = d1.col1)
end
from d1
然后你得到 ORA-00913: too many values
你应该做的可能是这样的:
with d1 as (select 1 col1, 'A' col2 from dual union select 2, 'B' from dual),
d2 as (select 1 col1, 'Q' col2, 'W' col3, 'E' col4 from dual),
d3 as (select 2 col1, 'R' col2, 'T' col3, 'Y' col4 from dual)
select d1.col1, d1.col2 decision_field,
case d1.col2 when 'A' then d2.col2 else d3.col2 end col2,
case d1.col2 when 'A' then d2.col3 else d3.col3 end col3,
case d1.col2 when 'A' then d2.col4 else d3.col4 end col4
from d1
left join d2 on d2.col1 = d1.col1
left join d3 on d3.col1 = d1.col1
结果:
col1 decision_field col2 col3 col4
1 A Q W E
2 B R T Y