Skip to content

Commit d4651f2

Browse files
committed
overhaul use of temp table to rely on csf_teachers_trained instead [ci skip]
1 parent 7e38d75 commit d4651f2

File tree

1 file changed

+9
-46
lines changed

1 file changed

+9
-46
lines changed

aws/redshift/tables/regional_partner_stats_csf.sql

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,6 @@ drop table if exists analysis_pii.regional_partner_stats_csf;
1717
create table analysis_pii.regional_partner_stats_csf AS
1818

1919
with
20-
csf_teachers_trained_temp as
21-
(
22-
select distinct
23-
user_id, -- multiple entries per person (if they attended multiple workshops)
24-
u.studio_person_id,
25-
'CS Fundamentals'::varchar as course,
26-
school_year as school_year,
27-
regional_partner as regional_partner,
28-
regional_partner_id as regional_partner_id,
29-
trained_at as trained_at, -- this is the 'min' date from the teachers trained at table (the first time they were trained)
30-
workshop_date as workshop_date -- this is the date of the workshop (and the source of multiple entries per person)
31-
from
32-
(
33-
SELECT
34-
ctt.user_id,
35-
ctt.trained_at,
36-
pds.start as workshop_date,
37-
regional_partner_id::int,
38-
rp.name::varchar as regional_partner
39-
FROM
40-
analysis.csf_teachers_trained ctt
41-
LEFT JOIN dashboard_production_pii.pd_enrollments pde
42-
ON pde.user_id = ctt.user_id
43-
LEFT JOIN dashboard_production_pii.pd_attendances pda
44-
ON pda.pd_enrollment_id = pde.id
45-
LEFT JOIN dashboard_production_pii.pd_workshops pdw
46-
ON pdw.id = pde.pd_workshop_id
47-
AND course = 'CS Fundamentals'
48-
LEFT JOIN dashboard_production_pii.pd_sessions pds
49-
ON pds.pd_workshop_id = pdw.id
50-
LEFT JOIN dashboard_production_pii.regional_partners rp
51-
ON pdw.regional_partner_id = rp.id
52-
) csf_train
53-
JOIN analysis.training_school_years sy on csf_train.trained_at between sy.started_at and sy.ended_at
54-
JOIN dashboard_production.users u on u.id = csf_train.user_id
55-
),
5620
completed as
5721
(
5822
select
@@ -93,12 +57,12 @@ pd_facilitators as
9357
)
9458
SELECT distinct
9559
d.user_id,
96-
d.studio_person_id,
60+
u.studio_person_id,
9761
FIRST_VALUE(pde.first_name) OVER (PARTITION BY d.user_id ORDER BY (CASE WHEN pde.first_name IS NULL THEN 1 ELSE 2 END), pde.pd_workshop_id DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) as first_name,
9862
FIRST_VALUE(pde.last_name) OVER (PARTITION BY d.user_id ORDER BY (CASE WHEN pde.last_name IS NULL THEN 1 ELSE 2 END), pde.pd_workshop_id DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) as last_name,
9963
FIRST_VALUE(pde.email) OVER (PARTITION BY d.user_id ORDER BY (CASE WHEN pde.email IS NULL THEN 1 ELSE 2 END), pde.pd_workshop_id DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) as email,
100-
d.course,
101-
d.school_year as school_year_trained,
64+
'CS Fundamentals'::varchar as course,
65+
sy.school_year as school_year_trained,
10266
s.school_year as school_year_taught,
10367
s.script_name,
10468
CASE WHEN rp.name is null THEN 'No Partner' ELSE rp.name END as regional_partner_name,
@@ -116,7 +80,7 @@ pd_facilitators as
11680
CASE WHEN csfa.subject is null THEN 'Intro Workshop' else csfa.subject END as subject,
11781
CASE WHEN csfa.trained_by_regional_partner is null then 0 else csfa.trained_by_regional_partner END as trained_by_regional_partner,
11882
d.trained_at as trained_at,
119-
coalesce(d.workshop_date, csfa.workshop_date, d.trained_at) as workshop_date,
83+
coalesce(csfa.workshop_date, d.trained_at) as workshop_date,
12084
extract(month from csfa.workshop_date)::varchar(16) || '/'::varchar(2) || extract(day from csfa.workshop_date)::varchar(16) || '/'::varchar(2) || extract(year from csfa.workshop_date)::varchar(16) || ', id:'::varchar(2) || csfa.workshop_id::varchar(16) as workshop_id_year,
12185
pwf.facilitator_names,
12286
-- started and completed
@@ -133,7 +97,8 @@ pd_facilitators as
13397
-- student gender
13498
sa.students_female as students_female_total,
13599
sa.students_gender as students_gender_total
136-
FROM csf_teachers_trained_temp d
100+
FROM csf_teachers_trained d
101+
JOIN training_school_years sy on d.trained_at between sy.started_at and sy.ended_at
137102
-- school info
138103
LEFT JOIN dashboard_production_pii.users u -- users needed to get school_info_id
139104
ON d.user_id = u.id
@@ -145,9 +110,7 @@ pd_facilitators as
145110
-- LEFT JOIN analysis.csf_workshop_attendance csfa -- functions mostly to get the regional partner's location info and to decide whether the person was 'trained_by_partner'
146111
LEFT JOIN analysis.csf_workshop_attendance csfa
147112
ON csfa.user_id = d.user_id
148-
AND csfa.course = d.course
149-
AND csfa.school_year = d.school_year
150-
-- AND trunc(csfa.workshop_date) = d.trained_at -- this must be the PROBLEM!! investigat it
113+
AND csfa.school_year = sy.school_year
151114
AND csfa.not_attended = 0
152115
--pii tables (regional partner names, person names, emails, locations)
153116
LEFT JOIN pd_facilitators pwf
@@ -156,11 +119,11 @@ pd_facilitators as
156119
ON csfa.regional_partner_id = rp.id
157120
LEFT JOIN pd_enrollments_with_year pde -- only join pde if they are are trained by regional partner
158121
ON pde.user_id = d.user_id
159-
AND pde.school_year = d.school_year
122+
AND pde.school_year = sy.school_year
160123
-- analysis tables
161124
LEFT JOIN started s
162125
ON s.user_id = d.user_id
163-
AND s.school_year >= d.school_year
126+
AND s.school_year >= sy.school_year
164127
LEFT JOIN completed c
165128
ON c.user_id = d.user_id
166129
AND c.script_name = s.script_name

0 commit comments

Comments
 (0)