Skip to content

Commit cf2adfa

Browse files
Bump reporting totals view to v07
This is the commit that adds the new SQL file with no changes.
1 parent 3ad00e8 commit cf2adfa

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
class UpdateReportingAPITotalsToVersion7 < ActiveRecord::Migration[8.1]
4+
def change
5+
update_view :reporting_api_totals,
6+
version: 7,
7+
revert_to_version: 6,
8+
materialized: true
9+
end
10+
end
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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,
29+
pat.local_authority_mhclg_code, '') AS patient_local_authority_code,
30+
COALESCE(la.official_name,
31+
pat_la.official_name, '') AS patient_local_authority_official_name,
32+
COALESCE(la.mhclg_code, '') AS patient_school_local_authority_code,
33+
34+
-- School info (for CSV grouping by school)
35+
CASE
36+
WHEN school.urn IS NOT NULL THEN school.urn
37+
WHEN pat.home_educated = true THEN '999999'
38+
ELSE '888888'
39+
END AS patient_school_urn,
40+
CASE
41+
WHEN school.name IS NOT NULL THEN school.name
42+
WHEN pat.home_educated = true THEN 'Home-schooled'
43+
ELSE 'Unknown school'
44+
END AS patient_school_name,
45+
46+
-- Status flags
47+
ar.patient_id IS NOT NULL AS is_archived, -- Scope: .not_archived
48+
49+
-- Parent declared "already vaccinated" (counts toward vaccinated total)
50+
EXISTS (
51+
SELECT 1 FROM consents con
52+
WHERE con.patient_id = pps.patient_id
53+
AND con.programme_type = pps.programme_type
54+
AND con.academic_year = pps.academic_year
55+
AND con.invalidated_at IS NULL
56+
AND con.withdrawn_at IS NULL
57+
AND con.response = 1 -- refused
58+
AND con.reason_for_refusal = 1 -- already_vaccinated
59+
) AS has_already_vaccinated_consent
60+
61+
-- Source: pre-computed patient status per programme/year
62+
FROM patient_programme_statuses pps
63+
64+
-- Patient record (for demographics and exclusion checks)
65+
JOIN patients pat
66+
ON pat.id = pps.patient_id
67+
68+
-- Where the patient is enrolled this year (links to team via location)
69+
JOIN patient_locations pl
70+
ON pl.patient_id = pps.patient_id
71+
AND pl.academic_year = pps.academic_year
72+
73+
-- Which team owns this location (for team_id filtering)
74+
JOIN team_locations tl
75+
ON tl.location_id = pl.location_id
76+
AND tl.academic_year = pps.academic_year
77+
78+
-- Check if patient is archived by this team (LEFT: most aren't)
79+
LEFT JOIN archive_reasons ar
80+
ON ar.patient_id = pps.patient_id
81+
AND ar.team_id = tl.team_id
82+
83+
-- Patient's school (LEFT: home-educated patients have no school)
84+
LEFT JOIN locations school
85+
ON school.id = pat.school_id
86+
87+
-- School's local authority (LEFT: school may not have LA set)
88+
LEFT JOIN local_authorities la
89+
ON la.gias_code = school.gias_local_authority_code
90+
91+
-- Patient's direct local authority (LEFT: fallback when school has no LA)
92+
LEFT JOIN local_authorities pat_la
93+
ON pat_la.mhclg_code = pat.local_authority_mhclg_code
94+
95+
-- Exclude patients who shouldn't appear in any reports
96+
WHERE pat.invalidated_at IS NULL -- Merged/duplicate record
97+
AND pat.restricted_at IS NULL -- S31 restricted access
98+
AND pat.date_of_death IS NULL -- Deceased

0 commit comments

Comments
 (0)