简体   繁体   中英

sql left outer join not working for multiple columns

we have a query which should return 5 values and n/a if any of them are not in database. Here is the query

SELECT NVL(IR16.invoice_refnum_value, 'N/A') AS GL_CODE1,
       NVL(IR15.invoice_refnum_value, 'N/A') AS GL_AMOUNT1, 
       NVL(IR17.invoice_refnum_value, 'N/A') AS GL_RECEIVING_BU,
       NVL(IR18.invoice_refnum_value,'N/A') AS GL_SHIPPING_BU,
       NVL(IR19.invoice_refnum_value, 'N/A') AS GL_SALES_ORDER_NUMBER
    FROM   invoice i2 
    LEFT outer JOIN invoice_refnum ir16 
      ON i2.invoice_gid = ir16.invoice_gid 
    LEFT outer JOIN invoice_refnum ir15 
      ON i2.invoice_gid = ir15.invoice_gid
    LEFT outer JOIN invoice_refnum ir17 
      ON i2.invoice_gid = ir17.invoice_gid
    LEFT outer JOIN invoice_refnum ir18 
      ON i2.invoice_gid = ir18.invoice_gid
    LEFT outer JOIN invoice_refnum ir19 
      ON i2.invoice_gid = ir19.invoice_gid
    where ir15.invoice_refnum_qual_gid like 'GL AMOUNT%' 
      AND ir16.invoice_refnum_qual_gid like 'GL CODE%' 
      AND ir17.invoice_refnum_qual_gid like 'GL RECEIVING BU%'
      AND ir18.invoice_refnum_qual_gid like 'GL SHIPPING BU%'
      AND ir19.invoice_refnum_qual_gid like 'GL SALES ORDER NUMBER%'
      AND i2.invoice_gid = 'TEST' 
      and regexp_substr(ir16.invoice_refnum_qual_gid,'\d+$') = regexp_substr( ir15.invoice_refnum_qual_gid,'\d+$')
      and regexp_substr(ir16.invoice_refnum_qual_gid,'\d+$') = regexp_substr( ir17.invoice_refnum_qual_gid,'\d+$')
      and regexp_substr(ir16.invoice_refnum_qual_gid,'\d+$') = regexp_substr( ir18.invoice_refnum_qual_gid,'\d+$')
      and regexp_substr(ir16.invoice_refnum_qual_gid,'\d+$') = regexp_substr( ir19.invoice_refnum_qual_gid,'\d+$')

But what is happening is if anytime there is a value missing for even one refnum qual( ie it is not in database) it will just omit the entire row. it should not do this, it should just replace missing value with N/A and display rest of 4 values.

Sample data expected to be working like:

  • test1 test1 test1 test1 test1
    • test2 test2 N/A test2 test2
    • test3 test3 test3 N/A test3
  • test4 test4 test4 test4 test4

how is current results coming:

  • test1 test1 test1 test1 test1
  • test4 test4 test4 test4 test4

any pointers will be greatly appreciated. i dont know if i am approaching this thing the right way!

Your from and where clauses should be:

FROM   invoice i2 
       LEFT outer JOIN invoice_refnum ir16 
              ON i2.invoice_gid = ir16.invoice_gid and ir16.invoice_refnum_qual_gid like 'GL CODE%'
       LEFT outer JOIN invoice_refnum ir15 
              ON i2.invoice_gid = ir15.invoice_gid and ir15.invoice_refnum_qual_gid like 'GL AMOUNT%'
       LEFT outer JOIN invoice_refnum ir17 
              ON i2.invoice_gid = ir17.invoice_gid and ir17.invoice_refnum_qual_gid like 'GL RECEIVING BU%'
       LEFT outer JOIN invoice_refnum ir18 
              ON i2.invoice_gid = ir18.invoice_gid and ir18.invoice_refnum_qual_gid like 'GL SHIPPING BU%'
       LEFT outer JOIN invoice_refnum ir19 
              ON i2.invoice_gid = ir19.invoice_gid and ir19.invoice_refnum_qual_gid like 'GL SALES ORDER NUMBER%' and
                 regexp_substr(ir16.invoice_refnum_qual_gid,'\d+$') = regexp_substr( ir15.invoice_refnum_qual_gid,'\d+$') and
                 regexp_substr(ir16.invoice_refnum_qual_gid,'\d+$') = regexp_substr( ir17.invoice_refnum_qual_gid,'\d+$') and
                 regexp_substr(ir16.invoice_refnum_qual_gid,'\d+$') = regexp_substr( ir18.invoice_refnum_qual_gid,'\d+$') and
                 regexp_substr(ir16.invoice_refnum_qual_gid,'\d+$') = regexp_substr( ir19.invoice_refnum_qual_gid,'\d+$')
WHERE i2.invoice_gid = 'TEST' 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM