From 5e5f53df2158b6b3c96d2f48b3fd423204dbbd79 Mon Sep 17 00:00:00 2001 From: Peter Yates Date: Tue, 8 Nov 2022 16:26:34 +0000 Subject: [PATCH 1/3] Rejig the analytics queries for multiple cohorts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Written under Nathan Easey's supervision! 😅 --- analytics_queries/Makefile | 11 ++-- analytics_queries/induction_tutors.sql | 21 ++++++++ analytics_queries/partnerships.sql | 24 +++++---- analytics_queries/schools.sql | 69 +++++++++++++++++--------- 4 files changed, 86 insertions(+), 39 deletions(-) create mode 100644 analytics_queries/induction_tutors.sql diff --git a/analytics_queries/Makefile b/analytics_queries/Makefile index 780c41fc68..94787e1ac1 100644 --- a/analytics_queries/Makefile +++ b/analytics_queries/Makefile @@ -1,7 +1,8 @@ -flatten_queries: +flatten_queries: mkdir -p /tmp/exports rm -f /tmp/exports/*.sql - cat declarations.sql | tr --delete '\n' > /tmp/exports/declarations.sql - cat participants.sql | tr --delete '\n' > /tmp/exports/participants.sql - cat partnerships.sql | tr --delete '\n' > /tmp/exports/partnerships.sql - cat schools.sql | tr --delete '\n' > /tmp/exports/schools.sql + cat declarations.sql | tr --delete '\n' > /tmp/exports/declarations.sql + cat participants.sql | tr --delete '\n' > /tmp/exports/participants.sql + cat induction_tutors.sql | tr --delete '\n' > /tmp/exports/induction_tutors.sql + cat partnerships.sql | tr --delete '\n' > /tmp/exports/partnerships.sql + cat schools.sql | tr --delete '\n' > /tmp/exports/schools.sql diff --git a/analytics_queries/induction_tutors.sql b/analytics_queries/induction_tutors.sql new file mode 100644 index 0000000000..bb93e016dc --- /dev/null +++ b/analytics_queries/induction_tutors.sql @@ -0,0 +1,21 @@ +\copy ( + SELECT s.urn, + (icp.id IS NOT NULL) AS tutor_nominated, + icp.created_at AS tutor_nominated_at, + (u.current_sign_in_at IS NOT NULL) AS tutor_signed_in, + u.full_name AS tutor_name, + u.email AS tutor_email, + (pp.id IS NOT NULL) AS sit_mentor, + u.id AS sit_mentor_id + + FROM schools s + + LEFT OUTER JOIN induction_coordinator_profiles_schools icps on s.id = icps.school_id + LEFT OUTER JOIN induction_coordinator_profiles icp on icps.induction_coordinator_profile_id = icp.id + LEFT OUTER JOIN users u on icp.user_id = u.id + LEFT OUTER JOIN teacher_profiles tp on u.id = tp.user_id + LEFT OUTER JOIN participant_profiles pp + ON tp.id = pp.teacher_profile_id + AND pp.status = 'active' + AND pp.type = 'ParticipantProfile::Mentor' +) to '/tmp/exports/induction_tutors.csv' with csv header; diff --git a/analytics_queries/partnerships.sql b/analytics_queries/partnerships.sql index b90920eec5..76742a12d0 100644 --- a/analytics_queries/partnerships.sql +++ b/analytics_queries/partnerships.sql @@ -1,12 +1,16 @@ \copy ( - SELECT s.urn as school_urn, - lp.name as lead_provider_name, - dp.name as delivery_partner_name, - p.created_at AS partnership_reported_at, - p.challenge_reason, - p.challenged_at - FROM partnerships p - JOIN lead_providers lp on lp.id = p.lead_provider_id - JOIN delivery_partners dp on p.delivery_partner_id = dp.id - JOIN schools s on p.school_id = s.id + SELECT s.urn as school_urn, + lp.name as lead_provider_name, + dp.name as delivery_partner_name, + p.created_at as partnership_reported_at, + p.challenge_reason, + p.challenged_at, + c.start_year as cohort, + p.relationship + FROM partnerships p + JOIN lead_providers lp on lp.id = p.lead_provider_id + JOIN delivery_partners dp on p.delivery_partner_id = dp.id + JOIN schools s on p.school_id = s.id + JOIN cohorts c on p.cohort_id = c.id + WHERE NOT p.relationship ) to '/tmp/exports/partnerships.csv' with csv header; diff --git a/analytics_queries/schools.sql b/analytics_queries/schools.sql index 01afbd25ab..4fc18b0131 100644 --- a/analytics_queries/schools.sql +++ b/analytics_queries/schools.sql @@ -1,28 +1,49 @@ \copy ( - SELECT DISTINCT s.urn, - sc.induction_programme_choice, - (p.id IS NOT NULL) as in_partnership, - lp.name as lead_provider_name, - dp.name as delivery_partner_name, - (icp.id IS NOT NULL) as tutor_nominated, - icp.created_at as tutor_nominated_at, - (u.current_sign_in_at IS NOT NULL) as tutor_signed_in, - u.full_name as tutor_name, - u.email as tutor_email, - (pp.id IS NOT NULL) as sit_mentor, - u.id as sit_mentor_id + with everything as ( + select + s.urn, + sc.induction_programme_choice, + (p.id is not null) as in_partnership, + lp.name as lead_provider_name, + dp.name as delivery_partner_name, + c.start_year as cohort FROM schools s - LEFT OUTER JOIN school_cohorts sc on s.id = sc.school_id - LEFT OUTER JOIN cohorts c on sc.cohort_id = c.id - LEFT OUTER JOIN partnerships p on s.id = p.school_id - LEFT OUTER JOIN lead_providers lp on p.lead_provider_id = lp.id - LEFT OUTER JOIN delivery_partners dp on p.delivery_partner_id = dp.id - LEFT OUTER JOIN induction_coordinator_profiles_schools icps on s.id = icps.school_id - LEFT OUTER JOIN induction_coordinator_profiles icp on icps.induction_coordinator_profile_id = icp.id - LEFT OUTER JOIN users u on icp.user_id = u.id - LEFT OUTER JOIN teacher_profiles tp on u.id = tp.user_id - LEFT OUTER JOIN participant_profiles pp on tp.id = pp.teacher_profile_id and pp.status = 'active' and - pp.type = 'ParticipantProfile::Mentor' - WHERE (c.start_year > 2020 OR c.id IS NULL) + + left outer join school_cohorts sc on s.id = sc.school_id + left outer join cohorts c on sc.cohort_id = c.id + left outer join partnerships p on s.id = p.school_id and c.id = p.cohort_id + left outer join lead_providers lp on p.lead_provider_id = lp.id + left outer join delivery_partners dp on p.delivery_partner_id = dp.id + + where (c.start_year > 2020 or c.id is null) + ), + just_2021 as ( + select * + from everything + where cohort = 2021 + ), + just_2022 as ( + select * + from everything + where cohort = 2022 + ) + select + everything.urn, + + just_2021.induction_programme_choice as "2021_induction_programme_choice", + just_2021.in_partnership as "2021_in_partnership", + just_2021.lead_provider_name as "2021_lead_provider_name", + just_2021.delivery_partner_name as "2021_delivery_partner_name", + + just_2022.induction_programme_choice as "2022_induction_programme_choice", + just_2022.in_partnership as "2022_in_partnership", + just_2022.lead_provider_name as "2022_lead_provider_name", + just_2022.delivery_partner_name as "2022_delivery_partner_name" + from + everything + left outer join + just_2021 on everything.urn = just_2021.urn + left outer join + just_2022 on everything.urn = just_2022.urn ) to '/tmp/exports/schools.csv' with csv header; From 5e557240cee9384c6bfd763d2ae7b05e2b89c994 Mon Sep 17 00:00:00 2001 From: Peter Yates Date: Tue, 15 Nov 2022 10:14:22 +0000 Subject: [PATCH 2/3] Only return unchallenged partnerships --- analytics_queries/schools.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/analytics_queries/schools.sql b/analytics_queries/schools.sql index 4fc18b0131..5757b57673 100644 --- a/analytics_queries/schools.sql +++ b/analytics_queries/schools.sql @@ -8,7 +8,7 @@ dp.name as delivery_partner_name, c.start_year as cohort - FROM schools s + from schools s left outer join school_cohorts sc on s.id = sc.school_id left outer join cohorts c on sc.cohort_id = c.id @@ -17,6 +17,7 @@ left outer join delivery_partners dp on p.delivery_partner_id = dp.id where (c.start_year > 2020 or c.id is null) + and p.challenged_at is null ), just_2021 as ( select * From 1e1fbbcef18e222586e2665392cf74448e6f5e46 Mon Sep 17 00:00:00 2001 From: Peter Yates Date: Mon, 5 Dec 2022 12:21:56 +0000 Subject: [PATCH 3/3] Use the participant identity's external id --- analytics_queries/induction_tutors.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/analytics_queries/induction_tutors.sql b/analytics_queries/induction_tutors.sql index bb93e016dc..d34379e9d6 100644 --- a/analytics_queries/induction_tutors.sql +++ b/analytics_queries/induction_tutors.sql @@ -6,7 +6,7 @@ u.full_name AS tutor_name, u.email AS tutor_email, (pp.id IS NOT NULL) AS sit_mentor, - u.id AS sit_mentor_id + pi.external_identifier AS sit_mentor_id FROM schools s @@ -18,4 +18,5 @@ ON tp.id = pp.teacher_profile_id AND pp.status = 'active' AND pp.type = 'ParticipantProfile::Mentor' + LEFT OUTER JOIN participant_identities pi on pp.participant_identity_id = pi.id ) to '/tmp/exports/induction_tutors.csv' with csv header;