Zum Inhalt springen

Lektion 24 · Projects and Reference

Project: Build a Retail Sales Warehouse

Build a retail sales warehouse end to end in SQL: design the grain, create dimensions and facts from raw data, reconcile totals and answer questions.

In this project you take messy sales exports and turn them into a small, trustworthy star schema. You will follow the same steps as in a real project: design, load, check and answer questions.

Everything runs in SQLite. The data is made up for practice.

The brief

Harbor Outfitters wants to know:

  1. Which categories earn the most each month?
  2. How do weekdays compare with weekends?
  3. What share of revenue comes from each store?

You receive one export of receipt lines. Its problems are common ones: untidy product codes, a duplicated line and a product that is missing from the product list.

Step 1: design

Item Decision
Process Retail sales
Grain One row per product line on a receipt
Dimensions Date, product, store
Degenerate dimension Receipt number
Facts quantity, net_amount (additive)
Money Stored as integer cents
Unknown products Kept, with a key of -1
Duplicates Exact repeats of a receipt line are removed and counted

Writing these choices down first means that your SQL has something to be checked against.

Step 2: load and model

The next block creates raw data, cleans it, builds the dimensions and loads the fact table. Read it from top to bottom, then run it.

SQL Build the star schema

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

Step 3: read the results

Check the control numbers first: 8 raw rows, 1 duplicate removed, 7 fact rows and 1 row with an unknown product. Staging and fact totals match.

Then think about what the answers do not say:

  • With so few rows, nothing here supports a business conclusion. It only shows that the model works.
  • Question 2 divides by every calendar day in the loaded range, including days without sales. If the stores were closed on some days, you would need a store calendar.
  • The unknown product still counts toward revenue. Report it, and ask the product team to add XX-99.

Notice also a weakness in the duplicate rule. DISTINCT removes only exact repeats. If the same receipt line arrived twice with different amounts, both would survive staging and the fact table's primary key would reject the load. That is a good failure: it forces someone to decide which version is right.

Step 4: extend it

Try these in the editor:

  1. Add a reject_line table for rows with a quantity of zero or less, and include it in the counts.
  2. Add a daily aggregate table and reconcile it with the facts.
  3. Add XX-99 to raw_product before the dimensions are built. Which results change?
  4. Add city and country to the store dimension and group by country.
  5. Write the design sheet for a second fact table, returns, and add its row to a bus matrix.

What each part of the script teaches

Raw first. The raw table keeps the untidy product code bt-01 exactly as received. Cleanup happens in the staging step, where you can see and change the rule.

Dimensions before facts. The fact load can only look up keys that already exist. The reserved product row has an explicit key of -1.

A complete calendar. The date dimension covers every day from 1 January to 28 February, not only days with sales. That is why the weekend comparison can divide by calendar days.

Left joins in the lookup. The unmatched product XX-99 becomes key -1. The date and store lookups have no fallback, on purpose: the NOT NULL constraints make the load fail if either is missing. For this project, a sale without a valid date or store is an error that a person should examine.

Explain your results

A warehouse project ends with an explanation, not only a query. Write a short note for the person who asked, covering:

  • What is included: the date range, stores and rows.
  • Definitions: revenue means net line amount, in currency units converted from cents.
  • Known issues: one duplicate line was removed, and one sale has an unknown product.
  • What cannot be concluded: the sample is far too small to compare stores or day types.

This habit builds trust. People can work with a limitation they know about.

Prepare it for daily running

To turn the script into a pipeline, you would need to change several things. Dimensions would be updated in place so that keys remain stable. The date dimension would be generated well ahead of time. Facts would be loaded by batch, with a safe rerun strategy from loading fact tables. The check queries would become tests that stop publication on failure. Each run would write its row counts to an audit table. Sketch this order of steps as a small dependency diagram before writing any code.

Break it on purpose

You learn how reliable the checks are by making the data bad. Make one change at a time, predict the result and then run the script:

  • Give a raw line a store name with a trailing space. A new, separate store appears, because store names are not cleaned. How would you fix it?
  • Change a date to 2026-03-05, outside the generated calendar. The load fails, because no date key is found.
  • Add a second R2 line 1 with a different amount. The primary key rejects it.

The first problem passes silently, which is the most dangerous kind. Add a check that would catch it.

Review checklist

Before you call it done

Grain Written and enforced
Keys No empty or orphan keys
1 / 2

In a real warehouse you would not drop and rebuild everything on each run. You would load incrementally, keep surrogate keys stable between runs (the ROW_NUMBER approach here would renumber products when one is added) and keep rejected data for follow-up.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What enforces the grain of fact_sales in this project?

    The key allows only one row per receipt line.
  2. 2. Why is ROW_NUMBER unsuitable for stable surrogate keys across rebuilds?

    Keys must stay the same once facts and users depend on them.
  3. 3. Why keep the unknown product row in the revenue?

    Dropping it would make totals disagree with the source.

Summary

  • You designed a grain, built three dimensions and loaded a fact table.
  • Control counts and totals showed that no data was lost unexpectedly.
  • Unknown members kept real sales visible.
  • Stable keys and incremental loading are the next steps for production.

Next: Project: Track Customer History

Dein Fortschritt wird in diesem Browser gespeichert. Kein Konto nötig.