
Two imports contain four records, but a combined SQLite report returns only two rows. Before blaming the import, I would inspect the compound query. UNION removes duplicate rows from its result. If you selected only a category or status, different events may look identical after that projection.
Start with a fixture where repeated values are intentional
Run this in a new, disposable SQLite database. The two tables represent separate feeds. Repeated statuses are valid observations, not mistakes to delete.
CREATE TABLE east_events (id INTEGER, status TEXT);
CREATE TABLE west_events (id INTEGER, status TEXT);
INSERT INTO east_events VALUES (1, 'queued'), (2, 'queued');
INSERT INTO west_events VALUES (1, 'queued'), (2, 'done');SELECT status FROM east_events
UNION
SELECT status FROM west_events
ORDER BY status;
-- done
-- queuedThe selected result has one column, so the three queued values collapse into one row. SQLite does not know that they came from three business events. It compares the result rows produced by the SELECT statements, not the unselected columns in the original tables.
Keep every observation with UNION ALL
SELECT status FROM east_events
UNION ALL
SELECT status FROM west_events
ORDER BY status;
-- done
-- queued
-- queued
-- queuedThis preserves all four input rows. That makes it a suitable starting point for an event-count report where repeated statuses contribute separately. It does not prove the feeds contain no accidental duplicate events. If both feeds can report the same event, define that event key and a reconciliation rule rather than treating every repeated status as an error.
Preserve identity before deciding what to deduplicate
SELECT 'east' AS source, id, status FROM east_events
UNION ALL
SELECT 'west' AS source, id, status FROM west_events
ORDER BY source, id;The source column distinguishes IDs that are only unique within each feed. In this fixture, the pair of source and id identifies each observation. Using id alone across both tables would conflate two separate namespaces. In a real system, check the producer contract before calling any column combination a stable key.
Adding a source column also changes how UNION deduplication behaves: rows with different source values are no longer equal across every selected column. That is not a harmless presentation-only change. Decide whether the report needs source-specific observations or one reconciled entity, and document that choice.
Keep the query contract explicit
Each SELECT in a SQLite compound query must return the same number of columns. Match their meaning and position too: two columns can line up syntactically while representing different things. Prefer explicit column lists over SELECT * when the feeds have independently changing schemas.
Without ORDER BY, result order is undefined. Do not rely on whichever table appears first to provide the first rows. The examples place ORDER BY at the end so it applies to the combined result. For a stable order where sort values can tie, include the needed tie-breakers in the output and ordering.
One edge case matters when auditing missing rows: SQLite considers NULL equal to another NULL for duplicate removal in compound SELECT results. Two rows whose selected values are all the same, including NULLs, can collapse under UNION. Text collation also affects comparison, so do not generalize this fixture into a universal case-sensitive matching rule.
Check counts and keys before changing the report
I tested these queries in an in-memory SQLite fixture. The UNION status query returned two rows; UNION ALL returned four; the source-tagged query retained four distinct source-and-id pairs. I also checked that two selected NULL values become one UNION row and two UNION ALL rows. These are correctness checks on small synthetic data, not production benchmarks.
My decision rule is simple: preserve observations with UNION ALL, and use UNION only when removing equal projected rows is the intended result. Neither operator repairs a wrong join, resolves conflicting versions or chooses the latest event. Compare representative keys and counts before replacing the query used by a downstream report.
Comments
Post a Comment