← Back to Case StudiesProduction experience

SQL Data Warehouse & ETL

Why do two reports give different numbers for the same shift?

My Role

I built and maintained 40+ automated SQL and Python analytics jobs delivering risk, throughput, and capacity metrics. I ingested tens of millions of operational records, cleaning and aggregating them into Parquet and JSON for downstream analytics and ML models. I wrote a reusable Python library for data warehouse, query engine, data catalog, and object storage access — adopted across the team to standardize connections and alerting.

The Problem

Most companies have data scattered across multiple SQL databases, SaaS APIs, and application logs. Each system has its own schema, update frequency, and access pattern. When teams need a unified view — for reporting, analytics, or training ML models — they either build fragile point-to-point integrations or rely on manual exports that quickly become stale. The result is inconsistent metrics, delayed decisions, and data that can't be trusted for downstream use.

Data platform responsibilities and confirmed connections

1. Processing, storage, and query

Lambda and Spark/DataNet write datasets to S3. Glue holds schema and partition metadata, Lake Formation cross-account sharing provides data to other teams in Andes, and Redshift Spectrum reads the Parquet data in S3.

Loading diagram…

2. Consumers and historical publication

Redshift serves dashboards and analytics queries. S3 datasets feed model training. Historical tables reach other teams through an internal platform with about 40 minutes of lag.

Loading diagram…

This diagram shows the confirmed responsibilities in the data platform. Lambda parses and cleans raw data and writes Parquet to S3. Redshift Spectrum queries Parquet data in S3 using Glue catalog metadata. Redshift also supports native-table workflows and ETL/ELT. Glue provides catalog schemas, table metadata, and discovery. Spark/DataNet performs transformations and loading. S3 datasets serve as model-training inputs. An internal platform publishes shared historical tables for other teams with approximately 40 minutes of lag. Some historical wiring between source systems and processing services is not established; those connections are described in the service table below.

Evidence status: This reconstruction is based on my own account of systems I built and maintained. The historical source code and configuration are not available for publication, so these details are owner-confirmed, not independently verified.

Design Decisions

The table below shows confirmed service responsibilities. Some historical wiring between source systems and processing services is not established.

ServiceResponsibility
LambdaParsed and cleaned raw inputs; produced partitioned Parquet in S3
GlueMaintained catalog schemas, table metadata/versioning, and discovery. A daily crawler ran for catalog updates.
RedshiftExternal Spectrum queries against S3 Parquet, native-table workflows, dashboard inputs, and ETL/ELT
Spark/DataNetTransformations, extraction, and loading for complex workflows
Lake FormationCross-account sharing of data with other teams in Andes
S3 datasetsInputs for model training
Internal platformPublished shared historical tables for other teams with approximately 40 minutes of lag

Note: The daily Glue crawler is not presented as the mechanism that made every new partition immediately queryable. The exact automatic partition registration mechanism remains unconfirmed. Timestamped Parquet files and Glue table versions do not establish transactional dataset versioning.

Why Two Reports Disagree: A Worked Example

Synthetic fixture: Three shifts, each with two events in shift_events.

shift_idplanned_headcountNumber of events
Shift A82
Shift B82
Shift C102

Row grain before the join: Each row in shift_events represents one event during one shift. The shifts table has one row per shift, with planned headcount. shift_id is unique in this fixture.

The join: shift_events JOIN shifts ON shift_events.shift_id = shifts.shift_id. Each shift's headcount value is repeated for every event row. With 6 events across 3 shifts, the joined result has 6 rows.

Where disagreement happens:

  • Report A sums headcount from the joined rows: SUM(shifts.planned_headcount) = 8+8+8+8+10+10 = 52. The join repeated each shift's headcount for every event row, inflating the total.
  • Report B uses SUM(DISTINCT shifts.planned_headcount) = 8+10 = 18. This deduplicates values, not shifts. Shift A and Shift B both had headcount 8, so DISTINCT collapses them into one value, undercounting by 8.

Correct approach: Aggregate at the shift level before summing headcount, using only the shifts represented in the reporting population:

-- Correct total planned headcount across shifts represented in shift_events
WITH represented_shifts AS (
  SELECT DISTINCT shift_id
  FROM shift_events
)
SELECT SUM(s.planned_headcount) AS total_planned_headcount
FROM shifts s
JOIN represented_shifts r
  ON r.shift_id = s.shift_id;

-- Result: 8 + 8 + 10 = 26

This query returns 26, which is the correct total planned staffing across the three shifts. It sums one row per shift, not one row per event.

Important notes:

  • The result sums planned staffing across shifts. It does not count distinct people across multiple shifts.
  • A shift with no events is excluded by this particular reporting-population definition. Counting all scheduled shifts would require a different query.
  • Document the metric definition — does "total headcount" mean unique workers per shift, or the sum across all shifts?

Results

40+ automated SQL and Python analytics jobs delivering risk, throughput, and capacity metrics. Tens of millions of operational records ingested, cleaned, and aggregated into Parquet and JSON for downstream analytics and ML models. A reusable Python library for data warehouse, query engine, data catalog, and object storage access — adopted across the team to standardize connections and alerting.

See the code

I rebuilt the S3, Glue, Athena and Redshift parts of that library from scratch as a public, tested package, with a step-by-step walkthrough: AWS data helpers that know which environment they're in.

Working through a similar problem?

Discuss a project →