Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Restaurant partnerships renegotiate commission, so zomato_commission_rates keeps one row per rate version with the window it applied to. Finance needs commission per restaurant billed at the rate that was in force when each order was placed — not at today's rate.
Return one row per restaurant, ordered by restaurant_id.
Result columns · in this order
restaurant_id | The restaurant. |
orders | Orders they fulfilled. |
gross_value | Total basket value of those orders. |
commission | Commission billed at the rate in force per order. |
How to approach it
Run the starter and count how many rows one r1 order produces — that is the whole problem.
Sample input
| order_id | restaurant_id | ordered_on | order_value |
|---|---|---|---|
| 7001 | r1 | 2026-01-05 | 400 |
| 7002 | r1 | 2026-02-01 | 600 |
| 7003 | r1 | 2026-02-20 | 250 |
| 7004 | r1 | 2026-03-10 | 500 |
| 7005 | r2 | 2026-01-11 | 800 |
| 7006 | r2 | 2026-02-15 | 300 |
| 7007 | r2 | 2026-03-01 | 450 |
| 7008 | r3 | 2026-01-20 | 700 |
| 7009 | r3 | 2026-02-25 | 900 |
| 7010 | r3 | 2026-03-14 | 350 |
| 7011 | r4 | 2026-02-08 | 260 |
| 7012 | r4 | 2026-03-05 | 640 |
12 rows — scroll inside the table to see them all.
| restaurant_id | rate_pct | effective_from | effective_to |
|---|---|---|---|
| r1 | 18 | 2025-12-01 | 2026-02-01 |
| r1 | 22 | 2026-02-01 | 2026-03-01 |
| r1 | 25 | 2026-03-01 | null |
| r2 | 20 | 2025-11-15 | 2026-03-01 |
| r2 | 15 | 2026-03-01 | null |
| r3 | 12 | 2025-10-01 | 2026-02-10 |
| r3 | 17 | 2026-02-10 | null |
| r4 | 20 | 2026-01-01 | null |
8 rows — all rows shown.
Expected output
| restaurant_id | orders | gross_value | commission |
|---|---|---|---|
| r1 | 4 | 1750 | 384 |
| r2 | 3 | 1550 | 287.5 |
| r3 | 3 | 1950 | 296.5 |
| r4 | 2 | 900 | 180 |
4 rows — all rows shown.
Constraints
ordered_on.effective_from is inclusive, effective_to is exclusive — an order on the change date belongs to the new rate.effective_to is NULL for the version still in force. NULL means 'no end', not 'never applies'.orders must equal the number of real orders: 4 for r1, 3 for r2, 3 for r3, 2 for r4. Anything higher means an order matched more than one rate row.order_value multiplied by rate_pct / 100, summed per restaurant and rounded to 2 decimal places.restaurant_id.Worked example
r1 has three rate versions — 18% from 1 December, 22% from 1 February, 25% from 1 March — and four orders. Order 7002 was placed exactly on 2026-02-01, so it bills at 22%, not 18%. The four orders total 1750 and the correct commission is 384.00.
Join on restaurant_id alone and every r1 order matches all three rate rows: orders reports 12 instead of 4, gross_value triples to 5250, and commission comes out at 1137.50. Using only the current rate is the other failure — it bills January's 400 order at 25% and quietly overcharges the partner.
What this tests
The as-of join against a slowly changing dimension: matching a fact to the one version of a dimension row that was valid at the fact's timestamp, including the half-open boundary and the open-ended current row.
Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.