Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Let's load your sample data from the question</p> <pre><code>mysql&gt; drop database if exists javier; Query OK, 1 row affected (0.02 sec) mysql&gt; create database javier; Query OK, 1 row affected (0.01 sec) mysql&gt; use javier Database changed mysql&gt; create table mytable -&gt; ( -&gt; id int not null auto_increment, -&gt; qty1 float,qty2 float,qty3 float,qty4 float, -&gt; primary key (id) -&gt; ); Query OK, 0 rows affected (0.08 sec) mysql&gt; insert into mytable (qty1,qty2,qty3,qty4) values -&gt; ( 0.0 , 0.0 , 10 , 20 ),( 1.5 , 0.0 , 7.5 , 18 ), -&gt; ( 1.0 , 2.0 , 7.5 , 18 ),( 0.0 , 0.5 , 5 , 13 ); Query OK, 4 rows affected (0.05 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql&gt; select * from mytable; +----+------+------+------+------+ | id | qty1 | qty2 | qty3 | qty4 | +----+------+------+------+------+ | 1 | 0 | 0 | 10 | 20 | | 2 | 1.5 | 0 | 7.5 | 18 | | 3 | 1 | 2 | 7.5 | 18 | | 4 | 0 | 0.5 | 5 | 13 | +----+------+------+------+------+ 4 rows in set (0.00 sec) mysql&gt; </code></pre> <h1><strong>FINAL QUERY THAT FULLY WORKS</strong></h1> <pre><code>select BBBB.* from (select id,sums FROM (select A.id,A.sums from (select id,(select sum(qty1+qty2+qty3+qty4) from mytable BB where BB.id&lt;=AA.id) sums from mytable AA order by id) A INNER JOIN (SELECT 50 mylimit) B ON A.sums &lt;= B.mylimit) AAA UNION (select A.id,A.sums from (select id,(select sum(qty1+qty2+qty3+qty4) from mytable BB where BB.id&lt;=AA.id) sums from mytable AA order by id) A where A.sums=(select min(A.sums) sums from (select id, (select sum(qty1+qty2+qty3+qty4) from mytable BB where BB.id&lt;=AA.id) sums from mytable AA order by id) A INNER JOIN (SELECT 50 mylimit) B ON A.sums &gt;= B.mylimit))) AAAA JOIN mytable BBBB USING (id); </code></pre> <h1><strong>FINAL QUERY COMPLEMENT THAT FULLY WORKS</strong></h1> <pre><code>select BBBB.* from mytable BBBB LEFT JOIN (select id,sums FROM (select A.id,A.sums from ( select id,(select sum(qty1+qty2+qty3+qty4) from mytable BB where BB.id&lt;=AA.id) sums from mytable AA order by id) A INNER JOIN (SELECT 50 mylimit) B ON A.sums &lt;= B.mylimit) AAA UNION (select A.id,A.sums from (select id, (select sum(qty1+qty2+qty3+qty4) from mytable BB where BB.id&lt;=AA.id) sums from mytable AA order by id) A where A.sums=(select min(A.sums) sums from ( select id,(select sum(qty1+qty2+qty3+qty4) from mytable BB where BB.id&lt;=AA.id) sums from mytable AA order by id) A INNER JOIN (SELECT 50 mylimit) B ON A.sums &gt;= B.mylimit))) AAAA USING (id) WHERE AAAA.id IS NULL; </code></pre> <p>Here is the output for 57</p> <pre><code>mysql&gt; select BBBB.* from (select id,sums FROM (select A.id,A.sums from -&gt; (select id,(select sum(qty1+qty2+qty3+qty4) from mytable BB -&gt; where BB.id&lt;=AA.id) sums from mytable AA order by id) A -&gt; INNER JOIN (SELECT 57 mylimit) B ON A.sums &lt;= B.mylimit) AAA -&gt; UNION -&gt; (select A.id,A.sums from (select id,(select sum(qty1+qty2+qty3+qty4) -&gt; from mytable BB where BB.id&lt;=AA.id) sums from mytable AA order by id) A -&gt; where A.sums=(select min(A.sums) sums from (select id, -&gt; (select sum(qty1+qty2+qty3+qty4) from mytable BB where BB.id&lt;=AA.id) sums -&gt; from mytable AA order by id) A INNER JOIN (SELECT 57 mylimit) B -&gt; ON A.sums &gt;= B.mylimit))) AAAA JOIN mytable BBBB USING (id); +----+------+------+------+------+ | id | qty1 | qty2 | qty3 | qty4 | +----+------+------+------+------+ | 1 | 0 | 0 | 10 | 20 | | 2 | 1.5 | 0 | 7.5 | 18 | +----+------+------+------+------+ 2 rows in set (0.00 sec) mysql&gt; select BBBB.* from mytable BBBB LEFT JOIN -&gt; (select id,sums FROM (select A.id,A.sums from ( -&gt; select id,(select sum(qty1+qty2+qty3+qty4) -&gt; from mytable BB where BB.id&lt;=AA.id) sums -&gt; from mytable AA order by id) A INNER JOIN -&gt; (SELECT 57 mylimit) B ON A.sums &lt;= B.mylimit) AAA -&gt; UNION -&gt; (select A.id,A.sums from (select id, -&gt; (select sum(qty1+qty2+qty3+qty4) from mytable BB -&gt; where BB.id&lt;=AA.id) sums from mytable AA order by id) A -&gt; where A.sums=(select min(A.sums) sums from ( -&gt; select id,(select sum(qty1+qty2+qty3+qty4) from mytable BB -&gt; where BB.id&lt;=AA.id) sums from mytable AA order by id) A -&gt; INNER JOIN (SELECT 57 mylimit) B ON A.sums &gt;= B.mylimit))) AAAA -&gt; USING (id) WHERE AAAA.id IS NULL; +----+------+------+------+------+ | id | qty1 | qty2 | qty3 | qty4 | +----+------+------+------+------+ | 3 | 1 | 2 | 7.5 | 18 | | 4 | 0 | 0.5 | 5 | 13 | +----+------+------+------+------+ 2 rows in set (0.00 sec) mysql&gt; </code></pre> <p>Here is the output for 50</p> <pre><code>mysql&gt; select BBBB.* from (select id,sums FROM (select A.id,A.sums from -&gt; (select id,(select sum(qty1+qty2+qty3+qty4) from mytable BB -&gt; where BB.id&lt;=AA.id) sums from mytable AA order by id) A -&gt; INNER JOIN (SELECT 50 mylimit) B ON A.sums &lt;= B.mylimit) AAA -&gt; UNION -&gt; (select A.id,A.sums from (select id,(select sum(qty1+qty2+qty3+qty4) -&gt; from mytable BB where BB.id&lt;=AA.id) sums from mytable AA order by id) A -&gt; where A.sums=(select min(A.sums) sums from (select id, -&gt; (select sum(qty1+qty2+qty3+qty4) from mytable BB where BB.id&lt;=AA.id) sums -&gt; from mytable AA order by id) A INNER JOIN (SELECT 50 mylimit) B -&gt; ON A.sums &gt;= B.mylimit))) AAAA JOIN mytable BBBB USING (id); +----+------+------+------+------+ | id | qty1 | qty2 | qty3 | qty4 | +----+------+------+------+------+ | 1 | 0 | 0 | 10 | 20 | | 2 | 1.5 | 0 | 7.5 | 18 | +----+------+------+------+------+ 2 rows in set (0.00 sec) mysql&gt; select BBBB.* from mytable BBBB LEFT JOIN -&gt; (select id,sums FROM (select A.id,A.sums from ( -&gt; select id,(select sum(qty1+qty2+qty3+qty4) -&gt; from mytable BB where BB.id&lt;=AA.id) sums -&gt; from mytable AA order by id) A INNER JOIN -&gt; (SELECT 50 mylimit) B ON A.sums &lt;= B.mylimit) AAA -&gt; UNION -&gt; (select A.id,A.sums from (select id, -&gt; (select sum(qty1+qty2+qty3+qty4) from mytable BB -&gt; where BB.id&lt;=AA.id) sums from mytable AA order by id) A -&gt; where A.sums=(select min(A.sums) sums from ( -&gt; select id,(select sum(qty1+qty2+qty3+qty4) from mytable BB -&gt; where BB.id&lt;=AA.id) sums from mytable AA order by id) A -&gt; INNER JOIN (SELECT 50 mylimit) B ON A.sums &gt;= B.mylimit))) AAAA -&gt; USING (id) WHERE AAAA.id IS NULL; +----+------+------+------+------+ | id | qty1 | qty2 | qty3 | qty4 | +----+------+------+------+------+ | 3 | 1 | 2 | 7.5 | 18 | | 4 | 0 | 0.5 | 5 | 13 | +----+------+------+------+------+ 2 rows in set (0.01 sec) mysql&gt; </code></pre> <p>Please remember to set the number for mylimit in the <code>(SELECT 50 mylimit)</code> subquery twice each.</p> <p>Please tell I got this one...</p>
 

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