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

Update flat hiv summary script #251

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
91 changes: 75 additions & 16 deletions etl-scripts/stored-procedures/generate_flat_hiv_summary.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ CREATE TABLE IF NOT EXISTS flat_hiv_summary_v15b (
is_transit INT,
is_clinical_encounter INT,
location_id INT,
last_non_transit_location_id INT NULL,
location_uuid VARCHAR(100),
visit_num INT,
mdt_session_number INT,
Expand Down Expand Up @@ -147,7 +148,7 @@ CREATE TABLE IF NOT EXISTS flat_hiv_summary_v15b (
is_cross_border_country INT,
cross_border_service_offered INT,
cross_border_country_travelled INT,
is_cross_border_county INT,
is_cross_border_county INT,
is_cross_border INT,
tb_tx_stop_date DATETIME,
country_of_residence INT,
Expand All @@ -158,6 +159,10 @@ CREATE TABLE IF NOT EXISTS flat_hiv_summary_v15b (
ca_cx_screening_result INT,
ca_cx_screening_result_datetime DATETIME,
ca_cx_screening_datetime DATETIME,
tb_modality_test INT,
tb_test_result INT,
tb_test_date DATETIME,
gbv_screening_result INT NULL,
PRIMARY KEY encounter_id (encounter_id),
INDEX person_date (person_id , encounter_datetime),
INDEX person_uuid (uuid),
Expand All @@ -172,10 +177,10 @@ CREATE TABLE IF NOT EXISTS flat_hiv_summary_v15b (



if(@query_type="build") then
if(@query_type="build") then
select 'BUILDING..........................................';
set @write_table = concat("flat_hiv_summary_temp_",queue_number);
set @queue_table = concat("flat_hiv_summary_build_queue_",queue_number);
set @queue_table = concat("flat_hiv_summary_build_queue_",queue_number);

SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table);
PREPARE s1 from @dyn_sql;
Expand All @@ -189,7 +194,7 @@ CREATE TABLE IF NOT EXISTS flat_hiv_summary_v15b (
DEALLOCATE PREPARE s1;


SET @dyn_sql=CONCAT('delete t1 from flat_hiv_summary_build_queue t1 join ',@queue_table, ' t2 using (person_id);');
SET @dyn_sql=CONCAT('delete t1 from flat_hiv_summary_sync_queue t1 join ',@queue_table, ' t2 using (person_id);');
PREPARE s1 from @dyn_sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
Expand Down Expand Up @@ -316,7 +321,7 @@ SELECT @person_ids_count AS 'num patients to sync';
t1.obs,
t1.obs_datetimes,

case
case
when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,117,120,127,128,138,140,153,154,158,162,163,225,226) then 1
when t1.encounter_type in (186) AND v.visit_type_id not in (24,25,80,104) AND v.visit_type_id is NOT NULL then 1
when t1.encounter_type in (158) AND v.visit_type_id not in (104) AND v.visit_type_id is NOT NULL then 1
Expand All @@ -341,7 +346,7 @@ SELECT @person_ids_count AS 'num patients to sync';
left join etl.flat_orders t2 using(encounter_id)
left join amrs.visit v on (v.visit_id = t1.visit_id)
where (t1.encounter_type in (1,2,3,4,10,14,15,17,19,22,23,26,32,33,43,47,21,105,106,110,111,112,113,114,116,117,120,127,128,129,138,140,153,154,158,161,162,163,212,214,218,220,225,226)
or (t1.encounter_type in (186) and v.visit_type_id not in (80,104)))
or (t1.encounter_type in (186) and (v.visit_type_id not in (80,104) OR v.visit_type_id IS NULL) ))
AND NOT obs regexp "!!5303=(822|664|1067)!!"
AND NOT obs regexp "!!9082=9036!!"
);
Expand Down Expand Up @@ -430,6 +435,7 @@ SELECT @person_ids_count AS 'num patients to sync';
set @ovc_exit_date = null;
set @hiv_start_date = null;
set @cur_location = null;
set @cur_last_non_transit_location_id = null;
set @cur_rtc_date = null;
set @prev_rtc_date = null;
set @med_pickup_rtc_date = null;
Expand Down Expand Up @@ -513,6 +519,10 @@ SELECT @person_ids_count AS 'num patients to sync';
set @ca_cx_screening_result = null;
set @ca_cx_screening_result_datetime = null;
set @ca_cx_screening_datetime = null;
set @tb_modality_test = null;
set @tb_test_result = null;
set @tb_test_date = null;
set @gbv_screening_result = null;



Expand Down Expand Up @@ -587,7 +597,11 @@ SELECT @person_ids_count AS 'num patients to sync';
when @prev_id = @cur_id then @cur_location
else null
end as location_id,

case
when @prev_id = @cur_id and @cur_last_non_transit_location_id is not null and visit_type in (23, 24, 119, 124, 129,43,80,118,120,123) then @cur_last_non_transit_location_id
when location_id then @cur_last_non_transit_location_id := location_id
else null
end as last_non_transit_location_id,
case
when @prev_id=@cur_id and t1.encounter_type not in (5,6,7,8,9,21) then @visit_num:= @visit_num + 1
when @prev_id != @cur_id then @visit_num := 1
Expand Down Expand Up @@ -1810,8 +1824,51 @@ SELECT @person_ids_count AS 'num patients to sync';
when obs regexp "!!10400=1065" then @ca_cx_screening_datetime := encounter_datetime
when @cur_id = @prev_id then @ca_cx_screening_datetime
else @ca_cx_screening_datetime := null
end as ca_cx_screening_datetime

end as ca_cx_screening_datetime,

case
when obs regexp "!!8070=" then @tb_modality_test := 8070
when obs regexp "!!307=" then @tb_modality_test := 307
when obs regexp "!!2311=" then @tb_modality_test := 2311
when obs regexp "!!1506=" then @tb_modality_test := 1506
when obs regexp "!!12=" then @tb_modality_test := 12
when @prev_id = @cur_id then @tb_modality_test
else @tb_modality_test := 0
end as tb_modality_test,

case
when obs regexp "!!8070=" then @tb_test_result :=
replace(replace((substring_index(substring(obs,locate("!!8070=",obs)),@sep,1)),"!!8070=",""),"!!","")
when obs regexp "!!307=" then @tb_test_result :=
replace(replace((substring_index(substring(obs,locate("!!307=",obs)),@sep,1)),"!!307=",""),"!!","")
when obs regexp "!!2311=" then @tb_test_result :=
replace(replace((substring_index(substring(obs,locate("!!2311=",obs)),@sep,1)),"!!2311=",""),"!!","")
when obs regexp "!!1506=" then @tb_test_result :=
replace(replace((substring_index(substring(obs,locate("!!1506=",obs)),@sep,1)),"!!1506=",""),"!!","")
when obs regexp "!!12=" then @tb_test_result :=
replace(replace((substring_index(substring(obs,locate("!!12=",obs)),@sep,1)),"!!12=",""),"!!","")
when @prev_id = @cur_id then @tb_test_result
else @tb_test_result := 0
end as tb_test_result,

case
when obs regexp "!!8070=" then @tb_test_date := date(encounter_datetime)
when obs regexp "!!307=" then @tb_test_date := date(encounter_datetime)
when obs regexp "!!2311=" then @tb_test_date := date(encounter_datetime)
when obs regexp "!!1506=" then @tb_test_date := date(encounter_datetime)
when obs regexp "!!12=" then @tb_test_date := date(encounter_datetime)
when @prev_id = @cur_id then @tb_test_date
else @tb_test_date := null
end as tb_test_date,

case
when obs regexp "!!11866=1065" OR obs regexp "!!11565=1065" OR obs regexp "!!11865=1065" OR obs regexp "!!11855=1065"
OR obs regexp "!!9303=1065" OR obs regexp "!!11867=1065" then @gbv_screening_result := 1
when obs regexp "!!11866=1066" OR obs regexp "!!11565=1066" OR obs regexp "!!11865=1066" OR obs regexp "!!11855=1066"
OR obs regexp "!!9303=1066" OR obs regexp "!!11867=1066" then @gbv_screening_result := 0
when @prev_id = @cur_id then @gbv_screening_result
else @gbv_screening_result := null
end as gbv_screening_result

from flat_hiv_summary_0 t1
join amrs.person p using (person_id)
Expand Down Expand Up @@ -2114,20 +2171,20 @@ SELECT @person_ids_count AS 'num patients to sync';

case
when obs regexp "!!7015=" then @transfer_in := 1
when prev_clinical_location_id != location_id and (visit_type is null or visit_type not in (23,24,33)) and (prev_visit_type is null or prev_visit_type not in (23,24,33)) then @transfer_in := 1
when prev_clinical_location_id != location_id and (visit_type is null or visit_type not in (23,24,33,124)) and (prev_visit_type is null or prev_visit_type not in (23,24,33)) then @transfer_in := 1
else @transfer_in := null
end as transfer_in,

case
when obs regexp "!!7015=" then @transfer_in_date := date(encounter_datetime)
when prev_clinical_location_id != location_id and (visit_type is null or visit_type not in (23,24,33)) and (prev_visit_type is null or prev_visit_type not in (23,24,33)) then @transfer_in_date := date(encounter_datetime)
when prev_clinical_location_id != location_id and (visit_type is null or visit_type not in (23,24,33,124)) and (prev_visit_type is null or prev_visit_type not in (23,24,33)) then @transfer_in_date := date(encounter_datetime)
when @cur_id = @prev_id then @transfer_in_date
else @transfer_in_date := null
end transfer_in_date,

case
when obs regexp "!!7015=1287" then @transfer_in_location_id := 9999
when prev_clinical_location_id != location_id and (visit_type is null or visit_type not in (23,24,33)) and (prev_visit_type is null or prev_visit_type not in (23,24,33)) then @transfer_in_location_id := cur_clinical_location_id
when prev_clinical_location_id != location_id and (visit_type is null or visit_type not in (23,24,33,124)) and (prev_visit_type is null or prev_visit_type not in (23,24,33)) then @transfer_in_location_id := cur_clinical_location_id
when @cur_id = @prev_id then @transfer_in_location_id
else @transfer_in_location_id := null
end transfer_in_location_id,
Expand Down Expand Up @@ -2187,6 +2244,7 @@ SELECT @total_rows_written;
is_transit,
is_clinical_encounter,
location_id,
last_non_transit_location_id,
t2.uuid as location_uuid,
visit_num,
mdt_session_number,
Expand Down Expand Up @@ -2314,7 +2372,11 @@ SELECT @total_rows_written;
ca_cx_screen,
ca_cx_screening_result,
ca_cx_screening_result_datetime,
ca_cx_screening_datetime
ca_cx_screening_datetime,
tb_modality_test,
tb_test_result,
tb_test_date,
gbv_screening_result

from flat_hiv_summary_4 t1
join amrs.location t2 using (location_id))');
Expand Down Expand Up @@ -2408,9 +2470,6 @@ SELECT
' second(s)');

set @end = now();
#if (@query_type="sync") then
#insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end));
#end if;
SELECT
CONCAT(@table_version,
' : Time to complete: ',
Expand Down