Cross-connect solutions such as Diomidis Spinellis suggested taking O (N ^ 2) time. Recursive CTE can run faster if you can handle confusing code.
This gives the same result as his.
WITH RECURSIVE running(id, name, gdppc, rt) AS ( SELECT row1._rowid_, row1.name, row1.gdppc, COALESCE(row1.gdppc,0) FROM gdppc AS row1 WHERE row1._rowid_ = ( SELECT a._rowid_ FROM gdppc AS a ORDER BY a.gdppc, a.name, a._rowid_ LIMIT 1) UNION ALL SELECT row_n._rowid_, row_n.name, row_n.gdppc, COALESCE(row_n.gdppc,0)+running.rt FROM gdppc AS row_n INNER JOIN running ON row_n._rowid_ = ( SELECT a._rowid_ FROM gdppc AS a WHERE (a.gdppc, a.name, a._rowid_) > (running.gdppc, running.name, running.id) ORDER BY a.gdppc, a.name, a._rowid_ LIMIT 1)) SELECT running.name, running.gdppc, running.rt FROM running;
Ordering and comparison allow for duplicates, COALESCE here to ignore NULL.
If you have a good index, it should be O (N log N). Since SQLite does not support cursors, the O (N) solution probably does not exist without relying on an external application.
relatively_random
source share