SQL选择最小结果集,其总和涵盖给定值

时间:2013-01-25 21:21:36

标签: mysql sql

我需要SQL才能选择总和> = 260的最小记录集。

我在数据库中定义的数据是这样的:

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');

我试过了:

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

但是该查询只返回我想要的集合的第一行。所需的结果集如下所示:

+----+----------+--------+---------------------+---------+
| 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 |
+----+----------+--------+---------------------+---------+

1 个答案:

答案 0 :(得分:3)

试试这个

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