Skip to content

Commit

Permalink
Merge pull request #490 from patryllus/feat/AddLabsets
Browse files Browse the repository at this point in the history
Updated etl_laboratory_extract to include order_test_name, test_result_name and set_member_conceptId
  • Loading branch information
njorocs authored Dec 12, 2024
2 parents b7e75bc + cdaade3 commit d718f7f
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 159 deletions.
8 changes: 7 additions & 1 deletion omod/src/main/resources/sql/DDL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ order_id VARCHAR(200),
lab_test VARCHAR(180),
urgency VARCHAR(50),
order_reason VARCHAR(180),
order_test_name VARCHAR(180),
obs_id VARCHAR(180),
result_test_name VARCHAR(180),
result_name VARCHAR(400),
set_member_conceptId VARCHAR(100),
test_result VARCHAR(180),
date_test_requested DATE DEFAULT null,
date_test_result_received DATE,
Expand All @@ -396,7 +401,8 @@ INDEX(visit_date),
INDEX(encounter_id),
INDEX(patient_id),
INDEX(lab_test),
INDEX(test_result)
INDEX(test_result),
INDEX(set_member_conceptId)

);
SELECT "Successfully created etl_laboratory_extract table";
Expand Down
118 changes: 82 additions & 36 deletions omod/src/main/resources/sql/DML.sql
Original file line number Diff line number Diff line change
Expand Up @@ -599,57 +599,103 @@ order_id,
lab_test,
urgency,
order_reason,
order_test_name,
obs_id,
result_test_name,
result_name,
set_member_conceptId,
test_result,
date_test_requested,
date_test_result_received,
date_created,
date_last_modified,
created_by
)
select
e.uuid,
WITH FilteredOrders AS (SELECT patient_id,
encounter_id,
order_id,
concept_id,
date_activated,
urgency,
order_reason
FROM openmrs.orders
WHERE order_type_id = 3
AND voided = 0
GROUP BY patient_id, encounter_id ,concept_id),
LabOrderConcepts AS (SELECT cs.concept_set_id AS set_id,
cs.concept_id AS member_concept_id,
c.datatype_id AS member_datatype,
c.class_id AS member_class,
n.name
FROM concept_set cs
INNER JOIN concept c ON cs.concept_id = c.concept_id
INNER JOIN concept_name n ON c.concept_id = n.concept_id
AND n.locale = 'en'
AND n.concept_name_type = 'FULLY_SPECIFIED'
WHERE cs.concept_set = 1000628),
CodedLabOrderResults AS (SELECT o.obs_id as obs_id, o.order_id, o.concept_id, o.obs_datetime,o.date_created, o.value_coded, n.name, n1.name as test_name
from obs o
inner join concept c on o.concept_id = c.concept_id
inner join concept_datatype cd on c.datatype_id = cd.concept_datatype_id and cd.name = 'Coded'
left join concept_name n
on o.value_coded = n.concept_id AND n.locale = 'en' AND
n.concept_name_type = 'FULLY_SPECIFIED'
left join concept_name n1
on o.concept_id = n1.concept_id AND n1.locale = 'en' AND
n1.concept_name_type = 'FULLY_SPECIFIED'
where o.order_id is not null ),
NumericLabOrderResults AS (SELECT o.obs_id as obs_id, o.order_id, o.concept_id, o.value_numeric, n.name, n1.name as test_name
from obs o
inner join concept c on o.concept_id = c.concept_id
inner join concept_datatype cd on c.datatype_id = cd.concept_datatype_id and cd.name = 'Numeric'
inner join concept_name n
on o.concept_id = n.concept_id AND n.locale = 'en' AND
n.concept_name_type = 'FULLY_SPECIFIED'
left join concept_name n1
on o.concept_id = n1.concept_id AND n1.locale = 'en' AND
n1.concept_name_type = 'FULLY_SPECIFIED'
where o.order_id is not null ),
TextLabOrderResults AS (SELECT o.obs_id as obs_id, o.order_id, o.concept_id, o.value_text, c.class_id, n.name, n1.name as test_name
from obs o
inner join concept c on o.concept_id = c.concept_id
inner join concept_datatype cd on c.datatype_id = cd.concept_datatype_id and cd.name = 'Text'
inner join concept_name n
on o.concept_id = n.concept_id AND n.locale = 'en' AND
n.concept_name_type = 'FULLY_SPECIFIED'
left join concept_name n1
on o.concept_id = n1.concept_id AND n1.locale = 'en' AND
n1.concept_name_type = 'FULLY_SPECIFIED'
where o.order_id is not null )
SELECT
UUID(),
e.encounter_id,
e.patient_id,
e.location_id,
coalesce(od.date_activated,o.obs_datetime) as visit_date,
coalesce(o.date_activated,obs_datetime) as visit_date,
e.visit_id,
od.order_id,
od.concept_id,
od.urgency,
od.order_reason,
(CASE when o.concept_id in(5497,730,654,790,856,21,653,5475,887,1015,849,678,676,855,1000443,161469,166395,160912,159644,163594,1006,1007,1009,1008,161153,161481,161482,
166018,785,655,717,848,163699,160913,1011,159655,159654,161500,163595,163596,1336,1338,1017,1018,851,729,679,1016,163426,160914) then o.value_numeric
when o.concept_id in(1030,1305,1325,159430,161472,1029,1031,1619,1032,162202,307,45,167718,
163722,167452,167459,1643,32,1366,1042,299,300,305,306,1618,1875,161470,885,165562,161478,160225,160232,1356,161233,167810,159362,163654,
168114,163348,162202,1000612,1000614,1000615,1000616,1000617,1000613) then o.value_coded
when o.concept_id in (302,1367,56,1000071,163613,163603,163603,1000451,165552,165402,161156,161155,159648,159649,161467) then o.value_text
END) AS test_result,
od.date_activated as date_test_requested,
o.order_id,
o.concept_id,
o.urgency,
o.order_reason,
lc.name as order_test_name,
COALESCE(cr.obs_id,nr.obs_id,tr.obs_id) as obs_id,
if(cr.test_name IS NOT NULL,cr.test_name,if(nr.test_name is not null, nr.test_name,if(tr.test_name is not null, tr.test_name,''))) as result_test_name,
COALESCE(cr.name,nr.value_numeric,tr.value_text) as result_name,
if(cr.concept_id IS NOT NULL,cr.concept_id,if(nr.concept_id is not null, nr.concept_id,if(tr.concept_id is not null, tr.concept_id,''))) set_member_conceptId,
COALESCE(cr.value_coded,nr.value_numeric,tr.value_text) as test_result,
o.date_activated as date_test_requested,
e.encounter_datetime as date_test_result_received,
-- test requested by
e.date_created,
if(max(o.date_created) > min(e.date_created),max(o.date_created),NULL) as date_last_modified,
e.date_changed as date_last_modified,
e.creator
from encounter e
inner join person p on p.person_id=e.patient_id and p.voided=0
left join
(
select encounter_type_id, uuid, name from encounter_type where uuid in('17a381d1-7e29-406a-b782-aa903b963c28',
'a0034eee-1940-4e35-847f-97537a35d05e',
'e1406e88-e9a9-11e8-9f32-f2801f1b9fd1',
'de78a6be-bfc5-4634-adc3-5f1a280455cc',
'bcc6da85-72f2-4291-b206-789b8186a021',
'7df67b83-1b84-4fe2-b1b7-794b4e9bfcc3')
) et on et.encounter_type_id=e.encounter_type
left join obs o on e.encounter_id=o.encounter_id and o.voided=0 and o.concept_id in (5497,730,654,790,856,1030,1305,1325,159430,161472,1029,1031,1619,1032,162202,307,45,167718,163722,167452,167459,1643,32,1366,1000612,1019,21,
657,1042,653,5473,5475,299,887,302,1015,300,1367,305,306,1618,1875,849,678,676,1336,855,161470,1000443,
885,56,165562,161469,161478,160225,1000071,166395,160912,159644,163594,1006,1007,1009,1008,161153,161481,161482,166018,159829,785,655,
717,848,163699,1000069,160232,1356,161233,163613,163602,160913,167810,161532,1011,159655,159654,161500,168167,159362,163654,168114,163603,163348,
1000451,165552,165402,161156,161155,159648,159649,161467,163595,163596,1338,1017,1018,851,729,679,1016,163426,160914,1000612,1000614,1000615,1000616,1000617,1000613)
left join orders od on od.order_id = o.order_id and od.order_type_id = 3 and od.voided=0
where e.voided=0
group by o.encounter_id;

