Data quality and lineage
Overview
Data quality is fitness for the intended use, including completeness, validity, uniqueness, consistency and timeliness. Lineage describes where data came from and which transformations produced it. Keep source identifiers, batch identifiers, rules and transformation versions to trace errors.
First example
SELECT batch,order_id,COUNT(*) FROM de_stage WHERE order_id IS NOT NULL GROUP BY batch,order_id HAVING COUNT(*)>1;Observed SQLite result (rows in JSON):
[ [ 7, 2, 2 ]]How it works
Constraints protect the curated target but cannot detect every business-quality issue. A staging table can intentionally admit bad rows for quarantine. Count input, accepted, duplicates and rejects with nonoverlapping categories; reconcile totals at the same grain. The duplicate query below identifies groups, not the number of excess rows. A lineage record should connect source location and batch to target key and transformation version; it is not automatically complete just because an order ID matches.
Practical example
SELECT batch,line,order_id FROM de_stage WHERE order_id=2 ORDER BY batch,line;Observed SQLite result (rows in JSON):
[ [ 7, 2, 2 ], [ 7, 3, 2 ]]Practice and mistakes
Practice: identify duplicate order 2 and list its source lines. Hint: group by batch and order_id; then join back to stage. Check duplicate group count 2 and source lines 2 and 3. Mistake: deleting all rows for a duplicated key without investigation.
PostgreSQL 17 example — NOT RUN
SELECT id,COUNT(*) FROM (VALUES(1),(2),(2)) AS s(id) GROUP BY id HAVING COUNT(*)>1;Expected result, not executed: "Row (2,2)."