Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select some rows in a table so that they sum up to certain value

Tags:

select

mysql

sum

How can I select just some rows in the following table so that they sum up to certain value?

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

Let's say, the top value I want is 57...

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:

id | qty1 | qty2 | qty3 | qty4
------------------------------
1  | 0.0  | 0.0  | 10   | 20
2  | 1.5  | 0.0  | 7.5  | 18

Because 10+20+1.5+7.5+18 = 57, so I discard rows 3 & 4...

Now I wish that the top value is 50, then I should get:

id | qty1 | qty2 | qty3 | qty4
------------------------------
1  | 0.0  | 0.0  | 10   | 20
2  | 1.5  | 0.0  | 7.5  | 11

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...)

What if I would like the complement of this? I mean, the other two rows I didn't got in the last result.

First case:

id | qty1 | qty2 | qty3 | qty4
------------------------------
3  | 1.0  | 2.0  | 7.5  | 18
4  | 0.0  | 0.5  | 5    | 13

Second case:

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

(If the second case is too complicated, how about obtaining:

id | qty1 | qty2 | qty3 | qty4
------------------------------
1  | 0.0  | 0.0  | 10   | 20

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:

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

)

like image 757
Javier Novoa C. Avatar asked Mar 01 '12 23:03

Javier Novoa C.


4 Answers

The simplified option in parentheses is not too bad:

SELECT foo1.*
  FROM foo AS foo1
  JOIN foo AS foo2
    ON foo2.id <= foo1.id
 GROUP
    BY foo1.id
HAVING SUM(foo2.qty1 + foo2.qty2 + foo2.qty3 + foo2.qty4) <= 57
;

(You didn't mention the table's name, so I went with foo.)

The complement would be:

SELECT *
  FROM foo
 WHERE id NOT IN
        ( SELECT foo1.id
            FROM foo AS foo1
            JOIN foo AS foo2
              ON foo2.id <= foo1.id
           GROUP
              BY foo1.id
          HAVING SUM(foo2.qty1 + foo2.qty2 + foo2.qty3 + foo2.qty4) <= 57
        )
;

The unparenthesized option is much trickier; it's doable, but you'd be much better off using a stored procedure.

like image 141
ruakh Avatar answered Oct 24 '22 10:10

ruakh


Let's put it this way: If SQL was a religion I'd go to hell for providing this solution. SQL is not meant to solve this kind of problems, so any solution would be horrible. Mine is no exception :)

set @limitValue := 50;
select id, newQty1, newQty2, newQty3, newQty4 from (
  select id,
  if(@limitValue - qty1 > 0, qty1, greatest(@limitValue, 0)) newQty1,
  @limitValue := @limitValue - qty1 Total1,
  if(@limitValue - qty2 > 0, qty2, greatest(@limitValue, 0)) newQty2,
  @limitValue := @limitValue - qty2 Total2,
  if(@limitValue - qty3 > 0, qty3, greatest(@limitValue, 0)) newQty3,
  @limitValue := @limitValue - qty3 Total3,
  if(@limitValue - qty4 > 0, qty4, greatest(@limitValue, 0)) newQty4,
  @limitValue := @limitValue - qty4 Total4
  from (
    select id, qty1, qty2, qty3, qty4,
      @rowTotal < @limitValue Useful,
      @previousRowTotal := @rowTotal PreviousRowTotal,
      @rowTotal := @rowTotal + qty1 + qty2 + qty3 + qty4 AllRowsTotal,
      @rowTotal - @previousRowTotal CurrentRowTotal 
    from t,
    (select @rowTotal := 0, @previousRowTotal := 0) S1
  ) MarkedUseful
  where useful = 1
) Final

For the provided data, this results in:

+----+---------+---------+---------+---------+
| ID | NEWQTY1 | NEWQTY2 | NEWQTY3 | NEWQTY4 |
+----+---------+---------+---------+---------+
|  1 | 0       |       0 | 10      |      20 |
|  2 | 1.5     |       0 | 7.5     |      11 |
+----+---------+---------+---------+---------+

And the complement:

set @limitValue := 50;
select t1.id,
  coalesce(t1.qty1 - newQty1, t1.qty1) newQty1,
  coalesce(t1.qty2 - newQty2, t1.qty2) newQty2,
  coalesce(t1.qty3 - newQty3, t1.qty3) newQty3,
  coalesce(t1.qty4 - newQty4, t1.qty4) newQty4
