From 923df3d14e2163fa4c787358e48b3cc293095323 Mon Sep 17 00:00:00 2001 From: ruhanga Date: Thu, 27 Jun 2024 13:10:41 +0300 Subject: [PATCH] KH-501: Attempted to fix attribute types not populating in stream mode --- analytics/dsl/flattening/queries/patients.sql | 64 +++++++++---------- analytics/dsl/flattening/queries/visits.sql | 51 +++++++-------- 2 files changed, 54 insertions(+), 61 deletions(-) diff --git a/analytics/dsl/flattening/queries/patients.sql b/analytics/dsl/flattening/queries/patients.sql index 0484853..91c4cec 100755 --- a/analytics/dsl/flattening/queries/patients.sql +++ b/analytics/dsl/flattening/queries/patients.sql @@ -3,11 +3,8 @@ SELECT person_name.given_name AS given_name, person_name.middle_name AS middle_name, person_name.family_name AS family_name, - ( - SELECT LISTAGG(CONCAT_WS(': ', identifier_type.name, identifier.identifier), ', ') - FROM patient_identifier identifier - LEFT JOIN patient_identifier_type identifier_type ON identifier.identifier_type = identifier_type.patient_identifier_type_id - WHERE patient.patient_id = identifier.patient_id + LISTAGG(DISTINCT + CONCAT_WS(': ', identifier_type.name, identifier.identifier), ', ' ) AS identifiers, person.gender AS gender, person.birthdate AS birthdate, @@ -34,33 +31,12 @@ SELECT person_address.address15 AS address_15, person_address.latitude AS address_latitude, person_address.longitude AS address_longitude, - ( - SELECT LISTAGG( - CONCAT_WS( - ': ', - a.attribute_type_name, - a.attribute_value - ), ' / ' - ) - FROM ( - SELECT DISTINCT - pa.person_id, - pa_type.name AS attribute_type_name, - CASE - WHEN pa_type.format = 'org.openmrs.Concept' THEN cn.name - ELSE pa.`value` - END AS attribute_value - FROM - person_attribute pa - LEFT JOIN person_attribute_type pa_type ON pa.person_attribute_type_id = pa_type.person_attribute_type_id - LEFT JOIN concept c ON pa.`value` = c.uuid - LEFT JOIN concept_name cn ON c.concept_id = cn.concept_id - WHERE - pa.person_id = patient.patient_id - AND cn.locale_preferred = '1' - AND cn.locale = 'en' - AND cn.voided = '0' - ) AS a + LISTAGG(DISTINCT + CONCAT_WS( + ': ', + a.attribute_type_name, + a.attribute_value + ), ' / ' ) AS attributes, person.dead AS dead, person.death_date AS death_date, @@ -74,6 +50,26 @@ FROM LEFT JOIN person ON patient.patient_id = person.person_id LEFT JOIN person_name ON person.person_id = person_name.person_id LEFT JOIN person_address ON person.person_id = person_address.person_id +LEFT JOIN ( + SELECT DISTINCT + pa.person_id, + pa_type.name AS attribute_type_name, + CASE + WHEN pa_type.format = 'org.openmrs.Concept' THEN cn.name + ELSE pa.`value` + END AS attribute_value + FROM + person_attribute pa + LEFT JOIN person_attribute_type pa_type ON pa.person_attribute_type_id = pa_type.person_attribute_type_id + LEFT JOIN concept c ON pa.`value` = c.uuid + LEFT JOIN concept_name cn ON c.concept_id = cn.concept_id + WHERE + cn.locale_preferred = '1' + AND cn.locale = 'en' + AND cn.voided = '0' +) a ON a.person_id = person.person_id +LEFT JOIN patient_identifier identifier ON patient.patient_id = identifier.patient_id +LEFT JOIN patient_identifier_type identifier_type ON identifier.identifier_type = identifier_type.patient_identifier_type_id GROUP BY patient.patient_id, person_name.given_name, @@ -82,6 +78,7 @@ GROUP BY person.gender, person.birthdate, person.birthdate_estimated, + person.uuid, person_address.city_village, person_address.county_district, person_address.state_province, @@ -109,5 +106,4 @@ GROUP BY person.creator, person.date_created, person.voided, - person.void_reason, - person.uuid \ No newline at end of file + person.void_reason \ No newline at end of file diff --git a/analytics/dsl/flattening/queries/visits.sql b/analytics/dsl/flattening/queries/visits.sql index 619df8c..36c8c54 100755 --- a/analytics/dsl/flattening/queries/visits.sql +++ b/analytics/dsl/flattening/queries/visits.sql @@ -5,33 +5,12 @@ SELECT visit.date_started AS date_started, visit.date_stopped AS date_stopped, visit_type.name AS type, - ( - SELECT LISTAGG( - CONCAT_WS( - ': ', - a.attribute_type_name, - a.attribute_value - ), ' / ' - ) - FROM ( - SELECT - va.visit_id, - vat.name AS attribute_type_name, - CASE - WHEN vat.datatype = 'org.openmrs.customdatatype.datatype.ConceptDatatype' THEN cn.name - ELSE va.value_reference - END AS attribute_value - FROM - visit_attribute va - LEFT JOIN visit_attribute_type vat ON va.attribute_type_id = vat.visit_attribute_type_id - LEFT JOIN concept c ON va.value_reference = c.uuid - LEFT JOIN concept_name cn ON c.concept_id = cn.concept_id - WHERE - va.visit_id = visit.visit_id - AND cn.locale_preferred = '1' - AND cn.locale = 'en' - AND cn.voided = '0' - ) AS a + LISTAGG(DISTINCT + CONCAT_WS( + ': ', + a.attribute_type_name, + a.attribute_value + ), ' / ' ) AS visit_attributes, person.gender AS patient_gender, person.birthdate AS patient_birthdate, @@ -51,6 +30,24 @@ FROM LEFT JOIN person person ON visit.patient_id = person.person_id LEFT JOIN person creator ON visit.creator = creator.person_id LEFT JOIN location location ON visit.location_id = location.location_id + LEFT JOIN ( + SELECT DISTINCT + va.visit_id, + vat.name AS attribute_type_name, + CASE + WHEN vat.datatype = 'org.openmrs.customdatatype.datatype.ConceptDatatype' THEN cn.name + ELSE va.value_reference + END AS attribute_value + FROM + visit_attribute va + LEFT JOIN visit_attribute_type vat ON va.attribute_type_id = vat.visit_attribute_type_id + LEFT JOIN concept c ON va.value_reference = c.uuid + LEFT JOIN concept_name cn ON c.concept_id = cn.concept_id + WHERE + cn.locale_preferred = '1' + AND cn.locale = 'en' + AND cn.voided = '0' + ) a ON a.visit_id = visit.visit_id GROUP BY visit.visit_id, visit.voided,