feat(sql): thread opportunity_score from location_opportunity_profile into pSEO serving chain

- dim_cities: add geoname_id to geonames_pop CTE and final SELECT
  Creates FK between dim_cities (city-with-padel-venues) and dim_locations (all GeoNames),
  enabling joins to location_opportunity_profile for the first time.
- city_market_profile: pass geoname_id through base CTE and final SELECT
- pseo_city_costs_de: LEFT JOIN location_opportunity_profile on (country_code, geoname_id),
  add opportunity_score to output columns
- pseo_country_overview: add avg_opportunity_score, top_opportunity_score, top_opportunity_slugs,
  top_opportunity_names aggregates

Cities with no GeoNames name match get opportunity_score = NULL; templates guard with
{% if opportunity_score %}.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Deeman
2026-02-26 20:07:25 +01:00
parent 88378cbfa7
commit 7186d4582a
4 changed files with 17 additions and 2 deletions

View File

@@ -75,7 +75,7 @@ uk_pop AS (
),
-- GeoNames global fallback (all cities ≥50K)
geonames_pop AS (
SELECT city_name, country_code, population, ref_year
SELECT geoname_id, city_name, country_code, population, ref_year
FROM staging.stg_population_geonames
QUALIFY ROW_NUMBER() OVER (PARTITION BY geoname_id ORDER BY ref_year DESC) = 1
)
@@ -153,7 +153,10 @@ SELECT
)::INTEGER AS population_year,
vc.padel_venue_count,
ci.median_income_pps,
ci.income_year
ci.income_year,
-- GeoNames ID: FK to dim_locations / location_opportunity_profile.
-- NULL when city name doesn't match any GeoNames entry.
gn.geoname_id
FROM venue_cities vc
LEFT JOIN country_income ci ON vc.country_code = ci.country_code
-- Eurostat EU population (via city code→name lookup)