Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In this case you should add one extra RowNumber column (using <a href="http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions137.htm" rel="nofollow">ROW_NUMBER() OVER</a>) to the <code>Orders</code> and <code>Payments</code> tables and then join them with this column. </p> <p><a href="http://sqlfiddle.com/#!4/973e7/20" rel="nofollow">SQLFiddle demo</a></p> <pre><code>select tr.transaction_id, Orders.order_id, Payments.payment_id from TRANSACTIONS tr LEFT JOIN ( select transaction_id,order_id, ROW_NUMBER() OVER (PARTITION BY transaction_id ORDER BY order_id) as rn FROM ORDERS ) Orders on tr.transaction_id=Orders.transaction_id LEFT JOIN ( select transaction_id,payment_id, ROW_NUMBER() OVER (PARTITION BY transaction_id ORDER BY payment_id) as rn FROM PAYMENTS ) Payments on (tr.transaction_id=Payments.transaction_id) AND (Orders.rn=Payments.rn) UNION select tr.transaction_id, Orders.order_id, Payments.payment_id from TRANSACTIONS tr LEFT JOIN ( select transaction_id,payment_id, ROW_NUMBER() OVER (PARTITION BY transaction_id ORDER BY payment_id) as rn FROM PAYMENTS ) Payments on (tr.transaction_id=Payments.transaction_id) LEFT JOIN ( select transaction_id,order_id, ROW_NUMBER() OVER (PARTITION BY transaction_id ORDER BY order_id) as rn FROM ORDERS ) Orders on tr.transaction_id=Orders.transaction_id AND (Orders.rn=Payments.rn) </code></pre> <p>Another way is here without union. In this case we need to generate sequence 1,2,3,4.... and then JOIN both tables joining also with RowNumber to this sequence. In this example we generate sequence from 1 to 1000 (<code>CONNECT BY LEVEL &lt;= 1000</code>). If you don't know what is the maximum rows count possible then you can generate sequence using <code>select COUNT() ...</code> from both tables as a maximum number for sequence generator.</p> <p><a href="http://sqlfiddle.com/#!4/973e7/46" rel="nofollow">SQLFiddle demo</a></p> <pre><code>select tr.transaction_id, Orders.order_id, Payments.payment_id from TRANSACTIONS tr CROSS JOIN (SELECT LEVEL as rn FROM dual CONNECT BY LEVEL &lt;= 1000 ) Cnt LEFT JOIN ( select transaction_id,order_id, ROW_NUMBER() OVER (PARTITION BY transaction_id ORDER BY order_id) as rn FROM ORDERS ) Orders on (tr.transaction_id=Orders.transaction_id) AND (cnt.rn=Orders.rn ) LEFT JOIN ( select transaction_id,payment_id, ROW_NUMBER() OVER (PARTITION BY transaction_id ORDER BY payment_id) as rn FROM PAYMENTS ) Payments on (tr.transaction_id=Payments.transaction_id) AND (cnt.rn=Payments.rn) WHERE Payments.payment_id IS NOT NULL or Orders.Order_id IS NOT NULL order by tr.transaction_id,cnt.rn </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.
 

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