From d2b94c79e026545d583f6f5765cec44bb2592e5c Mon Sep 17 00:00:00 2001 From: Ruhanga <41738040+Ruhanga@users.noreply.github.com> Date: Tue, 25 Jun 2024 09:52:00 +0300 Subject: [PATCH] Fixed patient attributes based on concepts (#15) --- analytics/dsl/flattening/queries/patients.sql | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/analytics/dsl/flattening/queries/patients.sql b/analytics/dsl/flattening/queries/patients.sql index 4e12077..0484853 100755 --- a/analytics/dsl/flattening/queries/patients.sql +++ b/analytics/dsl/flattening/queries/patients.sql @@ -36,18 +36,31 @@ SELECT person_address.longitude AS address_longitude, ( SELECT LISTAGG( - CONCAT_WS(': ', pa_type.name, + 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 - ), ' / ' - ) - 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 AND cn.locale_preferred = '1' AND cn.locale = 'en' AND cn.voided = '0' - WHERE pa.person_id = patient.patient_id + 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 ) AS attributes, person.dead AS dead, person.death_date AS death_date,