-- Intermediate model: Latest Ofsted inspection per URN
-- Picks the most recent inspection for each school

with ranked as (
    select
        *,
        row_number() over (
            partition by urn
            order by inspection_date desc
        ) as rn
    from {{ ref('stg_ofsted_inspections') }}
)

select
    urn,
    inspection_date,
    inspection_type,
    framework,
    overall_effectiveness,
    quality_of_education,
    behaviour_attitudes,
    personal_development,
    leadership_management,
    early_years_provision,
    sixth_form_provision,
    ungraded_outcome,
    ungraded_grade,
    rc_safeguarding_met,
    rc_inclusion,
    rc_curriculum_teaching,
    rc_achievement,
    rc_attendance_behaviour,
    rc_personal_development,
    rc_leadership_governance,
    rc_early_years,
    rc_sixth_form,
    rc_inspection_date,
    report_url
from ranked
where rn = 1
