- Add StatCan CMHC parser to fetch rental data from Statistics Canada API - Create year spine (2014-2025) as time dimension driver instead of census - Add CMA-level rental and income intermediate models - Update mart_neighbourhood_overview to use rental years as base - Fix neighbourhood_service queries to match dbt schema - Add CMHC data loading to pipeline script Data now flows correctly: 158 neighbourhoods × 12 years = 1,896 records Rent data available 2019-2025, crime data 2014-2024 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
57 lines
1.6 KiB
SQL
57 lines
1.6 KiB
SQL
-- Intermediate: Housing indicators by neighbourhood
|
|
-- Combines census housing data with allocated CMHC rental data
|
|
-- Grain: One row per neighbourhood per year
|
|
|
|
with neighbourhoods as (
|
|
select * from {{ ref('stg_toronto__neighbourhoods') }}
|
|
),
|
|
|
|
census as (
|
|
select * from {{ ref('stg_toronto__census') }}
|
|
),
|
|
|
|
allocated_rentals as (
|
|
select * from {{ ref('int_rentals__neighbourhood_allocated') }}
|
|
),
|
|
|
|
housing as (
|
|
select
|
|
n.neighbourhood_id,
|
|
n.neighbourhood_name,
|
|
n.geometry,
|
|
|
|
coalesce(r.year, c.census_year, 2021) as year,
|
|
|
|
-- Census housing metrics
|
|
c.pct_owner_occupied,
|
|
c.pct_renter_occupied,
|
|
c.average_dwelling_value,
|
|
c.median_household_income,
|
|
|
|
-- Allocated rental metrics (weighted average from CMHC zones)
|
|
r.avg_rent_2bed,
|
|
r.vacancy_rate,
|
|
|
|
-- Affordability calculations
|
|
case
|
|
when c.median_household_income > 0 and r.avg_rent_2bed > 0
|
|
then round((r.avg_rent_2bed * 12 / c.median_household_income) * 100, 2)
|
|
else null
|
|
end as rent_to_income_pct,
|
|
|
|
-- Affordability threshold (30% of income)
|
|
case
|
|
when c.median_household_income > 0 and r.avg_rent_2bed > 0
|
|
then r.avg_rent_2bed * 12 <= c.median_household_income * 0.30
|
|
else null
|
|
end as is_affordable
|
|
|
|
from neighbourhoods n
|
|
left join census c on n.neighbourhood_id = c.neighbourhood_id
|
|
left join allocated_rentals r
|
|
on n.neighbourhood_id = r.neighbourhood_id
|
|
and r.year = c.census_year
|
|
)
|
|
|
|
select * from housing
|