sqlite按总和过滤 [英] sqlite filtering by sum
本文介绍了sqlite按总和过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含 1000 条记录的数据库,每行包含一个文件名和一个文件大小.如果所有文件大小的总和超过特定限制,则:- 我需要创建一个 sql 查询来删除所有剩余的行,从最旧到最新
I have a database with 1000 records containing a file name and a file size on each row. If the SUM of all file sizes exceeds a specific limit, then: - I need to create a sql query to delete all the remaining rows, from oldest to newest
有什么建议吗?谢谢
推荐答案
假设一个表是这样创建的:
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;
要删除超过阈值的文件 ID:
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:);
注意:order by
是可选的.
这篇关于sqlite按总和过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文