Files
school_compare/pipeline/transform/models/marts/dim_location.sql
T
TudorandClaude Fable 5 de81e9cdbd feat(pipeline): include 'Open, but proposed to close' schools in dims
These schools are still operating and publish results; they drop out
automatically when GIAS flips them to Closed since marts fully rebuild
each run.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-08 21:54:08 +01:00

36 lines
1.3 KiB
SQL

-- Mart: School location dimension — one row per URN, PostGIS-enabled
-- Geometry derived from GIAS easting/northing (British National Grid → WGS84).
-- For schools missing easting/northing, the geocode script backfills via Postcodes.io.
select
s.urn,
s.address_line1,
s.address_line2,
s.town,
s.county,
s.postcode,
s.local_authority_code,
s.local_authority_name,
s.parliamentary_constituency,
s.urban_rural,
s.easting,
s.northing,
case
when s.easting is not null and s.northing is not null
then ST_Transform(ST_SetSRID(ST_MakePoint(s.easting::double precision, s.northing::double precision), 27700), 4326)
else null
end as geom,
case
when s.easting is not null and s.northing is not null
then ST_Y(ST_Transform(ST_SetSRID(ST_MakePoint(s.easting::double precision, s.northing::double precision), 27700), 4326))
else null
end as latitude,
case
when s.easting is not null and s.northing is not null
then ST_X(ST_Transform(ST_SetSRID(ST_MakePoint(s.easting::double precision, s.northing::double precision), 27700), 4326))
else null
end as longitude
from {{ ref('stg_gias_establishments') }} s
-- Must match dim_school's status filter exactly (the API inner-joins the two).
where s.status in ('Open', 'Open, but proposed to close')