FROM encounter e
INNER JOIN FilteredOrders o ON o.encounter_id = e.encounter_id
LEFT JOIN LabOrderConcepts lc ON o.concept_id = lc.member_concept_id
LEFT JOIN CodedLabOrderResults cr on o.order_id = cr.order_id
LEFT JOIN NumericLabOrderResults nr on o.order_id = nr.order_id
LEFT JOIN TextLabOrderResults tr on o.order_id = tr.order_id
where e.voided=0 group by obs_id;
/*-- >>>>>>>>>>>>>>> ----------------------------------- Wagners input ------------------------------------------------------------
insert into kenyaemr_etl.etl_laboratory_extract(
encounter_id,
Expand Down
62 changes: 22 additions & 40 deletions omod/src/main/resources/sql/DataTools.sql
Original file line number Diff line number Diff line change
Expand Up @@ -274,46 +274,28 @@ SELECT "Successfully created hiv followup table";
-- -------------------------------- create table laboratory_extract ------------------------------------------
create table kenyaemr_datatools.laboratory_extract as
select
uuid,
encounter_id,
patient_id,
location_id,
visit_date,
visit_id,
(case lab_test when 5497 then "CD4 Count" when 167718 then "CD4 Count" when 730 then "CD4 PERCENT " when 654 then "ALT" when 790 then "Serum creatinine (umol/L)"
when 856 then "HIV VIRAL LOAD" when 1305 then "HIV VIRAL LOAD" when 21 then "Hemoglobin (HGB)" when 1029 then "VDRL Titre" when 1031 then "Treponema Pallidum Hemagglutination Assay"
when 1619 then "Rapid Plasma Reagin" when 1032 then "Treponema Pallidum Hemagglutination Assay, Qualitative" when 45 then "Urine Pregnancy Test" when 167452 then "Serum Cryptococcal Ag" when 167459 then "TB LAM"
when 307 then "Sputum for Acid Fast Bacilli" when 162202 then "GeneXpert" when 160914 then "Post-prandial blood glucose measurement (mg/dL)" else "" end) as lab_test,
urgency,
(case order_reason when 843 then 'Confirmation of treatment failure (repeat VL)' when 1259 then 'Single Drug Substitution' when 1434 then 'Pregnancy'
when 159882 then 'Breastfeeding' when 160566 then 'Immunologic failure' when 160569 then 'Virologic failure'
when 161236 then 'Routine' when 162080 then 'Baseline VL (for infants diagnosed through EID)' when 162081 then 'Repeat' when 163523 then 'Clinical failure'
when 160032 then 'Confirmation of persistent low level Viremia (PLLV)' when 1040 then 'Initial PCR (6week or first contact)' when 1326 then '2nd PCR (6 months)' when 164860 then '3rd PCR (12months)'
when 162082 then 'Confirmatory PCR and Baseline VL' when 164460 then 'Ab test 6 weeks after cessation of breastfeeding'
when 164860 then 'Ab test at 18 months (1.5 years)'
when 167389 then 'Recency assay'
else '' end) as order_reason,
if(lab_test=299, (case test_result when 1228 then "REACTIVE" when 1229 then "NON-REACTIVE" when 1304 then "POOR SAMPLE QUALITY" end),
if(lab_test=1030, (case test_result when 1138 then "INDETERMINATE" when 664 then "NEGATIVE" when 703 then "POSITIVE" when 1304 then "POOR SAMPLE QUALITY" end),
if(lab_test=163722, (case test_result when 1138 then "INDETERMINATE" when 664 then "NEGATIVE" when 703 then "POSITIVE" when 1304 then "POOR SAMPLE QUALITY" end),
if(lab_test=32, (case test_result when 664 then "NEGATIVE" when 703 then "POSITIVE" when 1138 then "INDETERMINATE" end),
if(lab_test=1305, (case test_result when 1306 then "BEYOND DETECTABLE LIMIT" when 1301 then "DETECTED" when 1302 then "LDL" when 1304 then "POOR SAMPLE QUALITY" end),
if(lab_test=167718, (case test_result when 1254 then "CD4 COUNT GREATER THAN 200" when 167717 then "CD4 count less than or equal to 200" end),
if(lab_test=1029, (case test_result when 664 then "Negative" when 703 then "Positive" end),
if(lab_test=1031, (case test_result when 1311 then "<1:2" when 1312 then "01:02" when 1313 then "1:4" when 1314 then "01:08" when 1315 then "01:16" when 1316 then "01:32" when 1317 then ">1:32" when 1304 then "Poor Sample Quality"
when 163621 then "1:64" when 163622 then "1:128" when 163623 then "1:256" when 163624 then ">1:572" end),
if(lab_test=1032, (case test_result when 703 then "Positive" when 664 then "Negative" when 1300 then "Equivocal" when 1304 then "Poor Sample Quality" end),
if(lab_test=307, (case test_result when 1364 then "Three Plus" when 1362 then "One Plus" when 1363 then "Two Plus" when 664 then "Negative" when 159985 then "Scanty" when 703 then "Positive"
when 160008 then "Contaminated specimen" when 164369 then "Results not available" when 1118 then "Not done" end),
if(lab_test=162202, (case test_result when 664 then "NEGATIVE" when 162203 then "Mycobacterium tuberculosis detected with rifampin resistance" when 162204 then "Mycobacterium tuberculosis detected without rifampin resistance"
when 164104 then "Mycobacterium tuberculosis detected with indeterminate rifampin resistance" when 163611 then "Invalid" when 1138 then "INDETERMINATE" end),
if(lab_test=1619, (case test_result when 703 then "Positive" when 664 then "Negative" when 1067 then "Unknown" end),
if(lab_test=45, (case test_result when 703 then "Positive" when 664 then "Negative" when 1138 then "Indeterminate" when 1304 then "Poor Quality Sample" end),
if(lab_test=167452, (case test_result when 703 then "Positive" when 664 then "Negative" when 1067 then "Unknown" end),
if(lab_test=167459, (case test_result when 163747 then "Absent" when 163748 then "Present" end),
test_result ))))))))))))))) AS test_result,
date_created,
created_by
uuid,
encounter_id,
patient_id,
location_id,
visit_date,
visit_id,
order_id,
lab_test,
urgency,
order_reason,
order_test_name,
obs_id,
result_test_name,
result_name,
set_member_conceptId,
test_result,
date_test_requested,
date_test_result_received,
test_requested_by,
date_created,
date_last_modified,
created_by
from kenyaemr_etl.etl_laboratory_extract;

ALTER TABLE kenyaemr_datatools.laboratory_extract ADD FOREIGN KEY (patient_id) REFERENCES kenyaemr_datatools.patient_demographics(patient_id);
Expand Down
Loading

0 comments on commit d718f7f

Please sign in to comment.