runtime
86 / 88

task CTEs · hard

Capacity 3: the next delivery date and all ties

For each site, find the earliest delivery date on or after the snapshot date and sum every delivery tied on that date. Use NULL date and zero units when none exists. Order by site id.

what to return

Columns
site_idnext_dateincoming_units in this order
Rows
4 rows · in the order asked
Watch for
Every stage runs on a fresh database; earlier queries do not carry forward.
One unit is one compute unit. Snapshot date: 2026-04-01. Capacity intervals include valid_from and exclude valid_to; NULL valid_to means no end.
Future supply is incremental capacity, available on delivery_date. The snapshot excludes same-day deliveries, so projections add them once.
Skill
Produce reproducible capacity reports with explicit units, dates and grain. Refresher: Multi-grain joins without fan-out →

expected result

Your query should return exactly these columns and values. Run it and compare.

expected result4 rows · in this order
site_idnext_dateincoming_units
12026-04-1015
22026-04-305
32026-04-0112

first 3 of 4 rows

data you can query

Sample rows from each table. Each column shows its type underneath; → marks a link to another table.

capacity_regions2 columns · 3 rows
idinteger · keynametext
1North
2South
3West

all 3 rows

capacity_sites3 columns · 4 rows
idinteger · keyregion_idinteger → capacity_regionsnametext
11Alder
21Birch
32Cedar
4NULLDrift

all 4 rows

current_capacity5 columns · 5 rows
idinteger · keysite_idinteger → capacity_sitesunitsintegervalid_fromtextvalid_totext
11402026-01-01NULL
22402026-01-01NULL
33992025-01-012026-04-01
4152026-05-01NULL
54102026-01-01NULL

all 5 rows

future_supply4 columns · 6 rows
idinteger · keysite_idinteger → capacity_sitesunitsintegerdelivery_datetext
1172026-04-10
2182026-04-10
31202026-05-01
4252026-04-30
53122026-04-01

showing 5 of 6 rows

solution.sql
 

Hints and the answer are always available. Using them counts the question as solved with help.