In DHIS2 analytics, retrieving Enrollment data often requires derived fields (counts, last recorded values) from associated Event tables. Historically, this was done using correlated subqueries in the SELECT and WHERE clauses.
To improve performance in PostgreSQL and ensure compatibility with analytics engines like ClickHouse or Doris (which do not support referencing outer fields in subqueries), DHIS2 uses a Common Table Expression (CTE) approach.
Instead of running a subquery for every single row, the system:
- Computes required aggregates or latest values upfront in independent CTEs.
- Uses window functions like
ROW_NUMBER() OVER (PARTITION BY enrollment ORDER BY ...) within those CTEs to identify specific records (e.g., the most recent event). - Joins these CTE results to the main enrollment table using
LEFT JOIN on the enrollment ID.