JOIN 问题:更正要解决的 SQL 语句:ORA-01799:列可能不会外连接到子查询

Bil*_*lıç 1 sql oracle join left-join

正如你在下面看到的;我怎样才能实现 fx.ftf_validitystartdate= ... 这行值,因为 oracle 不允许我在下面这样做。

    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
    )
Run Code Online (Sandbox Code Playgroud)

Gor*_*off 5

如果您将其切换为where子句,它应该可以工作:

select *
from acc_accounts acc join
     kp_paramcore p
     on acc.account_no = p.accountnum and
        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'
 where fx.ftf_validitystartdate= (SELECT MAX(ff.ftf_validitystartdate)
                                 FROM ftf_rates ff
                                 WHERE ff.status = 'A' and
                                       ff.statusrec = 'A'
                                       p.v_CurrentDate BETWEEN ff.systemstartdate AND ff.systemfinishdate                                            AND ff.currencycode = acc.currencsw_kod
                                )
Run Code Online (Sandbox Code Playgroud)

但是,您失去了“左外连接”特征,因此您还需要添加: or fx.ftf_validitystartdate is null。我猜 v_CurrentDate 来自“p”。在列名之前使用表别名总是一个好主意。

但是,我质疑是否真的需要子查询。仅当子查询内满足条件的记录不止一条时才需要。否则,我认为您可以将on条款更改为:

    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
       p.v_CurrentDate BETWEEN fx.systemstartdate AND fx.systemfinishdate
Run Code Online (Sandbox Code Playgroud)