lesson 86 Warehouse & reporting projects · advanced
Stage, validate & atomically promote a refresh
Validate staged data before replacing a published result.
the idea
Refresh a published table in a transaction: BEGIN, delete the old rows, insert the staged rows, then COMMIT. Readers see either the old snapshot or the new one, never a half-empty table. If anything fails before COMMIT, ROLLBACK leaves the old snapshot intact.
Dataset: Retail
The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.
published_totals2 columns · 15 rows
| customer_idtext · unique id | total_salesdecimal |
|---|---|
| 18102 | 15331.08 |
| 17389 | 12361.36 |
| 15482 | 8191.46 |
| 15838 | 5309.5 |
| 12798 | 3651.6 |
| 14866 | 2681.26 |
- customer_idtext · unique id
- 18102
- total_salesdecimal
- 15331.08
- customer_idtext · unique id
- 17389
- total_salesdecimal
- 12361.36
- customer_idtext · unique id
- 15482
- total_salesdecimal
- 8191.46
- customer_idtext · unique id
- 15838
- total_salesdecimal
- 5309.5
- customer_idtext · unique id
- 12798
- total_salesdecimal
- 3651.6
- customer_idtext · unique id
- 14866
- total_salesdecimal
- 2681.26
first 6 of 15 rows · your queries use all of them
refresh_stage2 columns · 15 rows
| customer_idtext | total_salesdecimal |
|---|---|
| 18102 | 11485.54 |
| 17389 | 5876.78 |
| 16440 | 975.43 |
| 14895 | 925.62 |
| 14418 | 897.35 |
| 16839 | 840.48 |
- customer_idtext
- 18102
- total_salesdecimal
- 11485.54
- customer_idtext
- 17389
- total_salesdecimal
- 5876.78
- customer_idtext
- 16440
- total_salesdecimal
- 975.43
- customer_idtext
- 14895
- total_salesdecimal
- 925.62
- customer_idtext
- 14418
- total_salesdecimal
- 897.35
- customer_idtext
- 16839
- total_salesdecimal
- 840.48
first 6 of 15 rows · your queries use all of them
worked example
Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not count toward progress.
Compare published and staged totals. published_totals is what readers see today; refresh_stage holds the next snapshot before you promote it in a transaction.the pattern
example.sqlSELECT p.customer_id, p.total_sales AS published, s.total_sales AS staged FROM published_totals p LEFT JOIN refresh_stage s ON s.customer_id = p.customer_id;
Validate the stage before promotion. Check row count, null keys, and negative totals on refresh_stage so you do not commit a bad refresh.
step-2.sqlSELECT COUNT(*) AS rows, SUM(customer_id IS NULL) AS missing_keys, SUM(total_sales < 0) AS negative_totals FROM refresh_stage;
Confirm rollback leaves data intact. DELETE inside BEGIN is undone by ROLLBACK—staged_rows should match the count before the failed experiment.
step-3.sqlBEGIN; DELETE FROM refresh_stage; ROLLBACK; SELECT COUNT(*) AS staged_rows FROM refresh_stage;
common mistake
Deleting and inserting without a transaction leaves an empty or partial table if the insert fails.