JOIN问题:更正要解决的SQL语句:ORA-01799:列可能未外部联接到子查询 [英] JOIN Issue : Correct the SQL Statement to solve : ORA-01799: a column may not be outer-joined to a subquery

查看:172
本文介绍了JOIN问题:更正要解决的SQL语句:ORA-01799:列可能未外部联接到子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下所示;我如何实现fx.ftf_validitystartdate = ...这行值,因为oracle不允许我这样做如下

As you see below; how can I implement fx.ftf_validitystartdate= ... this lines value since oracle does not allow me to do it like this below .

    select * from  acc_accounts acc
    join kp_paramcore p on
    acc.account_no = p.accountnum
    acc.suffix = p.suffixc
         LEFT JOIN ftf_rates fx
              ON p.maturestart = fx.ftf_vadealtsinir
             AND p.maturefinish = fx.ftf_vadeustsinir
             AND fx.statusrec = 'A'
             AND fx.currencycode = acc.currencsw_kod
             AND fx.status= 'A' 
  and  fx.ftf_validitystartdate= (SELECT MAX(ff.ftf_validitystartdate)
                                               FROM ftf_rates ff
                                              WHERE ff.status = 'A'
                                                AND ff.statusrec = 'A'
                                                AND v_CurrentDate BETWEEN ff.systemstartdate AND ff.systemfinishdate                                            AND ff.currencycode = acc.currencsw_kod
    )

推荐答案

我发布了CTE解决方法,并且仅在Oracle 11g中进行了测试.

I publish the workaround with CTE and tested only in Oracle 11g.

要进行测试,我创建了此架构:

create table t_a ( a int );
create table t_b ( a int);
create table t_c ( a int);

insert into t_a values (1);
insert into t_a values (2);
insert into t_a values (3);

insert into t_b values (1);
insert into t_b values (2);
insert into t_b values (3);

insert into t_c values (1);
insert into t_c values (2);
insert into t_c values (3);

这时,我对该查询强加了错误:

At this time I force error with this query:

select * 
from t_a
left outer join t_b
  on t_a.a = t_b.a and
     t_b.a = ( select max( a )
             from t_c);

现在我用CTE重写查询:

And now I rewrite query with CTE:

with cte (a ) as (
   select a
   from t_b
   where t_b.a = ( select min( a )
             from t_c)
)
select * 
from t_a
left outer join cte
  on t_a.a = cte.a;

第二个查询返回正确的结果.

This second query returns right results.

我用CTE重写了您的查询:

I rewrite your query with CTE:

with CTE as (
   select * from ftf_rates 
   where ftf_validitystartdate= (SELECT MAX(ff.ftf_validitystartdate)
                                 FROM ftf_rates ff
                                 WHERE ff.status = 'A'
                                      AND ff.statusrec = 'A'
                                      AND v_CurrentDate BETWEEN ff.systemstartdate 
                                      AND ff.systemfinishdate                   
                                      AND ff.currencycode = acc.currencsw_kod )

)
    select * from  acc_accounts acc
    join kp_paramcore p on
    acc.account_no = p.accountnum
    acc.suffix = p.suffixc
         LEFT JOIN CTE fx
              ON p.maturestart = fx.ftf_vadealtsinir
             AND p.maturefinish = fx.ftf_vadeustsinir
             AND fx.statusrec = 'A'
             AND fx.currencycode = acc.currencsw_kod
             AND fx.status= 'A' 

注意,仅在Oracle 11g中进行了测试.参见@a_horse_with_no_name注释:

Notice, only tested in Oracle 11g. See @a_horse_with_no_name coment:

@danihp:CTE在Oracle 11g之前就已经存在了(我认为它们是 在9.1中甚至可能更早引入-但它们绝对是 在10.x版本中可用). 11.2引入了不需要的递归CTE 在这种情况下. –

@danihp: CTEs were available long before Oracle 11g (I think they were introducted in 9.1 maybe even earlier - but they are definitely available in 10.x). 11.2 introduced recursive CTEs which is not needed in this case. –

这篇关于JOIN问题:更正要解决的SQL语句:ORA-01799:列可能未外部联接到子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