diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java index d302e6ee4a0c..8d6fc1f302d6 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java @@ -30,6 +30,7 @@ import static java.lang.String.join; import static java.util.stream.Collectors.groupingBy; import static org.apache.commons.collections4.CollectionUtils.emptyIfNull; +import static org.apache.commons.collections4.CollectionUtils.isNotEmpty; import static org.hisp.dhis.analytics.AnalyticsTableType.TRACKED_ENTITY_INSTANCE; import static org.hisp.dhis.analytics.table.JdbcEventAnalyticsTableManager.EXPORTABLE_EVENT_STATUSES; import static org.hisp.dhis.analytics.util.AnalyticsUtils.getColumnType; @@ -176,25 +177,27 @@ private List getColumns( (Map>) params.getExtraParam("", PROGRAMS_BY_TET_KEY); List columns = new ArrayList<>(getFixedColumns()); - - String enrolledInProgramExpression = - """ - \s exists(select 1 from ${enrollment} en_0 \ - where en_0.trackedentityid = te.trackedentityid \ - and en_0.programid = ${programId})"""; - - emptyIfNull(programsByTetUid.get(trackedEntityType.getUid())) - .forEach( - program -> - columns.add( - AnalyticsTableColumn.builder() - .name(program.getUid()) - .dataType(BOOLEAN) - .selectExpression( - replaceQualify( - enrolledInProgramExpression, - Map.of("programId", String.valueOf(program.getId())))) - .build())); + List programs = programsByTetUid.get(trackedEntityType.getUid()); + + if (isNotEmpty(programs)) { + String enrolledInProgramExpression = + """ + \s exists(select 1 from ${enrollment} en_0 \ + where en_0.trackedentityid = te.trackedentityid \ + and en_0.programid = ${programId})"""; + + programs.forEach( + program -> + columns.add( + AnalyticsTableColumn.builder() + .name(program.getUid()) + .dataType(BOOLEAN) + .selectExpression( + replaceQualify( + enrolledInProgramExpression, + Map.of("programId", String.valueOf(program.getId())))) + .build())); + } List trackedEntityAttributes = getAllTrackedEntityAttributes(trackedEntityType, programsByTetUid) @@ -271,7 +274,6 @@ private List getFixedColumns() { columns.addAll(getOrganisationUnitLevelColumns()); columns.add(getOrganisationUnitNameHierarchyColumn()); columns.addAll(getFixedNonGroupByColumns()); - return columns; } @@ -308,18 +310,23 @@ public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTableParti left join analytics_rs_organisationunitgroupsetstructure ougs on te.organisationunitid=ougs.organisationunitid""", Map.of())); - ((List) - params.getExtraParam(trackedEntityType.getUid(), ALL_NON_CONFIDENTIAL_TET_ATTRIBUTES)) - .forEach( - tea -> - sql.append( - replaceQualify( - """ - \s left join ${trackedentityattributevalue} ${teaUid} on ${teaUid}.trackedentityid=te.trackedentityid \ - and ${teaUid}.trackedentityattributeid = ${teaId}""", - Map.of( - "teaUid", quote(tea.getUid()), - "teaId", String.valueOf(tea.getId()))))); + List attributes = + ((List) + params.getExtraParam(trackedEntityType.getUid(), ALL_NON_CONFIDENTIAL_TET_ATTRIBUTES)); + + if (isNotEmpty(attributes)) { + attributes.forEach( + tea -> + sql.append( + replaceQualify( + """ + \s left join ${trackedentityattributevalue} ${teaUid} on ${teaUid}.trackedentityid=te.trackedentityid \ + and ${teaUid}.trackedentityattributeid = ${teaId}""", + Map.of( + "teaUid", quote(tea.getUid()), + "teaId", String.valueOf(tea.getId()))))); + } + sql.append( replaceQualify( """