How do you ensure data quality and consistency in a high-volume pipeline?
Write a PostgreSQL query that audits each pipeline batch for completeness and record-level quality issues. Return every batch, including batches with no ingested records, and classify each batch as passing or failing.
quality_status.batch_date, then batch_id.batch_id, source_name, batch_date, expected_event_count, ingested_event_count, duplicate_event_key_count, null_event_key_count, null_event_ts_count, invalid_amount_count, invalid_status_count, quality_status.batch_date ascending, then batch_id ascending.event_key occurrences beyond distinct values, and count NULL event_key and NULL event_ts values.amount as invalid, and accept only non-NULL statuses equal to accepted or rejected.quality_status to PASS only when ingested equals expected and every issue count is zero; otherwise set it to FAIL| Column | Type | Description |
|---|---|---|
| batch_idPK | INT | Unique pipeline batch identifier |
| source_name | VARCHAR(50) | Name of the upstream data source |
| batch_date | DATE | Date assigned to the pipeline batch |
| expected_event_count | INT | Expected number of events in the batch |
| Column | Type | Description |
|---|---|---|
| event_idPK | BIGINT | Unique ingested event row identifier |
| batch_id | INT | Pipeline batch containing the event |
| event_key | VARCHAR(80) | Source event identifier expected to be unique within a batch |
| event_ts | TIMESTAMP | Timestamp supplied by the source |
| amount | NUMERIC(12,2) | Non-negative event amount |
| status | VARCHAR(20) | Event processing status |