423 | | |
424 | | |
425 | | |
| 423 | == **[[span(style=color: #BF1B1B, 9:)]]** |
| 424 | |
| 425 | {{{ |
| 426 | WITH counted_events AS ( |
| 427 | select |
| 428 | s.student_id, |
| 429 | COUNT(distinct(se.calendar_event_id)) AS student_events, |
| 430 | COUNT(distinct(sje.calendar_event_id)) as subject_events |
| 431 | FROM |
| 432 | student s |
| 433 | LEFT join |
| 434 | student_event se ON s.student_id = se.student_id |
| 435 | left join |
| 436 | subject_at_term sat on sat.student_id = s.student_id |
| 437 | LEFT JOIN |
| 438 | subject_event sje ON sat.student_id = sje.student_id |
| 439 | LEFT JOIN |
| 440 | calendar_event ce ON se.calendar_event_id = ce.calendar_event_id or sje.calendar_event_id = ce.calendar_event_id |
| 441 | LEFT JOIN |
| 442 | repeating_event re ON ce.calendar_event_id = re.calendar_event_id |
| 443 | AND EXTRACT(YEAR FROM CURRENT_DATE) >= EXTRACT(YEAR FROM re.starting_date) |
| 444 | AND EXTRACT(YEAR FROM CURRENT_DATE) <= EXTRACT(YEAR FROM re.ending_date) |
| 445 | LEFT JOIN |
| 446 | non_repeating_event nre ON ce.calendar_event_id = nre.calendar_event_id |
| 447 | AND EXTRACT(YEAR FROM nre.event_date) = EXTRACT(YEAR FROM CURRENT_DATE) |
| 448 | GROUP BY |
| 449 | s.student_id |
| 450 | ) |
| 451 | |
| 452 | SELECT |
| 453 | AVG(student_events) AS average_student_events, |
| 454 | AVG(subject_events) AS average_subject_events |
| 455 | FROM |
| 456 | counted_events |
| 457 | |
| 458 | |
| 459 | }}} |
| 460 | |