from t t1 left join (
    select id,
    if(@limitValue - qty1 > 0, qty1, greatest(@limitValue, 0)) newQty1,
    @limitValue := @limitValue - qty1 Total1,
    if(@limitValue - qty2 > 0, qty2, greatest(@limitValue, 0)) newQty2,
    @limitValue := @limitValue - qty2 Total2,
    if(@limitValue - qty3 > 0, qty3, greatest(@limitValue, 0)) newQty3,
    @limitValue := @limitValue - qty3 Total3,
    if(@limitValue - qty4 > 0, qty4, greatest(@limitValue, 0)) newQty4,
    @limitValue := @limitValue - qty4 Total4
    from (
      select id, qty1, qty2, qty3, qty4,
        @rowTotal < @limitValue Useful,
        @previousRowTotal := @rowTotal PreviousRowTotal,
        @rowTotal := @rowTotal + qty1 + qty2 + qty3 + qty4 AllRowsTotal,
        @rowTotal - @previousRowTotal CurrentRowTotal 
      from t,
      (select @rowTotal := 0, @previousRowTotal := 0) S1
    ) MarkedUseful
    where useful = 1
) Final
on t1.id = final.id
where Total1 < 0 or Total2 < 0 or Total3 < 0 or Total4 < 0 or final.id is null

For the provided data, this results in:

+----+---------+---------+---------+---------+
| ID | NEWQTY1 | NEWQTY2 | NEWQTY3 | NEWQTY4 |
+----+---------+---------+---------+---------+
|  2 |       0 | 0       | 0       |       7 |
|  3 |       1 | 2       | 7.5     |      18 |
|  4 |       0 | 0.5     | 5       |      13 |
+----+---------+---------+---------+---------+

Enjoy!

like image 30
Mosty Mostacho Avatar answered Oct 24 '22 10:10

Mosty Mostacho


You should adjust only the @limit variable initialization in the init subquery. The first query outputs data up to limit, the secnd query outputs its complement.

SELECT
  id,
  @qty1 as qty1,
  @qty2 as qty2,
  @qty3 as qty3,
  @qty4 as qty4
FROM quantities q,
  (SELECT @qty1:=0.0, @qty2:=0.0,
          @qty3:=0.0, @qty4:=0.0,
          @limit:=50.0) init
WHERE
  IF(@limit > 0,
     GREATEST(1,
       IF(@limit-qty1 >=0,
          @limit:=(@limit-(@qty1:=qty1)),
          @qty1:=@limit + LEAST(@limit, @limit:=0)),
       IF(@limit-qty2 >=0,
          @limit:=(@limit-(@qty2:=qty2)),
          @qty2:=@limit + LEAST(@limit, @limit:=0)),
       IF(@limit-qty3 >=0,
          @limit:=(@limit-(@qty3:=qty3)),
          @qty3:=@limit + LEAST(@limit, @limit:=0)),
       IF(@limit-qty4 >=0,
          @limit:=(@limit-(@qty4:=qty4)),
          @qty4:=@limit + LEAST(@limit, @limit:=0))),0)
;

The complement:

SELECT
  id,
  IF(qty1=@qty1, qty1, qty1-@qty1) as qty1,
  IF(qty2=@qty2, qty2, qty2-@qty2) as qty2,
  IF(qty3=@qty3, qty3, qty3-@qty3) as qty3,
  IF(qty4=@qty4, qty4, qty4-@qty4) as qty4
FROM quantities q,
  (SELECT @qty1:=0.0, @qty2:=0.0,
          @qty3:=0.0, @qty4:=0.0,
          @limit:=50.0) init
WHERE
  IF(
    LEAST(
      IF(@limit-qty1 >=0,
         @limit:=(@limit-(@qty1:=qty1)),
         @qty1:=@limit + LEAST(@limit, @limit:=0)),
      IF(@limit-qty2 >=0,
         @limit:=(@limit-(@qty2:=qty2)),
         @qty2:=@limit + LEAST(@limit, @limit:=0)),
      IF(@limit-qty3 >=0,
         @limit:=(@limit-(@qty3:=qty3)),
         @qty3:=@limit + LEAST(@limit, @limit:=0)),
      IF(@limit-qty4 >=0,
         @limit:=(@limit-(@qty4:=qty4)),
         @qty4:=@limit + LEAST(@limit, @limit:=0)),
      @limit), 0, 1)
