|
| 1 | +SELECT |
| 2 | + -- Composite key for unique index (required for concurrent refresh) |
| 3 | + pps.patient_id || '-' || |
| 4 | + pps.programme_type || '-' || |
| 5 | + tl.team_id || '-' || |
| 6 | + pl.location_id || '-' || |
| 7 | + pps.academic_year AS id, |
| 8 | + |
| 9 | + -- Identifiers (for counting and grouping) |
| 10 | + pps.patient_id, -- COUNT(DISTINCT patient_id) for totals |
| 11 | + pps.academic_year, -- Filter: ?academic_year=2024 |
| 12 | + pps.programme_type, -- Filter: ?programme=hpv |
| 13 | + pps.status, -- Scope: .vaccinated (status IN 60,61) |
| 14 | + tl.team_id, -- Filter: by user's team_ids |
| 15 | + pl.location_id AS session_location_id, -- Year group eligibility subquery |
| 16 | + |
| 17 | + -- Patient demographics (used for filtering and CSV grouping) |
| 18 | + CASE pat.gender_code |
| 19 | + WHEN 0 THEN 'not known' |
| 20 | + WHEN 1 THEN 'male' |
| 21 | + WHEN 2 THEN 'female' |
| 22 | + WHEN 9 THEN 'not specified' |
| 23 | + ELSE NULL |
| 24 | + END AS patient_gender, -- Filter: ?gender=female |
| 25 | + |
| 26 | + pps.academic_year |
| 27 | + - pat.birth_academic_year - 5 AS patient_year_group, -- Filter: ?year_group=8,9 |
| 28 | + COALESCE(la.mhclg_code, '') AS patient_local_authority_code, -- Filter: ?local_authority=E09000001 |
| 29 | + COALESCE(la.mhclg_code, '') AS patient_school_local_authority_code, -- Filter: ?school_local_authority=E09000001 |
| 30 | + |
| 31 | + -- School info (for CSV grouping by school) |
| 32 | + CASE |
| 33 | + WHEN school.urn IS NOT NULL THEN school.urn |
| 34 | + WHEN pat.home_educated = true THEN '999999' |
| 35 | + ELSE '888888' |
| 36 | + END AS patient_school_urn, |
| 37 | + CASE |
| 38 | + WHEN school.name IS NOT NULL THEN school.name |
| 39 | + WHEN pat.home_educated = true THEN 'Home-schooled' |
| 40 | + ELSE 'Unknown school' |
| 41 | + END AS patient_school_name, |
| 42 | + |
| 43 | + -- Status flags |
| 44 | + ar.patient_id IS NOT NULL AS is_archived, -- Scope: .not_archived |
| 45 | + |
| 46 | + -- Parent declared "already vaccinated" (counts toward vaccinated total) |
| 47 | + EXISTS ( |
| 48 | + SELECT 1 FROM consents con |
| 49 | + WHERE con.patient_id = pps.patient_id |
| 50 | + AND con.programme_type = pps.programme_type |
| 51 | + AND con.academic_year = pps.academic_year |
| 52 | + AND con.invalidated_at IS NULL |
| 53 | + AND con.withdrawn_at IS NULL |
| 54 | + AND con.response = 1 -- refused |
| 55 | + AND con.reason_for_refusal = 1 -- already_vaccinated |
| 56 | + ) AS has_already_vaccinated_consent |
| 57 | + |
| 58 | +-- Source: pre-computed patient status per programme/year |
| 59 | +FROM patient_programme_statuses pps |
| 60 | + |
| 61 | +-- Patient record (for demographics and exclusion checks) |
| 62 | +JOIN patients pat |
| 63 | + ON pat.id = pps.patient_id |
| 64 | + |
| 65 | +-- Where the patient is enrolled this year (links to team via location) |
| 66 | +JOIN patient_locations pl |
| 67 | + ON pl.patient_id = pps.patient_id |
| 68 | + AND pl.academic_year = pps.academic_year |
| 69 | + |
| 70 | +-- Which team owns this location (for team_id filtering) |
| 71 | +JOIN team_locations tl |
| 72 | + ON tl.location_id = pl.location_id |
| 73 | + AND tl.academic_year = pps.academic_year |
| 74 | + |
| 75 | +-- Check if patient is archived by this team (LEFT: most aren't) |
| 76 | +LEFT JOIN archive_reasons ar |
| 77 | + ON ar.patient_id = pps.patient_id |
| 78 | + AND ar.team_id = tl.team_id |
| 79 | + |
| 80 | +-- Patient's school (LEFT: home-educated patients have no school) |
| 81 | +LEFT JOIN locations school |
| 82 | + ON school.id = pat.school_id |
| 83 | + |
| 84 | +-- School's local authority (LEFT: school may not have LA set) |
| 85 | +LEFT JOIN local_authorities la |
| 86 | + ON la.gias_code = school.gias_local_authority_code |
| 87 | + |
| 88 | +-- Exclude patients who shouldn't appear in any reports |
| 89 | +WHERE pat.invalidated_at IS NULL -- Merged/duplicate record |
| 90 | + AND pat.restricted_at IS NULL -- S31 restricted access |
| 91 | + AND pat.date_of_death IS NULL -- Deceased |
0 commit comments