Oracle SQL外连接查询难题

时间:2012-09-06 09:37:17

标签: sql oracle date join

select
  whatever
from
  bank_accs b1,
  bank_accs b2,
  table3 t3
where
  t3.bank_acc_id = t1.bank_acc_id and
  b2.bank_acc_number = b1.bank_acc_number and
  b2.currency_code(+) = t3.buy_currency and
  trunc(sysdate) between nvl(b2.start_date, trunc(sysdate)) and nvl(b2.end_date, trunc(sysdate));

我的问题是b2上的日期(现状)检查。现在,我需要为每个t3xb1返回一行(当然,t3 = ~10个表加入),即使b2中只有无效记录(日期)。如何正确地外接这个位? 不能使用ANSI连接,必须在单个平面查询中执行。 感谢。

2 个答案:

答案 0 :(得分:0)

如果我理解你,只需将外符号(+)添加到b2的所有列:

select
  whatever
from
  bank_accs b1,
  bank_accs b2,
  table3 t3
where
  t3.bank_acc_id = t1.bank_acc_id and
  b2.bank_acc_number = b1.bank_acc_number and
  b2.currency_code(+) = t3.buy_currency and
  trunc(sysdate) between nvl(b2.start_date(+), trunc(sysdate)) and nvl(b2.end_date(+), trunc(sysdate));

答案 1 :(得分:0)

可以使用不等式编写旧式外连接,但它容易出错。我建议你使用内联视图,外连接将清晰明确:

SELECT whatever
  FROM bank_accs b1,
       table3 t3,
       (SELECT b2.*
          FROM bank_accs b2
         WHERE trunc(sysdate) BETWEEN nvl(b2.start_date, trunc(sysdate)) 
                                  AND nvl(b2.end_date, trunc(sysdate))
       ) b2
 WHERE t3.bank_acc_id = t1.bank_acc_id
   AND b2.bank_acc_number = b1.bank_acc_number
   AND b2.currency_code(+) = t3.buy_currency;