;
like image 4
newtover Avatar answered Oct 24 '22 10:10

newtover


Let's load your sample data from the question

mysql> drop database if exists javier;
Query OK, 1 row affected (0.02 sec)

mysql> create database javier;
Query OK, 1 row affected (0.01 sec)

mysql> use javier
Database changed
mysql> create table mytable
    -> (
    ->     id int not null auto_increment,
    ->     qty1 float,qty2 float,qty3 float,qty4 float,
    ->     primary key (id)
    -> );
Query OK, 0 rows affected (0.08 sec)

mysql> insert into mytable (qty1,qty2,qty3,qty4) values
    -> ( 0.0 , 0.0 , 10  , 20 ),( 1.5 , 0.0 , 7.5 , 18 ),
    -> ( 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> 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>

FINAL QUERY THAT FULLY WORKS

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<=AA.id) sums from mytable AA order by id) A 
INNER JOIN (SELECT 50 mylimit) B ON A.sums <= B.mylimit) AAA
UNION
(select A.id,A.sums from (select id,(select sum(qty1+qty2+qty3+qty4)  
from mytable BB where BB.id<=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<=AA.id) sums 
from mytable AA order by id) A INNER JOIN (SELECT 50 mylimit) B 
ON A.sums >= B.mylimit))) AAAA JOIN mytable BBBB USING (id);

FINAL QUERY COMPLEMENT THAT FULLY WORKS

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<=AA.id) sums 
from mytable AA order by id) A INNER JOIN
(SELECT 50 mylimit) B ON A.sums <= B.mylimit) AAA
UNION
(select A.id,A.sums from (select id, 
(select sum(qty1+qty2+qty3+qty4) from mytable BB 
where BB.id<=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<=AA.id) sums from mytable AA order by id) A 
INNER JOIN (SELECT 50 mylimit) B ON A.sums >= B.mylimit))) AAAA
USING (id) WHERE AAAA.id IS NULL;

Here is the output for 57

mysql>     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<=AA.id) sums from mytable AA order by id) A
    ->     INNER JOIN (SELECT 57 mylimit) B ON A.sums <= B.mylimit) AAA
    ->     UNION
    ->     (select A.id,A.sums from (select id,(select sum(qty1+qty2+qty3+qty4)
    ->     from mytable BB where BB.id<=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<=AA.id) sums
    ->     from mytable AA order by id) A INNER JOIN (SELECT 57 mylimit) B
    ->     ON A.sums >= 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>     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<=AA.id) sums
    ->     from mytable AA order by id) A INNER JOIN
    ->     (SELECT 57 mylimit) B ON A.sums <= B.mylimit) AAA
    ->     UNION
    ->     (select A.id,A.sums from (select id,
    ->     (select sum(qty1+qty2+qty3+qty4) from mytable BB
    ->     where BB.id<=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<=AA.id) sums from mytable AA order by id) A
    ->     INNER JOIN (SELECT 57 mylimit) B ON A.sums >= B.mylimit))) AAAA
    ->     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>

Here is the output for 50

mysql>     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<=AA.id) sums from mytable AA order by id) A
    ->     INNER JOIN (SELECT 50 mylimit) B ON A.sums <= B.mylimit) AAA
    ->     UNION
    ->     (select A.id,A.sums from (select id,(select sum(qty1+qty2+qty3+qty4)
    ->     from mytable BB where BB.id<=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<=AA.id) sums
    ->     from mytable AA order by id) A INNER JOIN (SELECT 50 mylimit) B
    ->     ON A.sums >= 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>     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<=AA.id) sums
    ->     from mytable AA order by id) A INNER JOIN
    ->     (SELECT 50 mylimit) B ON A.sums <= B.mylimit) AAA
    ->     UNION
    ->     (select A.id,A.sums from (select id,
    ->     (select sum(qty1+qty2+qty3+qty4) from mytable BB
    ->     where BB.id<=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<=AA.id) sums from mytable AA order by id) A
    ->     INNER JOIN (SELECT 50 mylimit) B ON A.sums >= B.mylimit))) AAAA
    ->     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>

Please remember to set the number for mylimit in the (SELECT 50 mylimit) subquery twice each.

Please tell I got this one...

like image 4
RolandoMySQLDBA Avatar answered Oct 24 '22 09:10

RolandoMySQLDBA