You need to do cumulative judgment,due to your mysql version didn't support window function so the solution will be a little hard to read,becuase you need to write subquery instead of window function.
make the cumulative number by Id
column, then get the MIN(ID)
in subquery, when Id = 3
the value will greater than 0.8
.
finally get the Id small and equal the MIN(ID)
you will get your expect result.
CREATE TABLE trade(
id INT,
value FLOAT
);
INSERT INTO trade VALUES(1,0.1);
INSERT INTO trade VALUES(2,0.5);
INSERT INTO trade VALUES(3,0.9);
INSERT INTO trade VALUES(4,0.3);
Query 1:
SELECT COUNT(*)
FROM trade t1
WHERE t1.id <= (
SELECT MIN(ID) FROM (
SELECT ID,(
SELECT SUM(tt.value)
FROM trade tt
WHERE tt.id <= t1.id
) total
FROM trade t1
) t1
where total > 0.8
)
Results:
| COUNT(*) |
|----------|
| 3 |