Note that there are some explanatory texts on larger screens.

plurals
  1. POJoin two tables on one without mutyplying rows per record
    primarykey
    data
    text
    <p>Let say we have 3 tables named <code>TRANSACTIONS</code>, <code>ORDERS</code>, <code>PAYMENTS</code> all of them having <code>TRANSACTION_ID</code>.</p> <p>This is the regular output I get when joining transaction that has 5 orders and 3 payments:</p> <pre><code>transaction_id, order_id, payment_id ---------- 1, 20, 35 1, 20, 36 1, 20, 37 1, 21, 35 1, 21, 36 1, 21, 37 .... </code></pre> <p>What I should get is:</p> <pre><code>transaction_id, order_id, payment_id ---------- 1, 20, 35 1, 21, 36 1, 22, 37 1, 23, null 1, 24, null </code></pre> <p>If there are more payments than orders, it should have <code>NULL</code> in the <code>order_id</code> column.</p> <p>Basically I need to have number of rows per <code>transaction_id</code> equal to the bigger number of count of <code>orders</code>/<code>payments</code> (in this example 5 > 3 so 5).</p> <p>Have in mind that each of these tables have couple of million records.</p> <p><strong>EDIT</strong>:</p> <p>By the request in the comment, above query is simple join</p> <pre><code>SELECT t.transaction_id, o.order_id, p.payment_id FROM TRANSACTION t LEFT JOIN ORDERS o on o.transaction_id = t.transaction_id LEFT JOIN PAYMENTS p on p.transaction_id = o.transaction_id </code></pre> <p><strong>EDIT 2</strong>:</p> <p>I cannot disclose full table schemes, I only wrote columns essential for the query to work. In reality every of those tables has 20+ columns, and the query should return a combined total of around 20 columns. Again, <code>TRANSACTIONS</code> has over 100m records, and both <code>ORDERS</code> and <code>PAYMENTS</code> have 150m+ records of which we need around 100k records to be returned.</p>
    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.
 

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