所以我用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
时,我需要确保我收到最新的文件。如果我把那条线拿走,那么我可能会得到错误的日期,这会使我的输出变得混乱。
我在问他们是否可以解决这个问题,或者我是否陷入困境。提前谢谢。
答案 0 :(得分: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标准连接语法重写查询。
答案 1 :(得分:0)
试试这个?
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