
A second attempt can succeed after a failure. A release sign-off wants the final observed attempt for each build-device pair while the flakiness report must keep both. Each row is an individual test attempt. A failed attempt can still emit a duration. A missing duration means the measurement is unavailable; it does not assert that the device completed immediately.
Build the small reproduction
The fixture uses a fresh SQLite database. Its small size allows the result to be checked by hand before the query is applied to an export with thousands of rows. The unit of observation matters here because a query can execute cleanly and still answer a different editorial or operational question.
CREATE TABLE runs (
id INTEGER PRIMARY KEY,
build TEXT,
device TEXT,
attempt INTEGER,
ms INTEGER,
passed INTEGER
);
INSERT INTO runs VALUES
(1, 'r1', 'lite', 1, 800, 1),
(2, 'r1', 'pro', 1, 610, 1),
(3, 'r2', 'lite', 1, 1220, 0),
(4, 'r2', 'lite', 2, 880, 1),
(5, 'r2', 'pro', 1, 640, 1),
(6, 'r3', 'lite', 1, NULL, 0);Run the query and inspect its grain
WITH ranked AS (SELECT *,row_number() OVER(PARTITION BY build,device
ORDER BY attempt DESC,id DESC) AS n
FROM runs) SELECT build,device,attempt,passed
FROM ranked
WHERE n=1
ORDER BY build,device;| build | device | attempt | passed |
|---|---|---|---|
| r1 | lite | 1 | 1 |
| r1 | pro | 1 | 1 |
| r2 | lite | 2 | 1 |
| r2 | pro | 1 | 1 |
| r3 | lite | 1 | 0 |
An ID tie breaker makes the selection stable if two recorded attempts reuse a number. Do not discard the earlier records from the source table.
What changes with one more record?
Append this single row to the same fixture and rerun the query. Its named fields are id = 7, build = r3, device = pro, attempt = 1, ms = 710, passed = 1. Keep the other rows intact so a comparison isolates the effect of this one insert.
INSERT INTO runs VALUES (7, 'r3', 'pro', 1, 710, 1);The original query returned 5 rows. After the insert it returns 6. One resulting row is ["r3", "pro", 1, 1]. 5 earlier output rows still appear unchanged.
Before exporting the result, document what one displayed row represents and which fields decide its order. Look for the same shape of input in an anonymized sample from the application. A query validated only against an empty development database cannot show what happens when a record is missing, repeated or tied with another record.
SQLite reference for row_number · SQLite SQL syntax reference
Sources and further reading
Cover photograph: 2019 Rozebrane dyski twarde by Jacek Halicki. CC BY-SA 4.0. Cropped and typeset for this cover. The photograph illustrates the topic. This image adaptation uses the same share-alike license.
