Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KH-501: Attempted to fix attribute types not populating in stream mode #17

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 30 additions & 34 deletions analytics/dsl/flattening/queries/patients.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -109,5 +106,4 @@ GROUP BY
person.creator,
person.date_created,
person.voided,
person.void_reason,
person.uuid
person.void_reason
51 changes: 24 additions & 27 deletions analytics/dsl/flattening/queries/visits.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down