Skip to content
Advertisement

joining same table multiple times in Oracle

Hi I am tuning one legacy code. we have below 2 tables in a big query.

  fnd_currencies, and pa_commitment_txns

I have seen in from clause

  fnd_currencies fca,
  fnd_currencies fcr,
  fnd_currencies fcp,
  pa_commitment_txns pct
 

first table has been used thrice and it has been outer joined with same column with same table.

 AND fca.currency_code(+) = pct.acct_currency_code
  AND fcr.currency_code( +) = pct.receipt_currency_code
  AND fcp.currency_code(+) = pct.project_currency_code

Can above 3 lines be handled using fnd_currencies table only once. is there any smarter way to do that?

Advertisement

Answer

You can ensure that you only query fnd_currencies once by using a subquery factoring clause. That would look like this (and keeps @gordonlinoff happy by using ANSI 92 syntax):

with ccy as ( select * 
              from fnd_currencies )
select fca.descr as acct_currency
      ,fcr.descr as receipt_currency
      ,fcp.descr as project_currency
      ,pct.*
from pa_commitment_txns pct
left outer join ccy     fca on fca.currency_code = pct.acct_currency_code
left outer join ccy     fcr on fcr.currency_code = pct.receipt_currency_code
left outer join ccy     fcp on fcp.currency_code = pct.project_currency_code

Whether this will actually give you an improved execution time depends on the details of your data, which you haven’t vouchsafed to us.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement