From daeb87736e73f0f16dcb606b5284feb038bbecbd Mon Sep 17 00:00:00 2001 From: JieunSon96 Date: Tue, 4 Jun 2024 14:09:28 -0700 Subject: [PATCH 1/2] fixed selecting distinct queries based on id --- .../request_api/models/FOIMinistryRequests.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/request-management-api/request_api/models/FOIMinistryRequests.py b/request-management-api/request_api/models/FOIMinistryRequests.py index cb41b37c9..af3a27201 100644 --- a/request-management-api/request_api/models/FOIMinistryRequests.py +++ b/request-management-api/request_api/models/FOIMinistryRequests.py @@ -408,12 +408,12 @@ def getrequestssubquery(cls, groups, filterfields, keyword, additionalfilter, us #subquery for getting extension count subquery_extension_count = _session.query(FOIRequestExtension.foiministryrequest_id, func.count(distinct(FOIRequestExtension.foirequestextensionid)).filter(FOIRequestExtension.isactive == True).label('extensions')).group_by(FOIRequestExtension.foiministryrequest_id).subquery() - - #subquery for getting all, distinct oipcs for foiministry request + + #subquery for selecting distinct records based on foiministryrequest_id, grouping by foiministryrequestversion_id subquery_with_oipc_sql = """ - SELECT distinct on (foiministryrequest_id) foiministryrequest_id, foiministryrequestversion_id, outcomeid - FROM "FOIRequestOIPC" fo - order by foiministryrequest_id, foiministryrequestversion_id desc + SELECT distinct on (foiministryrequest_id) foiministryrequest_id, foiministryrequestversion_id, + CASE WHEN COUNT(outcomeid) = COUNT(*) THEN MAX(outcomeid) ELSE NULL END AS outcomeid FROM "FOIRequestOIPC" fo GROUP BY foiministryrequest_id, foiministryrequestversion_id + ORDER BY foiministryrequest_id, foiministryrequestversion_id DESC """ subquery_with_oipc = text(subquery_with_oipc_sql).columns(FOIRequestOIPC.foiministryrequest_id, FOIRequestOIPC.foiministryrequestversion_id, FOIRequestOIPC.outcomeid).alias("oipcnoneoutcomes") joincondition_oipc = [ @@ -662,7 +662,7 @@ def getrequestssubquery(cls, groups, filterfields, keyword, additionalfilter, us ), isouter=True ).filter(or_(FOIMinistryRequest.requeststatuslabel != StateName.closed.name, - and_(FOIMinistryRequest.isoipcreview == True, FOIMinistryRequest.requeststatusid == 3))) + and_(FOIMinistryRequest.isoipcreview == True, FOIMinistryRequest.requeststatusid == 3,subquery_with_oipc.c.outcomeid == None))) From 8af383b03b71f5da52f1de351f4758b1f5ef2b97 Mon Sep 17 00:00:00 2001 From: JieunSon96 Date: Wed, 5 Jun 2024 14:04:32 -0700 Subject: [PATCH 2/2] removed count func and replaced with exists to handle null values --- .../request_api/models/FOIMinistryRequests.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/request-management-api/request_api/models/FOIMinistryRequests.py b/request-management-api/request_api/models/FOIMinistryRequests.py index af3a27201..b31d1fc23 100644 --- a/request-management-api/request_api/models/FOIMinistryRequests.py +++ b/request-management-api/request_api/models/FOIMinistryRequests.py @@ -411,9 +411,9 @@ def getrequestssubquery(cls, groups, filterfields, keyword, additionalfilter, us #subquery for selecting distinct records based on foiministryrequest_id, grouping by foiministryrequestversion_id subquery_with_oipc_sql = """ - SELECT distinct on (foiministryrequest_id) foiministryrequest_id, foiministryrequestversion_id, - CASE WHEN COUNT(outcomeid) = COUNT(*) THEN MAX(outcomeid) ELSE NULL END AS outcomeid FROM "FOIRequestOIPC" fo GROUP BY foiministryrequest_id, foiministryrequestversion_id - ORDER BY foiministryrequest_id, foiministryrequestversion_id DESC + SELECT DISTINCT ON (foiministryrequest_id) foiministryrequest_id, foiministryrequestversion_id, + CASE WHEN EXISTS (SELECT 1 FROM "FOIRequestOIPC" WHERE fo.foiministryrequest_id = foiministryrequest_id AND fo.foiministryrequestversion_id = foiministryrequestversion_id AND outcomeid IS NULL) THEN NULL ELSE MAX(outcomeid) END AS outcomeid + FROM "FOIRequestOIPC" fo GROUP BY foiministryrequest_id, foiministryrequestversion_id ORDER BY foiministryrequest_id, foiministryrequestversion_id DESC """ subquery_with_oipc = text(subquery_with_oipc_sql).columns(FOIRequestOIPC.foiministryrequest_id, FOIRequestOIPC.foiministryrequestversion_id, FOIRequestOIPC.outcomeid).alias("oipcnoneoutcomes") joincondition_oipc = [