Analytics warehouse
Tables an analytics engineer works with: a raw order load with repeats and late updates, a type 2 customer history, daily exchange rates and stock snapshots. 2025.
stg_orders
Raw order rows as loaded from the source. The same order can arrive several times: each change in the source sends a new copy, and some loads repeat a copy exactly. No primary key. 669 rows in the larger version, 241 in the smaller.
| order_id | int | Order id from the source. |
| customer_id | int | Customer id. |
| order_total | numeric(10,2) | Order total in the order currency. |
| currency | text | USD, EUR or GBP. |
| order_status | text | placed, shipped, delivered or cancelled. |
| updated_at | timestamp | When the source last changed this order (UTC). |
| loaded_at | timestamp | When our pipeline loaded this copy (UTC). |
dim_customers_history
Type 2 history: one row per version of a customer. A version is valid from valid_from up to, not including, valid_to. Key: customer_id, valid_from. 120 rows in the larger version, 48 in the smaller.
| customer_id | int | Customer id. |
| customer_name | text | Name. |
| tier | text | bronze, silver or gold. |
| region | text | EMEA, AMER or APAC. |
| valid_from | timestamp | Start of this version (UTC). |
| valid_to | timestamp, nullable | End of this version (not included). NULL for the current version. |
fx_rates
Closing exchange rates on business days only (no weekend rows). Key: rate_date, currency. 254 rows in the larger version, 254 in the smaller.
| rate_date | date | Business day. |
| currency | text | EUR or GBP. |
| usd_per_unit | numeric(12,6) | US dollars for one unit of the currency. |
inventory_snapshots
Stock on hand per product at the end of each day. A product missing from a day had no snapshot taken. Key: snapshot_date, sku. 336 rows in the larger version, 173 in the smaller.
| snapshot_date | date | Day of the snapshot. |
| sku | text | Product code. |
| on_hand | int | Units in stock. |
Questions on this database
- Each day's rate next to the month-end rateAn analytics engineer is building a table that shows each exchange rate next to the closing rate of its month.
- Customers by tier on a past dateA report needs the customer mix as it stood at 2025-05-01 00:00:00. dim_customers_history keeps one row per version of a customer.
- Find exact duplicate rows in a raw order loadThe pipeline sometimes loads the same copy of an order twice.
- How many rows would deduplication remove?Before building the order model, the team plans to keep exactly one row for each order_id and updated_at pair in stg_orders and delete the rest.
- Orders by current status from a raw loadstg_orders holds every copy of every order our pipeline has loaded.
- Current order copy when two copies share a timestampOperations wants the list of orders that are shipped but not yet delivered, taken from the raw load in stg_orders.
- Missing inventory snapshot ranges per SKUThe warehouse takes one inventory snapshot per product per day, but some days were skipped.
- Current gold customers by region from type 2 historyThe loyalty team wants today's gold customers by region. dim_customers_history has one row per version of a customer.
- Order copies that arrived more than 12 hours lateData quality wants to know which rows reach the warehouse late.
- Customer tier at a point in time from type 2 historyA campaign was sent at 2025-03-15 12:00:00 (UTC) and marketing wants to know which tier each customer was in at that moment, not today.
- Latest copy of each order loaded after a watermarkAn incremental model processes only new rows from stg_orders. The last run finished with the watermark 2025-06-15 00:00:00.
- Turn stock snapshots into restock eventsThe warehouse stores a snapshot of stock per product at the end of each day. Operations wants an event table of restocks instead.
- Daily stock levels with a date spine and carry forwardThe planning dashboard needs a stock figure for every product on every day, even though some end-of-day snapshots are missing.
- Delivered revenue in USD with as-of exchange ratesFinance reports delivered revenue in US dollars by the month orders were placed. Build it from the raw order load.
- Merge unchanged versions in a type 2 historydim_customers_history has one row per version of a customer, but some versions changed only a column we do not track.
- Orders by the customer's tier when the order was placedMarketing wants to know how orders split by the tier the customer had when they ordered, not the tier they have today.