Latest copy of each order loaded after a watermark
The task
An incremental model processes only new rows from stg_orders. The last run finished with the watermark 2025-06-15 00:00:00. The next run must read the rows whose loaded_at is strictly after that time.
From those new rows only, keep one row per order: the copy with the latest updated_at (if two tie, the later loaded_at). Do not look at rows loaded earlier, even when they are newer by updated_at.
Return order_id, order_status, order_total and updated_at of the kept copies. Sort by order_id.
Expected output
order_idorder_statusorder_totalupdated_at- Sorted by order_id 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
- Current gold customers by region from type 2 historyeasy
- Daily stock levels with a date spine and carry forwardhard
- Delivered revenue in USD with as-of exchange rateshard
- Order copies that arrived more than 12 hours lateeasy
- Orders by the customer's tier when the order was placedhard
- Active accounts and seats per day from subscription datesmedium
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL