@@ -43,6 +43,7 @@ student_orgs as (
4343 dim_school .k_lea ,
4444 dim_school .k_school ,
4545 dim_school .school_id ,
46+ {{ gen_sourced_id(' school' ) }} as sourced_id,
4647 student_school .tenant_code
4748 from student_school
4849 join dim_school
@@ -51,8 +52,10 @@ student_orgs as (
5152student_orgs_agg as (
5253 select
5354 k_student,
54- k_lea,
55- listagg({{ gen_sourced_id(' school' ) }}, ' ,' ) as orgs
55+ listagg(distinct sourced_id, ' ,' ) as orgs,
56+ -- create columns for primary school extension
57+ max_by(sourced_id, is_primary_school, 1 )[0 ] as primary_school_sourced_id,
58+ max_by(sourced_id, entry_date, 1 )[0 ] as latest_school_sourced_id
5659 from student_orgs
5760 group by all
5861),
@@ -71,7 +74,7 @@ formatted as (
7174 true as " enabledUser" ,
7275 student_orgs_agg .orgs as " orgSourcedIds" ,
7376 ' student' as " role" ,
74- null :: varchar as " username" ,
77+ student_email . email_address as " username" ,
7578 user_ids .ids as " userIds" ,
7679 dim_student .first_name as " givenName" ,
7780 dim_student .last_name as " familyName" ,
@@ -85,11 +88,11 @@ formatted as (
8588 null ::string as " password" ,
8689 student_keys .natural_key as " metadata.edu.natural_key" ,
8790 null ::string as " metadata.edu.staff_classfication" ,
91+ coalesce(student_orgs_agg .primary_school_sourced_id , student_orgs_agg .latest_school_sourced_id ) as " metadata.edu.primary_school" ,
8892 dim_student .tenant_code
8993 from dim_student
9094 join student_keys
9195 on dim_student .k_student = student_keys .k_student
92- -- note that this join expands the grain by district in certain cases
9396 left join student_orgs_agg
9497 on dim_student .k_student = student_orgs_agg .k_student
9598 left join user_ids
@@ -101,4 +104,4 @@ formatted as (
101104 left join grade_level_xwalk
102105 on dim_student .grade_level = grade_level_xwalk .edfi_grade_level
103106)
104- select * from formatted
107+ select * from formatted
0 commit comments