Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL select some rows in a table so that they sum up to certain value
    primarykey
    data
    text
    <p>How can I select just some rows in the following table so that they sum up to certain value?</p> <pre><code>Table ----- id | qty1 | qty2 | qty3 | qty4 ------------------------------ 1 | 0.0 | 0.0 | 10 | 20 2 | 1.5 | 0.0 | 7.5 | 18 3 | 1.0 | 2.0 | 7.5 | 18 4 | 0.0 | 0.5 | 5 | 13 </code></pre> <p>Let's say, the top value I want is 57...</p> <p>So I need to select the rows from the previous table such that qty1+qty2+qty3+qty4 of each row, get until that 57 value, and discard the other rows. In this example, I would get the following:</p> <pre><code>id | qty1 | qty2 | qty3 | qty4 ------------------------------ 1 | 0.0 | 0.0 | 10 | 20 2 | 1.5 | 0.0 | 7.5 | 18 </code></pre> <p>Because 10+20+1.5+7.5+18 = 57, so I discard rows 3 &amp; 4...</p> <p>Now I wish that the top value is 50, then I should get:</p> <pre><code>id | qty1 | qty2 | qty3 | qty4 ------------------------------ 1 | 0.0 | 0.0 | 10 | 20 2 | 1.5 | 0.0 | 7.5 | 11 </code></pre> <p>Since these values sum up to 50, and the 7 from row2,qty4 is left out... (BTW the rows are ordered in this particular way because that's the order in which I wish to account for the sums of qtys... It's not valid to sum up first row1, then 3, then 2 then 4, for example... They should always be sumed in the order 1,2,3,4...)</p> <p>What if I would like the complement of this? I mean, the other two rows I didn't got in the last result.</p> <p>First case:</p> <pre><code>id | qty1 | qty2 | qty3 | qty4 ------------------------------ 3 | 1.0 | 2.0 | 7.5 | 18 4 | 0.0 | 0.5 | 5 | 13 </code></pre> <p>Second case:</p> <pre><code>id | qty1 | qty2 | qty3 | qty4 ------------------------------ 2 | 0.0 | 0.0 | 0.0 | 7 3 | 1.0 | 2.0 | 7.5 | 18 4 | 0.0 | 0.5 | 5 | 13 </code></pre> <p>(If the second case is too complicated, how about obtaining:</p> <pre><code>id | qty1 | qty2 | qty3 | qty4 ------------------------------ 1 | 0.0 | 0.0 | 10 | 20 </code></pre> <p>Because adding up the original qtys of row 2 would surpass the 50 value, I discard it... The complement in this case should just be:</p> <pre><code>id | qty1 | qty2 | qty3 | qty4 ------------------------------ 2 | 1.5 | 0.0 | 7.5 | 18 3 | 1.0 | 2.0 | 7.5 | 18 4 | 0.0 | 0.5 | 5 | 13 </code></pre> <p>)</p>
    singulars
    1. This table or related slice is empty.
    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