sql left outer join不适用于多列

时间:2014-11-10 23:57:34

标签: sql oracle

我们有一个查询,如果它们中的任何一个不在数据库中,则应该返回5个值和n / a。这是查询

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+$')

但是发生的事情是,如果任何时候即使一个refnum qual缺少一个值(即它不在数据库中),它也只会省略整行。它不应该这样做,它应该只用N / A替换缺失值并显示4个值的其余部分。

预计样本数据的工作方式如下:

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

目前的结果如何:

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

任何指针都将非常感激。我不知道我是否正确地接近这件事!

1 个答案:

答案 0 :(得分:2)

您的fromwhere条款应为:

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'