Sql s表B中的记录与客户端101连接。如果我向客户端102调用我的函数,它将返回2011年2月2日。如果我调用客户端103的函数,它将返回2011年4月4日,因为表B中没有与104客户端连接的记录,但表C中有这样的记录。如果我在数据集为空的情况下运行查询..不
Sql s表B中的记录与客户端101连接。如果我向客户端102调用我的函数,它将返回2011年2月2日。如果我调用客户端103的函数,它将返回2011年4月4日,因为表B中没有与104客户端连接的记录,但表C中有这样的记录。如果我在数据集为空的情况下运行查询..不,sql,oracle,plsql,outer-join,Sql,Oracle,Plsql,Outer Join,s表B中的记录与客户端101连接。如果我向客户端102调用我的函数,它将返回2011年2月2日。如果我调用客户端103的函数,它将返回2011年4月4日,因为表B中没有与104客户端连接的记录,但表C中有这样的记录。如果我在数据集为空的情况下运行查询..不给出任何错误..您尝试使用您给出的数据运行查询,并检查是否有任何错误我不需要UNION。在任何情况下,它都将返回C中的所有记录以及B中与客户机\u ref\uuu连接的所有记录,但如果存在,我需要B中的记录,如果不存在,我需要C中的记录。存在是
s表B中的记录与客户端101连接。如果我向客户端102调用我的函数,它将返回2011年2月2日。如果我调用客户端103的函数,它将返回2011年4月4日,因为表B中没有与104客户端连接的记录,但表C中有这样的记录。如果我在数据集为空的情况下运行查询..不给出任何错误..您尝试使用您给出的数据运行查询,并检查是否有任何错误我不需要UNION。在任何情况下,它都将返回C中的所有记录以及B中与
客户机\u ref\uuu
连接的所有记录,但如果存在,我需要B中的记录,如果不存在,我需要C中的记录。存在是什么意思。。您能否共享所有3个表中的示例数据。如果表B中有与客户端\u ref\uuu
连接的行,我需要它们。如果B中没有与client\u ref\uu
连接的行,我需要从C中选择与client\u ref\uu\uu连接的所有行,我不需要UNION。在任何情况下,它都将返回C中的所有记录以及B中与客户机\u ref\uuu
连接的所有记录,但如果存在,我需要B中的记录,如果不存在,我需要C中的记录。存在是什么意思。。您能否共享所有3个表中的示例数据。如果表B中有与客户端\u ref\uuu
连接的行,我需要它们。如果B中没有与client\u ref\u连接的行,我需要从C中选择与client\u ref\u连接的所有行
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_)
select max(l_date) from A where t_ref in (select c.t_id from C c where c.client_ref = client_ref_)
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;
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>
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;
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
;