
A dashboard provides two reasons an asset might require attention. A published image legitimately belongs to both feeds and the reviewer wants to see both reasons. Each row names an asset at one folder location. A repeated basename does not establish identical bytes. The size is an integer estimate in KiB; a null means the size has yet to be measured.
Start from a known dataset
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 assets (
id INTEGER PRIMARY KEY,
kind TEXT,
folder TEXT,
name TEXT,
kib INTEGER,
live INTEGER
);
INSERT INTO assets VALUES
(1, 'image', 'issue-1', 'cover.jpg', 120, 1),
(2, 'text', 'issue-1', 'intro.txt', 4, 1),
(3, 'image', 'issue-2', 'cover.webp', 80, 0),
(4, 'audio', 'issue-2', 'theme.ogg', 300, 1),
(5, 'text', 'issue-2', 'notes.md', NULL, 0),
(6, 'image', 'shared', 'icon.svg', 2, 1);Calculate the report
SELECT name,'published' AS source
FROM assets
WHERE live=1 UNION ALL SELECT name,'image review'
FROM assets
WHERE kind='image'
ORDER BY 1,2;| name | source |
|---|---|
| cover.jpg | image review |
| cover.jpg | published |
| cover.webp | image review |
| icon.svg | image review |
| icon.svg | published |
| intro.txt | published |
| theme.ogg | published |
UNION ALL preserves rows. A later deduplication step must specify whether its unit is an asset, an event or a reason.
What changes with one more record?
Append this single row to the same fixture and rerun the query. Its named fields are id = 7, kind = audio, folder = issue-3, name = theme.ogg, kib = 300, live = 0. Keep the other rows intact so a comparison isolates the effect of this one insert.
INSERT INTO assets VALUES (7, 'audio', 'issue-3', 'theme.ogg', 300, 0);The original query returned 7 rows. After the insert it returns 7. Every output row remains the same. The extra record is a useful negative test because adding data to the table has not changed the reported observation. 7 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 UNION ALL · SQLite SQL syntax reference
Sources and further reading
Cover photograph: HP-HP9000-375-Workstation-Modules 23 by Thomas Schanz. CC BY-SA 3.0. Cropped and typeset for this cover. The photograph illustrates the topic. This image adaptation uses the same share-alike license.
