【问题标题】:Outer join trouble外连接麻烦
【发布时间】:2013-09-06 14:55:27
【问题描述】:

所以我写了一个select statement 和一个outer join,但我遇到了一些逻辑问题。首先是声明:

SELECT DISTINCT ah.ACCOUNT, lr.recall_status, lr.cancel_recall, lr.suit_atty, lb.note_sent, lb.current_atty, 
                  lr.file_date, ah.attorney_id, ah.transaction_date, rle.id_code, ah.rule_id, lr.processed, ah.transaction_code
  FROM legal_bankruptcy lb, legal_recall lr, legal_transaction_review ah, atlas.rlglenty rle
  WHERE ah.ACCOUNT = lb.ACCOUNT
  AND ah.ACCOUNT = lr.ACCOUNT(+)
  AND lb.current_atty = rle.id_code
  AND lr.file_date = (SELECT MAX(file_date) FROM legal_recall WHERE ACCOUNT = ah.ACCOUNT)
  AND ah.rule_id IN (1,2,114,191)
  AND ah.batch_id = p_batch_id

现在这应该是如何工作的,并非所有帐户都会在legal_recall 表中,特别是如果他们的帐户没有被召回,但我仍然需要确定是否通过以下方式向公司发送了一张便条legal_bankruptcy。我也知道这个select statement 没有返回任何rows 的原因是因为这条线:

AND lr.file_date = (SELECT MAX(file_date) FROM legal_recall WHERE ACCOUNT = ah.ACCOUNT)

当我将其注释掉时,我会返回值。不过,我现在遇到的问题是,当帐户位于 legal_recall 时,我需要确保获得最新的文件。如果我把那条线去掉,那么我可能会得到错误的日期,这会弄乱我的输出。

我在问他们是否可以解决这个问题,或者我是否陷入困境。提前致谢。

【问题讨论】:

    标签: sql oracle outer-join


    【解决方案1】:

    我认为您可以通过更改此行来解决您的问题:

    AND lr.file_date = (SELECT MAX(file_date) FROM legal_recall WHERE ACCOUNT = ah.ACCOUNT)
    

    AND (lr.file_date is null or lr.file_date = (SELECT MAX(file_date) FROM legal_recall WHERE ACCOUNT = ah.ACCOUNT))
    

    但是,我建议您使用 ANSI 标准连接语法重写查询。

    【讨论】:

    • 好像已经做到了,不敢相信我没有看到这么简单的东西。感谢您的帮助,我也会考虑您的建议。
    【解决方案2】:

    试试这个?

    AND lr.file_date = (SELECT MAX(file_date) FROM legal_recall WHERE ACCOUNT = ah.ACCOUNT)(+)
    

    编辑:

    好的,你的 sql 很难阅读,所以我重新格式化了一下,(我从来没有运气好使用 '(+)' 语法)但是试一试:

    SELECT DISTINCT ah.ACCOUNT, 
       lr.recall_status, 
       lr.cancel_recall, 
       lr.suit_atty, 
       lb.note_sent, 
       lb.current_atty, 
       lr.file_date, 
       ah.attorney_id, 
       ah.transaction_date, 
       rle.id_code, 
       ah.rule_id, 
       lr.processed, 
       ah.transaction_code
    FROM legal_bankruptcy lb 
       LEFT OUTER JOIN legal_recall lr
          ON lr.file_date = (SELECT MAX(file_date) 
                             FROM legal_recall 
                             WHERE ACCOUNT = ah.ACCOUNT)
       JOIN legal_transaction_review ah
          ON ah.rule_id IN (1,2,114,191)
         AND lb.ACCOUNT = ah.ACCOUNT
         AND p_batch_id = ah.batch_id
       JOIN atlas.rlglenty rle
         ON lb.current_atty = rle.id_code
    

    【讨论】:

    • 不,不能那样做。只给出 ORA-00933: SQL 命令未正确结束
    • 仍然没有骰子,我认为它不喜欢 select statement ORA-01799 上的连接:列可能不会外连接到子查询
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多