Lesson 5 of 5 Data Transformation and Integration

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.

Troubleshooting scenario

A join that went wrong

Exam skills 1.1.6

Fix the problems in a training table built from three sources, one at a time.

Log in to try this activity Interactive activities are free with an account.

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.

ProblemSymptomFix
One-to-many joinMore rows than customersAggregate before joining
Key mismatchMany unexpected nullsNormalize key types and formats
Time zone mismatchEvents on wrong dayConvert all timestamps to UTC
Future data in featuresOffline accuracy far above productionPoint-in-time joins

The following diagram shows which month of usage the join uses for a label.

The point-in-time join in the Athena query. Each label, such as churn recorded in March, is joined to usage from the month before the label date, so features use only data available at that moment. Joining the latest usage instead would leak future behaviour into training and inflate offline accuracy.
The months are illustrative.
True or false

Point-in-time joins

Exam skills 1.1.6 · 1.2.2

Check the rules that keep future information out of training rows.

Log in to try this activity Interactive activities are free with an account.
Worked example

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 only

Joining 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.

Check yourself

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.