Lompat ke konten

Pelajaran 25 ยท Projects and Reference

Project: Track Customer History

Build a customer history pipeline in SQL: process daily snapshots into an SCD Type 2 dimension, handle deletes and returns, load sales and test results.

This project builds a customer dimension that remembers the past. You process daily snapshots one at a time, create Type 2 versions, handle a customer who disappears and then returns, attach sales to the right versions and test the result.

The rules

Rule Decision
Natural key customer_id
city Type 2: keep history
name Type 1: correct all versions
Missing from a full snapshot Close the current version
Customer returns Open a new version
Periods Half-open: valid_from included, valid_to excluded

The third rule is only correct because each snapshot is complete. With a partial or failed extract, it would close real customers. A production pipeline should check that the snapshot looks complete before applying it.

How the pipeline works

SQLite has no stored procedures or loops that suit this job, so the block keeps all snapshots in one staging table and uses a work table that holds the current batch. The same three statements run for each day. In a real system, an orchestrator would run one script with the batch date as a parameter.

SQL Process three daily snapshots

Press Run (or Ctrl + Enter) to see the result here.

Read the output

The dimension has five rows. Customer C1 has a Lagos version and an Abuja version. C2 has a Leeds version that ends on 1 March and a York version that starts on 1 April, leaving a deliberate gap for the month the customer was absent. The corrected name "Jon Smith" appears on both.

In the revenue result, sale 4 falls in the gap and is reported as Unknown. That is a finding to investigate: how did a deleted customer make a purchase? Possible explanations include a faulty snapshot, a late deletion or a sales system that is out of step.

The sale on 1 February uses Abuja, because the move's validity starts that day. These dates are snapshot dates. If the customer actually moved on 20 January, the warehouse cannot know it unless the source supplies an effective date.

All three tests return zero. The last one also confirms that no sale was duplicated or lost.

Walk through the statements

Each batch after the first runs the same three steps, and their order matters.

(a) Close. A current row survives only if the batch contains the same customer with the same city. Both a move and a disappearance fail that test, so one statement handles both. The comparison uses IS, which treats two NULL cities as equal.

(b) Open. Anyone in the batch without a current row gets a new version. This covers three cases at once: the customer who just moved, the brand-new customer and the one who returned.

(c) Correct names. Because name is Type 1, the latest value is copied to every version of each customer in the batch. A customer who is absent from the batch keeps the last name the warehouse saw.

If you ran (b) before (a), a customer who moved would still have a current row, so no new version would be added, and step (a) would then close the old one. The customer would be left with no current version. The script's tests only check for more than one current row, so they would miss this. Add a test that every customer in the latest snapshot has exactly one current row.

Test the behavior, not only the shape

Structural tests find overlaps and duplicates, but a history can be well formed and still wrong. Add expectation tests based on the story you know:

Expectation How to check
C1 was in Lagos in mid January Point-in-time query for 15 January
C2 has no version in mid March The same query returns no row
C3 did not exist in February No version covers a February date
The old name spelling is gone No row contains "Smyth"

These small cases with known answers are a reliable way to protect the logic when someone changes it later. Keep the sample snapshots in version control next to the load code, and run them before each release.

Describe it for readers

Write down what the dimension's dates mean: they are the dates of the snapshots in which a change was first observed. Explain the gap rule and which attributes are rewritten. Without this note, a reader may treat valid_from as the exact day a customer moved, and may treat a gap as a loading error. Also say that sales in a gap are reported as unknown on purpose, and name the person or team who reviews them.

Improve it

  1. Add a completeness check that refuses a batch whose row count falls sharply compared with the previous one. Decide on the threshold and explain it.
  2. Run batch 4 a second time. Verify that nothing changes.
  3. Add a tier attribute with Type 2 behavior and update the change test.
  4. Add a current_city column to get Type 6 behavior.
  5. Describe in writing how you would erase one customer's personal data from staging, the dimension and any extracts, while keeping totals correct. Check the policy question with a privacy specialist.

Replaying an older batch after newer ones would corrupt this history. Production pipelines should apply snapshots in date order, wrap each batch in a transaction, and rebuild from staging when an earlier batch has to be corrected.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Why is it risky to close customers that are missing from a snapshot?

    The rule is only valid for complete snapshots, so completeness must be checked.
  2. 2. Why does C2 have a gap between versions?

    The version was closed when C2 vanished and a new one opened on return.
  3. 3. How are name corrections handled in this project?

    Name is a Type 1 attribute here.

Summary

  • Daily full snapshots can build a Type 2 history with a few repeatable statements.
  • Different attributes in one dimension followed different SCD rules.
  • Point-in-time joins matched each sale to history, and exposed an odd sale for follow-up.
  • Completeness checks, ordered batches and transactions make the pipeline safe.

Next: Glossary and Cheat Sheet

Progres Anda disimpan di browser ini. Tidak perlu akun.