diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java index e8e07dae548..dd1e6a441e7 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java @@ -752,9 +752,11 @@ protected String getColumnWithCte(QueryItem item, String suffix, CTEContext cteC if (cteDef.isRowContext()) { // Add additional status and exists columns for row context columns.add( - "(%s.%s IS NOT NULL) as %s" + "COALESCE(%s.rn = %s, false) as %s" .formatted( - cteDef.getAlias(programStageOffset), quote(colName), quote(alias + ".exists"))); + cteDef.getAlias(programStageOffset), + programStageOffset + 1, + quote(alias + ".exists"))); columns.add( "%s.eventstatus as %s" .formatted(cteDef.getAlias(programStageOffset), quote(alias + ".status")));