Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you <code>JOIN</code> two or more table together, you effectively get a cartesian product for these tables to which a filter stated in the <code>JOIN</code> condition is applied.</p> <p>This is more obvious when you use an obsolete implicit <code>JOIN</code> syntax.</p> <p>The <code>LEFT JOIN</code> guarantees that you get no <em>less</em> rows than the leftmost table contains, i. e. each row from the leftmost table is returned at least once.</p> <p>You can still get more rows, if the filter is not a one-to-one row mapping.</p> <p>In your case:</p> <pre><code>SELECT (b.descr || ' - ' || c.descr) description FROM tbl1 a LEFT JOIN tbl2 b ON b.ACCOUNT = a.ACCOUNT LEFT JOIN tbl3 c ON c.product = a.product WHERE a.descr50 = ' ' </code></pre> <p>either <code>acccount</code> or <code>product</code> are not unique in <code>b</code> or <code>c</code>.</p> <p>For these rows:</p> <pre><code>a.account 1 2 3 b.account b.description 1 Account 1 2 Account 2 - old 2 Account 2 - new </code></pre> <p>, the <code>JOIN</code> will return the following:</p> <pre><code>a.account b.account b.description 1 1 Account 1 2 2 Account 2 - old 2 2 Account 2 - new 3 NULL NULL </code></pre> <p>, giving you more rows than either of the tables contains.</p> <p>To just pick the first matching description from either table, use this:</p> <pre><code>SELECT ( SELECT FIRST_VALUE(descr) OVER (ORDER BY descr) FROM tbl2 b WHERE b.account = a.account AND rownum = 1 ) || ' - ' || ( SELECT FIRST_VALUE(descr) OVER (ORDER BY descr) FROM tbl3 c WHERE c.product= a.product AND rownum = 1 ) description FROM tbl1 a WHERE a.descr50 = ' ' </code></pre> <p>To update, just wrap the query into an inline view:</p> <pre><code>UPDATE ( SELECT ( SELECT FIRST_VALUE(descr) OVER (ORDER BY descr) FROM tbl2 b WHERE b.account = a.account AND rownum = 1 ) || ' - ' || ( SELECT FIRST_VALUE(descr) OVER (ORDER BY descr) FROM tbl3 c WHERE c.product= a.product AND rownum = 1 ) description FROM tbl1 a WHERE a.descr50 = ' ' ) SET descr50 = description </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload