diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/datavalue/hibernate/HibernateDataExportStore.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/datavalue/hibernate/HibernateDataExportStore.java index 04f144e22961..73e322f1ae7c 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/datavalue/hibernate/HibernateDataExportStore.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/datavalue/hibernate/HibernateDataExportStore.java @@ -174,10 +174,9 @@ ou_ids AS ( ), ou_with_descendants_ids AS ( SELECT DISTINCT ou.organisationunitid - FROM organisationunit ou - LEFT JOIN organisationunit parent_ou ON (ou.path LIKE parent_ou.path || '%') - WHERE ou.organisationunitid IN (SELECT organisationunitid FROM ou_ids) - OR parent_ou.organisationunitid IN (SELECT organisationunitid FROM ou_ids) + FROM ou_ids + JOIN organisationunit root USING (organisationunitid) + JOIN organisationunit ou ON ou.path LIKE root.path || '%' ) SELECT de.uid AS deid, diff --git a/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/datavalue/hibernate/DataExportQueryBuilderTest.java b/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/datavalue/hibernate/DataExportQueryBuilderTest.java index 568f597ad5fc..56322cefa350 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/datavalue/hibernate/DataExportQueryBuilderTest.java +++ b/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/datavalue/hibernate/DataExportQueryBuilderTest.java @@ -298,10 +298,9 @@ ou_ids AS ( ), ou_with_descendants_ids AS ( SELECT DISTINCT ou.organisationunitid - FROM organisationunit ou - LEFT JOIN organisationunit parent_ou ON (ou.path LIKE parent_ou.path || '%') - WHERE ou.organisationunitid IN (SELECT organisationunitid FROM ou_ids) - OR parent_ou.organisationunitid IN (SELECT organisationunitid FROM ou_ids) + FROM ou_ids + JOIN organisationunit root USING (organisationunitid) + JOIN organisationunit ou ON ou.path LIKE root.path || '%' ) SELECT de.uid AS deid, @@ -391,10 +390,9 @@ ou_ids AS ( ), ou_with_descendants_ids AS ( SELECT DISTINCT ou.organisationunitid - FROM organisationunit ou - LEFT JOIN organisationunit parent_ou ON (ou.path LIKE parent_ou.path || '%') - WHERE ou.organisationunitid IN (SELECT organisationunitid FROM ou_ids) - OR parent_ou.organisationunitid IN (SELECT organisationunitid FROM ou_ids) + FROM ou_ids + JOIN organisationunit root USING (organisationunitid) + JOIN organisationunit ou ON ou.path LIKE root.path || '%' ) SELECT de.uid AS deid,