合計が 260 以上の最も古いレコードの最小セットを選択できるようにするには、SQL が必要です。
次のように定義されたデータベースにデータがあります。
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 |
+----+----------+--------+---------------------+---------+