Skip to content

Commit

Permalink
Merge pull request #473 from njorocs/patient_contact_latest
Browse files Browse the repository at this point in the history
Patient contact latest
  • Loading branch information
patryllus authored Aug 14, 2024
2 parents 4f58602 + 3a62a98 commit 4d43fc6
Show file tree
Hide file tree
Showing 6 changed files with 400 additions and 224 deletions.
61 changes: 29 additions & 32 deletions omod/src/main/resources/sql/DDL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2256,37 +2256,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";

Expand All @@ -2306,7 +2303,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),
Expand Down
183 changes: 110 additions & 73 deletions omod/src/main/resources/sql/DML.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3005,22 +3005,21 @@ 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);
ALTER TABLE kenyaemr_etl.etl_hts_contacts ADD INDEX(patient_id);
ALTER TABLE kenyaemr_etl.etl_hts_contacts ADD INDEX(visit_date);

-- Linked contacts
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(patient_id);
ALTER TABLE kenyaemr_etl.etl_contacts_linked ADD INDEX(visit_date);

SELECT "Completed processing dashboard indicators", CONCAT("Time: ", NOW());
Expand Down Expand Up @@ -4953,64 +4952,102 @@ 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_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 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
(
select
pa.person_id,
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='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
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 ('3ca03c84-632d-4e53-95ad-91f1bd9d96d6', -- baseline_hiv_status
'35a08d84-9f80-4991-92b4-c4ae5903536e', -- living_with_patient
'59d1b886-90c8-4f7f-9212-08b20a9ee8cf', -- pns_approach
'49c543c2-a72a-4b0a-8cca-39c375c0726f' -- ipv_outcome
)
where pa.voided=0
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.ipv_outcome=att.ipv_outcome;

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 ------------------------

Expand All @@ -5034,8 +5071,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,
Expand All @@ -5051,7 +5088,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());
Expand Down Expand Up @@ -7683,14 +7720,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
Expand Down
32 changes: 15 additions & 17 deletions omod/src/main/resources/sql/DataTools.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1274,31 +1274,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";
Expand All @@ -1320,7 +1318,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";

Expand Down
Loading

0 comments on commit 4d43fc6

Please sign in to comment.