From 7ea0fceeb87e6fd6b038d4994f6b2511cffa3e83 Mon Sep 17 00:00:00 2001
From: Ruhanga <41738040+Ruhanga@users.noreply.github.com>
Date: Tue, 14 May 2024 17:20:11 +0300
Subject: [PATCH] KH-502: Sets, answers and concept mappings to be properly
flattened by the Concepts query (#7)
---
analytics/dsl/flattening/queries/concepts.sql | 44 +++++++++++++------
.../tables/openmrs/concept_answer.sql | 11 +++++
.../flattening/tables/openmrs/concept_set.sql | 10 +++++
.../analytics/changelogs/0001-init.xml | 10 ++---
4 files changed, 56 insertions(+), 19 deletions(-)
create mode 100644 analytics/dsl/flattening/tables/openmrs/concept_answer.sql
create mode 100644 analytics/dsl/flattening/tables/openmrs/concept_set.sql
diff --git a/analytics/dsl/flattening/queries/concepts.sql b/analytics/dsl/flattening/queries/concepts.sql
index 6c2bb32..c245a20 100755
--- a/analytics/dsl/flattening/queries/concepts.sql
+++ b/analytics/dsl/flattening/queries/concepts.sql
@@ -1,14 +1,30 @@
-SELECT `concept`.`concept_id` AS `concept_id`,
- `concept_reference_source`.`name` AS `Concept Mapping Source`,
- `concept_reference_term`.`code` AS `Concept Mapping Code`,
- `concept_reference_term`.`name` AS `Concept Mapping Name`,
- `concept_name`.`name` AS `name`,
- `concept_name`.`locale` AS `locale`,
- `concept_name`.`locale_preferred` AS `locale_preferred`,
- `concept`.`retired` AS `retired`,
- `concept`.`uuid` AS `uuid`
-FROM `concept`
-LEFT JOIN `concept_reference_map` `concept_reference_map` ON `concept`.`concept_id` = `concept_reference_map`.`concept_id`
-LEFT JOIN `concept_reference_term` `concept_reference_term` ON `concept_reference_map`.`concept_reference_term_id` = `concept_reference_term`.`concept_reference_term_id`
-LEFT JOIN `concept_reference_source` `concept_reference_source` ON `concept_reference_term`.`concept_source_id` = `concept_reference_source`.`concept_source_id`
-LEFT JOIN `concept_name` `concept_name` ON `concept`.`concept_id` = `concept_name`.`concept_id` AND `concept_name`.`locale` LIKE 'en' AND `concept_name`.`voided` = false AND `concept_name`.`locale_preferred` = true
\ No newline at end of file
+SELECT DISTINCT concept.concept_id AS concept_id,
+ LISTAGG(
+ CONCAT_WS(': ', concept_reference_source.name, concept_reference_term.code), ', '
+ ) AS concept_mappings_source_Codes,
+ concept_name.name AS name,
+ concept_name.locale AS locale,
+ concept_name.locale_preferred AS locale_preferred,
+ concept.retired AS retired,
+ concept.uuid AS uuid,
+ (
+ SELECT LISTAGG(c.uuid, ', ')
+ FROM concept AS c
+ INNER JOIN concept_answer AS ca ON c.concept_id = ca.answer_concept
+ WHERE ca.concept_id = concept.concept_id
+ ) AS question_concepts_uuids,
+ (
+ SELECT LISTAGG(c2.uuid, ', ')
+ FROM concept AS c2
+ INNER JOIN concept_set AS cs ON c2.concept_id = cs.concept_id
+ WHERE cs.concept_set = 75
+ ) AS set_concepts_uuids
+FROM concept concept
+LEFT JOIN concept_reference_map concept_reference_map ON concept.concept_id = concept_reference_map.concept_id
+LEFT JOIN concept_reference_term concept_reference_term ON concept_reference_map.concept_reference_term_id = concept_reference_term.concept_reference_term_id
+LEFT JOIN concept_reference_source concept_reference_source ON concept_reference_term.concept_source_id = concept_reference_source.concept_source_id
+LEFT JOIN concept_name concept_name ON concept.concept_id = concept_name.concept_id AND concept_name.locale LIKE 'en' AND concept_name.voided = false AND concept_name.locale_preferred = true
+LEFT JOIN concept_answer concept_answer ON concept.concept_id = concept_answer.answer_concept
+LEFT JOIN concept_set concept_set ON concept.concept_id = concept_set.concept_id
+GROUP BY
+ concept.concept_id, concept_name.name, concept_name.locale, concept_name.locale_preferred, concept.retired, concept.uuid;
\ No newline at end of file
diff --git a/analytics/dsl/flattening/tables/openmrs/concept_answer.sql b/analytics/dsl/flattening/tables/openmrs/concept_answer.sql
new file mode 100644
index 0000000..8b2f2cc
--- /dev/null
+++ b/analytics/dsl/flattening/tables/openmrs/concept_answer.sql
@@ -0,0 +1,11 @@
+ CREATE TABLE `concept_answer` (
+ `concept_answer_id` int,
+ `concept_id` int,
+ `answer_concept` int,
+ `answer_drug` int,
+ `creator` int,
+ `date_created` TIMESTAMP,
+ `sort_weight` DOUBLE,
+ `uuid` VARCHAR,
+ PRIMARY KEY (`concept_answer_id`) NOT ENFORCED
+)
\ No newline at end of file
diff --git a/analytics/dsl/flattening/tables/openmrs/concept_set.sql b/analytics/dsl/flattening/tables/openmrs/concept_set.sql
new file mode 100644
index 0000000..2310d5c
--- /dev/null
+++ b/analytics/dsl/flattening/tables/openmrs/concept_set.sql
@@ -0,0 +1,10 @@
+CREATE TABLE `concept_set` (
+ `concept_set_id` int,
+ `concept_id` int,
+ `concept_set` int,
+ `sort_weight` DOUBLE,
+ `creator` int,
+ `date_created` TIMESTAMP,
+ `uuid` VARCHAR,
+ PRIMARY KEY (`concept_set_id`) NOT ENFORCED
+)
\ No newline at end of file
diff --git a/analytics/liquibase/analytics/changelogs/0001-init.xml b/analytics/liquibase/analytics/changelogs/0001-init.xml
index dfd7b82..f3e46c8 100644
--- a/analytics/liquibase/analytics/changelogs/0001-init.xml
+++ b/analytics/liquibase/analytics/changelogs/0001-init.xml
@@ -78,19 +78,19 @@
-
+
-
-
-
+
-
+
+
+