@@ -139,6 +139,7 @@ fill_positive_attendance as (
139139 fct_student_school_att .attendance_event_category ,
140140 ' {{ var("edu:attendance:in_attendance_code") }}' )
141141 end as attendance_event_category,
142+
142143 coalesce(
143144 case
144145 when is_enrolled = 1 then fct_student_school_att .is_absent
@@ -184,6 +185,20 @@ positive_attendance_deduped as (
184185 )
185186 }}
186187),
188+ consecutive_unexcused_absence as (
189+ select
190+ * ,
191+ row_number() over (partition by k_student, k_school, grouping order by calendar_date) as nth_consec_unexcused_absence
192+ from
193+ (select * ,
194+ dense_rank() over ( partition by k_student, k_school order by calendar_date )
195+ - dense_rank() over ( partition by k_student,k_school, attendance_event_category order by calendar_date)
196+ as grouping
197+ from positive_attendance_deduped
198+ )
199+ where attendance_event_category ilike ' %unexcused absence%'
200+
201+ ),
187202cumulatives as (
188203 select
189204 positive_attendance_deduped .k_student ,
@@ -198,23 +213,28 @@ cumulatives as (
198213 positive_attendance_deduped .is_absent ,
199214 positive_attendance_deduped .is_present ,
200215 positive_attendance_deduped .is_enrolled ,
201- sum (is_enrolled) over(
202- partition by k_student, k_school) as total_days_enrolled,
203- sum (is_absent) over(
204- partition by k_student, k_school
205- order by calendar_date) as cumulative_days_absent,
206- sum (is_present) over(
207- partition by k_student, k_school
208- order by calendar_date) as cumulative_days_attended,
209- sum (is_enrolled) over(
210- partition by k_student, k_school
211- order by calendar_date) as cumulative_days_enrolled,
216+ sum (positive_attendance_deduped . is_enrolled ) over(
217+ partition by positive_attendance_deduped . k_student , positive_attendance_deduped . k_school ) as total_days_enrolled,
218+ sum (positive_attendance_deduped . is_absent ) over(
219+ partition by positive_attendance_deduped . k_student , positive_attendance_deduped . k_school
220+ order by positive_attendance_deduped . calendar_date ) as cumulative_days_absent,
221+ sum (positive_attendance_deduped . is_present ) over(
222+ partition by positive_attendance_deduped . k_student , positive_attendance_deduped . k_school
223+ order by positive_attendance_deduped . calendar_date ) as cumulative_days_attended,
224+ sum (positive_attendance_deduped . is_enrolled ) over(
225+ partition by positive_attendance_deduped . k_student , positive_attendance_deduped . k_school
226+ order by positive_attendance_deduped . calendar_date ) as cumulative_days_enrolled,
212227 round(100 * cumulative_days_attended / nullif(cumulative_days_enrolled, 0 ), 2 ) as cumulative_attendance_rate,
213228 cumulative_days_enrolled >= {{ var(' edu:attendance:chronic_absence_min_days' ) }} as meets_enrollment_threshold,
214229 {{ msr_chronic_absentee(' cumulative_attendance_rate' , ' cumulative_days_enrolled' ) }} as is_chronic_absentee,
215230 positive_attendance_deduped .event_duration ,
216- positive_attendance_deduped .school_attendance_duration
231+ positive_attendance_deduped .school_attendance_duration ,
232+ nth_consec_unexcused_absence
217233 from positive_attendance_deduped
234+ left join consecutive_unexcused_absence
235+ on positive_attendance_deduped .k_student = consecutive_unexcused_absence .k_student
236+ and positive_attendance_deduped .k_student = consecutive_unexcused_absence .k_student
237+ and positive_attendance_deduped .calendar_date = consecutive_unexcused_absence .calendar_date
218238),
219239metric_labels as (
220240 select
@@ -231,27 +251,9 @@ metric_labels as (
231251 left join metric_absentee_categories
232252 on cumulative_attendance_rate > metric_absentee_categories .threshold_lower
233253 and cumulative_attendance_rate <= metric_absentee_categories .threshold_upper
234- ),
235- consecutive as (
236- select
237- metric_labels.* ,
238- -- logic that assigns a constant value for all consecutive records of the same `is_unexcused_absence` ordered by date per student
239- -- the first dense_rank() computes the ranking for records for each student
240- -- the second dense_rank() computes the ranking for records for each unique (student + `is_unexcused_absence`)
241- -- the difference between these two `dense_rank()`'s is constant for all consecutive records of the same `is_unexcused_absence` per student.
242- dense_rank() over ( partition by k_student, k_school order by calendar_date )
243- - dense_rank() over ( partition by k_student, k_school, attendance_event_category order by calendar_date) as grouping
244- from metric_labels
245- ),
246- final as (
247- select
248- * ,
249- -- the consecutive count of unexcused absence per student per school
250- row_number() over (partition by k_student, k_school, grouping order by calendar_date) as nth_consec_attendance_event
251- from consecutive
252254)
253255
254- select * from final
256+ select * from metric_labels
255257
256258
257259
0 commit comments