From 6d2231b6e83b272d0dc70e3f607dd1db05f32128 Mon Sep 17 00:00:00 2001 From: njorocs Date: Tue, 23 Jul 2024 13:39:14 +0300 Subject: [PATCH 1/5] Revised patient contact etls --- omod/src/main/resources/sql/DDL.sql | 61 ++++--- omod/src/main/resources/sql/DML.sql | 150 ++++++++++-------- omod/src/main/resources/sql/DataTools.sql | 32 ++-- .../main/resources/sql/Scheduled_Updates.sql | 101 ++++++++++++ omod/src/main/resources/sql/dwapi/DDL.sql | 61 ++++--- omod/src/main/resources/sql/dwapi/DML.sql | 137 +++++++++------- 6 files changed, 339 insertions(+), 203 deletions(-) diff --git a/omod/src/main/resources/sql/DDL.sql b/omod/src/main/resources/sql/DDL.sql index 19313cbf..bfd93063 100644 --- a/omod/src/main/resources/sql/DDL.sql +++ b/omod/src/main/resources/sql/DDL.sql @@ -2254,37 +2254,34 @@ CREATE TABLE kenyaemr_etl.etl_patient_program ( SELECT "Successfully created etl_cervical_cancer_screening table"; -- --------------------- creating patient contact table ------------------------------- - CREATE TABLE kenyaemr_etl.etl_patient_contact ( - id INT(11), - uuid CHAR(38), - date_created DATE, - first_name VARCHAR(255), - middle_name VARCHAR(255), - last_name VARCHAR(255), - sex VARCHAR(50), - birth_date DATETIME, - physical_address VARCHAR(255), - phone_contact VARCHAR(255), - patient_related_to INT(11), - patient_id INT(11), - relationship_type INT(11), - appointment_date DATETIME, - baseline_hiv_status VARCHAR(255), - reported_test_date DATETIME, - ipv_outcome VARCHAR(255), - marital_status VARCHAR(100), - living_with_patient VARCHAR(100), - pns_approach VARCHAR(100), - contact_listing_decline_reason VARCHAR(255), - consented_contact_listing VARCHAR(100), - date_last_modified DATETIME, - voided INT(11), - CONSTRAINT FOREIGN KEY (patient_related_to) REFERENCES kenyaemr_etl.etl_patient_demographics(patient_id), - CONSTRAINT unique_uuid UNIQUE(uuid), - INDEX(date_created), - INDEX(id), - INDEX(id, date_created) - ); +CREATE TABLE kenyaemr_etl.etl_patient_contact ( + encounter_id INT(11), + patient_id INT(11), + patient_related_to INT(11), + relationship_type INT(11), + date_created DATE, + start_date DATE, + end_date DATE, + physical_address VARCHAR(255), + baseline_hiv_status VARCHAR(255), + reported_test_date DATETIME, + living_with_patient VARCHAR(100), + pns_approach VARCHAR(100), + appointment_date DATETIME, + ipv_outcome VARCHAR(255), + contact_listing_decline_reason VARCHAR(255), + consented_contact_listing VARCHAR(100), + encounter_provider INT(11), + date_last_modified DATETIME, + location_id INT(11) DEFAULT NULL, + voided INT(11), + uuid CHAR(38), + CONSTRAINT FOREIGN KEY (patient_related_to) REFERENCES kenyaemr_etl.etl_patient_demographics(patient_id), + CONSTRAINT unique_uuid UNIQUE(uuid), + INDEX(date_created), + INDEX(patient_id), + INDEX(patient_id, date_created) +); SELECT "Successfully created etl_patient_contact table"; @@ -2304,7 +2301,7 @@ CREATE TABLE kenyaemr_etl.etl_patient_program ( remarks VARCHAR(255), appointment_date DATETIME, voided INT(11), - CONSTRAINT FOREIGN KEY (client_id) REFERENCES kenyaemr_etl.etl_patient_contact(id), + CONSTRAINT FOREIGN KEY (client_id) REFERENCES kenyaemr_etl.etl_patient_contact(patient_id), CONSTRAINT unique_uuid UNIQUE(uuid), INDEX(date_created), INDEX(id), diff --git a/omod/src/main/resources/sql/DML.sql b/omod/src/main/resources/sql/DML.sql index f4b2668e..ae50d26c 100644 --- a/omod/src/main/resources/sql/DML.sql +++ b/omod/src/main/resources/sql/DML.sql @@ -3005,7 +3005,7 @@ on t.patient_id = vl.patient_id; DROP TABLE IF EXISTS kenyaemr_etl.etl_hts_contacts; CREATE TABLE kenyaemr_etl.etl_hts_contacts AS -select c.id,c.patient_id,c.relationship_type,c.baseline_hiv_status,t.visit_date,t.test_type,t.test_1_result,t.test_2_result,t.final_test_result from +select c.patient_id,c.relationship_type,c.baseline_hiv_status,t.visit_date,t.test_type,t.test_1_result,t.test_2_result,t.final_test_result from kenyaemr_etl.etl_patient_contact c inner join kenyaemr_etl.etl_hts_test t on c.patient_id = t.patient_id group by c.patient_id; ALTER TABLE kenyaemr_etl.etl_hts_contacts ADD INDEX(id); @@ -3016,9 +3016,9 @@ ALTER TABLE kenyaemr_etl.etl_hts_contacts ADD INDEX(visit_date); DROP TABLE IF EXISTS kenyaemr_etl.etl_contacts_linked; CREATE TABLE kenyaemr_etl.etl_contacts_linked AS -select c.id,c.patient_id,c.relationship_type,c.baseline_hiv_status,l.visit_date,t.final_test_result from kenyaemr_etl.etl_patient_contact c inner join kenyaemr_etl.etl_hts_test t on c.patient_id = t.patient_id +select c.patient_id,c.relationship_type,c.baseline_hiv_status,l.visit_date,t.final_test_result from kenyaemr_etl.etl_patient_contact c inner join kenyaemr_etl.etl_hts_test t on c.patient_id = t.patient_id inner join kenyaemr_etl.etl_hts_referral_and_linkage l on c.patient_id=l.patient_id -group by c.id; +group by c.patient_id; ALTER TABLE kenyaemr_etl.etl_contacts_linked ADD INDEX(id); ALTER TABLE kenyaemr_etl.etl_contacts_linked ADD INDEX(visit_date); @@ -4949,64 +4949,86 @@ END $$ DROP PROCEDURE IF EXISTS sp_populate_etl_patient_contact $$ CREATE PROCEDURE sp_populate_etl_patient_contact() BEGIN - SELECT "Processing patient contact ", CONCAT("Time: ", NOW()); - INSERT INTO kenyaemr_etl.etl_patient_contact( - id, - uuid, - date_created, - date_last_modified, - first_name, - middle_name, - last_name, - sex, - birth_date, - physical_address, - phone_contact, - patient_related_to, - patient_id, - relationship_type, - appointment_date, - baseline_hiv_status, - reported_test_date, - ipv_outcome, - marital_status, - living_with_patient, - pns_approach, - contact_listing_decline_reason, - consented_contact_listing, - voided - ) - select - pc.id, - pc.uuid, - pc.date_created, - pc.date_changed as date_last_modified, - pc.first_name, - pc.middle_name, - pc.last_name, - pc.sex, - pc.birth_date, - pc.physical_address, - pc.phone_contact, - pc.patient_related_to, - pc.patient_id, - pc.relationship_type, - pc.appointment_date, - pc.baseline_hiv_status, - pc.reported_test_date, - pc.ipv_outcome, - pc.marital_status, - pc.living_with_patient, - pc.pns_approach, - pc.contact_listing_decline_reason, - pc.consented_contact_listing, - pc.voided - from kenyaemr_hiv_testing_patient_contact pc - inner join kenyaemr_etl.etl_patient_demographics dm on dm.patient_id=pc.patient_related_to and dm.voided=0 - where pc.voided=0 - ; - SELECT "Completed processing patient contact data ", CONCAT("Time: ", NOW()); - END $$ + SELECT "Processing patient contact ", CONCAT("Time: ", NOW()); + insert into kenyaemr_etl.etl_patient_contact ( + uuid, + date_created, + encounter_id, + encounter_provider, + location_id, + patient_id, + patient_related_to, + relationship_type, + start_date, + end_date, + date_last_modified, + voided + ) + select + p.uuid, + date(e.encounter_datetime) as date_created, + encounter_id, + e.creator as encounter_provider, + e.location_id, + r.patient_id, + r.patient_related_to, + r.relationship as relationship_type, + r.start_date, + r.end_date, + e.date_changed as date_last_modified, + e.voided + from encounter e + inner join + ( + select encounter_type_id, uuid, name from encounter_type where uuid='de1f9d67-b73e-4e1b-90d0-036166fc6995' + ) et on et.encounter_type_id=e.encounter_type + inner join (select r.person_a as patient_id,r.person_b as patient_related_to,r.relationship,r.start_date,r.end_date, r.date_created,r.date_changed,r.voided + from relationship r inner join relationship_type t on r.relationship = t.relationship_type_id group by r.person_a) r on e.patient_id = r.patient_id and r.voided = 0 + inner join person p on p.person_id=e.patient_id and p.voided=0 + where e.voided=0; + + update kenyaemr_etl.etl_patient_contact c + join + ( + select + pa.person_id, + max(if(pat.uuid='3ca03c84-632d-4e53-95ad-91f1bd9d96d6', pa.value, null)) as baseline_hiv_status, + max(if(pat.uuid='35a08d84-9f80-4991-92b4-c4ae5903536e', pa.value, null)) as living_with_patient, + max(if(pat.uuid='7c94bd35-fba7-4ef7-96f5-29c89a318fcf', pa.value, null)) as pns_approach + from person_attribute pa + inner join + ( + select + pat.person_attribute_type_id, + pat.name, + pat.uuid + from person_attribute_type pat + where pat.retired=0 + ) pat on pat.person_attribute_type_id = pa.person_attribute_type_id + and pat.uuid in ( + 'b2c38640-2603-4629-aebd-3b54f33f1e3a', -- phone_contact + '3ca03c84-632d-4e53-95ad-91f1bd9d96d6', -- baseline_hiv_status + '35a08d84-9f80-4991-92b4-c4ae5903536e', -- living_with_patient + '7c94bd35-fba7-4ef7-96f5-29c89a318fcf' -- pns_approach + ) + where pa.voided=0 + group by pa.person_id + ) att on att.person_id = c.patient_id + set c.baseline_hiv_status=att.baseline_hiv_status, + c.living_with_patient=att.living_with_patient, + c.pns_approach=att.pns_approach; + + update kenyaemr_etl.etl_patient_contact c + left outer join (select pa.person_id, + max(pa.address1) physical_address, + max(pa.date_changed) date_last_modified + from person_address pa + where voided=0 + group by person_id) pa on c.patient_id = pa.person_id + set c.physical_address=pa.physical_address; + + SELECT "Completed processing patient contact data ", CONCAT("Time: ", NOW()); +END $$ -- ------------------------- process contact trace ------------------------ @@ -5030,8 +5052,8 @@ CREATE PROCEDURE sp_populate_etl_client_trace() appointment_date, voided ) - select - ct.id, + select + ct.id, ct.uuid, ct.date_created, ct.date_changed as date_last_modified, @@ -5047,7 +5069,7 @@ CREATE PROCEDURE sp_populate_etl_client_trace() ct.voided from kenyaemr_hiv_testing_client_trace ct inner join person p on p.person_id = ct.client_id and p.voided=0 - inner join kenyaemr_etl.etl_patient_contact pc on pc.id=ct.client_id and ct.voided=0 + inner join kenyaemr_etl.etl_patient_contact pc on pc.patient_id=ct.client_id and ct.voided=0 where pc.voided=0 ; SELECT "Completed processing client trace data ", CONCAT("Time: ", NOW()); diff --git a/omod/src/main/resources/sql/DataTools.sql b/omod/src/main/resources/sql/DataTools.sql index fb39e18e..98e4548a 100644 --- a/omod/src/main/resources/sql/DataTools.sql +++ b/omod/src/main/resources/sql/DataTools.sql @@ -1272,31 +1272,29 @@ SELECT "Successfully created enhanced adherence table"; -- create table datatools_patient_contact create table kenyaemr_datatools.patient_contact as select - id, - uuid, - date_created, - first_name, - middle_name, - last_name, - sex, - birth_date, - physical_address, - phone_contact, - patient_related_to, + encounter_id, patient_id, + patient_related_to, (case relationship_type when 970 then "Mother" when 971 then "Father" when 1528 then "Child" when 973 then "Grandparent" when 972 then "Sibling" when 160639 then "Guardian" when 1527 then "Parent" when 5617 then "Spouse" when 162221 then "Co-wife" when 163565 then "Sexual partner" when 157351 then "Injectable drug user" when 166606 then "SNS" when 5622 then "Other" else "" end) as relationship_type, - appointment_date, + date_created, + start_date, + end_date, + physical_address, baseline_hiv_status, reported_test_date, + (case living_with_patient when 1065 then "Yes" when 1066 then "No" when 162570 then "Declined to Answer" else "" end) as living_with_patient, + (case pns_approach when 162284 then "Dual referral" when 160551 then "Passive referral" when 161642 then "Contract referral" when 163096 then "Provider referral" else "" end) as pns_approach, + appointment_date, ipv_outcome, - (case marital_status when 1057 then "Single" when 5555 then "Married Monogamous" when 159715 then "Married Polygamous" when 1058 then "Divorced" when 1059 then "Widowed" else "" end) as marital_status, - (case living_with_patient when 1065 then "Yes" when 1066 then "No" when 162570 then "Declined to Answer" else "" end) as living_with_patient, - (case pns_approach when 162284 then "Dual referral" when 160551 then "Passive referral" when 161642 then "Contract referral" when 163096 then "Provider referral" else "" end) as pns_approach, contact_listing_decline_reason, (case consented_contact_listing when 1065 then "Yes" when 1066 then "No" when 1067 then "Unknown" else "" end) as consented_contact_listing, + encounter_provider, + date_last_modified, + location_id, + uuid, voided from kenyaemr_etl.etl_patient_contact; - ALTER TABLE kenyaemr_datatools.patient_contact ADD PRIMARY KEY(id); + ALTER TABLE kenyaemr_datatools.patient_contact ADD PRIMARY KEY(patient_id); ALTER TABLE kenyaemr_datatools.patient_contact ADD FOREIGN KEY (patient_related_to) REFERENCES kenyaemr_datatools.patient_demographics(patient_id); ALTER TABLE kenyaemr_datatools.patient_contact ADD INDEX(date_created); SELECT "Successfully created patient_contact table"; @@ -1318,7 +1316,7 @@ SELECT "Successfully created enhanced adherence table"; appointment_date, voided from kenyaemr_etl.etl_client_trace; - ALTER TABLE kenyaemr_datatools.client_trace ADD FOREIGN KEY (client_id) REFERENCES kenyaemr_datatools.patient_contact(id); + ALTER TABLE kenyaemr_datatools.client_trace ADD FOREIGN KEY (client_id) REFERENCES kenyaemr_datatools.patient_contact(patient_id); ALTER TABLE kenyaemr_datatools.client_trace ADD INDEX(date_created); SELECT "Successfully created client_trace table"; diff --git a/omod/src/main/resources/sql/Scheduled_Updates.sql b/omod/src/main/resources/sql/Scheduled_Updates.sql index b95ad2ae..5a9762f1 100644 --- a/omod/src/main/resources/sql/Scheduled_Updates.sql +++ b/omod/src/main/resources/sql/Scheduled_Updates.sql @@ -5142,6 +5142,106 @@ clinical_notes=VALUES(clinical_notes),voided=VALUES(voided); SELECT "Completed processing CAXC screening", CONCAT("Time: ", NOW()); END $$ +DROP PROCEDURE IF EXISTS sp_update_etl_patient_contact $$ +CREATE PROCEDURE sp_update_etl_patient_contact(IN last_update_time DATETIME) +BEGIN +SELECT "Processing patient contact ", CONCAT("Time: ", NOW()); +insert into kenyaemr_etl.etl_patient_contact ( + uuid, + date_created, + start_date, + end_date, + encounter_id, + encounter_provider, + location_id, + date_last_modified, + patient_related_to, + patient_id, + relationship_type, + voided +) +select + p.uuid, + date(e.encounter_datetime) as date_created, + r.start_date, + r.end_date, + encounter_id, + e.creator as encounter_provider, + e.location_id, + e.date_changed as date_last_modified, + r.patient_related_to, + r.patient_id, + r.relationship as relationship_type, + e.voided +from encounter e + inner join + ( + select encounter_type_id, uuid, name from encounter_type where uuid='de1f9d67-b73e-4e1b-90d0-036166fc6995' + ) et on et.encounter_type_id=e.encounter_type + inner join (select r.person_a as patient_id,r.person_b as patient_related_to,r.relationship,r.start_date,r.end_date, r.date_created,r.date_changed,r.voided + from relationship r inner join relationship_type t on r.relationship = t.relationship_type_id group by r.person_a) r on e.patient_id = r.patient_id and r.voided = 0 + inner join person p on p.person_id=e.patient_id and p.voided=0 +where e.voided = 0 + and (e.date_created >= last_update_time +or e.date_changed >= last_update_time +or p.date_changed >= last_update_time +or p.date_created >= last_update_time +or r.date_created >= last_update_time +or r.date_changed >= last_update_time) +ON DUPLICATE KEY UPDATE + start_date=start_date, + end_date=end_date, + patient_related_to=patient_related_to, + relationship_type=relationship_type +; + +update kenyaemr_etl.etl_patient_contact c + join + ( + select + pa.person_id, + max(if(pat.uuid='3ca03c84-632d-4e53-95ad-91f1bd9d96d6', pa.value, null)) as baseline_hiv_status, + max(if(pat.uuid='35a08d84-9f80-4991-92b4-c4ae5903536e', pa.value, null)) as living_with_patient, + max(if(pat.uuid='7c94bd35-fba7-4ef7-96f5-29c89a318fcf', pa.value, null)) as pns_approach + from person_attribute pa + inner join + ( + select + pat.person_attribute_type_id, + pat.name, + pat.uuid + from person_attribute_type pat + where pat.retired=0 + ) pat on pat.person_attribute_type_id = pa.person_attribute_type_id + and pat.uuid in ( + 'b2c38640-2603-4629-aebd-3b54f33f1e3a', -- phone_contact + '3ca03c84-632d-4e53-95ad-91f1bd9d96d6', -- baseline_hiv_status + '35a08d84-9f80-4991-92b4-c4ae5903536e', -- living_with_patient + '7c94bd35-fba7-4ef7-96f5-29c89a318fcf' -- pns_approach + ) + where pa.voided=0 and (pa.date_changed >= last_update_time + or pa.date_created >= last_update_time) + group by pa.person_id + ) att on att.person_id = c.patient_id +set c.baseline_hiv_status=att.baseline_hiv_status, + c.living_with_patient=att.living_with_patient, + c.pns_approach=att.pns_approach; + +update kenyaemr_etl.etl_patient_contact c +left outer join (select pa.person_id, + max(pa.address1) physical_address, + max(pa.date_changed) date_last_modified, + max(date_created) as date_created + from person_address pa + where voided = 0 + and (pa.date_changed >= last_update_time + or pa.date_created >= last_update_time) + group by person_id) pa on c.patient_id = pa.person_id +set c.physical_address=pa.physical_address; + +SELECT "Completed processing Patient contact", CONCAT("Time: ", NOW()); +END $$ + DROP PROCEDURE IF EXISTS sp_update_etl_kp_contact $$ CREATE PROCEDURE sp_update_etl_kp_contact(IN last_update_time DATETIME) BEGIN @@ -9075,6 +9175,7 @@ CREATE PROCEDURE sp_scheduled_updates() CALL sp_update_etl_otz_activity(last_update_time); CALL sp_update_etl_ovc_enrolment(last_update_time); CALL sp_update_etl_cervical_cancer_screening(last_update_time); + CALL sp_update_etl_patient_contact(last_update_time); CALL sp_update_etl_kp_contact(last_update_time); CALL sp_update_etl_kp_client_enrollment(last_update_time); CALL sp_update_etl_kp_clinical_visit(last_update_time); diff --git a/omod/src/main/resources/sql/dwapi/DDL.sql b/omod/src/main/resources/sql/dwapi/DDL.sql index 86fb0a60..41b6daa7 100644 --- a/omod/src/main/resources/sql/dwapi/DDL.sql +++ b/omod/src/main/resources/sql/dwapi/DDL.sql @@ -2224,37 +2224,34 @@ CREATE TABLE dwapi_etl.etl_patient_program ( SELECT "Successfully created etl_cervical_cancer_screening table"; -- --------------------- creating patient contact table ------------------------------- - CREATE TABLE dwapi_etl.etl_patient_contact ( - id INT(11), - uuid CHAR(38), - date_created DATE, - first_name VARCHAR(255), - middle_name VARCHAR(255), - last_name VARCHAR(255), - sex VARCHAR(50), - birth_date DATETIME, - physical_address VARCHAR(255), - phone_contact VARCHAR(255), - patient_related_to INT(11), - patient_id INT(11), - relationship_type INT(11), - appointment_date DATETIME, - baseline_hiv_status VARCHAR(255), - reported_test_date DATETIME, - ipv_outcome VARCHAR(255), - marital_status VARCHAR(100), - living_with_patient VARCHAR(100), - pns_approach VARCHAR(100), - contact_listing_decline_reason VARCHAR(255), - consented_contact_listing VARCHAR(100), - date_last_modified DATETIME, - voided INT(11), - CONSTRAINT FOREIGN KEY (patient_related_to) REFERENCES dwapi_etl.etl_patient_demographics(patient_id), - CONSTRAINT unique_uuid UNIQUE(uuid), - INDEX(date_created), - INDEX(id), - INDEX(id, date_created) - ); +CREATE TABLE dwapi_etl.etl_patient_contact ( + encounter_id INT(11), + patient_id INT(11), + patient_related_to INT(11), + relationship_type INT(11), + date_created DATE, + start_date DATE, + end_date DATE, + physical_address VARCHAR(255), + baseline_hiv_status VARCHAR(255), + reported_test_date DATETIME, + living_with_patient VARCHAR(100), + pns_approach VARCHAR(100), + appointment_date DATETIME, + ipv_outcome VARCHAR(255), + contact_listing_decline_reason VARCHAR(255), + consented_contact_listing VARCHAR(100), + encounter_provider INT(11), + date_last_modified DATETIME, + location_id INT(11) DEFAULT NULL, + voided INT(11), + uuid CHAR(38), + CONSTRAINT FOREIGN KEY (patient_related_to) REFERENCES dwapi_etl.etl_patient_demographics(patient_id), + CONSTRAINT unique_uuid UNIQUE(uuid), + INDEX(date_created), + INDEX(patient_id), + INDEX(patient_id, date_created) +); SELECT "Successfully created etl_patient_contact table"; @@ -2274,7 +2271,7 @@ CREATE TABLE dwapi_etl.etl_patient_program ( remarks VARCHAR(255), appointment_date DATETIME, voided INT(11), - CONSTRAINT FOREIGN KEY (client_id) REFERENCES dwapi_etl.etl_patient_contact(id), + CONSTRAINT FOREIGN KEY (client_id) REFERENCES dwapi_etl.etl_patient_contact(patient_id), CONSTRAINT unique_uuid UNIQUE(uuid), INDEX(date_created), INDEX(id), diff --git a/omod/src/main/resources/sql/dwapi/DML.sql b/omod/src/main/resources/sql/dwapi/DML.sql index ebd99da5..4a5daf7e 100644 --- a/omod/src/main/resources/sql/dwapi/DML.sql +++ b/omod/src/main/resources/sql/dwapi/DML.sql @@ -4714,63 +4714,84 @@ DROP PROCEDURE IF EXISTS sp_populate_dwapi_patient_contact $$ CREATE PROCEDURE sp_populate_dwapi_patient_contact() BEGIN SELECT "Processing patient contact ", CONCAT("Time: ", NOW()); - INSERT INTO dwapi_etl.etl_patient_contact( - id, - uuid, - date_created, - date_last_modified, - first_name, - middle_name, - last_name, - sex, - birth_date, - physical_address, - phone_contact, - patient_related_to, - patient_id, - relationship_type, - appointment_date, - baseline_hiv_status, - reported_test_date, - ipv_outcome, - marital_status, - living_with_patient, - pns_approach, - contact_listing_decline_reason, - consented_contact_listing, - voided - ) - select - pc.id, - pc.uuid, - pc.date_created, - pc.date_changed as date_last_modified, - pc.first_name, - pc.middle_name, - pc.last_name, - pc.sex, - pc.birth_date, - pc.physical_address, - pc.phone_contact, - pc.patient_related_to, - pc.patient_id, - pc.relationship_type, - pc.appointment_date, - pc.baseline_hiv_status, - pc.reported_test_date, - pc.ipv_outcome, - pc.marital_status, - pc.living_with_patient, - pc.pns_approach, - pc.contact_listing_decline_reason, - pc.consented_contact_listing, - pc.voided - from kenyaemr_hiv_testing_patient_contact pc - inner join dwapi_etl.etl_patient_demographics dm on dm.patient_id=pc.patient_related_to and dm.voided=0 - where pc.voided=0 - ; - SELECT "Completed processing patient contact data ", CONCAT("Time: ", NOW()); - END $$ + insert into dwapi_etl.etl_patient_contact ( + uuid, + date_created, + encounter_id, + encounter_provider, + location_id, + patient_id, + patient_related_to, + relationship_type, + start_date, + end_date, + date_last_modified, + voided + ) + select + p.uuid, + date(e.encounter_datetime) as date_created, + encounter_id, + e.creator as encounter_provider, + e.location_id, + r.patient_id, + r.patient_related_to, + r.relationship as relationship_type, + r.start_date, + r.end_date, + e.date_changed as date_last_modified, + e.voided + from encounter e + inner join + ( + select encounter_type_id, uuid, name from encounter_type where uuid='de1f9d67-b73e-4e1b-90d0-036166fc6995' + ) et on et.encounter_type_id=e.encounter_type + inner join (select r.person_a as patient_id,r.person_b as patient_related_to,r.relationship,r.start_date,r.end_date, r.date_created,r.date_changed,r.voided + from relationship r inner join relationship_type t on r.relationship = t.relationship_type_id group by r.person_a) r on e.patient_id = r.patient_id and r.voided = 0 + inner join person p on p.person_id=e.patient_id and p.voided=0; + + update dwapi_etl.etl_patient_contact c + join + ( + select + pa.person_id, + max(if(pat.uuid='3ca03c84-632d-4e53-95ad-91f1bd9d96d6', pa.value, null)) as baseline_hiv_status, + max(if(pat.uuid='35a08d84-9f80-4991-92b4-c4ae5903536e', pa.value, null)) as living_with_patient, + max(if(pat.uuid='7c94bd35-fba7-4ef7-96f5-29c89a318fcf', pa.value, null)) as pns_approach + from person_attribute pa + inner join + ( + select + pat.person_attribute_type_id, + pat.name, + pat.uuid + from person_attribute_type pat + where pat.retired=0 + ) pat on pat.person_attribute_type_id = pa.person_attribute_type_id + and pat.uuid in ( + 'b2c38640-2603-4629-aebd-3b54f33f1e3a', -- phone_contact + '3ca03c84-632d-4e53-95ad-91f1bd9d96d6', -- baseline_hiv_status + '35a08d84-9f80-4991-92b4-c4ae5903536e', -- living_with_patient + '7c94bd35-fba7-4ef7-96f5-29c89a318fcf' -- pns_approach + ) + where pa.voided=0 + group by pa.person_id + ) att on att.person_id = c.patient_id + set c.baseline_hiv_status=att.baseline_hiv_status, + c.living_with_patient=att.living_with_patient, + c.pns_approach=att.pns_approach; + + update dwapi_etl.etl_patient_contact c + left outer join (select pa.person_id, + max(pa.address1) physical_address, + max(pa.date_changed) date_last_modified + from person_address pa + where voided=0 + group by person_id) pa on c.patient_id = pa.person_id + set c.physical_address=pa.physical_address; + + SELECT "Completed processing patient contact data ", CONCAT("Time: ", NOW()); +END $$ -- ------------------------- process contact trace ------------------------ @@ -4811,7 +4832,7 @@ CREATE PROCEDURE sp_populate_dwapi_client_trace() ct.voided from kenyaemr_hiv_testing_client_trace ct inner join person p on p.person_id = ct.client_id and p.voided=0 - inner join dwapi_etl.etl_patient_contact pc on pc.id=ct.client_id + inner join dwapi_etl.etl_patient_contact pc on pc.patient_id=ct.client_id ; SELECT "Completed processing client trace data ", CONCAT("Time: ", NOW()); END $$ From 5025f448a9105d183ea40ef7422695e7875ca534 Mon Sep 17 00:00:00 2001 From: njorocs Date: Mon, 29 Jul 2024 16:54:20 +0300 Subject: [PATCH 2/5] Refactoring Patient contact etl to align with revised implementation --- omod/src/main/resources/sql/DML.sql | 29 +++++++++---------- .../main/resources/sql/Scheduled_Updates.sql | 28 +++++++++--------- omod/src/main/resources/sql/dwapi/DML.sql | 13 +++++---- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/omod/src/main/resources/sql/DML.sql b/omod/src/main/resources/sql/DML.sql index ae50d26c..7a9d5ba8 100644 --- a/omod/src/main/resources/sql/DML.sql +++ b/omod/src/main/resources/sql/DML.sql @@ -3008,7 +3008,6 @@ CREATE TABLE kenyaemr_etl.etl_hts_contacts AS select c.patient_id,c.relationship_type,c.baseline_hiv_status,t.visit_date,t.test_type,t.test_1_result,t.test_2_result,t.final_test_result from kenyaemr_etl.etl_patient_contact c inner join kenyaemr_etl.etl_hts_test t on c.patient_id = t.patient_id group by c.patient_id; -ALTER TABLE kenyaemr_etl.etl_hts_contacts ADD INDEX(id); ALTER TABLE kenyaemr_etl.etl_hts_contacts ADD INDEX(patient_id); ALTER TABLE kenyaemr_etl.etl_hts_contacts ADD INDEX(visit_date); @@ -3020,7 +3019,7 @@ select c.patient_id,c.relationship_type,c.baseline_hiv_status,l.visit_date,t.fin inner join kenyaemr_etl.etl_hts_referral_and_linkage l on c.patient_id=l.patient_id group by c.patient_id; -ALTER TABLE kenyaemr_etl.etl_contacts_linked ADD INDEX(id); +ALTER TABLE kenyaemr_etl.etl_contacts_linked ADD INDEX(patient_id); ALTER TABLE kenyaemr_etl.etl_contacts_linked ADD INDEX(visit_date); SELECT "Completed processing dashboard indicators", CONCAT("Time: ", NOW()); @@ -4970,7 +4969,7 @@ CREATE PROCEDURE sp_populate_etl_patient_contact() encounter_id, e.creator as encounter_provider, e.location_id, - r.patient_id, + r.patient_contact, r.patient_related_to, r.relationship as relationship_type, r.start_date, @@ -4982,10 +4981,10 @@ CREATE PROCEDURE sp_populate_etl_patient_contact() ( select encounter_type_id, uuid, name from encounter_type where uuid='de1f9d67-b73e-4e1b-90d0-036166fc6995' ) et on et.encounter_type_id=e.encounter_type - inner join (select r.person_a as patient_id,r.person_b as patient_related_to,r.relationship,r.start_date,r.end_date, r.date_created,r.date_changed,r.voided - from relationship r inner join relationship_type t on r.relationship = t.relationship_type_id group by r.person_a) r on e.patient_id = r.patient_id and r.voided = 0 + inner join (select r.person_a as patient_related_to,r.person_b as patient_contact,r.relationship,r.start_date,r.end_date, r.date_created,r.date_changed,r.voided + from relationship r inner join relationship_type t on r.relationship = t.relationship_type_id) r on e.patient_id = r.patient_contact and r.voided = 0 inner join person p on p.person_id=e.patient_id and p.voided=0 - where e.voided=0; + where e.voided=0 group by patient_contact, relationship_type, patient_related_to; update kenyaemr_etl.etl_patient_contact c join @@ -4994,7 +4993,7 @@ CREATE PROCEDURE sp_populate_etl_patient_contact() pa.person_id, max(if(pat.uuid='3ca03c84-632d-4e53-95ad-91f1bd9d96d6', pa.value, null)) as baseline_hiv_status, max(if(pat.uuid='35a08d84-9f80-4991-92b4-c4ae5903536e', pa.value, null)) as living_with_patient, - max(if(pat.uuid='7c94bd35-fba7-4ef7-96f5-29c89a318fcf', pa.value, null)) as pns_approach + max(if(pat.uuid='59d1b886-90c8-4f7f-9212-08b20a9ee8cf', pa.value, null)) as pns_approach from person_attribute pa inner join ( @@ -5009,7 +5008,7 @@ CREATE PROCEDURE sp_populate_etl_patient_contact() 'b2c38640-2603-4629-aebd-3b54f33f1e3a', -- phone_contact '3ca03c84-632d-4e53-95ad-91f1bd9d96d6', -- baseline_hiv_status '35a08d84-9f80-4991-92b4-c4ae5903536e', -- living_with_patient - '7c94bd35-fba7-4ef7-96f5-29c89a318fcf' -- pns_approach + '59d1b886-90c8-4f7f-9212-08b20a9ee8cf' -- pns_approach ) where pa.voided=0 group by pa.person_id @@ -7701,14 +7700,14 @@ DROP PROCEDURE IF EXISTS sp_populate_etl_patient_appointment $$ CREATE PROCEDURE sp_populate_etl_patient_appointment() BEGIN SELECT "Processing Patient appointment"; - INSERT INTO kenyaemr_etl.etl_patient_appointment(patient_appointment_id, - provider_id, - patient_id, - visit_date, - start_date_time, - end_date_time, + INSERT INTO kenyaemr_etl.etl_patient_appointment(patient_appointment_id, + provider_id, + patient_id, + visit_date, + start_date_time, + end_date_time, appointment_service_id, - status, + status, location_id, date_created) SELECT diff --git a/omod/src/main/resources/sql/Scheduled_Updates.sql b/omod/src/main/resources/sql/Scheduled_Updates.sql index 5a9762f1..f694ac2c 100644 --- a/omod/src/main/resources/sql/Scheduled_Updates.sql +++ b/omod/src/main/resources/sql/Scheduled_Updates.sql @@ -5170,7 +5170,7 @@ select e.location_id, e.date_changed as date_last_modified, r.patient_related_to, - r.patient_id, + r.patient_contact, r.relationship as relationship_type, e.voided from encounter e @@ -5178,8 +5178,8 @@ from encounter e ( select encounter_type_id, uuid, name from encounter_type where uuid='de1f9d67-b73e-4e1b-90d0-036166fc6995' ) et on et.encounter_type_id=e.encounter_type - inner join (select r.person_a as patient_id,r.person_b as patient_related_to,r.relationship,r.start_date,r.end_date, r.date_created,r.date_changed,r.voided - from relationship r inner join relationship_type t on r.relationship = t.relationship_type_id group by r.person_a) r on e.patient_id = r.patient_id and r.voided = 0 + inner join (select r.person_a as patient_related_to,r.person_b as patient_contact,r.relationship,r.start_date,r.end_date, r.date_created,r.date_changed,r.voided + from relationship r inner join relationship_type t on r.relationship = t.relationship_type_id) r on e.patient_id = r.patient_contact and r.voided = 0 inner join person p on p.person_id=e.patient_id and p.voided=0 where e.voided = 0 and (e.date_created >= last_update_time @@ -5188,12 +5188,12 @@ or p.date_changed >= last_update_time or p.date_created >= last_update_time or r.date_created >= last_update_time or r.date_changed >= last_update_time) +group by patient_contact, relationship_type, patient_related_to ON DUPLICATE KEY UPDATE start_date=start_date, end_date=end_date, patient_related_to=patient_related_to, - relationship_type=relationship_type -; + relationship_type=relationship_type; update kenyaemr_etl.etl_patient_contact c join @@ -5202,7 +5202,7 @@ update kenyaemr_etl.etl_patient_contact c pa.person_id, max(if(pat.uuid='3ca03c84-632d-4e53-95ad-91f1bd9d96d6', pa.value, null)) as baseline_hiv_status, max(if(pat.uuid='35a08d84-9f80-4991-92b4-c4ae5903536e', pa.value, null)) as living_with_patient, - max(if(pat.uuid='7c94bd35-fba7-4ef7-96f5-29c89a318fcf', pa.value, null)) as pns_approach + max(if(pat.uuid='59d1b886-90c8-4f7f-9212-08b20a9ee8cf', pa.value, null)) as pns_approach from person_attribute pa inner join ( @@ -5217,7 +5217,7 @@ update kenyaemr_etl.etl_patient_contact c 'b2c38640-2603-4629-aebd-3b54f33f1e3a', -- phone_contact '3ca03c84-632d-4e53-95ad-91f1bd9d96d6', -- baseline_hiv_status '35a08d84-9f80-4991-92b4-c4ae5903536e', -- living_with_patient - '7c94bd35-fba7-4ef7-96f5-29c89a318fcf' -- pns_approach + '59d1b886-90c8-4f7f-9212-08b20a9ee8cf' -- pns_approach ) where pa.voided=0 and (pa.date_changed >= last_update_time or pa.date_created >= last_update_time) @@ -8814,14 +8814,14 @@ DROP PROCEDURE IF EXISTS sp_update_etl_patient_appointments $$ CREATE PROCEDURE sp_update_etl_patient_appointments(IN last_update_time DATETIME) BEGIN SELECT "Processing Patient appointment updates"; -INSERT INTO kenyaemr_etl.etl_patient_appointment(patient_appointment_id, - provider_id, - patient_id, - visit_date, - start_date_time, - end_date_time, +INSERT INTO kenyaemr_etl.etl_patient_appointment(patient_appointment_id, + provider_id, + patient_id, + visit_date, + start_date_time, + end_date_time, appointment_service_id, - status, + status, location_id, date_created) SELECT diff --git a/omod/src/main/resources/sql/dwapi/DML.sql b/omod/src/main/resources/sql/dwapi/DML.sql index 4a5daf7e..7305be14 100644 --- a/omod/src/main/resources/sql/dwapi/DML.sql +++ b/omod/src/main/resources/sql/dwapi/DML.sql @@ -4734,7 +4734,7 @@ CREATE PROCEDURE sp_populate_dwapi_patient_contact() encounter_id, e.creator as encounter_provider, e.location_id, - r.patient_id, + r.patient_contact, r.patient_related_to, r.relationship as relationship_type, r.start_date, @@ -4746,9 +4746,10 @@ CREATE PROCEDURE sp_populate_dwapi_patient_contact() ( select encounter_type_id, uuid, name from encounter_type where uuid='de1f9d67-b73e-4e1b-90d0-036166fc6995' ) et on et.encounter_type_id=e.encounter_type - inner join (select r.person_a as patient_id,r.person_b as patient_related_to,r.relationship,r.start_date,r.end_date, r.date_created,r.date_changed,r.voided - from relationship r inner join relationship_type t on r.relationship = t.relationship_type_id group by r.person_a) r on e.patient_id = r.patient_id and r.voided = 0 - inner join person p on p.person_id=e.patient_id and p.voided=0; + inner join (select r.person_a as patient_related_to,r.person_b as patient_contact,r.relationship,r.start_date,r.end_date, r.date_created,r.date_changed,r.voided + from relationship r inner join relationship_type t on r.relationship = t.relationship_type_id) r on e.patient_id = r.patient_contact and r.voided = 0 + inner join person p on p.person_id=e.patient_id and p.voided=0 + where e.voided=0 group by patient_contact, relationship_type,patient_related_to; update dwapi_etl.etl_patient_contact c join @@ -4757,7 +4758,7 @@ CREATE PROCEDURE sp_populate_dwapi_patient_contact() pa.person_id, max(if(pat.uuid='3ca03c84-632d-4e53-95ad-91f1bd9d96d6', pa.value, null)) as baseline_hiv_status, max(if(pat.uuid='35a08d84-9f80-4991-92b4-c4ae5903536e', pa.value, null)) as living_with_patient, - max(if(pat.uuid='7c94bd35-fba7-4ef7-96f5-29c89a318fcf', pa.value, null)) as pns_approach + max(if(pat.uuid='59d1b886-90c8-4f7f-9212-08b20a9ee8cf', pa.value, null)) as pns_approach from person_attribute pa inner join ( @@ -4772,7 +4773,7 @@ CREATE PROCEDURE sp_populate_dwapi_patient_contact() 'b2c38640-2603-4629-aebd-3b54f33f1e3a', -- phone_contact '3ca03c84-632d-4e53-95ad-91f1bd9d96d6', -- baseline_hiv_status '35a08d84-9f80-4991-92b4-c4ae5903536e', -- living_with_patient - '7c94bd35-fba7-4ef7-96f5-29c89a318fcf' -- pns_approach + '59d1b886-90c8-4f7f-9212-08b20a9ee8cf' -- pns_approach ) where pa.voided=0 group by pa.person_id From d361d3cb282096d4f355c7ee44735ea4176d2874 Mon Sep 17 00:00:00 2001 From: njorocs Date: Tue, 30 Jul 2024 11:42:40 +0300 Subject: [PATCH 3/5] Removed deprecated id colum from dwapi dml --- omod/src/main/resources/sql/dwapi/DML.sql | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/omod/src/main/resources/sql/dwapi/DML.sql b/omod/src/main/resources/sql/dwapi/DML.sql index 7305be14..d7decfb8 100644 --- a/omod/src/main/resources/sql/dwapi/DML.sql +++ b/omod/src/main/resources/sql/dwapi/DML.sql @@ -2843,10 +2843,9 @@ SELECT "Processing hts patient contacts"; DROP TABLE IF EXISTS dwapi_etl.etl_hts_contacts; CREATE TABLE dwapi_etl.etl_hts_contacts AS -select c.uuid,c.id,c.patient_id,c.relationship_type,c.baseline_hiv_status,t.visit_date,t.test_type,t.test_1_result,t.test_2_result,t.final_test_result from +select c.uuid,c.patient_id,c.relationship_type,c.baseline_hiv_status,t.visit_date,t.test_type,t.test_1_result,t.test_2_result,t.final_test_result from dwapi_etl.etl_patient_contact c inner join dwapi_etl.etl_hts_test t on c.patient_id = t.patient_id group by c.patient_id; -ALTER TABLE dwapi_etl.etl_hts_contacts ADD INDEX(id); ALTER TABLE dwapi_etl.etl_hts_contacts ADD INDEX(patient_id); ALTER TABLE dwapi_etl.etl_hts_contacts ADD INDEX(visit_date); @@ -2854,11 +2853,11 @@ ALTER TABLE dwapi_etl.etl_hts_contacts ADD INDEX(visit_date); DROP TABLE IF EXISTS dwapi_etl.etl_contacts_linked; CREATE TABLE dwapi_etl.etl_contacts_linked AS -select c.id,c.patient_id,c.relationship_type,c.baseline_hiv_status,l.visit_date,t.final_test_result from dwapi_etl.etl_patient_contact c inner join dwapi_etl.etl_hts_test t on c.patient_id = t.patient_id +select c.patient_id,c.relationship_type,c.baseline_hiv_status,l.visit_date,t.final_test_result from dwapi_etl.etl_patient_contact c inner join dwapi_etl.etl_hts_test t on c.patient_id = t.patient_id inner join dwapi_etl.etl_hts_referral_and_linkage l on c.patient_id=l.patient_id -group by c.id; +group by c.patient_id; -ALTER TABLE dwapi_etl.etl_contacts_linked ADD INDEX(id); +ALTER TABLE dwapi_etl.etl_contacts_linked ADD INDEX(patient_id); ALTER TABLE dwapi_etl.etl_contacts_linked ADD INDEX(visit_date); SELECT "Completed processing hts patient contacts", CONCAT("Time: ", NOW()); From db698a97251678e00c0c403f32888a2c476f0ffc Mon Sep 17 00:00:00 2001 From: njorocs Date: Fri, 9 Aug 2024 15:50:59 +0300 Subject: [PATCH 4/5] Improving etl fetch for patient_contact --- omod/src/main/resources/sql/DML.sql | 57 +++++++++++------- .../main/resources/sql/Scheduled_Updates.sql | 60 +++++++++++-------- omod/src/main/resources/sql/dwapi/DML.sql | 57 +++++++++++------- 3 files changed, 109 insertions(+), 65 deletions(-) diff --git a/omod/src/main/resources/sql/DML.sql b/omod/src/main/resources/sql/DML.sql index 7a9d5ba8..4103bad1 100644 --- a/omod/src/main/resources/sql/DML.sql +++ b/omod/src/main/resources/sql/DML.sql @@ -4963,28 +4963,45 @@ CREATE PROCEDURE sp_populate_etl_patient_contact() date_last_modified, voided ) - select - p.uuid, - date(e.encounter_datetime) as date_created, - encounter_id, - e.creator as encounter_provider, - e.location_id, - r.patient_contact, - r.patient_related_to, - r.relationship as relationship_type, - r.start_date, - r.end_date, - e.date_changed as date_last_modified, - e.voided + select p.uuid, + date(e.encounter_datetime) as date_created, + encounter_id, + e.creator as encounter_provider, + e.location_id, + r.patient_contact, + r.patient_related_to, + r.relationship as relationship_type, + r.start_date, + r.end_date, + e.date_changed as date_last_modified, + e.voided from encounter e inner join - ( - select encounter_type_id, uuid, name from encounter_type where uuid='de1f9d67-b73e-4e1b-90d0-036166fc6995' - ) et on et.encounter_type_id=e.encounter_type - inner join (select r.person_a as patient_related_to,r.person_b as patient_contact,r.relationship,r.start_date,r.end_date, r.date_created,r.date_changed,r.voided - from relationship r inner join relationship_type t on r.relationship = t.relationship_type_id) r on e.patient_id = r.patient_contact and r.voided = 0 - inner join person p on p.person_id=e.patient_id and p.voided=0 - where e.voided=0 group by patient_contact, relationship_type, patient_related_to; + (select encounter_type_id, uuid, name from encounter_type where uuid = 'de1f9d67-b73e-4e1b-90d0-036166fc6995') et + on et.encounter_type_id = e.encounter_type + inner join (select r.person_a as patient_related_to, + r.person_b as patient_contact, + r.relationship, + r.start_date, + r.end_date, + r.date_created, + r.date_changed, + r.voided + from relationship r + inner join relationship_type t on r.relationship = t.relationship_type_id and + t.a_is_to_b in + ('Co-wife', 'Partner', 'Injectable-drug-user', 'SNS', + 'Spouse', 'Parent', 'Child', 'Sibling')) r + on e.patient_id = r.patient_contact and r.voided = 0 and (r.end_date is null or + r.end_date > current_date) + inner join person p on p.person_id = r.patient_contact and p.voided = 0 + inner join (select person_id + from person_attribute pa + join person_attribute_type t + on pa.person_attribute_type_id = t.person_attribute_type_id and + t.uuid = '7c94bd35-fba7-4ef7-96f5-29c89a318fcf') pt on e.patient_id = pt.person_id + where e.voided = 0 + group by patient_contact, relationship_type, patient_related_to; update kenyaemr_etl.etl_patient_contact c join diff --git a/omod/src/main/resources/sql/Scheduled_Updates.sql b/omod/src/main/resources/sql/Scheduled_Updates.sql index f694ac2c..31a17c6b 100644 --- a/omod/src/main/resources/sql/Scheduled_Updates.sql +++ b/omod/src/main/resources/sql/Scheduled_Updates.sql @@ -5160,34 +5160,44 @@ insert into kenyaemr_etl.etl_patient_contact ( relationship_type, voided ) -select - p.uuid, - date(e.encounter_datetime) as date_created, - r.start_date, - r.end_date, - encounter_id, - e.creator as encounter_provider, - e.location_id, - e.date_changed as date_last_modified, - r.patient_related_to, - r.patient_contact, - r.relationship as relationship_type, - e.voided +select p.uuid, + date(e.encounter_datetime) as date_created, + encounter_id, + e.creator as encounter_provider, + e.location_id, + r.patient_contact, + r.patient_related_to, + r.relationship as relationship_type, + r.start_date, + r.end_date, + e.date_changed as date_last_modified, + e.voided from encounter e inner join - ( - select encounter_type_id, uuid, name from encounter_type where uuid='de1f9d67-b73e-4e1b-90d0-036166fc6995' - ) et on et.encounter_type_id=e.encounter_type - inner join (select r.person_a as patient_related_to,r.person_b as patient_contact,r.relationship,r.start_date,r.end_date, r.date_created,r.date_changed,r.voided - from relationship r inner join relationship_type t on r.relationship = t.relationship_type_id) r on e.patient_id = r.patient_contact and r.voided = 0 - inner join person p on p.person_id=e.patient_id and p.voided=0 + (select encounter_type_id, uuid, name from encounter_type where uuid = 'de1f9d67-b73e-4e1b-90d0-036166fc6995') et + on et.encounter_type_id = e.encounter_type + inner join (select r.person_a as patient_related_to, + r.person_b as patient_contact, + r.relationship, + r.start_date, + r.end_date, + r.date_created, + r.date_changed, + r.voided + from relationship r + inner join relationship_type t on r.relationship = t.relationship_type_id and + t.a_is_to_b in + ('Co-wife', 'Partner', 'Injectable-drug-user', 'SNS', + 'Spouse', 'Parent', 'Child', 'Sibling')) r + on e.patient_id = r.patient_contact and r.voided = 0 and (r.end_date is null or + r.end_date > current_date) + inner join person p on p.person_id = r.patient_contact and p.voided = 0 + inner join (select person_id + from person_attribute pa + join person_attribute_type t + on pa.person_attribute_type_id = t.person_attribute_type_id and + t.uuid = '7c94bd35-fba7-4ef7-96f5-29c89a318fcf') pt on e.patient_id = pt.person_id where e.voided = 0 - and (e.date_created >= last_update_time -or e.date_changed >= last_update_time -or p.date_changed >= last_update_time -or p.date_created >= last_update_time -or r.date_created >= last_update_time -or r.date_changed >= last_update_time) group by patient_contact, relationship_type, patient_related_to ON DUPLICATE KEY UPDATE start_date=start_date, diff --git a/omod/src/main/resources/sql/dwapi/DML.sql b/omod/src/main/resources/sql/dwapi/DML.sql index d7decfb8..cb92f631 100644 --- a/omod/src/main/resources/sql/dwapi/DML.sql +++ b/omod/src/main/resources/sql/dwapi/DML.sql @@ -4727,28 +4727,45 @@ CREATE PROCEDURE sp_populate_dwapi_patient_contact() date_last_modified, voided ) - select - p.uuid, - date(e.encounter_datetime) as date_created, - encounter_id, - e.creator as encounter_provider, - e.location_id, - r.patient_contact, - r.patient_related_to, - r.relationship as relationship_type, - r.start_date, - r.end_date, - e.date_changed as date_last_modified, - e.voided + select p.uuid, + date(e.encounter_datetime) as date_created, + encounter_id, + e.creator as encounter_provider, + e.location_id, + r.patient_contact, + r.patient_related_to, + r.relationship as relationship_type, + r.start_date, + r.end_date, + e.date_changed as date_last_modified, + e.voided from encounter e inner join - ( - select encounter_type_id, uuid, name from encounter_type where uuid='de1f9d67-b73e-4e1b-90d0-036166fc6995' - ) et on et.encounter_type_id=e.encounter_type - inner join (select r.person_a as patient_related_to,r.person_b as patient_contact,r.relationship,r.start_date,r.end_date, r.date_created,r.date_changed,r.voided - from relationship r inner join relationship_type t on r.relationship = t.relationship_type_id) r on e.patient_id = r.patient_contact and r.voided = 0 - inner join person p on p.person_id=e.patient_id and p.voided=0 - where e.voided=0 group by patient_contact, relationship_type,patient_related_to; + (select encounter_type_id, uuid, name from encounter_type where uuid = 'de1f9d67-b73e-4e1b-90d0-036166fc6995') et + on et.encounter_type_id = e.encounter_type + inner join (select r.person_a as patient_related_to, + r.person_b as patient_contact, + r.relationship, + r.start_date, + r.end_date, + r.date_created, + r.date_changed, + r.voided + from relationship r + inner join relationship_type t on r.relationship = t.relationship_type_id and + t.a_is_to_b in + ('Co-wife', 'Partner', 'Injectable-drug-user', 'SNS', + 'Spouse', 'Parent', 'Child', 'Sibling')) r + on e.patient_id = r.patient_contact and r.voided = 0 and (r.end_date is null or + r.end_date > current_date) + inner join person p on p.person_id = r.patient_contact and p.voided = 0 + inner join (select person_id + from person_attribute pa + join person_attribute_type t + on pa.person_attribute_type_id = t.person_attribute_type_id and + t.uuid = '7c94bd35-fba7-4ef7-96f5-29c89a318fcf') pt on e.patient_id = pt.person_id + where e.voided = 0 + group by patient_contact, relationship_type, patient_related_to; update dwapi_etl.etl_patient_contact c join From 3a62a9853104d3f038d70025d52b9f76f875b46d Mon Sep 17 00:00:00 2001 From: njorocs Date: Mon, 12 Aug 2024 15:03:59 +0300 Subject: [PATCH 5/5] Handled baseline HIV status and added IPV outcome in DMLs --- omod/src/main/resources/sql/DML.sql | 23 +++++++++---------- .../main/resources/sql/Scheduled_Updates.sql | 23 +++++++++---------- omod/src/main/resources/sql/dwapi/DML.sql | 23 +++++++++---------- 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/omod/src/main/resources/sql/DML.sql b/omod/src/main/resources/sql/DML.sql index 4103bad1..cec7b8ce 100644 --- a/omod/src/main/resources/sql/DML.sql +++ b/omod/src/main/resources/sql/DML.sql @@ -4988,10 +4988,7 @@ CREATE PROCEDURE sp_populate_etl_patient_contact() r.date_changed, r.voided from relationship r - inner join relationship_type t on r.relationship = t.relationship_type_id and - t.a_is_to_b in - ('Co-wife', 'Partner', 'Injectable-drug-user', 'SNS', - 'Spouse', 'Parent', 'Child', 'Sibling')) r + inner join relationship_type t on r.relationship = t.relationship_type_id ) r on e.patient_id = r.patient_contact and r.voided = 0 and (r.end_date is null or r.end_date > current_date) inner join person p on p.person_id = r.patient_contact and p.voided = 0 @@ -5008,10 +5005,12 @@ CREATE PROCEDURE sp_populate_etl_patient_contact() ( select pa.person_id, - max(if(pat.uuid='3ca03c84-632d-4e53-95ad-91f1bd9d96d6', pa.value, null)) as baseline_hiv_status, + max(if(pat.uuid='3ca03c84-632d-4e53-95ad-91f1bd9d96d6', case pa.value when 703 then 'Positive' when 664 then 'Negative' when 1067 then 'Unknown' end, null)) as baseline_hiv_status, max(if(pat.uuid='35a08d84-9f80-4991-92b4-c4ae5903536e', pa.value, null)) as living_with_patient, - max(if(pat.uuid='59d1b886-90c8-4f7f-9212-08b20a9ee8cf', pa.value, null)) as pns_approach + max(if(pat.uuid='59d1b886-90c8-4f7f-9212-08b20a9ee8cf', pa.value, null)) as pns_approach, + max(if(pat.uuid='49c543c2-a72a-4b0a-8cca-39c375c0726f', pa.value, null)) as ipv_outcome from person_attribute pa + inner join person p on p.person_id = pa.person_id and p.voided = 0 inner join ( select @@ -5021,18 +5020,18 @@ CREATE PROCEDURE sp_populate_etl_patient_contact() from person_attribute_type pat where pat.retired=0 ) pat on pat.person_attribute_type_id = pa.person_attribute_type_id - and pat.uuid in ( - 'b2c38640-2603-4629-aebd-3b54f33f1e3a', -- phone_contact - '3ca03c84-632d-4e53-95ad-91f1bd9d96d6', -- baseline_hiv_status + and pat.uuid in ('3ca03c84-632d-4e53-95ad-91f1bd9d96d6', -- baseline_hiv_status '35a08d84-9f80-4991-92b4-c4ae5903536e', -- living_with_patient - '59d1b886-90c8-4f7f-9212-08b20a9ee8cf' -- pns_approach + '59d1b886-90c8-4f7f-9212-08b20a9ee8cf', -- pns_approach + '49c543c2-a72a-4b0a-8cca-39c375c0726f' -- ipv_outcome ) where pa.voided=0 - group by pa.person_id + group by p.person_id ) att on att.person_id = c.patient_id set c.baseline_hiv_status=att.baseline_hiv_status, c.living_with_patient=att.living_with_patient, - c.pns_approach=att.pns_approach; + c.pns_approach=att.pns_approach, + c.ipv_outcome=att.ipv_outcome; update kenyaemr_etl.etl_patient_contact c left outer join (select pa.person_id, diff --git a/omod/src/main/resources/sql/Scheduled_Updates.sql b/omod/src/main/resources/sql/Scheduled_Updates.sql index 31a17c6b..71b08e14 100644 --- a/omod/src/main/resources/sql/Scheduled_Updates.sql +++ b/omod/src/main/resources/sql/Scheduled_Updates.sql @@ -5185,10 +5185,7 @@ from encounter e r.date_changed, r.voided from relationship r - inner join relationship_type t on r.relationship = t.relationship_type_id and - t.a_is_to_b in - ('Co-wife', 'Partner', 'Injectable-drug-user', 'SNS', - 'Spouse', 'Parent', 'Child', 'Sibling')) r + inner join relationship_type t on r.relationship = t.relationship_type_id ) r on e.patient_id = r.patient_contact and r.voided = 0 and (r.end_date is null or r.end_date > current_date) inner join person p on p.person_id = r.patient_contact and p.voided = 0 @@ -5210,10 +5207,12 @@ update kenyaemr_etl.etl_patient_contact c ( select pa.person_id, - max(if(pat.uuid='3ca03c84-632d-4e53-95ad-91f1bd9d96d6', pa.value, null)) as baseline_hiv_status, + max(if(pat.uuid='3ca03c84-632d-4e53-95ad-91f1bd9d96d6', case pa.value when 703 then 'Positive' when 664 then 'Negative' when 1067 then 'Unknown' end, null)) as baseline_hiv_status, max(if(pat.uuid='35a08d84-9f80-4991-92b4-c4ae5903536e', pa.value, null)) as living_with_patient, - max(if(pat.uuid='59d1b886-90c8-4f7f-9212-08b20a9ee8cf', pa.value, null)) as pns_approach + max(if(pat.uuid='59d1b886-90c8-4f7f-9212-08b20a9ee8cf', pa.value, null)) as pns_approach, + max(if(pat.uuid='49c543c2-a72a-4b0a-8cca-39c375c0726f', pa.value, null)) as ipv_outcome from person_attribute pa + inner join person p on p.person_id = pa.person_id and p.voided = 0 inner join ( select @@ -5223,19 +5222,19 @@ update kenyaemr_etl.etl_patient_contact c from person_attribute_type pat where pat.retired=0 ) pat on pat.person_attribute_type_id = pa.person_attribute_type_id - and pat.uuid in ( - 'b2c38640-2603-4629-aebd-3b54f33f1e3a', -- phone_contact - '3ca03c84-632d-4e53-95ad-91f1bd9d96d6', -- baseline_hiv_status + and pat.uuid in ('3ca03c84-632d-4e53-95ad-91f1bd9d96d6', -- baseline_hiv_status '35a08d84-9f80-4991-92b4-c4ae5903536e', -- living_with_patient - '59d1b886-90c8-4f7f-9212-08b20a9ee8cf' -- pns_approach + '59d1b886-90c8-4f7f-9212-08b20a9ee8cf', -- pns_approach + '49c543c2-a72a-4b0a-8cca-39c375c0726f' -- ipv_outcome ) where pa.voided=0 and (pa.date_changed >= last_update_time or pa.date_created >= last_update_time) - group by pa.person_id + group by p.person_id ) att on att.person_id = c.patient_id set c.baseline_hiv_status=att.baseline_hiv_status, c.living_with_patient=att.living_with_patient, - c.pns_approach=att.pns_approach; + c.pns_approach=att.pns_approach, + c.ipv_outcome=att.ipv_outcome; update kenyaemr_etl.etl_patient_contact c left outer join (select pa.person_id, diff --git a/omod/src/main/resources/sql/dwapi/DML.sql b/omod/src/main/resources/sql/dwapi/DML.sql index cb92f631..d4726665 100644 --- a/omod/src/main/resources/sql/dwapi/DML.sql +++ b/omod/src/main/resources/sql/dwapi/DML.sql @@ -4752,10 +4752,7 @@ CREATE PROCEDURE sp_populate_dwapi_patient_contact() r.date_changed, r.voided from relationship r - inner join relationship_type t on r.relationship = t.relationship_type_id and - t.a_is_to_b in - ('Co-wife', 'Partner', 'Injectable-drug-user', 'SNS', - 'Spouse', 'Parent', 'Child', 'Sibling')) r + inner join relationship_type t on r.relationship = t.relationship_type_id ) r on e.patient_id = r.patient_contact and r.voided = 0 and (r.end_date is null or r.end_date > current_date) inner join person p on p.person_id = r.patient_contact and p.voided = 0 @@ -4772,10 +4769,12 @@ CREATE PROCEDURE sp_populate_dwapi_patient_contact() ( select pa.person_id, - max(if(pat.uuid='3ca03c84-632d-4e53-95ad-91f1bd9d96d6', pa.value, null)) as baseline_hiv_status, + max(if(pat.uuid='3ca03c84-632d-4e53-95ad-91f1bd9d96d6', case pa.value when 703 then 'Positive' when 664 then 'Negative' when 1067 then 'Unknown' end, null)) as baseline_hiv_status, max(if(pat.uuid='35a08d84-9f80-4991-92b4-c4ae5903536e', pa.value, null)) as living_with_patient, - max(if(pat.uuid='59d1b886-90c8-4f7f-9212-08b20a9ee8cf', pa.value, null)) as pns_approach + max(if(pat.uuid='59d1b886-90c8-4f7f-9212-08b20a9ee8cf', pa.value, null)) as pns_approach, + max(if(pat.uuid='49c543c2-a72a-4b0a-8cca-39c375c0726f', pa.value, null)) as ipv_outcome from person_attribute pa + inner join person p on p.person_id = pa.person_id and p.voided = 0 inner join ( select @@ -4785,18 +4784,18 @@ CREATE PROCEDURE sp_populate_dwapi_patient_contact() from person_attribute_type pat where pat.retired=0 ) pat on pat.person_attribute_type_id = pa.person_attribute_type_id - and pat.uuid in ( - 'b2c38640-2603-4629-aebd-3b54f33f1e3a', -- phone_contact - '3ca03c84-632d-4e53-95ad-91f1bd9d96d6', -- baseline_hiv_status + and pat.uuid in ('3ca03c84-632d-4e53-95ad-91f1bd9d96d6', -- baseline_hiv_status '35a08d84-9f80-4991-92b4-c4ae5903536e', -- living_with_patient - '59d1b886-90c8-4f7f-9212-08b20a9ee8cf' -- pns_approach + '59d1b886-90c8-4f7f-9212-08b20a9ee8cf', -- pns_approach + '49c543c2-a72a-4b0a-8cca-39c375c0726f' -- ipv_outcome ) where pa.voided=0 - group by pa.person_id + group by p.person_id ) att on att.person_id = c.patient_id set c.baseline_hiv_status=att.baseline_hiv_status, c.living_with_patient=att.living_with_patient, - c.pns_approach=att.pns_approach; + c.pns_approach=att.pns_approach, + c.ipv_outcome=att.ipv_outcome; update dwapi_etl.etl_patient_contact c left outer join (select pa.person_id,