Supposing a table created thus:
CREATE TABLE Files (Id INTEGER PRIMARY KEY, FileName TEXT, CreationDate DATE, Size INTEGER);
To get the running sum, use the following query:
SELECT f1.id AS FileId, sum(f2.size) AS RunningSumSize
FROM file f1 INNER JOIN file f2
ON f1.createdDate<=f2.createdDate
GROUP BY FileId
ORDER BY RunningSumSize DESC;
To delete the file ID's above the threshold:
DELETE FROM File WHERE Id IN
(SELECT FileId FROM
(SELECT f1.id AS FileId, sum(f2.size) AS RunningSumSize
FROM file f1 INNER JOIN file f2
ON f1.createdDate<=f2.createdDate
GROUP by FileId
ORDER by RunningSumSize DESC)
WHERE RunningSumSize > :ThresholdSize:);
Note: The order by
is optional.