
A diagnostic view should keep each recorded reading session while displaying a title-specific average beside it. A grouped aggregate would collapse the individual sessions. The table records a reading session, not the current state of a book. One reader may revisit several sections. Finished marks this session’s section completion and does not imply that the whole book was finished.
The rows behind the report
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 reads (
id INTEGER PRIMARY KEY,
reader TEXT,
book TEXT,
section INTEGER,
seconds INTEGER,
finished INTEGER
);
INSERT INTO reads VALUES
(1, 'Mira', 'Atlas', 1, 60, 1),
(2, 'Mira', 'Atlas', 2, 130, 1),
(3, 'Ira', 'Atlas', 1, 95, 1),
(4, 'Ira', 'Orbit', 1, 22, 0),
(5, 'Mira', 'Orbit', 1, 75, 1),
(6, 'Dev', 'Orbit', 2, NULL, 0);What the query returns
SELECT id,book,seconds,round(avg(seconds) OVER(PARTITION BY book),1) AS book_mean
FROM reads
ORDER BY id;| id | book | seconds | book_mean |
|---|---|---|---|
| 1 | Atlas | 60 | 95.0 |
| 2 | Atlas | 130 | 95.0 |
| 3 | Atlas | 95 | 95.0 |
| 4 | Orbit | 22 | 48.5 |
| 5 | Orbit | 75 | 48.5 |
| 6 | Orbit | NULL | 48.5 |
A null reading duration stays visible as an individual row and contributes no numeric input to AVG. The average can still appear beside that row.
What changes with one more record?
Append this single row to the same fixture and rerun the query. Its named fields are id = 7, reader = Dev, book = Atlas, section = 1, seconds = 90, finished = 1. Keep the other rows intact so a comparison isolates the effect of this one insert.
INSERT INTO reads VALUES (7, 'Dev', 'Atlas', 1, 90, 1);The original query returned 6 rows. After the insert it returns 7. One resulting row is [1, "Atlas", 60, 93.8]. The former row [1, "Atlas", 60, 95.0] no longer appears in exactly that form. 3 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 average over partition · SQLite SQL syntax reference
Sources and further reading
Cover photograph: Metal Particle Tape (8743839231) by Government & Heritage Library, State Library of NC from Raleigh, NC, United States. CC BY 2.0. Cropped and typeset for this cover. The photograph illustrates the topic.
