How do you keep exactly one latest run per experiment?
ROW_NUMBER numbers rows in each partition. An outer query can then select the row numbered one.
SQLite example and interpretation
Group A ran twice. Pick the latest time and use a run ID to break a timestamp tie. Window ordering alone does not order the final table.
WITH r(g,t,id) AS (VALUES('A',3,1),('A',3,2),('B',1,3)), n AS (SELECT *,row_number() OVER(PARTITION BY g ORDER BY t DESC,id DESC) k FROM r) SELECT g,id FROM n WHERE k=1 ORDER BY g;Result [["A", 2], ["B", 3]]
中文
每个实验组如何只保留最后一次运行?
ROW_NUMBER 在每个分区里依序编号,外层查询可筛出编号一的行。
SQLite 示例与适用条件
组 A 重跑两次,取时间最大的那次;有同一时间时用运行编号打破平局。窗口排序本身不会限定整张输出表的顺序。
WITH r(g,t,id) AS (VALUES('A',3,1),('A',3,2),('B',1,3)), n AS (SELECT *,row_number() OVER(PARTITION BY g ORDER BY t DESC,id DESC) k FROM r) SELECT g,id FROM n WHERE k=1 ORDER BY g;结果 [["A", 2], ["B", 3]]