SQL Select for the minimum result set that has a sum that covers a given value

I need SQL to be able to select the minimum set of oldest records that have a sum> 260.

I have data in a database defined as such:

CREATE TABLE record (
  id bigint(20) NOT NULL AUTO_INCREMENT,
  function varchar(10) NOT NULL,
  amount decimal(4,2) NOT NULL,
  timestamp datetime NOT NULL,
  journal int NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB;

insert into record(journal,function, amount, timestamp) values (1, 'debit', 81.15, '2013-01-01 01:01:02');
insert into record(journal,function, amount, timestamp) values (2, 'debit', 23.33, '2013-01-01 01:01:04');
insert into record(journal,function, amount, timestamp) values (1, 'debit', 68.19, '2013-01-01 01:01:06');
insert into record(journal,function, amount, timestamp) values (2, 'debit', 29.93, '2013-01-01 01:01:08');
insert into record(journal,function, amount, timestamp) values (1, 'debit', 71.01, '2013-01-01 01:01:10');
insert into record(journal,function, amount, timestamp) values (2, 'debit', 71.62, '2013-01-01 01:01:12');
insert into record(journal,function, amount, timestamp) values (1, 'debit', 88.94, '2013-01-01 01:01:14');
insert into record(journal,function, amount, timestamp) values (2, 'debit', 82.72, '2013-01-01 01:01:16');
insert into record(journal,function, amount, timestamp) values (1, 'debit', 44.26, '2013-01-01 01:01:18');
insert into record(journal,function, amount, timestamp) values (2, 'debit', 69.04, '2013-01-01 01:01:20');
insert into record(journal,function, amount, timestamp) values (1, 'debit', 96.83, '2013-01-01 01:01:22');
insert into record(journal,function, amount, timestamp) values (1, 'credit', 81.27, '2013-01-01 01:01:01');
insert into record(journal,function, amount, timestamp) values (2, 'credit', 30.86, '2013-01-01 01:01:03');
insert into record(journal,function, amount, timestamp) values (1, 'credit', 95.62, '2013-01-01 01:01:05');
insert into record(journal,function, amount, timestamp) values (2, 'credit', 16.20, '2013-01-01 01:01:07');
insert into record(journal,function, amount, timestamp) values (1, 'credit', 50.28, '2013-01-01 01:01:09');
insert into record(journal,function, amount, timestamp) values (2, 'credit', 44.42, '2013-01-01 01:01:11');
insert into record(journal,function, amount, timestamp) values (1, 'credit', 43.83, '2013-01-01 01:01:13');
insert into record(journal,function, amount, timestamp) values (2, 'credit', 10.40, '2013-01-01 01:01:15');
insert into record(journal,function, amount, timestamp) values (1, 'credit', 79.35, '2013-01-01 01:01:17');
insert into record(journal,function, amount, timestamp) values (1, 'credit', 79.02, '2013-01-01 01:01:19');
insert into record(journal,function, amount, timestamp) values (2, 'credit', 82.31, '2013-01-01 01:01:21');

      

I tried:

mysql> select * 
       from record 
       where journal=1 and function='credit' 
       having sum(amount) >= 260 
       order by timestamp asc;

      

However, this query only returns the first row of the set that I want. The desired set of results will look like this:

+----+----------+--------+---------------------+---------+
| id | function | amount | timestamp           | journal |
+----+----------+--------+---------------------+---------+
| 27 | credit   |  81.27 | 2013-01-01 01:01:01 |       1 |
| 29 | credit   |  95.62 | 2013-01-01 01:01:05 |       1 |
| 31 | credit   |  50.28 | 2013-01-01 01:01:09 |       1 |
| 33 | credit   |  43.83 | 2013-01-01 01:01:13 |       1 |
+----+----------+--------+---------------------+---------+

      

+3


source to share


1 answer


try it

SELECT ID, FUNCTION,AMOUNT, timestamp, journal 
FROM (
    select r.*,
    @sum := if(@journal = journal,@sum,0) + amount as SumAmount,
    @journal:=journal,
    @start := if(@sum > 260,@start + 1,0)  as start
    from record r, 
    (SELECT @journal := 0, @sum := 0, @start = 0) S 
    where journal=1 and function='credit' 
    order by timestamp asc
) V 
WHERE start <=1;

      



SQL DEMO HERE

+3


source







All Articles