Daily stock levels with a date spine and carry forward
The task
The planning dashboard needs a stock figure for every product on every day, even though some end-of-day snapshots are missing. inventory_snapshots has one row per product and day that was captured.
For the products SKU-104, SKU-107 and SKU-110, return one row for each day from 2025-06-13 to 2025-06-19 inclusive:
skudayon_hand: the snapshot of that day, or, when that day has no snapshot, the most recent earlier snapshot of the same product, even if it is from before 2025-06-13
A product with no snapshot at all on or before a day has no stock figure for that day, so leave that row out. Sort by sku, then day.
Expected output
skudayon_hand- Sorted by sku ascending, then day ascending.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables Analytics warehouse · click a name to insert it
stg_orders669 rows in the sample
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.
| int | Order id from the source. | |
| int | Customer id. | |
| numeric(10,2) | Order total in the order currency. | |
| text | USD, EUR or GBP. | |
| text | placed, shipped, delivered or cancelled. | |
| timestamp | When the source last changed this order (UTC). | |
| timestamp | When our pipeline loaded this copy (UTC). |
dim_customers_history120 rows in the sample
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.
| int | Customer id. | |
| text | Name. | |
| text | bronze, silver or gold. | |
| text | EMEA, AMER or APAC. | |
| timestamp | Start of this version (UTC). | |
| timestamp | End of this version (not included). NULL for the current version. |
fx_rates254 rows in the sample
Closing exchange rates on business days only (no weekend rows). Key: rate_date, currency.
| date | Business day. | |
| text | EUR or GBP. | |
| numeric(12,6) | US dollars for one unit of the currency. |
inventory_snapshots336 rows in the sample
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.
| date | Day of the snapshot. | |
| text | Product code. | |
| int | Units in stock. |
Related questions
- Active accounts and seats per day from subscription datesmedium
- Agent by month fact table of tickets created and resolvedmedium
- Customer tier at a point in time from type 2 historymedium
- Delivered revenue in USD with as-of exchange rateshard
- Latest copy of each order loaded after a watermarkmedium
- Monthly recurring revenue by plan with empty monthsmedium
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL