Merging Data from Multiple Sources Correctly
This lesson explains how to merge customer records, events, reference data and labels into training examples correctly, since join mistakes silently create duplicates, gaps and leakage.
Learning outcomes
After completing this lesson, you will be able to:
- Choose a tool for merging data.
- Align keys and check join cardinality.
- Build point-in-time correct training data.
Tools for merging
Training data often combines several sources, such as customer records, transactions and support interactions, and the right tool for merging them depends on their size and location. Small datasets can be merged with pandas in a notebook or SageMaker Processing job.
Large datasets are joined with Spark in AWS Glue or Amazon EMR, or with SQL in Amazon Athena, which queries data in S3 catalogued in Glue and can use federated queries to read Amazon RDS, DynamoDB and other sources in place, without copying them first. Amazon Redshift joins tables already in the warehouse and can unload the results to S3 as Parquet for training.
Join correctness
Joins are a common source of silent errors in training data. Before joining, keys must be aligned: the same type, case and format, including leading zeros, along with time zones, units and currencies; otherwise matching records fail to join and are lost or mismatched.
The cardinality of each join must be checked. A one-to-many join multiplies rows, so a customer table joined to transactions produces one row per transaction; the "many" side is usually aggregated first, for example into total spend per customer. The join type must also be chosen deliberately: an inner join silently drops customers with no events, biasing the data towards active customers, while a left join keeps them with null values that must then be imputed. Comparing row counts before and after each join catches most mistakes.
A join that went wrong
Point-in-time correctness
When labels are events in time, such as "did the customer churn in March", every feature must be computed only from data available before that moment. Joining the latest customer profile to old labels leaks future information into training and inflates offline accuracy. A point-in-time join matches each label to the most recent feature values before its timestamp. SageMaker Feature Store offline store queries support this pattern, as described in the Feature Store module.
The following table summarises common merge problems and their fixes.
| Problem | Symptom | Fix |
|---|---|---|
| One-to-many join | More rows than customers | Aggregate before joining |
| Key mismatch | Many unexpected nulls | Normalize key types and formats |
| Time zone mismatch | Events on wrong day | Convert all timestamps to UTC |
| Future data in features | Offline accuracy far above production | Point-in-time joins |
The following diagram shows which month of usage the join uses for a label.

Point-in-time joins
Assembling a training table with Athena
Labels (churned or not, with a label date) are in S3, monthly usage aggregates are in S3 and customer attributes are in an RDS PostgreSQL database registered with an Athena federated connector.
SELECT l.customer_id, l.churned, c.plan, c.region,
u.minutes_used, u.support_calls
FROM labels l
JOIN customers_rds.public.customers c ON c.customer_id = l.customer_id
JOIN usage_monthly u
ON u.customer_id = l.customer_id
AND u.month = date_trunc('month', l.label_date - interval '1' month) -- previous month onlyJoining usage from the month before the label date keeps the features point-in-time correct, and the federated query reads customer attributes without building a separate export. The result can be written to S3 with a CTAS statement for training.
What should be done before a one-to-many join to avoid duplicating rows?
Show answer
Aggregate the "many" side to one row per key first.
Exam Tips
Remember for the exam
- Large joins: Glue or EMR Spark; SQL joins on S3: Athena.
- Aggregate the many side before one-to-many joins.
- Normalize keys, units and time zones before joining.
- Use point-in-time joins to avoid label leakage.
- Athena federated queries read RDS and other sources in place.