我有3张桌子:
我需要从条件A中选择数据条件:
1)如果表B中的行与client_ref连接(如果查询
select b.t_ref from B b where b.client_ref = client_ref_
返回任何数据),我的查询将如下所示:
select max(l_date) from A where t_ref in (select b.t_ref from B b where b.client_ref = client_ref_)
2)如果上面的查询返回任何数据,我的查询将如下所示:
select max(l_date) from A where t_ref in (select c.t_id from C c where c.client_ref = client_ref_)
现在我写了一个PLSQL函数:
select max(aa.l_date) into l_date from A aa where aa.t_ref in (select bb.t_ref from B bb where bb.client_ref = client_ref_);
if l_date is null then
select max(aa.l_date) into l_date from A aa where t_ref in (select t_id from C c where c.client_ref = client_ref_);
end if;
return l_date;
它有效,但这不是一个好主意,因为我把表A称为2次。是否可以避免二次调用,并在一个查询中执行此操作?
答案 0 :(得分:1)
你可以这样使用:我在这里介绍了你的所有情况。请验证
create table A (t_ref integer, l_date date);
create table B (t_ref integer, client_ref integer);
create table C (t_id integer, client_ref integer);
SQL> select * from a
2 /
T_REF L_DATE
---------- ---------
10 01-JAN-11
20 02-FEB-11
30 02-MAR-11
SQL> select * from b;
T_REF CLIENT_REF
---------- ----------
10 101
20 102
SQL> select * from c;
T_ID CLIENT_REF
---------- ----------
10 101
20 102
30 101
40 103
sql> select max(aa.l_date)
--into l_date
from A aa
where aa.t_ref =any( case when ( select bb.t_ref from B bb where bb.client_ref = '101' and rownum <2 ) is null then
( select t_id from C c where c.client_ref = '101' )
when ( select t_id from C c where c.client_ref = '101' and rownum <2 ) is not null then
( select t_id from C c where c.client_ref = '101' and rownum <2 )
else
( select bb.t_ref from B bb where bb.client_ref = '101' )
end
);
MAX(AA.L_
---------
01-JAN-11
SQL> select max(aa.l_date)
--into l_date
from A aa
where aa.t_ref =any( case when ( select bb.t_ref from B bb where bb.client_ref = '102' and rownum <2 ) is null then
( select t_id from C c where c.client_ref = '102' )
when ( select t_id from C c where c.client_ref = '102' and rownum <2 ) is not null then
( select t_id from C c where c.client_ref = '102' )
else
( select bb.t_ref from B bb where bb.client_ref = '102' )
end
);
MAX(AA.L_
---------
02-FEB-11
SQL> select max(aa.l_date)
--into l_date
from A aa
where aa.t_ref =any( case when ( select bb.t_ref from B bb where bb.client_ref = '103' and rownum <2 ) is null then
( select t_id from C c where c.client_ref = '103' )
when ( select t_id from C c where c.client_ref = '103' and rownum <2 ) is not null then
( select t_id from C c where c.client_ref = '103' )
else
( select bb.t_ref from B bb where bb.client_ref = '103' )
end
);
MAX(AA.L_
---------
SQL>
答案 1 :(得分:1)
试试这个。
select max(aa.l_date) into l_date from A aa
where t_ref in
(SELECT t_ref from (select t_id t_ref,client_ref_ client_ref_ from C c
UNION
select t_ref t_ref,client_ref_ client_ref_ from B b)
tmp where tmp.client_ref = client_ref_)
return l_date;
答案 2 :(得分:0)
with
prep ( t_ref, idx ) as (
select b.t_ref, 1
from table_B b
where b.client_ref = :client_ref
union all
select c.t_ref, 2
from table_C c
where c.client_ref = :client_ref
)
select max(a.l_date) keep (dense_rank first order by p.idx) -- into l_date
from table_A a inner join prep p
on a.t_ref = p.t_ref
;
说明:首先扫描table_B
以查找匹配client_ref
的行;如果找到,请收集它们并附上1
的“索引”。然后扫描table_C
并执行相同操作,但索引为2
。 (如果table_C
非常大,则会在:client_ref
中找到table_B
时浪费时间;如果这是一个问题,可以使用更多代码解决它。然后table_A
加入到将这两组行放在一起的结果。 keep dense_rank first...
将确保只考虑idx = 1
max(l_date)
的行,但如果不存在此类行,则只会考虑idx = 2
行。
如果在表B和表C中找不到:client_ref
,则生成的max(l_date)
将为null
。
我用:clent_ref
作为绑定变量编写了查询,因此我可以测试查询;您可以将其更改为变量名称client_ref_