Missing inventory snapshot ranges per SKU
The task
The warehouse takes one inventory snapshot per product per day, but some days were skipped. An analytics engineer wants a list of the missing stretches to chase.
Look at every row of inventory_snapshots. For each sku, a gap is a stretch of calendar days between two of its snapshots that has no snapshot of its own: two consecutive snapshot dates of the SKU that are more than 1 day apart. Days before a SKU's first snapshot or after its last one are not gaps.
Return one row per gap:
skugap_start: the first missing day (the day after the earlier snapshot)gap_end: the last missing day (the day before the later snapshot)missing_days: how many days are missing
Sort by sku, then gap_start.
Expected output
skugap_startgap_endmissing_days- Sorted by sku ascending, then gap_start 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
- Listening sessions split by a 30-minute gaphard
- Longest run of consecutive driver cancellationshard
- Longest run of delivery days per campaignmedium
- Merge subscription rows into continuous customer stretcheshard
- Streaks of four or more days of opening the appmedium
- Merge unchanged versions in a type 2 historyhard
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL