diff --git a/etl-scripts/stored-procedures/generate_emr_hiv_summary_v15_12.sql b/etl-scripts/stored-procedures/generate_emr_hiv_summary_v15_12.sql deleted file mode 100644 index 1362edc..0000000 --- a/etl-scripts/stored-procedures/generate_emr_hiv_summary_v15_12.sql +++ /dev/null @@ -1,2126 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_v15_12`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_hiv_summary_v15b"; - set @query_type = query_type; - set @queue_table = ""; - set @total_rows_written = 0; - - set @start = now(); - set @table_version = "flat_hiv_summary_v2.19"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - -CREATE TABLE IF NOT EXISTS flat_hiv_summary_v15b ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id INT, - uuid VARCHAR(100), - visit_id INT, - visit_type SMALLINT, - encounter_id INT, - encounter_datetime DATETIME, - encounter_type INT, - is_transit INT, - is_clinical_encounter INT, - location_id INT, - location_uuid VARCHAR(100), - visit_num INT, - mdt_session_number INT, - enrollment_date DATETIME, - enrollment_location_id INT, - hiv_start_date DATETIME, - death_date DATETIME, - scheduled_visit INT, - transfer_in TINYINT, - transfer_in_location_id INT, - transfer_in_date DATETIME, - transfer_out TINYINT, - transfer_out_location_id INT, - transfer_out_date DATETIME, - patient_care_status INT, - out_of_care INT, - prev_rtc_date DATETIME, - rtc_date DATETIME, - med_pickup_rtc_date DATETIME, - arv_first_regimen VARCHAR(500), - arv_first_regimen_location_id INT, - arv_first_regimen_start_date DATETIME, - arv_first_regimen_start_date_flex DATETIME, - prev_arv_meds VARCHAR(500), - cur_arv_meds VARCHAR(500), - cur_arv_meds_strict VARCHAR(500), - cur_arv_drugs VARCHAR(500), - prev_arv_drugs VARCHAR(500), - arv_start_date DATETIME, - arv_start_location_id INT, - prev_arv_start_date DATETIME, - prev_arv_end_date DATETIME, - prev_arv_line INT, - cur_arv_line INT, - cur_arv_line_strict INT, - cur_arv_line_reported TINYINT, - prev_arv_adherence VARCHAR(200), - cur_arv_adherence VARCHAR(200), - hiv_status_disclosed INT, - is_pregnant BOOLEAN, - edd DATETIME, - tb_screen BOOLEAN, - tb_screening_result INT, - tb_screening_datetime DATETIME, - on_ipt BOOLEAN, - ipt_start_date DATETIME, - ipt_stop_date DATETIME, - ipt_completion_date DATETIME, - on_tb_tx BOOLEAN, - tb_tx_start_date DATETIME, - tb_tx_end_date DATETIME, - pcp_prophylaxis_start_date DATETIME, - condoms_provided_date DATETIME, - modern_contraceptive_method_start_date DATETIME, - contraceptive_method INT, - cur_who_stage INT, - discordant_status INT, - cd4_resulted DOUBLE, - cd4_resulted_date DATETIME, - cd4_1 DOUBLE, - cd4_1_date DATETIME, - cd4_2 DOUBLE, - cd4_2_date DATETIME, - cd4_percent_1 DOUBLE, - cd4_percent_1_date DATETIME, - cd4_percent_2 DOUBLE, - cd4_percent_2_date DATETIME, - vl_resulted INT, - vl_resulted_date DATETIME, - vl_1 INT, - vl_1_date DATETIME, - vl_2 INT, - vl_2_date DATETIME, - expected_vl_date SMALLINT, - vl_order_date DATETIME, - cd4_order_date DATETIME, - hiv_dna_pcr_order_date DATETIME, - hiv_dna_pcr_resulted INT, - hiv_dna_pcr_resulted_date DATETIME, - hiv_dna_pcr_1 INT, - hiv_dna_pcr_1_date DATETIME, - hiv_dna_pcr_2 INT, - hiv_dna_pcr_2_date DATETIME, - hiv_rapid_test_resulted INT, - hiv_rapid_test_resulted_date DATETIME, - prev_encounter_datetime_hiv DATETIME, - next_encounter_datetime_hiv DATETIME, - prev_encounter_type_hiv MEDIUMINT, - next_encounter_type_hiv MEDIUMINT, - prev_clinical_datetime_hiv DATETIME, - next_clinical_datetime_hiv DATETIME, - prev_clinical_location_id MEDIUMINT, - next_clinical_location_id MEDIUMINT, - prev_clinical_rtc_date_hiv DATETIME, - next_clinical_rtc_date_hiv DATETIME, - outreach_date_bncd DATETIME, - outreach_death_date_bncd DATETIME, - outreach_patient_care_status_bncd INT, - transfer_date_bncd DATETIME, - transfer_transfer_out_bncd DATETIME, - phone_outreach INT, - home_outreach INT, - outreach_attempts INT, - outreach_missed_visit_reason INT, - PRIMARY KEY encounter_id (encounter_id), - INDEX person_date (person_id , encounter_datetime), - INDEX person_uuid (uuid), - INDEX location_enc_date (location_uuid , encounter_datetime), - INDEX enc_date_location (encounter_datetime , location_uuid), - INDEX location_id_rtc_date (location_id , rtc_date), - INDEX location_uuid_rtc_date (location_uuid , rtc_date), - INDEX loc_id_enc_date_next_clinical (location_id , encounter_datetime , next_clinical_datetime_hiv), - INDEX encounter_type (encounter_type), - INDEX date_created (date_created) -); - - - - 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 @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_hiv_summary_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from flat_hiv_summary_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_hiv_summary_v15b"; - set @queue_table = "flat_hiv_summary_sync_queue"; -CREATE TABLE IF NOT EXISTS flat_hiv_summary_sync_queue ( - person_id INT PRIMARY KEY -); - - - - set @last_update = null; -SELECT - MAX(date_updated) -INTO @last_update FROM - etl.flat_log -WHERE - table_name = @table_version; - - replace into flat_hiv_summary_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - -SELECT @person_ids_count AS 'num patients to sync'; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - - drop temporary table if exists flat_hiv_summary_build_queue__0; - - - - SET @dyn_sql=CONCAT('create temporary table flat_hiv_summary_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - drop temporary table if exists flat_hiv_summary_0a; - create temporary table flat_hiv_summary_0a - (select - t1.person_id, - t1.visit_id, - v.visit_type_id as visit_type, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - case - when v.visit_type_id IS NULL then NULL - when v.visit_type_id = 24 then 1 - when v.visit_type_id IS NOT NULL AND v.visit_type_id != 24 THEN 0 - else null - end as is_transit, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - - 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) 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 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,115,117,120,127,128,138, 140, 153,154,158,162,163,186) then 10 - when t1.encounter_type in (129) then 5 - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - 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,186) - AND NOT obs regexp "!!5303=(822|664|1067)!!" - AND NOT obs regexp "!!9082=9036!!" - ); - - insert into flat_hiv_summary_0a - (select - t1.person_id, - null, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - ); - - drop temporary table if exists flat_hiv_summary_0; - create temporary table flat_hiv_summary_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @prev_id = -1; - set @cur_id = -1; - set @prev_encounter_date = null; - set @cur_encounter_date = null; - set @enrollment_date = null; - set @hiv_start_date = null; - set @cur_location = null; - set @cur_rtc_date = null; - set @prev_rtc_date = null; - set @med_pickup_rtc_date = null; - set @hiv_start_date = null; - set @prev_arv_start_date = null; - set @arv_start_date = null; - set @prev_arv_end_date = null; - set @arv_start_location_id = null; - set @arv_first_regimen_start_date = null; - set @arv_first_regimen_start_date_flex = null; - set @arv_first_regimen = null; - set @prev_arv_line = null; - set @cur_arv_line = null; - set @prev_arv_adherence = null; - set @cur_arv_adherence = null; - set @hiv_status_disclosed = null; - set @is_pregnant = null; - set @edd = null; - set @prev_arv_meds = null; - set @cur_arv_meds = null; - set @cur_arv_drugs = null; - set @prev_arv_drugs = null; - set @ipt_start_date = null; - set @ipt_end_date = null; - set @ipt_completion_date = null; - - set @on_tb_tx = null; - set @tb_tx_start_date = null; - set @tb_tx_end_date = null; - set @pcp_prophylaxis_start_date = null; - set @tb_screen = null; - set @tb_screening_result = null; - set @tb_screening_datetime = null; - - set @death_date = null; - - set @patient_care_status=null; - - set @condoms_provided_date = null; - set @modern_contraceptive_method_start_date = null; - set @contraceptive_method = null; - - - set @cur_who_stage = null; - - set @vl_1=null; - set @vl_2=null; - set @vl_1_date=null; - set @vl_2_date=null; - set @vl_resulted=null; - set @vl_resulted_date=null; - - set @cd4_resulted=null; - set @cd4_resulted_date=null; - set @cd4_1=null; - set @cd4_1_date=null; - set @cd4_2=null; - set @cd4_2_date=null; - set @cd4_percent_1=null; - set @cd4_percent_1_date=null; - set @cd4_percent_2=null; - set @cd4_percent_2_date=null; - set @vl_order_date = null; - set @cd4_order_date = null; - - set @hiv_dna_pcr_order_date = null; - set @hiv_dna_pcr_1=null; - set @hiv_dna_pcr_2=null; - set @hiv_dna_pcr_1_date=null; - set @hiv_dna_pcr_2_date=null; - - set @hiv_rapid_test_resulted=null; - set @hiv_rapid_test_resulted_date= null; - - - - - - - drop temporary table if exists flat_hiv_summary_1; - create temporary table flat_hiv_summary_1 (index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.visit_type, - t1.encounter_id, - @prev_encounter_date := date(@cur_encounter_date) as prev_encounter_date, - @cur_encounter_date := date(encounter_datetime) as cur_encounter_date, - t1.encounter_datetime, - t1.encounter_type, - t1.is_transit, - t1.is_clinical_encounter, - CASE - WHEN - (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7013=' - THEN - @enrollment_date:=REPLACE(REPLACE((SUBSTRING_INDEX(SUBSTRING(obs, LOCATE('!!7013=', obs)), - @sep, - 1)), - '!!7013=', - ''), - '!!', - '') - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:='1900-01-01' - WHEN - t1.encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:=DATE(encounter_datetime) - WHEN @prev_id = @cur_id THEN @enrollment_date - ELSE @enrollment_date:=NULL - END AS enrollment_date, - - - - CASE - WHEN - (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7030=5622' - THEN - @enrollment_location_id:=9999 - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollmen_location_id:=9999 - WHEN - encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_location_id:= location_id - WHEN @prev_id = @cur_id THEN @enrollment_location_id - ELSE @enrollment_location_id:=NULL - END AS enrollment_location_id, - - - - - - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as 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 - end as visit_num, - case - when encounter_type=110 then - case - when obs regexp "!!(10532)" then @mdt_session_number:= 4 - when obs regexp "!!(10527|10528|10529|10530|10531)" then @mdt_session_number:= 3 - when obs regexp "!!(10523|10524|10525|10526)" then @mdt_session_number:= 2 - when obs regexp "!!(10518|10519|10520|10521|10522)" then @mdt_session_number:= 1 - when @prev_id = @cur_id then @mdt_session_number - else null - end - else @mdt_session_number - end as mdt_session_number, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - case - when obs regexp "!!9605=" then @med_pickup_rtc_date := replace(replace((substring_index(substring(obs,locate("!!9605=",obs)),@sep,1)),"!!9605=",""),"!!","") - else @med_pickup_rtc_date := null - end as med_pickup_rtc_date, - - - - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504|1285)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - - - - - - - - - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when @prev_id != @cur_id or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := encounter_datetime - else @death_date := null - end - else @death_date - end as death_date, - - - - - - case - when @death_date <= encounter_datetime then @patient_care_status := 159 - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - - case - when obs regexp "!!9203=" then @hiv_start_date := replace(replace((substring_index(substring(obs,locate("!!9203=",obs)),@sep,1)),"!!9203=",""),"!!","") - when obs regexp "!!7015=" then @hiv_start_date := "1900-01-01" - when @hiv_start_date is null then @hiv_start_date := date(encounter_datetime) - when @prev_id = @cur_id then @hiv_start_date - else @hiv_start_date := null - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_location_id := location_id - when @prev_id = @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location_id := location_id - when @prev_id != @cur_id then @arv_start_location_id := null - else @arv_start_location_id - end as arv_start_location_id, - - - case - when @prev_id=@cur_id and @cur_arv_meds is not null then @prev_arv_meds := @cur_arv_meds - when @prev_id=@cur_id then @prev_arv_meds - else @prev_arv_meds := null - end as prev_arv_meds, - - - - - #2154 : PATIENT REPORTED CURRENT ANTIRETROVIRAL TREATMENT - #2157 : PATIENT REPORTED PAST ANTIRETROVIRAL TREATMENT - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - when @prev_id = @cur_id then @cur_arv_meds - else @cur_arv_meds:= null - end as cur_arv_meds, - - - case - when 1 then null - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - else null - end as cur_arv_meds_strict, - - case - - when @prev_id=@cur_id and @prev_arv_drugs is not null then @prev_arv_drugs := @cur_arv_drugs - when @prev_id=@cur_id then @prev_arv_drugs - else @prev_arv_drugs := null - - end as prev_arv_drugs, - - case - - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_drugs := null - when obs regexp "!!1250=" then @cur_arv_drugs := normalizeArvsDrugFormulation(obs) - - - when obs regexp "!!1088=" then @cur_arv_drugs := normalizeArvsDrugFormulation(obs) - - when obs regexp "!!2154=" then @cur_arv_drugs := normalizeArvsDrugFormulation(obs) - - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_drugs := normalizeArvsDrugFormulation(obs) - - when @prev_id = @cur_id then @cur_arv_drugs - else @cur_arv_drugs:= null - - end as cur_arv_drugs, - - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := @cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date, - - - CASE - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_start_date:=GetValues(obs,1499) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_start_date:='1900-01-01' - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_start_date:=DATE(encounter_datetime) - - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_start_date:= "1900-01-01" #DATE(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_start_date:= "1900-01-01" # DATE(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_start_date:='1900-01-01' - WHEN @prev_id = @cur_id THEN @arv_first_regimen_start_date - WHEN @prev_id != @cur_id THEN @arv_first_regimen_start_date:=NULL - ELSE @arv_first_regimen_start_date - END AS arv_first_regimen_start_date, - - - - - /* - CASE - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_start_date_flex := GetValues(obs,1499) - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_start_date_flex :='1900-01-01' - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_start_date_flex:=DATE(encounter_datetime) - - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen_start_date_flex :=date(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen_start_date_flex :=date(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_start_date_flex:='1900-01-01' - WHEN @prev_id = @cur_id THEN @arv_first_regimen_start_date_flex - WHEN @prev_id != @cur_id THEN @arv_first_regimen_start_date_flex:=NULL - ELSE @arv_first_regimen_start_date_flex - END - */ - null AS arv_first_regimen_start_date_flex, - - - -/* - case - when @arv_first_regimen is null and obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @arv_first_regimen := normalize_arvs(obs,'2157') - when obs regexp "!!7015=" and @arv_first_regimen is null then @arv_first_regimen := "unknown" - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id = @cur_id then @arv_first_regimen - when @prev_id != @cur_id then @arv_first_regimen := @cur_arv_meds - else "-1" - end as arv_first_regimen, -*/ - - CASE - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen:= "unknown" - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen:= "unknown" - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen := @cur_arv_meds - - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen:= "unknown" #@cur_arv_meds - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen:= "unknown" #@cur_arv_meds - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen := "unknown" - - WHEN @prev_id = @cur_id THEN @arv_first_regimen - WHEN @prev_id != @cur_id THEN @arv_first_regimen:=NULL - ELSE @arv_first_regimen - END AS arv_first_regimen, - - -/* - case - when @arv_first_regimen is null and obs regexp "!!1499=" then @arv_first_regimen_location_id := 9999 - when @prev_id != @cur_id and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @arv_first_regimen_location_id is null and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @prev_id = @cur_id then @arv_first_regimen_location_id - when @prev_id != @cur_id then @arv_first_regimen_location_id := null - else "-1" - end as arv_first_regimen_location_id, -*/ - - CASE - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_location_id := 9999 - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_location_id:=9999 - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_location_id:=location_id - - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_location_id:= 9999 #location_id - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_location_id:= 9999 #location_id - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_location_id:=9999 - WHEN @prev_id = @cur_id THEN @arv_first_regimen_location_id - WHEN @prev_id != @cur_id THEN @arv_first_regimen_location_id:=NULL - ELSE @arv_first_regimen_location_id - END AS arv_first_regimen_location_id, - - - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := null - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=(6156)!!" then @cur_arv_line := 3 - when @prev_id = @cur_id then @cur_arv_line - else @cur_arv_line := null - end as cur_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1250=(6156)!!" then 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1088=(6156)!!" then 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then 2 - when obs regexp "!!2154=(6156)!!" then 3 - else null - end as cur_arv_line_strict, - - - - - - - - - - case - when obs regexp "!!6976=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6976=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6976=6695!!" then @cur_arv_line_reported := 3 - - when obs regexp "!!6744=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6744=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6744=6695!!" then @cur_arv_line_reported := 3 - when @prev_id = @cur_id then @cur_arv_line_reported - else @cur_arv_line_reported := null - end as cur_arv_line_reported, - - - - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := null - end as prev_arv_start_date, - - - - - - - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - - when @cur_arv_meds != @prev_arv_meds then @arv_start_date := date(t1.encounter_datetime) - - when @prev_id != @cur_id then @arv_start_date := null - else @arv_start_date - end as arv_start_date, - - - case - when @prev_arv_start_date != @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end as prev_arv_end_date, - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := null - end as prev_arv_adherence, - - - - - - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id = @cur_id then @cur_arv_adherence - else @cur_arv_adherence := null - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when @prev_id = @cur_id then @hiv_status_disclosed - else @hiv_status_disclosed := null - end as hiv_status_disclosed, - - - - - - - - - - - - - - - - - - case - when obs regexp "!!8351=(48|50|1066|1624|6971|9608)!!" then @is_pregnant := null - when @prev_id != @cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" or obs regexp "!!8351=(1065|1484)!!" then @is_pregnant := true - else @is_pregnant := null - end - when @is_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") or obs regexp "!!8351=(1065|1484)!!"then @is_pregnant := true - when @is_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@is_pregnant,encounter_datetime) > 40 or timestampdiff(week,@edd,encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @is_pregnant := null - else @is_pregnant - end as is_pregnant, - - - - - - - - - case - when @prev_id != @cur_id then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd := null - end - when @edd is null then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - - - case - when obs regexp "!!6174=" then @tb_screen := true - when obs regexp "!!2022=1065!!" then @tb_screen := true - when obs regexp "!!307=" then @tb_screen := true - when obs regexp "!!12=" then @tb_screen := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screen := true - when orders regexp "(12|307|8064|2311|2323)" then @tb_screen := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screen := true - when obs regexp "!!5958=1077!!" then @tb_screen := true - when obs regexp "!!2020=1065!!" then @tb_screen := true - when obs regexp "!!2021=1065!!" then @tb_screen := true - when obs regexp "!!2028=" then @tb_screen := true - when obs regexp "!!1268=(1256|1850)!!" then @tb_screen := true - when obs regexp "!!5959=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!5971=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!1492=107!!" then @tb_screen := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screen := true - end as tb_screen, - - case - when obs regexp "!!8292=" then @tb_screening_result := - replace(replace((substring_index(substring(obs,locate("!!8292=",obs)),@sep,1)),"!!8292=",""),"!!","") - when @prev_id != @cur_id then @tb_screening_result := null - else @tb_screening_result - end as tb_screening_result, - - case - when obs regexp "!!6174=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2022=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!307=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!12=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime - when orders regexp "(12|307|8064|2311|2323)" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5958=1077!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2020=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2021=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2028=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1268=(1256|1850)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5959=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5971=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1492=107!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screening_datetime := encounter_datetime - when @cur_id = @prev_id then @tb_screening_datetime - else @tb_screening_datetime := null - end as tb_screening_datetime, - - - case - when obs regexp "!!1265=(1107|1260)!!" then @on_ipt := 0 - when obs regexp "!!1265=!!" then @on_ipt := 1 - when obs regexp "!!1110=656!!" then @on_ipt := 1 - when @cur_id != @prev_id then @on_ipt := null - else @on_ipt - end as on_ipt, - - - - case - when obs regexp "!!1265=(1256|1850)!!" then @ipt_start_date := encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" and @ipt_start_date is null then @ipt_start_date := encounter_datetime - when obs regexp "!!10591=1065" and obs regexp "!!1190=" then @ipt_start_date := replace(replace((substring_index(substring(obs,locate("!!1190=",obs)),@sep,1)),"!!1190=",""),"!!","") - when @cur_id != @prev_id then @ipt_start_date := null - else @ipt_start_date - end as ipt_start_date, - - - - case - when obs regexp "!!1266=" then @ipt_stop_date := encounter_datetime - when obs regexp "!!10591=1065" and obs regexp "!!8603=" then @ipt_start_date := replace(replace((substring_index(substring(obs,locate("!!8603=",obs)),@sep,1)),"!!8603=",""),"!!","") - when @cur_id = @prev_id then @ipt_stop_date - when @cur_id != @prev_id then @ipt_stop_date := null - else @ipt_stop_date - end as ipt_stop_date, - - case - when obs regexp "!!1266=1267!!" then @ipt_completion_date := encounter_datetime - when @cur_id = @prev_id then @ipt_completion_date - when @cur_id != @prev_id then @ipt_completion_date := null - else @ipt_completion_date - end as ipt_completion_date, - - - case - when obs regexp "!!1268=(1107|1260)!!" then @on_tb_tx := 0 - when obs regexp "!!1268=" then @on_tb_tx := 1 - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @on_tb_tx := 1 - else @on_tb_tx := 0 - end as on_tb_tx, - - - - case - when obs regexp "!!1113=" then @tb_tx_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @tb_tx_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" and obs regexp "!!7015=" and @tb_tx_start_date is null then @tb_tx_start_date := null - when obs regexp "!!1268=(1257|1259|1849|981)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_start_date - else @tb_tx_start_date := null - end as tb_tx_start_date, - - - - - case - when obs regexp "!!2041=" then @tb_tx_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_tx_end_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_end_date - else @tb_tx_end_date := null - end as tb_tx_end_date, - - - - - - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1261=1257!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1193=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when @prev_id=@cur_id then @pcp_prophylaxis_start_date - else @pcp_prophylaxis_start_date := null - end as pcp_prophylaxis_start_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - else @cd4_2:=null - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - else @cd4_2_date:=null - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") + 0 as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") + 0 as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=null - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=null - end as cd4_1_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - else @cd4_percent_2:=null - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - else @cd4_percent_2_date:=null - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") + 1 as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=null - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=null - end as cd4_percent_1_date, - - - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <> date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - else @vl_2:=null - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - else @vl_2_date:=null - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") + 0 as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") + 0 as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=null - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date:=null - end as vl_1_date, - - - - - - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(encounter_datetime) - when orders regexp "856" then @vl_order_date := date(encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - else @vl_order_date := null - end as vl_order_date, - - case - - WHEN (@cur_arv_meds IS NOT NULL AND @vl_1 > 1000) AND (TIMESTAMPDIFF(DAY, @vl_1_date,date(encounter_datetime)) >= 90) THEN 1 - - WHEN (TIMESTAMPDIFF(MONTH,@arv_start_date,date(encounter_datetime)) <= 12) AND (@vl_1_date IS NULL OR TIMESTAMPDIFF(MONTH,@vl_1_date,date(encounter_datetime)) >= 6) AND (TIMESTAMPDIFF(MONTH,@arv_start_date,date(encounter_datetime)) >= 6) THEN 1 - - WHEN (TIMESTAMPDIFF(MONTH,@arv_start_date, date(encounter_datetime)) >= 12) AND (@vl_1_date IS NULL OR TIMESTAMPDIFF(MONTH, @vl_1_date, date(encounter_datetime)) >= 12) THEN 1 - - ELSE 0 - - - end as expected_vl_date, - - - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := null - end as cd4_order_date, - - - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := null - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - else @hiv_dna_pcr_2:=null - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - else @hiv_dna_pcr_2_date:=null - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=null - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=null - end as hiv_dna_pcr_1_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - case - when obs regexp "!!8302=8305!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!6579=" then @condoms_provided_date := encounter_datetime - when @prev_id = @cur_id then @condoms_provided_date - else @condoms_provided_date := null - end as condoms_provided_date, - - - - - - - case - when obs regexp "!!7240=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(encounter_datetime) - when obs regexp "!!7240=!!" - then @modern_contraceptive_method_start_date := null - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" and obs regexp "!!1190=" - then @modern_contraceptive_method_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1190=",obs)),@sep,1)),"!!1190=",""),"!!","")) - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(encounter_datetime) - when obs regexp "!!374=!!" - then @modern_contraceptive_method_start_date := null - when @prev_id = @cur_id then @modern_contraceptive_method_start_date - else @modern_contraceptive_method_start_date := null - end as modern_contraceptive_method_start_date, - - - case - when obs regexp "!!7240=1107!!" then @contraceptive_method := null - when obs regexp "!!7240=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!7240=",obs)),@sep,1)),"!!7240=",""),"!!","") - when obs regexp "!!374=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!374=",obs)),@sep,1)),"!!374=",""),"!!","") - when @prev_id = @cur_id then @contraceptive_method - else @contraceptive_method := null - end as contraceptive_method, - - case - when obs regexp "!!5356=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!8287=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!8287=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!8287=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!8287=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!8307=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!8307=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!8307=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!8307=(1207|1223)!!" then @cur_who_stage := 4 - when @prev_id = @cur_id then @cur_who_stage - else @cur_who_stage := null - end as cur_who_stage, - - - case - when obs regexp "!!6096=1065" then @discordant_status := 1 - when obs regexp "!!6096=1066" then @discordant_status := 2 - when obs regexp "!!6096=1067" then @discordant_status := 3 - when obs regexp "!!6096=1175" then @discordant_status := 4 - when obs regexp "!!6096=6826" then @discordant_status := 5 - when obs regexp "!!6096=6827" then @discordant_status := 6 - when @prev_id = @cur_id then @discordant_status - else @discordant_status := null - end as discordant_status, - - - case - when encounter_type=21 then - case - when obs regexp "!!9063=1065" AND obs regexp "!!9600=1065" then @phone_outreach:= 1 - when obs regexp "!!1569=1555" AND obs regexp "!!9600=1065" then @phone_outreach:= 1 - when obs regexp "!!1558=1555" AND obs regexp "!!9600=1065" then @phone_outreach:= 1 - when obs regexp "!!1558=1555" AND obs regexp "!!1559=1065" then @phone_outreach:= 1 - when obs regexp "!!9063=(1065|1560|9064|9065|9066|5622)" AND obs regexp "!!9600=1066" then @phone_outreach:= 2 - when obs regexp "!!1558=1555" AND obs regexp "!!9600=1066" then @phone_outreach:= 2 - when obs regexp "!!1558=1555" AND obs regexp "!!1559=1066" then @phone_outreach:= 2 - else @phone_outreach:= null - end - else @phone_outreach := null - end as phone_outreach, - case - when encounter_type=21 then - case - when obs regexp "!!10085=1065" AND obs regexp "!!1558=(7066|6116|1556|1557)" AND obs regexp "!!1559=1065" then @home_outreach:= 1 - when obs regexp "!!1558=7066" and obs regexp "!!1559=1065" then @home_outreach := 1 - when obs regexp "!!1569=1567" AND obs regexp "!!1559=1065" then @home_outreach := 1 - when obs regexp "!!10085=1065" AND "!!1558=(7066|6116|1556|1557)" AND obs regexp "!!1559=1066" then @home_outreach := 2 - when obs regexp "!!1569=1567" AND obs regexp "!!1559=1066" then @home_outreach := 2 - else @home_outreach:= null - end - else @home_outreach := null - end as home_outreach, - - case when encounter_type = 21 then obs regexp - case - when obs regexp "!!1553=" then @outreach_attempts:= CAST(GetValues(obs,'1553') AS SIGNED) - when obs regexp "!!9062=" then @outreach_attempts:= CAST(GetValues(obs,'9062') AS SIGNED) - end - else @outreach_attempts := null - end as outreach_attempts, - - 0 as outreach_missed_visit_reason - - - - - from flat_hiv_summary_0 t1 - join amrs.person p using (person_id) - ); - - - - set @prev_id = -1; - set @cur_id = -1; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_hiv_summary_1 drop prev_id, drop cur_id, drop cur_clinical_datetime, drop cur_clinic_rtc_date; - - drop table if exists flat_hiv_summary_2; - create temporary table flat_hiv_summary_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_date_bncd - else null - end as outreach_date_bncd, - - case - when encounter_type=21 and @outreach_date_bncd is null then @outreach_date_bncd := encounter_datetime - when is_clinical_encounter then @outreach_date_bncd := null - when @prev_id != @cur_id then @outreach_date_bncd := null - else @outreach_date_bncd - end as next_outreach_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_death_date_bncd - else null - end as outreach_death_date_bncd, - - case - when encounter_type=21 and @outreach_death_date_bncd is null then @outreach_death_date_bncd := death_date - when is_clinical_encounter then @outreach_death_date_bncd := null - when @prev_id != @cur_id then @outreach_death_date_bncd := null - else @outreach_death_date_bncd - end as next_outreach_death_date_bncd, - - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then cast(@outreach_patient_care_status_bncd as unsigned) - else null - end as outreach_patient_care_status_bncd, - - case - when encounter_type=21 and @outreach_patient_care_status_bncd is null then @outreach_patient_care_status_bncd := patient_care_status - when is_clinical_encounter then @outreach_patient_care_status_bncd := null - when @prev_id != @cur_id then @outreach_patient_care_status_bncd := null - else @outreach_patient_care_status_bncd - end as next_outreach_patient_care_status_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_date_bncd - else null - end as transfer_date_bncd, - - case - when encounter_type=116 and @transfer_date_bncd is null then @transfer_date_bncd := encounter_datetime - when is_clinical_encounter then @transfer_date_bncd := null - when @prev_id != @cur_id then @transfer_date_bncd := null - else @transfer_date_bncd - end as next_transfer_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_transfer_out_bncd - else null - end as transfer_transfer_out_bncd, - - case - when encounter_type=116 and @transfer_transfer_out_bncd is null then @transfer_transfer_out_bncd := encounter_datetime - when is_clinical_encounter then @transfer_transfer_out_bncd := null - when @prev_id != @cur_id then @transfer_transfer_out_bncd := null - else @transfer_transfer_out_bncd - end as next_transfer_transfer_out_bncd - - from flat_hiv_summary_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = -1; - set @cur_id = -1; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_hiv_summary_3; - create temporary table flat_hiv_summary_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_hiv, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - -/* Moved to flat_hiv_summary_1 - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, -*/ - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id, - - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id - -/* MOVED to flat_hiv_summary_1 - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date -*/ - from flat_hiv_summary_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - alter table flat_hiv_summary_3 drop prev_id, drop cur_id; - - set @prev_id = null; - set @cur_id = null; - set @transfer_in = null; - set @transfer_in_date = null; - set @transfer_in_location_id = null; - set @transfer_out = null; - set @transfer_out_date = null; - set @transfer_out_location_id = null; - - drop temporary table if exists flat_hiv_summary_4; - - create temporary table flat_hiv_summary_4 ( index person_enc (person_id, encounter_datetime)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - - - case - when obs regexp "!!7015=" then @transfer_in := 1 - when prev_clinical_location_id != location_id 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 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 then @transfer_in_location_id := prev_clinical_location_id - when @cur_id = @prev_id then @transfer_in_location_id - else @transfer_in_location_id := null - end transfer_in_location_id, - - - - - - - - - - - - case - when obs regexp "!!1285=!!" then @transfer_out := 1 - when obs regexp "!!1596=1594!!" then @transfer_out := 1 - when obs regexp "!!9082=(1287|1594|9068|9504|1285)!!" then @transfer_out := 1 - when next_clinical_location_id != location_id then @transfer_out := 1 - else @transfer_out := null - end as transfer_out, - - case - when obs regexp "!!1285=(1287|9068|2050)!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9999 - when obs regexp "!!1285=1286!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9998 - when next_clinical_location_id != location_id then @transfer_out_location_id := next_clinical_location_id - else @transfer_out_location_id := null - end transfer_out_location_id, - - - case - when @transfer_out and next_clinical_datetime_hiv is null then @transfer_out_date := date(cur_rtc_date) - when next_clinical_location_id != location_id then @transfer_out_date := date(next_clinical_datetime_hiv) - when transfer_transfer_out_bncd then @transfer_out_date := date(transfer_transfer_out_bncd) - else @transfer_out_date := null - end transfer_out_date - - - - from flat_hiv_summary_3 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - -SELECT - COUNT(*) -INTO @new_encounter_rows FROM - flat_hiv_summary_4; - -SELECT @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; -SELECT @total_rows_written; - - - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - t1.uuid, - visit_id, - visit_type, - encounter_id, - encounter_datetime, - encounter_type, - is_transit, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - visit_num, - mdt_session_number, - enrollment_date, - enrollment_location_id, - hiv_start_date, - death_date, - scheduled_visit, - transfer_in, - transfer_in_location_id, - transfer_in_date, - transfer_out, - transfer_out_location_id, - transfer_out_date, - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date as rtc_date, - med_pickup_rtc_date, - arv_first_regimen, - arv_first_regimen_location_id, - arv_first_regimen_start_date, - arv_first_regimen_start_date_flex, - prev_arv_meds, - cur_arv_meds, - cur_arv_meds_strict, - cur_arv_drugs, - prev_arv_drugs, - arv_start_date, - arv_start_location_id, - prev_arv_start_date, - prev_arv_end_date, - prev_arv_line, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - is_pregnant, - edd, - tb_screen, - tb_screening_result, - tb_screening_datetime, - on_ipt, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - on_tb_tx, - tb_tx_start_date, - tb_tx_end_date, - pcp_prophylaxis_start_date, - condoms_provided_date, - modern_contraceptive_method_start_date, - contraceptive_method, - cur_who_stage, - discordant_status, - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - expected_vl_date, - vl_order_date, - cd4_order_date, - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_location_id, - next_clinical_location_id, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv, - outreach_date_bncd, - outreach_death_date_bncd, - outreach_patient_care_status_bncd, - transfer_date_bncd, - transfer_transfer_out_bncd, - phone_outreach, - home_outreach, - outreach_attempts, - outreach_missed_visit_reason - - from flat_hiv_summary_4 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_hiv_summary_build_queue__0 t2 using (person_id);'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - -SELECT - @person_ids_count AS 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); -SELECT - CONCAT(@start_write, - ' : Writing ', - @total_rows_to_write, - ' to ', - @primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); -SELECT - CONCAT(@finish_write, - ' : Completed writing rows. Time to write to primary table: ', - @time_to_write, - ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); -SELECT - CONCAT('Average Cycle Length: ', - @ave_cycle_length, - ' 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: ', - TIMESTAMPDIFF(MINUTE, @start, @end), - ' minutes'); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_appointment_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_appointment_v1_0.sql deleted file mode 100644 index 8e88e11..0000000 --- a/etl-scripts/stored-procedures/generate_flat_appointment_v1_0.sql +++ /dev/null @@ -1,1048 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_appointment`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - select @start := now(); - select @table_version := "flat_appointment_v1.0"; - set @primary_table := "flat_appointment"; - set @query_type = query_type; - set @queue_table = ""; - set @total_rows_written = 0; - - - set session sort_buffer_size=512000000; - select @sep := " ## "; - select @last_date_created := (select max(max_date_created) from etl.flat_obs); - - #drop table if exists etl.appointment_2; - create table if not exists etl.flat_appointment - ( - date_created timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - encounter_id int primary key, - encounter_datetime datetime, - visit_id int, - visit_type_id smallint, - location_id smallint, - program_id smallint, - department_id smallint, - visit_start_datetime datetime, - is_clinical boolean, - - prev_encounter_datetime datetime, - next_encounter_datetime datetime, - - prev_encounter_type smallint, - encounter_type smallint, - next_encounter_type smallint, - - prev_rtc_date datetime, - rtc_date datetime, - next_rtc_date datetime, - - prev_clinical_encounter_datetime datetime, - next_clinical_encounter_datetime datetime, - - prev_clinical_rtc_date datetime, - next_clinical_rtc_date datetime, - - prev_clinical_encounter_type smallint, - next_clinical_encounter_type smallint, - - prev_program_encounter_datetime datetime, - next_program_encounter_datetime datetime, - - prev_program_rtc_date datetime, - next_program_rtc_date datetime, - - prev_program_encounter_type datetime, - next_program_encounter_type smallint, - - prev_program_clinical_datetime datetime, - next_program_clinical_datetime datetime, - - prev_program_clinical_rtc_date datetime, - next_program_clinical_rtc_date datetime, - - prev_department_encounter_datetime datetime, - next_department_encounter_datetime datetime, - - prev_department_rtc_date datetime, - next_department_rtc_date datetime, - - prev_department_encounter_type datetime, - next_department_encounter_type smallint, - - prev_department_clinical_datetime datetime, - next_department_clinical_datetime datetime, - - prev_department_clinical_rtc_date datetime, - next_department_clinical_rtc_date datetime, - - - index person_date (person_id, encounter_datetime), - index location_rtc (location_id,rtc_date), - index location_enc_date (location_id,encounter_datetime), - index enc_date_location (encounter_datetime, location_id), - index loc_id_enc_date_next_clinical (location_id, program_id, encounter_datetime, next_program_encounter_datetime), - index encounter_type (encounter_type) - ); - - if(query_type="build") then - select 'BUILDING..........................................'; - set @write_table = concat("flat_appointment_temp_",queue_number); - set @primary_queue_table = "flat_appointment_build_queue"; - set @queue_table = concat("flat_appointment_build_queue_",queue_number); - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from ', @primary_queue_table, ' limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_hiv_summary_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from ', @primary_queue_table, ' t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - if(query_type="sync") then - select "SYNCING......................................."; - set @primary_queue_table = "flat_appointment_sync_queue"; - set @write_table = concat("flat_appointment"); - set @queue_table = "flat_appointment_sync_queue"; - create table if not exists flat_hiv_summary_sync_queue (person_id int primary key); - - select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); - - # then use the max_date_created from amrs.encounter. This takes about 10 seconds and is better to avoid. - select @last_update := - if(@last_update is null, - (select max(e.date_created) from amrs.encounter e join etl.flat_appointment using (encounter_id)), - @last_update); - - #otherwise set to a date before any encounters had been created (i.g. we will get all encounters) - select @last_update := if(@last_update,@last_update,'1900-01-01'); - - - replace into etl.flat_appointment_sync_queue - (select distinct patient_id #, min(encounter_datetime) as start_date - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_appointment_sync_queue - (select distinct person_id #, min(encounter_datetime) as start_date - from etl.flat_obs - where max_date_created > @last_update - # group by person_id - # limit 10 - ); - - end if; - - create table if not exists flat_appointment_queue(person_id int, primary key (person_id)); - - # Remove test patients - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('delete t1 from ',@write_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - drop temporary table if exists flat_appointment_queue__0; - SET @dyn_sql=CONCAT('create temporary table flat_appointment_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - drop temporary table if exists etl.mapping_data_0; - create temporary table etl.mapping_data_0 - (select - t1.patient_id as person_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.visit_id, - t1.location_id, - t2.date_started as visit_start_datetime, - t3.visit_type_id - ,t4.obs - from flat_appointment_queue__0 t5 - join amrs.encounter t1 on t5.person_id = t1.patient_id - left outer join amrs.visit t2 using (visit_id) - left outer join amrs.visit_type t3 using (visit_type_id) - join etl.flat_obs t4 using (encounter_id) - where t1.voided=0 - order by t1.patient_id,t1.encounter_datetime - ); - - drop temporary table if exists etl.mapping_data; - create temporary table etl.mapping_data - (select t1.*, - t5.is_clinical, - t5.program_id, - t7.department_id - from etl.mapping_data_0 t1 - left outer join etl.clinical_encounter_type_map t5 using (encounter_type) - left outer join etl.program_department_map t7 using (program_id) - ); - - - #### Add "next" columns - set @cur_id = null; - set @prev_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_rtc_date = null; - set @rtc_date = null; - set @prev_is_clinical = null; - set @cur_is_clinical = null; - set @prev_clinical_rtc_date = null; - set @cur_clinical_rtc_date = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - - - drop temporary table if exists etl.foo_0; - create temporary table etl.foo_0 - (select t1.*, - @prev_id := @cur_id as prev_id, - # @cur_id := person_id as cur_id, - @cur_id := person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as next_rtc_date, - - # 5096 = return visit date - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type := null - end as next_encounter_type, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - # Clinical Appointments - - case - when @prev_id = @cur_id then @prev_is_clinical := @cur_is_clinical - else @prev_is_clinical := null - end as next_is_clinical, - - - case - when is_clinical - then @cur_is_clinical := is_clinical - else @cur_is_clinical := null - end as cur_is_clinical, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime, - - - case - when is_clinical then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date, - - case - when is_clinical then @cur_clinical_rtc_date := @cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @prev_id=@cur_id then @prev_clinical_encounter_type := @cur_clinical_encounter_type - else @prev_clinical_encounter_type := null - end as next_clinical_encounter_type, - - case - when is_clinical then @cur_clinical_encounter_type := encounter_type - when @prev_id = @cur_id then @cur_clinical_encounter_type - else @cur_clinical_encounter_type := null - end as cur_clinical_encounter_type - - - from etl.mapping_data t1 - order by person_id, encounter_datetime desc - ); - - ###*************************************************************************************************************************************** - # Adding "prev" columns - - alter table etl.foo_0 - drop prev_id - ,drop cur_id - ,drop cur_rtc_date - ,drop cur_encounter_type - ,drop cur_encounter_datetime - ,drop cur_is_clinical - ,drop cur_clinical_datetime - ,drop cur_clinical_encounter_type - ,drop cur_clinical_rtc_date - ; - - set @cur_id = null; - set @prev_id = null; - - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_rtc_date = null; - set @rtc_date = null; - - set @prev_is_clinical = null; - set @cur_is_clinical = null; - - set @prev_clinical_rtc_date = null; - set @cur_clinical_rtc_date = null; - - set @prev_encounter_type = null; - set @cur_encounter_type = null; - - - drop temporary table if exists etl.foo_1; - create temporary table etl.foo_1 - (select t1.*, - @prev_id := @cur_id as prev_id, - # @cur_id := person_id as cur_id, - @cur_id := person_id as cur_id, - - -# case -# when @prev_id=@cur_id then @prev_visit_id := @cur_visit_id -# else @prev_visit_id := null -# end as prev_visit_id, - -# @cur_visit_id := visit_id as cur_visit_id, - -# case -# when @prev_id != @cur_id then @prev_visit_type_id := null -# when @cur_visit_id != @prev_visit_id then @prev_visit_type_id := @cur_visit_type_id -# else @prev_visit_type_id -# end as prev_visit_type_id, - -# @cur_visit_type_id := visit_type_id as cur_visit_type_id, - - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - # 5096 = return visit date - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type := null - end as prev_encounter_type, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - # Clinical Appointments - - case - when @prev_id = @cur_id then @prev_is_clinical := @cur_is_clinical - else @prev_is_clinical := null - end as prev_is_clinical, - - - case - when is_clinical - then @cur_is_clinical := is_clinical - else @cur_is_clinical := null - end as cur_is_clinical, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime, - - - case - when is_clinical then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date, - - case - when is_clinical then @cur_clinical_rtc_date := @cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @prev_id=@cur_id then @prev_clinical_encounter_type := @cur_clinical_encounter_type - else @prev_clinical_encounter_type := null - end as prev_clinical_encounter_type, - - case - when is_clinical then @cur_clinical_encounter_type := encounter_type - when @prev_id = @cur_id then @cur_clinical_encounter_type - else @cur_clinical_encounter_type := null - end as cur_clinical_encounter_type - - - from etl.foo_0 t1 - order by person_id, encounter_datetime - ); - - - # *********************************************************************************************************************************** - # PROGAM BASED APPOINTMENT DATA - # Adding "next" columns - - alter table etl.foo_1 - drop prev_id - ,drop cur_id - ,drop cur_encounter_type - ,drop cur_encounter_datetime - ; - -#select * from foo_1; - - set @cur_id = null; - set @prev_id = null; - - set @prev_program_id = null; - set @cur_program_id = null; - - set @prev_program_encounter_datetime = null; - set @cur_program_encounter_datetime = null; - - set @prev_program_rtc_date = null; - set @cur_program_rtc_date = null; - - set @prev_program_encounter_type = null; - set @cur_program_encounter_type = null; - - set @prev_program_clinical_datetime = null; - set @cur_program_clinical_datetime = null; - - set @prev_program_clinical_rtc_date = null; - set @cur_program_clinical_rtc_date = null; - - - drop temporary table if exists etl.foo_2; - create temporary table etl.foo_2 - (select *, - @prev_id := @cur_id as prev_id, - # @cur_id := person_id as cur_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_program_id := @cur_program_id - else @prev_program_id := null - end as next_program_id, - - @cur_program_id := program_id as cur_program_id, - - case - when @prev_id != @cur_id then @prev_program_datetime := null - when @prev_program_id != @cur_program_id then @prev_program_datetime := null - else @prev_program_datetime := @cur_program_datetime - end as next_program_encounter_datetime, - - case - when program_id is not null then @cur_program_datetime := encounter_datetime - else @cur_program_datetime := null - end as cur_program_encounter_datetime, - - case - when @prev_id != @cur_id then @prev_program_rtc_date := null - when @prev_program_id != @cur_program_id then @prev_program_rtc_date := null - else @prev_program_rtc_date := @cur_program_rtc_date - end as next_program_rtc_date, - - @cur_program_rtc_date := cur_rtc_date as cur_program_rtc_date, - - case - when @prev_id!=@cur_id then @prev_program_encounter_type := null - when @prev_program_id != @cur_program_id then @prev_program_encounter_type := null - else @prev_program_encounter_type := @cur_program_encounter_type - end as next_program_encounter_type, - - @cur_program_encounter_type := encounter_type as cur_program_encounter_type, - - case - when @prev_id != @cur_id then @prev_program_clinical_datetime := null - when @prev_program_id != @cur_program_id then @prev_program_clinical_datetime := null - else @prev_program_clinical_datetime := @cur_program_clinical_datetime - end as next_program_clinical_datetime, - - case - when program_id is not null and is_clinical then @cur_program_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_program_clinical_datetime - else @cur_program_clinical_datetime := null - end cur_program_clinical_datetime, - - - case - when @prev_id != @cur_id then @prev_program_clinical_rtc_date := null - when @prev_program_id != @cur_program_id then @prev_program_clinical_rtc_date := null - else @prev_program_clinical_rtc_date := @cur_program_clinical_rtc_date - end as next_program_clinical_rtc_date, - - case - when program_id is not null and is_clinical then @cur_program_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_program_clinical_rtc_date - else @cur_program_clinical_rtc_date := null - end cur_program_clinical_rtc_date - - - from etl.foo_1 - order by person_id, program_id, encounter_datetime desc - ); - - - #******************#****************** - # Adding Program "prev" columns - - alter table etl.foo_2 - drop prev_id - ,drop cur_id - ,drop cur_program_id - ,drop cur_program_encounter_datetime - ,drop cur_program_encounter_type - ,drop cur_program_rtc_date - ,drop cur_program_clinical_datetime - ,drop cur_program_clinical_rtc_date - ; - - - set @cur_id = null; - set @prev_id = null; - - set @prev_program_id = null; - set @cur_program_id = null; - - set @prev_program_encounter_datetime = null; - set @cur_program_encounter_datetime = null; - - set @prev_program_rtc_date = null; - set @cur_program_rtc_date = null; - - set @prev_program_encounter_type = null; - set @cur_program_encounter_type = null; - - set @prev_program_clinical_datetime = null; - set @cur_program_clinical_datetime = null; - - set @prev_program_clinical_rtc_date = null; - set @cur_program_clinical_rtc_date = null; - - - drop temporary table if exists etl.foo_3; - create temporary table etl.foo_3 - (select *, - @prev_id := @cur_id as prev_id, - # @cur_id := person_id as cur_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_program_id := @cur_program_id - else @prev_program_id := null - end as prev_program_id, - - @cur_program_id := program_id as cur_program_id, - - case - when @prev_id != @cur_id then @prev_program_datetime := null - when @prev_program_id != @cur_program_id then @prev_program_datetime := null - else @prev_program_datetime := @cur_program_datetime - end as prev_program_encounter_datetime, - - case - when program_id is not null then @cur_program_datetime := encounter_datetime - else @cur_program_datetime := null - end as cur_program_encounter_datetime, - - - case - when @prev_id != @cur_id then @prev_program_rtc_date := null - when @prev_program_id != @cur_program_id then @prev_program_rtc_date := null - else @prev_program_rtc_date := @cur_program_rtc_date - end as prev_program_rtc_date, - - case - when program_id is not null then @cur_program_rtc_date := cur_rtc_date - else @cur_program_rtc_date := null - end as cur_program_rtc_date, - - case - when @prev_id!=@cur_id then @prev_program_encounter_type := null - when @prev_program_id != @cur_program_id then @prev_program_encounter_type := null - else @prev_program_encounter_type := @cur_program_encounter_type - end as prev_program_encounter_type, - - case - when program_id is not null then @cur_program_encounter_type := encounter_type - else @cur_program_encounter_type := null - end as cur_program_encounter_type, - - - case - when @prev_id != @cur_id then @prev_program_clinical_datetime := null - when @prev_program_id != @cur_program_id then @prev_program_clinical_datetime := null - else @prev_program_clinical_datetime := @cur_program_clinical_datetime - end as prev_program_clinical_datetime, - - case - when program_id is not null and is_clinical then @cur_program_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_program_clinical_datetime - else @cur_program_clinical_datetime := null - end cur_program_clinical_datetime, - - - case - when @prev_id != @cur_id then @prev_program_clinical_rtc_date := null - when @prev_program_id != @cur_program_id then @prev_program_clinical_rtc_date := null - else @prev_program_clinical_rtc_date := @cur_program_clinical_rtc_date - end as prev_program_clinical_rtc_date, - - case - when program_id is not null and is_clinical then @cur_program_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_program_clinical_rtc_date - else @cur_program_clinical_rtc_date := null - end cur_program_clinical_rtc_date - - - from etl.foo_2 - order by person_id, program_id, encounter_datetime - ); - - - - - # *********************************************************************************************************************************** - # DEPARTMENT BASED APPOINTMENT DATA - # Adding "next" columns - - - alter table etl.foo_3 - drop prev_id - ,drop cur_id - ,drop cur_program_encounter_type - ,drop cur_program_encounter_datetime - ,drop cur_program_clinical_datetime - ,drop cur_program_rtc_date - ,drop cur_program_clinical_rtc_date - - ; - - set @cur_id = null; - set @prev_id = null; - - set @prev_department_id = null; - set @cur_department_id = null; - - set @prev_department_datetime = null; - set @cur_department_datetime = null; - - set @prev_department_rtc_date = null; - set @cur_department_rtc_date = null; - - set @prev_department_encounter_type = null; - set @cur_department_encounter_type = null; - - set @prev_department_clinical_datetime = null; - set @cur_department_clinical_datetime = null; - set @prev_department_clinical_rtc_date = null; - set @cur_department_clinical_rtc_date = null; - - - drop temporary table if exists etl.foo_4; - create temporary table etl.foo_4 - (select *, - @prev_id := @cur_id as prev_id, - # @cur_id := person_id as cur_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_department_id := @cur_department_id - else @prev_department_id := null - end as next_department_id, - - @cur_department_id := department_id as cur_department_id, - - case - when @prev_id != @cur_id then @prev_department_datetime := null - when @prev_department_id != @cur_department_id then @prev_department_datetime := null - else @prev_department_datetime := @cur_department_datetime - end as next_department_encounter_datetime, - - case - when department_id is not null then @cur_department_datetime := encounter_datetime - else @cur_department_datetime := null - end as cur_department_encounter_datetime, - - case - when @prev_id != @cur_id then @prev_department_rtc_date := null - when @prev_department_id != @cur_department_id then @prev_department_rtc_date := null - else @prev_department_rtc_date := @cur_department_rtc_date - end as next_department_rtc_date, - - @cur_department_rtc_date := cur_rtc_date as cur_department_rtc_date, - - case - when @prev_id!=@cur_id then @prev_department_encounter_type := null - when @prev_department_id != @cur_department_id then @prev_department_encounter_type := null - else @prev_department_encounter_type := @cur_department_encounter_type - end as next_department_encounter_type, - - @cur_department_encounter_type := encounter_type as cur_department_encounter_type, - - case - when @prev_id != @cur_id then @prev_department_clinical_datetime := null - when @prev_department_id != @cur_department_id then @prev_department_clinical_datetime := null - else @prev_department_datetime := @cur_department_clinical_datetime - end as next_department_clinical_datetime, - - case - when department_id is not null and is_clinical then @cur_department_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_department_clinical_datetime - else @cur_department_clinical_datetime := null - end cur_department_clinical_datetime, - - - case - when @prev_id != @cur_id then @prev_department_clinical_rtc_date := null - when @prev_department_id != @cur_department_id then @prev_department_clinical_rtc_date := null - else @prev_department_clinical_rtc_date := @cur_department_clinical_rtc_date - end as next_department_clinical_rtc_date, - - case - when department_id is not null and is_clinical then @cur_department_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_department_clinical_rtc_date - else @cur_department_clinical_rtc_date := null - end cur_department_clinical_rtc_date - - - from etl.foo_3 - order by person_id, department_id, encounter_datetime desc - ); - - - - #******************#****************** - # Adding Department "prev" columns - - alter table etl.foo_4 - drop prev_id - ,drop cur_id - ,drop cur_department_id - ,drop cur_department_encounter_datetime - ,drop cur_department_encounter_type - ,drop cur_department_rtc_date - ,drop cur_department_clinical_datetime - ,drop cur_department_clinical_rtc_date - ; - - - set @cur_id = null; - set @prev_id = null; - - set @prev_department_id = null; - set @cur_department_id = null; - - set @prev_department_datetime = null; - set @cur_department_datetime = null; - - set @prev_department_rtc_date = null; - set @cur_department_rtc_date = null; - - set @prev_department_encounter_type = null; - set @cur_department_encounter_type = null; - - set @prev_department_clinical_datetime = null; - set @cur_department_clinical_datetime = null; - - set @prev_department_clinical_rtc_date = null; - set @cur_department_clinical_rtc_date = null; - - - drop temporary table if exists etl.foo_5; - create temporary table etl.foo_5 - (select *, - @prev_id := @cur_id as prev_id, - # @cur_id := person_id as cur_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_department_id := @cur_department_id - else @prev_department_id := null - end as prev_department_id, - - @cur_department_id := department_id as cur_department_id, - - case - when @prev_id != @cur_id then @prev_department_datetime := null - when @prev_department_id != @cur_department_id then @prev_department_datetime := null - else @prev_department_datetime := @cur_department_datetime - end as prev_department_encounter_datetime, - - case - when department_id is not null then @cur_department_datetime := encounter_datetime - else @cur_department_datetime := null - end as cur_department_encounter_datetime, - - - case - when @prev_id != @cur_id then @prev_department_rtc_date := null - when @prev_department_id != @cur_department_id then @prev_department_rtc_date := null - else @prev_department_rtc_date := @cur_department_rtc_date - end as prev_department_rtc_date, - - case - when department_id is not null then @cur_department_rtc_date := cur_rtc_date - else @cur_department_rtc_date := null - end as cur_department_rtc_date, - - case - when @prev_id!=@cur_id then @prev_department_encounter_type := null - when @prev_department_id != @cur_department_id then @prev_department_encounter_type := null - else @prev_department_encounter_type := @cur_department_encounter_type - end as prev_department_encounter_type, - - case - when department_id is not null then @cur_department_encounter_type := encounter_type - else @cur_department_encounter_type := null - end as cur_department_encounter_type, - - - case - when @prev_id != @cur_id then @prev_department_clinical_datetime := null - when @prev_department_id != @cur_department_id then @prev_department_clinical_datetime := null - else @prev_department_clinical_datetime := @cur_department_clinical_datetime - end as prev_department_clinical_datetime, - - case - when department_id is not null and is_clinical then @cur_department_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_department_clinical_datetime - else @cur_department_clinical_datetime := null - end cur_department_clinical_datetime, - - - case - when @prev_id != @cur_id then @prev_department_clinical_rtc_date := null - when @prev_department_id != @cur_department_id then @prev_department_clinical_rtc_date := null - else @prev_department_clinical_rtc_date := @cur_department_clinical_rtc_date - end as prev_department_clinical_rtc_date, - - case - when department_id is not null and is_clinical then @cur_department_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_department_clinical_rtc_date - else @cur_department_clinical_rtc_date := null - end cur_department_clinical_rtc_date - - - from etl.foo_4 - order by person_id, program_id, encounter_datetime - ); - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - NULL, - person_id, - encounter_id, - encounter_datetime, - visit_id, - visit_type_id, - location_id, - program_id, - department_id, - visit_start_datetime, - is_clinical boolean, - - prev_encounter_datetime, - next_encounter_datetime, - - prev_encounter_type, - encounter_type, - next_encounter_type, - - prev_rtc_date, - cur_rtc_date, - next_rtc_date, - - prev_clinical_datetime, - next_clinical_datetime, - - prev_clinical_rtc_date, - next_clinical_rtc_date, - - prev_clinical_encounter_type, - next_clinical_encounter_type, - - prev_program_encounter_datetime, - next_program_encounter_datetime, - - prev_program_rtc_date, - next_program_rtc_date, - - prev_program_encounter_type, - next_program_encounter_type, - - prev_program_clinical_datetime, - next_program_clinical_datetime, - - prev_program_clinical_rtc_date, - next_program_clinical_rtc_date, - - - prev_department_encounter_datetime, - next_department_encounter_datetime, - - prev_department_rtc_date, - next_department_rtc_date, - - prev_department_encounter_type, - next_department_encounter_type, - - prev_department_clinical_datetime, - next_department_clinical_datetime, - - prev_department_clinical_rtc_date, - next_department_clinical_rtc_date - - from etl.foo_5 - );'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_appointment_queue__0 t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - select @person_ids_count as remaining_in_build_queue; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - select @end := now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_appointments_v1_1.sql b/etl-scripts/stored-procedures/generate_flat_appointments.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_flat_appointments_v1_1.sql rename to etl-scripts/stored-procedures/generate_flat_appointments.sql index 6366a06..4d4db22 100644 --- a/etl-scripts/stored-procedures/generate_flat_appointments_v1_1.sql +++ b/etl-scripts/stored-procedures/generate_flat_appointments.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_appointment_v1_1`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_appointment`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN select @start := now(); SELECT @table_version:='flat_appointment_v1.1'; diff --git a/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening.sql b/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening.sql index fb48463..72b5c65 100644 --- a/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening.sql +++ b/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening.sql @@ -1,1229 +1,1247 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_breast_cancer_screening_v1_2`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_breast_cancer_screening`(IN query_type VARCHAR(50), IN queue_number INT, IN queue_size INT, IN cycle_size INT) BEGIN - set @primary_table := "flat_breast_cancer_screening"; - set @query_type = query_type; -#set @query_type = "build"; + -- v1.3: Rename `breast_complaints_3months` column to `breast_complaints_3months` + -- Rename `Ultrasound_results` to `ultrasound_results` + -- Add new columns: + -- breast_mass_location, + -- nipple_discharge_location, + -- nipple_retraction_location, + -- breast_erythrema_location, + -- breast_rash_location, + -- breast_pain_location, + -- other_changes_location, + -- history_of_mammogram, + -- mammogram_results, + -- breast_ultrasound_history, + -- breast_ultrasound_result, + -- history_of_breast_biopsy, + -- breast_biopsy_results, + -- number_of_biopsies, + -- biopsy_type, + -- prev_exam_results + + SET @primary_table := "flat_breast_cancer_screening"; + SET @query_type := query_type; - set @total_rows_written = 0; + SET @total_rows_written := 0; - set @encounter_types = "(86,145,146,160)"; - set @clinical_encounter_types = "(86,145,146,160)"; - set @non_clinical_encounter_types = "(-1)"; - set @other_encounter_types = "(-1)"; - - set @start = now(); - set @table_version = "flat_breast_cancer_screening_v1.2"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @boundary = "!!"; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - #delete from etl.flat_log where table_name like "%flat_breast_cancer_screening%"; - #drop table etl.flat_breast_cancer_screening; - - - #drop table if exists flat_breast_cancer_screening; - create table if not exists flat_breast_cancer_screening ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - encounter_id int, - encounter_type int, - encounter_datetime datetime, - visit_id int, - location_id int, - location_uuid varchar (100), - #location_name char (100), - gender char (100), - age int, - encounter_purpose int, - other_encounter_purpose varchar(1000), - menstruation_before_12 char, - menses_stopped_permanently char, - menses_stop_age int, - hrt_use char, - hrt_start_age int, - hrt_end_age int, - hrt_use_years int, - hrt_type_used int, - given_birth int, - age_first_birth int, - gravida int, - parity int, - cigarette_smoking int, - cigarette_smoked_day int, - tobacco_use int, - tobacco_use_duration_yrs int, - alcohol_drinking int, - alcohol_type int, - alcohol_use_period_yrs int, - breast_complaints_3moths int, - prev_exam_results int, - fam_brca_history_bf50 int, - fam_brca_history_aft50 int, - fam_male_brca_history int, - fam_ovarianca_history int, - fam_relatedca_history int, - fam_otherca_specify int, - cur_physical_findings int, - lymph_nodes_findings int, - cur_screening_findings int, - #cur_screening_findings_date int, - patient_education int, - patient_education_other varchar(1000), - referred_orderd int, - referred_date datetime, - procedure_done int, - next_app_date datetime, - - cbe_imaging_concordance int, - mammogram_results int, - mammogram_workup_date datetime, - date_patient_notified_of_mammogram_results datetime, - Ultrasound_results int, - ultrasound_workup_date datetime, - date_patient_notified_of_ultrasound_results datetime, - fna_results int, - fna_tumor_size int, - fna_degree_of_malignancy int, - fna_workup_date datetime, - date_patient_notified_of_fna_results datetime, - breast_biopsy_results int, - biopsy_tumor_size int, - biopsy_degree_of_malignancy int, - biopsy_workup_date datetime, - date_patient_notified_of_biopsy_results datetime, - date_patient_informed_and_referred_for_management datetime, - screening_mode int, - diagnosis int, - diagnosis_date datetime, - cancer_staging int, - hiv_status INT, - - prev_encounter_datetime_breast_cancer_screening datetime, - next_encounter_datetime_breast_cancer_screening datetime, - prev_encounter_type_breast_cancer_screening mediumint, - next_encounter_type_breast_cancer_screening mediumint, - prev_clinical_datetime_breast_cancer_screening datetime, - next_clinical_datetime_breast_cancer_screening datetime, - prev_clinical_location_id_breast_cancer_screening mediumint, - next_clinical_location_id_breast_cancer_screening mediumint, - prev_clinical_rtc_date_breast_cancer_screening datetime, - next_clinical_rtc_date_breast_cancer_screening datetime, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,next_app_date), - index location_uuid_rtc_date (location_uuid,next_app_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_breast_cancer_screening), - index encounter_type (encounter_type), - index date_created (date_created) + SET @encounter_types = "(86, 145, 146, 160)"; + SET @clinical_encounter_types = "(86, 145, 146, 160)"; + SET @non_clinical_encounter_types = "(-1)"; + SET @other_encounter_types = "(-1)"; + + SET @start := NOW(); + SET @table_version := "flat_breast_cancer_screening_v1.3"; + + SET session sort_buffer_size := 512000000; + + SET @sep := " ## "; + SET @boundary := "!!"; + SET @last_date_created := (SELECT MAX(max_date_created) FROM etl.flat_obs); + + CREATE TABLE IF NOT EXISTS flat_breast_cancer_screening ( + date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + person_id int, + encounter_id int, + encounter_type int, + encounter_datetime datetime, + visit_id int, + location_id int, + location_uuid varchar (100), + gender char (100), + age int, + encounter_purpose int, + other_encounter_purpose varchar(1000), + menstruation_before_12 char, + menses_stopped_permanently char, + menses_stop_age int, + hrt_use char, + hrt_start_age int, + hrt_end_age int, + hrt_use_years int, + hrt_type_used int, + given_birth int, + age_first_birth int, + gravida int, + parity int, + cigarette_smoking int, + cigarette_smoked_day int, + tobacco_use int, + tobacco_use_duration_yrs int, + alcohol_drinking int, + alcohol_type int, + alcohol_use_period_yrs int, + breast_complaints_3months int, + breast_mass_location tinyint, + nipple_discharge_location tinyint, + nipple_retraction_location tinyint, + breast_erythrema_location tinyint, + breast_rash_location tinyint, + breast_pain_location tinyint, + other_changes_location tinyint, + history_of_mammogram tinyint, + mammogram_results tinyint, + breast_ultrasound_history tinyint, + breast_ultrasound_result tinyint, + history_of_breast_biopsy tinyint, + breast_biopsy_results tinyint, + number_of_biopsies tinyint, + biopsy_type tinyint, + prev_exam_results int, + fam_brca_history_bf50 int, + fam_brca_history_aft50 int, + fam_male_brca_history int, + fam_ovarianca_history int, + fam_relatedca_history int, + fam_otherca_specify int, + cur_physical_findings int, + lymph_nodes_findings int, + cur_screening_findings int, + #cur_screening_findings_date int, + patient_education int, + patient_education_other varchar(1000), + referrals_ordered int, + referred_date datetime, + procedure_done int, + next_app_date datetime, + cbe_imaging_concordance int, + mammogram_workup_date datetime, + date_patient_notified_of_mammogram_results datetime, + ultrasound_results int, + ultrasound_workup_date datetime, + date_patient_notified_of_ultrasound_results datetime, + fna_results int, + fna_tumor_size int, + fna_degree_of_malignancy int, + fna_workup_date datetime, + date_patient_notified_of_fna_results datetime, + biopsy_tumor_size int, + biopsy_degree_of_malignancy int, + biopsy_workup_date datetime, + date_patient_notified_of_biopsy_results datetime, + biopsy_description varchar(1000), + diagnosis_date datetime, + date_patient_informed_and_referred_for_management datetime, + screening_mode int, + diagnosis int, + cancer_staging int, + hiv_status int, + prev_encounter_datetime_breast_cancer_screening datetime, + next_encounter_datetime_breast_cancer_screening datetime, + prev_encounter_type_breast_cancer_screening mediumint, + next_encounter_type_breast_cancer_screening mediumint, + prev_clinical_datetime_breast_cancer_screening datetime, + next_clinical_datetime_breast_cancer_screening datetime, + prev_clinical_location_id_breast_cancer_screening mediumint, + next_clinical_location_id_breast_cancer_screening mediumint, + prev_clinical_rtc_date_breast_cancer_screening datetime, + next_clinical_rtc_date_breast_cancer_screening datetime, + primary key encounter_id (encounter_id), + index person_date (person_id, encounter_datetime), + index location_enc_date (location_uuid,encounter_datetime), + index enc_date_location (encounter_datetime, location_uuid), + index location_id_rtc_date (location_id,next_app_date), + index location_uuid_rtc_date (location_uuid,next_app_date), + index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_breast_cancer_screening), + index encounter_type (encounter_type), + index date_created (date_created) + ); + + -- Build step + IF (@query_type = "build") THEN + SELECT 'BUILDING..........................................'; - ); + SET @write_table := CONCAT('flat_breast_cancer_screening_temp_', queue_number); + SET @queue_table := CONCAT('flat_breast_cancer_screening_build_queue_', queue_number); + + SET @dyn_sql := CONCAT('CREATE TABLE IF NOT EXISTS ', @write_table, ' LIKE ', @primary_table); + PREPARE s1 FROM @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SET @dyn_sql := CONCAT('CREATE TABLE IF NOT EXISTS ', @queue_table, ' (SELECT * FROM flat_breast_cancer_screening_build_queue LIMIT ', queue_size, ');'); + PREPARE s1 FROM @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SET @dyn_sql := CONCAT('DELETE t1 FROM flat_breast_cancer_screening_build_queue t1 join ', @queue_table, ' t2 using (person_id);'); + PREPARE s1 FROM @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + END IF; + + -- Sync step + IF (@query_type = 'sync') THEN + SELECT 'SYNCING.....................................'; + + SET @write_table := 'flat_breast_cancer_screening'; + SET @queue_table := 'flat_breast_cancer_screening_sync_queue'; + + CREATE TABLE IF NOT EXISTS flat_breast_cancer_screening_sync_queue (person_id INT PRIMARY KEY); + + SET @last_update := null; + + SELECT MAX(date_updated) INTO @last_update FROM etl.flat_log WHERE table_name = @table_version; + + SELECT 'Finding patients in amrs.encounters...'; + REPLACE INTO flat_breast_cancer_screening_sync_queue + (SELECT DISTINCT + patient_id + FROM + amrs.encounter + WHERE + date_changed > @last_update + ); + + SELECT 'Finding patients in flat_obs...'; + REPLACE INTO flat_breast_cancer_screening_sync_queue + (SELECT DISTINCT + person_id + FROM + etl.flat_obs + WHERE + max_date_created > @last_update + ); + + SELECT 'Finding patients in flat_lab_obs...'; + REPLACE INTO flat_breast_cancer_screening_sync_queue + (SELECT DISTINCT + person_id + FROM + etl.flat_lab_obs + WHERE + max_date_created > @last_update + ); + + SELECT 'Finding patients in flat_orders...'; + REPLACE INTO flat_breast_cancer_screening_sync_queue + (SELECT DISTINCT + person_id + FROM + etl.flat_orders + WHERE + max_date_created > @last_update + ); + + REPLACE INTO flat_breast_cancer_screening_sync_queue + (SELECT + person_id + FROM + amrs.person + WHERE + date_voided > @last_update + ); + + REPLACE INTO flat_breast_cancer_screening_sync_queue + (SELECT + person_id + FROM + amrs.person + WHERE + date_changed > @last_update + ); + END IF; + + -- Remove test patients + SET @dyn_sql := CONCAT('DELETE t1 FROM ', @queue_table, ' t1 + JOIN amrs.person_attribute t2 USING (person_id) + WHERE t2.person_attribute_type_id = 28 AND value = "true" AND voided = 0'); + PREPARE s1 FROM @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SET @person_ids_count = 0; + SET @dyn_sql=CONCAT('SELECT COUNT(*) INTO @person_ids_count FROM ', @queue_table); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SELECT @person_ids_count AS 'num of patients to update'; + + SET @dyn_sql := CONCAT('DELETE t1 FROM ',@primary_table, ' t1 JOIN ', @queue_table,' t2 USING (person_id);'); + PREPARE s1 FROM @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SET @person_ids_count = 0; + SET @dyn_sql := CONCAT('SELECT COUNT(*) INTO @person_ids_count FROM ', @queue_table); + PREPARE s1 FROM @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SELECT @person_ids_count AS 'num patients to update'; + + SET @dyn_sql := CONCAT('DELETE t1 FROM ', @primary_table, ' t1 join ', @queue_table,' t2 USING (person_id);'); + PREPARE s1 FROM @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SET @total_time = 0; + SET @cycle_number = 0; + + SET @total_time = 0; + SET @cycle_number = 0; + + WHILE @person_ids_count > 0 DO + SET @loop_start_time := NOW(); + + -- Create temporary table with a set of person ids + DROP TEMPORARY TABLE IF EXISTS flat_breast_cancer_screening_build_queue__0; + + SET @dyn_sql := CONCAT('CREATE TEMPORARY TABLE flat_breast_cancer_screening_build_queue__0 (person_id INT PRIMARY KEY) (SELECT * FROM ', @queue_table, ' LIMIT ', cycle_size, ');'); + PREPARE s1 FROM @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + DROP TEMPORARY TABLE IF EXISTS flat_breast_cancer_screening_0a; + SET @dyn_sql := CONCAT( + 'CREATE TEMPORARY TABLE flat_breast_cancer_screening_0a + (SELECT + t1.person_id, + t1.visit_id, + t1.encounter_id, + t1.encounter_datetime, + t1.encounter_type, + t1.location_id, + t1.obs, + t1.obs_datetimes, + case + when t1.encounter_type in ',@clinical_encounter_types,' then 1 + else null + end as is_clinical_encounter, + case + when t1.encounter_type in ',@non_clinical_encounter_types,' then 20 + when t1.encounter_type in ',@clinical_encounter_types,' then 10 + when t1.encounter_type in', @other_encounter_types, ' then 5 + else 1 + end as encounter_type_sort_index, + t2.orders + FROM + etl.flat_obs t1 + JOIN + flat_breast_cancer_screening_build_queue__0 t0 USING (person_id) + LEFT JOIN + etl.flat_orders t2 USING (encounter_id) + WHERE + t1.encounter_type IN ', @encounter_types, ');'); + + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + INSERT INTO flat_breast_cancer_screening_0a + (SELECT + t1.person_id, + null, + t1.encounter_id, + t1.test_datetime, + t1.encounter_type, + null, -- t1.location_id, + t1.obs, + null, -- obs_datetimes + 0 as is_clinical_encounter, + 1 as encounter_type_sort_index, + null + FROM + etl.flat_lab_obs t1 + JOIN + flat_breast_cancer_screening_build_queue__0 t0 USING (person_id) + ); + + DROP TEMPORARY TABLE IF EXISTS flat_breast_cancer_screening_0; + CREATE TEMPORARY TABLE flat_breast_cancer_screening_0 (INDEX encounter_id (encounter_id), INDEX person_enc (person_id, encounter_datetime)) + (SELECT + * + FROM + flat_breast_cancer_screening_0a + ORDER BY + person_id, DATE(encounter_datetime) + ); - - - if(@query_type="build") then - select 'BUILDING..........................................'; - -#set @write_table = concat("flat_breast_cancer_screening_temp_",1); -#set @queue_table = concat("flat_breast_cancer_screening_build_queue_",1); - - set @write_table = concat("flat_breast_cancer_screening_temp_",queue_number); - set @queue_table = concat("flat_breast_cancer_screening_build_queue_",queue_number); - - -#drop table if exists flat_breast_cancer_screening_temp_1; - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_breast_cancer_screening_build_queue limit ', queue_size, ');'); -#SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_breast_cancer_screening_build_queue limit 500);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_breast_cancer_screening_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from flat_breast_cancer_screening_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_breast_cancer_screening"; - set @queue_table = "flat_breast_cancer_screening_sync_queue"; - create table if not exists flat_breast_cancer_screening_sync_queue (person_id int primary key); - - set @last_update = null; - - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - -#select max(date_created) into @last_update from etl.flat_log where table_name like "%breast_cancer_screening%"; - -#select @last_update; -select "Finding patients in amrs.encounters..."; - - replace into flat_breast_cancer_screening_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); + SET @encounter_purpose := null; + SET @other_encounter_purpose := null; + SET @menstruation_before_12 := null; + SET @menses_stopped_permanently := null; + SET @menses_stop_age := null; + SET @hrt_use := null; + SET @hrt_start_age := null; + SET @hrt_end_age := null; + SET @hrt_use_years := null; + SET @hrt_type_used =null; + SET @given_birth := null; + SET @age_first_birth := null; + SET @gravida := null; + SET @parity := null; + SET @cigarette_smoking := null; + SET @cigarette_smoked_day := null; + SET @tobacco_use := null; + SET @tobacco_use_duration_yrs := null; + SET @alcohol_drinking := null; + SET @alcohol_type := null; + SET @alcohol_use_period_yrs := null; + SET @breast_complaints_3months := null; + SET @breast_mass_location := null; + SET @nipple_discharge_location := null; + SET @nipple_retraction_location := null; + SET @breast_erythrema_location := null; + SET @breast_rash_location := null; + SET @breast_pain_location := null; + SET @other_changes_location := null; + SET @history_of_mammogram := null; + SET @mammogram_results := null; + SET @breast_ultrasound_history := null; + SET @breast_ultrasound_result := null; + SET @history_of_breast_biopsy := null; + SET @breast_biopsy_results := null; + SET @number_of_biopsies := null; + SET @biopsy_type := null; + SET @prev_exam_results := null; + SET @fam_brca_history_bf50 := null; + SET @fam_brca_history_aft50 := null; + SET @fam_male_brca_history := null; + SET @fam_ovarianca_history := null; + SET @fam_relatedca_history := null; + SET @fam_otherca_specify := null; + SET @cur_physical_findings := null; + SET @lymph_nodes_findings := null; + SET @cur_screening_findings := null; + -- SET @cur_screening_findings_date := null; + SET @patient_education := null; + SET @patient_education_other := null; + SET @referrals_ordered := null; + SET @procedure_done := null; + SET @referred_date := null; + SET @next_app_date := null; + SET @cbe_imaging_concordance := null; + SET @mammogram_workup_date := null; + SET @date_patient_notified_of_mammogram_results := null; + SET @ultrasound_results := null; + SET @ultrasound_workup_date := null; + SET @date_patient_notified_of_ultrasound_results := null; + SET @fna_results := null; + SET @fna_tumor_size := null; + SET @fna_degree_of_malignancy := null; + SET @fna_workup_date := null; + SET @date_patient_notified_of_fna_results := null; + SET @biopsy_tumor_size := null; + SET @biopsy_degree_of_malignancy := null; + SET @biopsy_workup_date := null; + SET @date_patient_notified_of_biopsy_results := null; + SET @biopsy_description := null; + SET @diagnosis_date := null; + SET @date_patient_informed_and_referred_for_management := null; + SET @screening_mode := null; + SET @diagnosis := null; + SET @cancer_staging := null; + SET @hiv_status := null; + + DROP TEMPORARY TABLE IF EXISTS flat_breast_cancer_screening_1; + + CREATE TEMPORARY TABLE flat_breast_cancer_screening_1 + (SELECT + obs, + encounter_type_sort_index, + @prev_id := @cur_id as prev_id, + @cur_id := t1.person_id as cur_id, + t1.person_id, + t1.encounter_id, + t1.encounter_type, + t1.encounter_datetime, + t1.visit_id, + -- t4.name as location_name, + t1.location_id, + t1.is_clinical_encounter, + p.gender, + p.death_date, + case + when timestampdiff(year,birthdate,curdate()) > 0 then round(timestampdiff(year,birthdate,curdate()),0) + else round(timestampdiff(month,birthdate,curdate())/12,2) + end as age, + case + when obs regexp "!!1834=9651!!" then @encounter_purpose := 1 + when obs regexp "!!1834=1154!!" then @encounter_purpose := 2 + when obs regexp "!!1834=1246!!" then @encounter_purpose := 3 + when obs regexp "!!1834=5622!!" then @encounter_purpose := 4 + else @encounter_purpose := null + end as encounter_purpose, + case + when obs regexp "!!1915=" then @other_encounter_purpose := GetValues(obs,1915) + else @other_encounter_purpose := null + end as other_encounter_purpose, + case + when obs regexp "!!9560=1065!!" then @menstruation_before_12 := 1 + when obs regexp "!!9560=1066!!" then @menstruation_before_12 := 0 + else @menstruation_before_12 := null + end as menstruation_before_12, + case + when obs regexp "!!9561=1065!!" then @menses_stopped_permanently := 1 + when obs regexp "!!9561=1066!!" then @menses_stopped_permanently := 0 + when obs regexp "!!9561=9568!!" then @menses_stopped_permanently := 2 + else @menses_stopped_permanently := null + end as menses_stopped_permanently, + case + when obs regexp "!!9562=[0-9]" and t1.encounter_type = @lab_encounter_type then @menses_stop_age:=cast(GetValues(obs,9562) as unsigned) + when @prev_id = @cur_id then @menses_stop_age + else @menses_stop_age := null + end as menses_stop_age, + case + when obs regexp "!!9626=1065!!" then @hrt_use := 1 + when obs regexp "!!9626=1066!!" then @hrt_use := 0 + when obs regexp "!!9626=9568!!" then @hrt_use := 2 + else @hrt_use := null + end as hrt_use, + case + when obs regexp "!!9627=[0-9]" then @hrt_start_age := cast(GetValues(obs,9627) as unsigned) + else @hrt_start_age := null + end as hrt_start_age, + case + when obs regexp "!!9723=[0-9]" then @hrt_end_age := cast(GetValues(obs,9723) as unsigned) + else @hrt_end_age := null + end as hrt_end_age, + case + when obs regexp "!!97629=[0-9]" then @hrt_use_years := cast(GetValues(obs,9629) as unsigned) + else @hrt_use_years := null + end as hrt_use_years, + case + when obs regexp "!!9630=9573!!" then @hrt_type_used := 1 + when obs regexp "!!9630=6217!!" then @hrt_type_used := 2 + when obs regexp "!!9630=6218!!" then @hrt_type_used := 3 + else @hrt_type_used := null + end as hrt_type_used, + case + when obs regexp "!!9563=1065!!" then @given_birth := 1 + when obs regexp "!!9563=1066!!" then @given_birth := 0 + else @given_birth := null + end as given_birth, + case + when obs regexp "!!5574=[0-9]" then @age_first_birth := cast(GetValues(obs,5574) as unsigned) + else @age_first_birth := null + end as age_first_birth, + case + when obs regexp "!!5624=[0-9]" then @gravida := cast(GetValues(obs,5624) as unsigned) + else @gravida := null + end as gravida, + case + when obs regexp "!!1053=[0-9]" then @parity := cast(GetValues(obs,1053) as unsigned) + else @parity := null + end as parity, + case + when obs regexp "!!9333=" then @cigarette_smoking := GetValues(obs,9333) + else @cigarette_smoking := null + end as cigarette_smoking, + case + when obs regexp "!!2069=[0-9]" then @cigarette_smoked_day := cast(GetValues(obs,2069) as unsigned) + else @cigarette_smoked_day := null + end as cigarette_smoked_day, + case + when obs regexp "!!7973=1065!!" then @tobacco_use := 1 + when obs regexp "!!7973=1066!!" then @tobacco_use := 0 + when obs regexp "!!7973=1679!!" then @tobacco_use := 2 + else @tobacco_use := null + end as tobacco_use, + case + when obs regexp "!!8144=[0-9]" then @tobacco_use_duration_yrs := cast(GetValues(obs, 8144) as unsigned) + else @tobacco_use_duration_yrs := null + end as tobacco_use_duration_yrs, + case + when obs regexp "!!1684=1065!!" then @alcohol_drinking := 1 + when obs regexp "!!1684=1066!!" then @alcohol_drinking := 0 + when obs regexp "!!1684=1679!!" then @alcohol_drinking := 2 + else @alcohol_drinking := null + end as alcohol_drinking, + case + when obs regexp "!!1685=1682!!" then @alcohol_type := 1 + when obs regexp "!!1685=1681!!" then @alcohol_type := 2 + when obs regexp "!!1685=1680!!" then @alcohol_type := 3 + when obs regexp "!!1685=1683!!" then @alcohol_type := 4 + when obs regexp "!!1685=2059!!" then @alcohol_type := 5 + when obs regexp "!!1685=5622!!" then @alcohol_type := 6 + else @alcohol_type := null + end as alcohol_type, + case + when obs regexp "!!8170=[0-9]" then @alcohol_use_period_yrs := cast(GetValues(obs,8170) as unsigned) + else @alcohol_use_period_yrs := null + end as alcohol_use_period_yrs, + case + when obs regexp "!!9553=5006!!" then @breast_complaints_3months := 1 + when obs regexp "!!9553=1068!!" then @breast_complaints_3months := 2 + else @breast_complaints_3months := null + end as breast_complaints_3months, + case + when obs regexp "!!6697=2399!!" then @breast_mass_location := 1 + when obs regexp "!!6697=5139!!" then @breast_mass_location := 2 + when obs regexp "!!6697=5141!!" then @breast_mass_location := 3 + when obs regexp "!!6697=6589!!" then @breast_mass_location := 4 + when obs regexp "!!6697=6590!!" then @breast_mass_location := 5 + when obs regexp "!!6697=6591!!" then @breast_mass_location := 6 + when obs regexp "!!6697=9625!!" then @breast_mass_location := 7 + else @breast_mass_location := null + end as breast_mass_location, + case + when obs regexp "!!9557=5139!!" then @nipple_discharge_location := 1 + when obs regexp "!!9557=5141!!" then @nipple_discharge_location := 2 + when obs regexp "!!9557=9625!!" then @nipple_discharge_location := 3 + else @nipple_discharge_location := null + end as nipple_discharge_location, + case + when obs regexp "!!9558=5139!!" then @nipple_retraction_location := 1 + when obs regexp "!!9558=5141!!" then @nipple_retraction_location := 2 + when obs regexp "!!9558=9625!!" then @nipple_retraction_location := 3 + else @nipple_retraction_location := null + end as nipple_retraction_location, + case + when obs regexp "!!9574=5139!!" then @breast_erythrema_location := 1 + when obs regexp "!!9574=5141!!" then @breast_erythrema_location := 2 + when obs regexp "!!9574=9625!!" then @breast_erythrema_location := 3 + else @breast_erythrema_location := null + end as breast_erythrema_location, + case + when obs regexp "!!9577=5139!!" then @breast_rash_location := 1 + when obs regexp "!!9577=5141!!" then @breast_rash_location := 2 + when obs regexp "!!9577=9625!!" then @breast_rash_location := 3 + else @breast_rash_location := null + end as breast_rash_location, + case + when obs regexp "!!9577=5139!!" then @breast_pain_location := 1 + when obs regexp "!!9577=5141!!" then @breast_pain_location := 2 + when obs regexp "!!9577=9625!!" then @breast_pain_location := 3 + else @breast_pain_location := null + end as breast_pain_location, + case + when obs regexp "!!9576=5139!!" then @other_changes_location := 1 + when obs regexp "!!9576=5141!!" then @other_changes_location := 2 + when obs regexp "!!9576=9625!!" then @other_changes_location := 3 + else @other_changes_location := null + end as other_changes_location, + case + when obs regexp "!!9594=1066!!" then @history_of_mammogram := 0 + when obs regexp "!!9594=1065!!" then @history_of_mammogram := 1 + else @history_of_mammogram := null + end as history_of_mammogram, + case + when obs regexp "!!9595=1115!!" then @mammogram_results := 1 + when obs regexp "!!9595=1116!!" then @mammogram_results := 2 + when obs regexp "!!9595=1118!!" then @mammogram_results := 3 + when obs regexp "!!9595=1138!!" then @mammogram_results := 4 + when obs regexp "!!9595=1267!!" then @mammogram_results := 5 + when obs regexp "!!9595=1067!!" then @mammogram_results := 6 + else @mammogram_results := null + end as mammogram_results, + case + when obs regexp "!!9597=1066!!" then @breast_ultrasound_history := 0 + when obs regexp "!!9597=1065!!" then @breast_ultrasound_history := 1 + else @breast_ultrasound_history := null + end as breast_ultrasound_history, + case + when obs regexp "!!9596=1115!!" then @breast_ultrasound_result := 1 + when obs regexp "!!9596=1116!!" then @breast_ultrasound_result := 2 + when obs regexp "!!9596=1067!!" then @breast_ultrasound_result := 3 + when obs regexp "!!9596=1118!!" then @breast_ultrasound_result := 4 + else @breast_ultrasound_result := null + end as breast_ultrasound_result, + case + when obs regexp "!!9598=1066!!" then @history_of_breast_biopsy := 0 + when obs regexp "!!9598=1065!!" then @history_of_breast_biopsy := 1 + else @history_of_breast_biopsy := null + end as history_of_breast_biopsy, + case + when obs regexp "!!8184=1115!!" then @breast_biopsy_results := 1 + when obs regexp "!!8184=1116!!" then @breast_biopsy_results := 2 + when obs regexp "!!8184=1118!!" then @breast_biopsy_results := 3 + when obs regexp "!!8184=1067!!" then @breast_biopsy_results := 4 + when obs regexp "!!8184=9691!!" then @breast_biopsy_results := 5 + when obs regexp "!!8184=10052!!" then @breast_biopsy_results := 6 + else @breast_biopsy_results := null + end as breast_biopsy_results, + case + when obs regexp "!!9599=" then @number_of_biopsies := GetValues(obs, 9599) + else @number_of_biopsies := null + end as number_of_biopsies, + case + when obs regexp "!!6509=6510!!" then @biopsy_type := 1 + when obs regexp "!!6509=6511!!" then @biopsy_type := 2 + when obs regexp "!!6509=6512!!" then @biopsy_type := 3 + when obs regexp "!!6509=6513!!" then @biopsy_type := 4 + when obs regexp "!!6509=7190!!" then @biopsy_type := 5 + when obs regexp "!!6509=8184!!" then @biopsy_type := 6 + when obs regexp "!!6509=10075!!" then @biopsy_type := 7 + when obs regexp "!!6509=10076!!" then @biopsy_type := 8 + else @biopsy_type := null + end as biopsy_type, + case + when obs regexp "!!9694=1115!!" then @prev_exam_results := 1 + when obs regexp "!!9694=1116!!" then @prev_exam_results := 0 + when obs regexp "!!9694=1067!!" then @prev_exam_results := 2 + else @prev_exam_results := null + end as prev_exam_results, + case + when obs regexp "!!9631=1672!!" then @fam_brca_history_bf50 := 1 + when obs regexp "!!9631=1107!!" then @fam_brca_history_bf50 := 0 + when obs regexp "!!9631=978!!" then @fam_brca_history_bf50 := 2 + when obs regexp "!!9631=972!!" then @fam_brca_history_bf50 := 3 + when obs regexp "!!9631=1671!!" then @fam_brca_history_bf50 := 4 + when obs regexp "!!9631=1393!!" then @fam_brca_history_bf50 := 5 + when obs regexp "!!9631=1392!!" then @fam_brca_history_bf50 := 6 + when obs regexp "!!9631=1395!!" then @fam_brca_history_bf50 := 7 + when obs regexp "!!9631=1394!!" then @fam_brca_history_bf50 := 8 + else @fam_brca_history_bf50 := null + end as fam_brca_history_bf50, + case + when obs regexp "!!9632=978!!" then @fam_brca_history_aft50 := 1 + when obs regexp "!!9632=1672!!" then @fam_brca_history_aft50 := 2 + when obs regexp "!!9632=972!!" then @fam_brca_history_aft50 := 3 + when obs regexp "!!9632=1671!!" then @fam_brca_history_aft50 := 4 + when obs regexp "!!9632=1393!!" then @fam_brca_history_aft50 := 5 + when obs regexp "!!9632=1392!!" then @fam_brca_history_aft50 := 6 + when obs regexp "!!9632=1395!!" then @fam_brca_history_aft50 := 7 + when obs regexp "!!9632=1394!!" then @fam_brca_history_aft50 := 8 + else @fam_brca_history_aft50 := null + end as fam_brca_history_aft50, + case + when obs regexp "!!9633=978!!" then @fam_male_brca_history := 1 + when obs regexp "!!9633=1672!!" then @fam_male_brca_history := 2 + when obs regexp "!!9633=972!!" then @fam_male_brca_history := 3 + when obs regexp "!!9633=1671!!" then @fam_male_brca_history := 4 + when obs regexp "!!9633=1393!!" then @fam_male_brca_history := 5 + when obs regexp "!!9633=1392!!" then @fam_male_brca_history := 6 + when obs regexp "!!9633=1395!!" then @fam_male_brca_history := 7 + when obs regexp "!!9633=1394!!" then @fam_male_brca_history := 8 + else @fam_male_brca_history := null + end as fam_male_brca_history, + case + when obs regexp "!!9634=978!!" then @fam_ovarianca_history := 1 + when obs regexp "!!9634=1672!!" then @fam_ovarianca_history := 2 + when obs regexp "!!9634=972!!" then @fam_ovarianca_history := 3 + when obs regexp "!!9634=1671!!" then @fam_ovarianca_history := 4 + when obs regexp "!!9634=1393!!" then @fam_ovarianca_history := 5 + when obs regexp "!!9634=1392!!" then @fam_ovarianca_history := 6 + when obs regexp "!!9634=1395!!" then @fam_ovarianca_history := 7 + when obs regexp "!!9634=1394!!" then @fam_ovarianca_history := 8 + else @fam_ovarianca_history := null + end as fam_ovarianca_history, + case + when obs regexp "!!9635=978!!" then @fam_relatedca_history:= 1 + when obs regexp "!!9635=1672!!" then @fam_relatedca_history := 2 + when obs regexp "!!9635=972!!" then @fam_relatedca_history := 3 + when obs regexp "!!9635=1671!!" then @fam_relatedca_history := 4 + when obs regexp "!!9635=1393!!" then @fam_relatedca_history := 5 + when obs regexp "!!9635=1392!!" then @fam_relatedca_history := 6 + when obs regexp "!!9635=1395!!" then @fam_relatedca_history := 7 + when obs regexp "!!9635=1394!!" then @fam_relatedca_history := 8 + else @fam_relatedca_history := null + end as fam_relatedca_history, + case + when obs regexp "!!7176=6529!!" then @fam_otherca_specify := 1 + when obs regexp "!!7176=9636!!" then @fam_otherca_specify := 2 + when obs regexp "!!7176=9637!!" then @fam_otherca_specify := 3 + when obs regexp "!!7176=6485!!" then @fam_otherca_specify := 4 + when obs regexp "!!7176=9638!!" then @fam_otherca_specify := 5 + when obs regexp "!!7176=9639!!" then @fam_otherca_specify := 6 + when obs regexp "!!7176=6522!!" then @fam_otherca_specify := 5 + when obs regexp "!!7176=216!!" then @fam_otherca_specify := 6 + else @fam_otherca_specify := null + end as fam_otherca_specify, + case + when obs regexp "!!6251=1115!!" then @cur_physical_findings:= 0 + when obs regexp "!!6251=115!!" then @cur_physical_findings := 1 + when obs regexp "!!6251=6250!!" then @cur_physical_findings := 2 + when obs regexp "!!6251=6249!!" then @cur_physical_findings := 3 + when obs regexp "!!6251=5622!!" then @cur_physical_findings := 4 + when obs regexp "!!6251=582!!" then @cur_physical_findings := 5 + when obs regexp "!!6251=6493!!" then @cur_physical_findings := 6 + when obs regexp "!!6251=6499!!" then @cur_physical_findings := 7 + when obs regexp "!!6251=1118!!" then @cur_physical_findings := 8 + when obs regexp "!!6251=1481!!" then @cur_physical_findings := 9 + when obs regexp "!!6251=6729!!" then @cur_physical_findings := 10 + when obs regexp "!!6251=1116!!" then @cur_physical_findings := 11 + when obs regexp "!!6251=8188!!" then @cur_physical_findings := 12 + when obs regexp "!!6251=8189!!" then @cur_physical_findings := 13 + when obs regexp "!!6251=1067!!" then @cur_physical_findings := 14 + when obs regexp "!!6251=9689!!" then @cur_physical_findings := 15 + when obs regexp "!!6251=9690!!" then @cur_physical_findings := 16 + when obs regexp "!!6251=9687!!" then @cur_physical_findings := 17 + when obs regexp "!!6251=9688!!" then @cur_physical_findings := 18 + when obs regexp "!!6251=5313!!" then @cur_physical_findings := 19 + when obs regexp "!!6251=9691!!" then @cur_physical_findings := 20 + else @cur_physical_findings := null + end as cur_physical_findings, + case + when obs regexp "!!1121=1115!!" then @lymph_nodes_findings := 1 + when obs regexp "!!1121=161!!" then @lymph_nodes_findings := 2 + when obs regexp "!!1121=9675!!" then @lymph_nodes_findings:= 3 + when obs regexp "!!1121=9676!!" then @lymph_nodes_findings:= 4 + else @lymph_nodes_findings := null + end as lymph_nodes_findings, + case + when obs regexp "!!9748=1115!!" then @cur_screening_findings := 1 + when obs regexp "!!9748=9691!!" then @cur_screening_findings := 3 + when obs regexp "!!9748=1116!!" then @cur_screening_findings := 2 + else @cur_screening_findings := null + end as cur_screening_findings, + case + when obs regexp "!!6327=9651!!" then @patient_education := 1 + when obs regexp "!!6327=2345!!" then @patient_education := 2 + when obs regexp "!!6327=9692!!" then @patient_education:= 3 + when obs regexp "!!6327=5622!!" then @patient_education:= 4 + else @patient_education := null + end as patient_education, + case + when obs regexp "!!1915=" then @patient_education_other := GetValues(obs,1915) + else @patient_education_other := null + end as patient_education_other, + case + when obs regexp "!!1272=1107!!" then @referrals_ordered := 0 + when obs regexp "!!1272=1496!!" then @referrals_ordered:= 1 + else @referrals_ordered := null + end as referrals_ordered, + case + when obs regexp "!!1272=9596!!" then @procedure_done := 1 + when obs regexp "!!1272=9595!!" then @procedure_done:= 2 + when obs regexp "!!1272=6510!!" then @procedure_done := 3 + when obs regexp "!!1272=7190!!" then @procedure_done:= 4 + when obs regexp "!!1272=6511!!" then @procedure_done := 5 + when obs regexp "!!1272=9997!!" then @procedure_done:= 6 + else @procedure_done := null + end as procedure_done, + case + when obs regexp "!!9158=" then @referred_date := GetValues(obs,9158) + else @referred_date := null + end as referred_date, + case + when obs regexp "!!5096=" then @next_app_date := GetValues(obs,5096) + else @next_app_date := null + end as next_app_date, + case + when obs regexp "!!9702=9703!!" then @cbe_imaging_concordance := 1 + when obs regexp "!!9702=9704!!" then @cbe_imaging_concordance := 2 + when obs regexp "!!9702=1175!!" then @cbe_imaging_concordance := 3 + else @cbe_imaging_concordance := null + end as cbe_imaging_concordance, + case + when obs regexp "!!9708=" then @mammogram_workup_date := GetValues(obs,9708) + else @mammogram_workup_date := null + end as mammogram_workup_date, + case + when obs regexp "!!9705=" then @date_patient_notified_of_mammogram_results := GetValues(obs,9705) + else @date_patient_notified_of_mammogram_results := null + end as date_patient_notified_of_mammogram_results, + case + when obs regexp "!!9596=1115!!" then @ultrasound_results := 1 + when obs regexp "!!9596=1116!!" then @ultrasound_results := 2 + when obs regexp "!!9596=1067!!" then @ultrasound_results := 3 + when obs regexp "!!9596=1118!!" then @ultrasound_results := 4 + else @ultrasound_results := null + end as ultrasound_results, + case + when obs regexp "!!9708=" then @ultrasound_workup_date := GetValues(obs,9708) + else @ultrasound_workup_date := null + end as ultrasound_workup_date, + case + when obs regexp "!!10047=" then @date_patient_notified_of_ultrasound_results := GetValues(obs,10047) + else @date_patient_notified_of_ultrasound_results := null + end as date_patient_notified_of_ultrasound_results, + case + when obs regexp "!!10051=9691!!" then @fna_results := 1 + when obs regexp "!!10051=10052!!" then @fna_results := 2 + when obs regexp "!!9596=1118!!" then @fna_results := 3 + else @fna_results := null + end as fna_results, + case + when obs regexp "!!10053=[0-9]" then @fna_tumor_size:= cast(GetValues(obs,10053) as unsigned) + else @fna_tumor_size := null + end as fna_tumor_size, + case + when obs regexp "!!10054=10055!!" then @fna_degree_of_malignancy := 1 + when obs regexp "!!10054=10056!!" then @fna_degree_of_malignancy := 2 + else @fna_degree_of_malignancy := null + end as fna_degree_of_malignancy, + case + when obs regexp "!!10057=" then @fna_workup_date := GetValues(obs,10057) + else @fna_workup_date := null + end as fna_workup_date, + case + when obs regexp "!!10059=" then @date_patient_notified_of_fna_results := GetValues(obs,10059) + else @date_patient_notified_of_fna_results := null + end as date_patient_notified_of_fna_results, + case + when obs regexp "!!10053=[0-9]" then @biopsy_tumor_size:= cast(GetValues(obs,10053) as unsigned) + else @biopsy_tumor_size := null + end as biopsy_tumor_size, + case + when obs regexp "!!10054=10055!!" then @biopsy_degree_of_malignancy := 1 + when obs regexp "!!10054=10056!!" then @biopsy_degree_of_malignancy := 2 + else @biopsy_degree_of_malignancy := null + end as biopsy_degree_of_malignancy, + case + when obs regexp "!!10060=" then @biopsy_workup_date := GetValues(obs,10060) + else @biopsy_workup_date := null + end as biopsy_workup_date, + case + when obs regexp "!!10061=" then @date_patient_notified_of_biopsy_resultse := GetValues(obs,10061) + else @date_patient_notified_of_biopsy_results := null + end as date_patient_notified_of_biopsy_results, + case + when obs regexp "!!7400=" then @biopsy_description := GetValues(obs,7400) + else @biopsy_description := null + end as biopsy_description, + case + when obs regexp "!!9728=" then @diagnosis_date := GetValues(obs, 9728) + else @diagnosis_date := null + end as diagnosis_date, + case + when obs regexp "!!9706=" then @date_patient_informed_and_referred_for_management := GetValues(obs, 9706) + else @date_patient_informed_and_referred_for_management := null + end as date_patient_informed_and_referred_for_management, + case + when obs regexp "!!10068=9595!!" then @screening_mode := 1 + when obs regexp "!!10068=10067!!" then @screening_mode := 2 + when obs regexp "!!10068=9596!!" then @screening_mode := 3 + else @screening_mode := null + end as screening_mode, + case + when obs regexp "!!6042=" then @diagnosis := GetValues(obs,6042) + else @diagnosis := null + end as diagnosis, + case + when obs regexp "!!9868=9852!!" then @cancer_staging := 1 + when obs regexp "!!9868=9856!!" then @cancer_staging := 2 + when obs regexp "!!9868=9860!!" then @cancer_staging := 3 + when obs regexp "!!9868=9864!!" then @cancer_staging := 4 + else @cancer_staging := null + end as cancer_staging, + case + when obs regexp "!!6709=664!!" then @hiv_status := 1 + when obs regexp "!!6709=703!!" then @hiv_status := 2 + when obs regexp "!!6709=1067!!" then @hiv_status := 3 + else @hiv_status := null + end as hiv_status + FROM + flat_breast_cancer_screening_0 t1 + JOIN amrs.person p using (person_id) + ORDER BY person_id, date(encounter_datetime) DESC, encounter_type_sort_index DESC + ); + + SET @prev_id = null; + SET @cur_id = null; + set @prev_encounter_datetime = null; + set @cur_encounter_datetime = null; + set @prev_clinical_datetime = null; + set @cur_clinical_datetime = null; + + set @next_encounter_type = null; + set @cur_encounter_type = null; + + set @prev_clinical_location_id = null; + set @cur_clinical_location_id = null; - -select "Finding patients in flat_obs..."; - - replace into flat_breast_cancer_screening_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - -select "Finding patients in flat_lab_obs..."; - replace into flat_breast_cancer_screening_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); + ALTER TABLE flat_breast_cancer_screening_1 DROP prev_id, DROP cur_id; -select "Finding patients in flat_orders..."; + drop table if exists flat_breast_cancer_screening_2; + create temporary table flat_breast_cancer_screening_2 + (select *, + @prev_id := @cur_id as prev_id, + @cur_id := person_id as cur_id, - replace into flat_breast_cancer_screening_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_breast_cancer_screening_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_breast_cancer_screening_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - + case + when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime + else @prev_encounter_datetime := null + end as next_encounter_datetime_breast_cancer_screening, - # Remove test patients - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; + @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; + case + when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type + else @next_encounter_type := null + end as next_encounter_type_breast_cancer_screening, - select @person_ids_count as 'num patients to update'; + @cur_encounter_type := encounter_type as cur_encounter_type, + case + when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime + else @prev_clinical_datetime := null + end as next_clinical_datetime_breast_cancer_screening, - #delete t1 from flat_breast_cancer_screening t1 join @queue_table t2 using (person_id); - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists flat_breast_cancer_screening_build_queue__0; - - #create temporary table flat_breast_cancer_screening_build_queue__0 (select * from flat_breast_cancer_screening_build_queue_2 limit 5000); #TODO - change this when data_fetch_size changes + case + when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id + else @prev_clinical_location_id := null + end as next_clinical_location_id_breast_cancer_screening, -#SET @dyn_sql=CONCAT('create temporary table flat_breast_cancer_screening_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit 100);'); - SET @dyn_sql=CONCAT('create temporary table flat_breast_cancer_screening_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - drop temporary table if exists flat_breast_cancer_screening_0a; - SET @dyn_sql = CONCAT( - 'create temporary table flat_breast_cancer_screening_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - case - when t1.encounter_type in ',@clinical_encounter_types,' then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in ',@non_clinical_encounter_types,' then 20 - when t1.encounter_type in ',@clinical_encounter_types,' then 10 - when t1.encounter_type in', @other_encounter_types, ' then 5 - else 1 - end as encounter_type_sort_index, - t2.orders - from etl.flat_obs t1 - join flat_breast_cancer_screening_build_queue__0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_id) - where t1.encounter_type in ',@encounter_types,');'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - + case + when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime + when @prev_id = @cur_id then @cur_clinical_datetime + else @cur_clinical_datetime := null + end as cur_clinic_datetime, - - - insert into flat_breast_cancer_screening_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, #t1.location_id, - t1.obs, - null, #obs_datetimes - # in any visit, there many be multiple encounters. for this dataset, we want to include only clinical encounters (e.g. not lab or triage visit) - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_breast_cancer_screening_build_queue__0 t0 using (person_id) - ); - - - drop temporary table if exists flat_breast_cancer_screening_0; - create temporary table flat_breast_cancer_screening_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_breast_cancer_screening_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @encounter_purpose = null; - set @other_encounter_purpose = null; - set @menstruation_before_12 = null; - set @menses_stopped_permanently = null; - set @menses_stop_age = null; - set @hrt_use = null; - set @hrt_start_age = null; - set @hrt_end_age = null; - set @hrt_use_years = null; - set @hrt_type_used =null; - set @given_birth = null; - set @age_first_birth = null; - set @gravida = null; - set @parity = null; - set @cigarette_smoking = null; - set @cigarette_smoked_day =null; - set @tobacco_use =null; - set @tobacco_use_duration_yrs = null; - set @alcohol_drinking =null; - set @alcohol_type = null; - set @alcohol_use_period_yrs = null; - set @breast_complaints_3moths = null; - set @prev_exam_results = null; - set @fam_brca_history_bf50 = null; - set @fam_brca_history_aft50 = null; - set @fam_male_brca_history = null; - set @fam_ovarianca_history =null; - set @fam_relatedca_history =null; - set @fam_otherca_specify = null; - set @cur_physical_findings = null; - set @lymph_nodes_findings =null; - set @cur_screening_findings = null; - #set @cur_screening_findings_date = null; - set @patient_education = null; - set @patient_education_other = null; - set @referred_orderd = null; - set @procedure_done =null; - set @referred_date =null; - set @next_app_date = null; - - set @cbe_imaging_concordance =null; - set @mammogram_results =null; - set @mammogram_workup_date =null; - set @date_patient_notified_of_mammogram_results =null; - set @Ultrasound_results =null; - set @ultrasound_workup_date =null; - set @date_patient_notified_of_ultrasound_results =null; - set @fna_results =null; - set @fna_tumor_size =null; - set @fna_degree_of_malignancy =null; - set @fna_workup_date =null; - set @date_patient_notified_of_fna_results =null; - set @breast_biopsy_results =null; - set @biopsy_tumor_size =null; - set @biopsy_degree_of_malignancy =null; - set @biopsy_workup_date =null; - set @date_patient_notified_of_biopsy_results =null; - set @date_patient_informed_and_referred_for_management =null; - set @screening_mode =null; - set @diagnosis =null; - set @diagnosis_date =null; - set @cancer_staging =null; - set @hiv_status = null; - - drop temporary table if exists flat_breast_cancer_screening_1; - create temporary table flat_breast_cancer_screening_1 #(index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - t1.encounter_id, - t1.encounter_type, - t1.encounter_datetime, - t1.visit_id, - #t4.name as location_name, - t1.location_id, - t1.is_clinical_encounter, - p.gender, - p.death_date, - - - case - when timestampdiff(year,birthdate,curdate()) > 0 then round(timestampdiff(year,birthdate,curdate()),0) - else round(timestampdiff(month,birthdate,curdate())/12,2) - end as age, - - case - when obs regexp "!!1834=9651!!" then @encounter_purpose := 1 - when obs regexp "!!1834=1154!!" then @encounter_purpose := 2 - when obs regexp "!!1834=1246!!" then @encounter_purpose := 3 - when obs regexp "!!1834=5622!!" then @encounter_purpose := 4 - else @encounter_purpose := null - end as encounter_purpose, - - case - when obs regexp "!!1915=" then @other_encounter_purpose := GetValues(obs,1915) - else @other_encounter_purpose := null - end as other_encounter_purpose, - - case - when obs regexp "!!9560=1065!!" then @menstruation_before_12 := 1 - when obs regexp "!!9560=1066!!" then @menstruation_before_12 := 0 - else @menstruation_before_12 := null - end as menstruation_before_12, - - case - when obs regexp "!!9561=1065!!" then @menstruation_before_12 := 1 - when obs regexp "!!9561=1066!!" then @menstruation_before_12 := 0 - when obs regexp "!!9561=9568!!" then @menstruation_before_12 := 2 - else @menses_stopped_permanently := null - end as menses_stopped_permanently, - - - case - when obs regexp "!!9562=[0-9]" and t1.encounter_type = @lab_encounter_type then @menses_stop_age:=cast(GetValues(obs,9562) as unsigned) - when @prev_id=@cur_id then@menses_stop_age - else @menses_stop_age:=null - end as menses_stop_age, - - case - when obs regexp "!!9626=1065!!" then @hrt_use := 1 - when obs regexp "!!9626=1066!!" then @hrt_use := 0 - when obs regexp "!!9626=9568!!" then @hrt_use := 2 - else @hrt_use := null - end as hrt_use, - - - case - when obs regexp "!!9627=[0-9]" then @hrt_start_age:=cast(GetValues(obs,9627) as unsigned) - else @hrt_start_age := null - end as hrt_start_age, - - - case - when obs regexp "!!9723=[0-9]" then @hrt_end_age:=cast(GetValues(obs,9723) as unsigned) - else @hrt_end_age := null - end as hrt_end_age, - - - case - when obs regexp "!!97629=[0-9]" then @hrt_use_years:=cast(GetValues(obs,9629) as unsigned) - else @hrt_use_years := null - end as hrt_use_years, - - - case - when obs regexp "!!9630=9573!!" then @hrt_type_used := 1 - when obs regexp "!!9630=6217!!" then @hrt_type_used := 2 - when obs regexp "!!9630=6218!!" then @hrt_type_used := 3 - else @hrt_type_used := null - end as hrt_type_used, - - case - when obs regexp "!!9563=1065!!" then @given_birth := 1 - when obs regexp "!!9563=1066!!" then @given_birth := 0 - else @given_birth := null - end as given_birth, - - - case - when obs regexp "!!5574=[0-9]" then @age_first_birth:=cast(GetValues(obs,5574) as unsigned) - else @age_first_birth := null - end as age_first_birth, - - - case - when obs regexp "!!5624=[0-9]" then @gravida:= cast(GetValues(obs,5624) as unsigned) - else @gravida := null - end as gravida, - - - case - when obs regexp "!!1053=[0-9]" then @parity:=cast(GetValues(obs,1053) as unsigned) - else @parity := null - end as parity, - - - case - when obs regexp "!!9333=" then @cigarette_smoking := GetValues(obs,9333) - else @cigarette_smoking := null - end as cigarette_smoking, - - - case - when obs regexp "!!2069=[0-9]" then @cigarette_smoked_day:=cast(GetValues(obs,2069) as unsigned) - else @cigarette_smoked_day := null - end as cigarette_smoked_day, - - - case - when obs regexp "!!7973=1065!!" then @tobacco_use := 1 - when obs regexp "!!7973=1066!!" then @tobacco_use := 0 - when obs regexp "!!7973=1679!!" then @tobacco_use := 2 - else @hrt_use := null - end as tobacco_use, - - - case - when obs regexp "!!8144=[0-9]" then @tobacco_use_duration_yrs:= cast(GetValues(obs,8144) as unsigned) - else @tobacco_use_duration_yrs := null - end as tobacco_use_duration_yrs, - - - case - when obs regexp "!!1684=1065!!" then @alcohol_drinking := 1 - when obs regexp "!!1684=1066!!" then @alcohol_drinking := 0 - when obs regexp "!!1684=1679!!" then @alcohol_drinking := 2 - else @alcohol_drinking := null - end as alcohol_drinking, - - - case - when obs regexp "!!1685=1682!!" then @alcohol_type := 1 - when obs regexp "!!1685=1681!!" then @alcohol_type := 2 - when obs regexp "!!1685=1680!!" then @alcohol_type := 3 - when obs regexp "!!1685=1683!!" then @alcohol_type := 4 - when obs regexp "!!1685=2059!!" then @alcohol_type := 5 - when obs regexp "!!1685=5622!!" then @alcohol_type := 6 - else @hrt_type_used := null - end as alcohol_type, - - - case - when obs regexp "!!8170=[0-9]" then @alcohol_use_period_yrs:= cast(GetValues(obs,8170) as unsigned) - else @alcohol_use_period_yrs := null - end as alcohol_use_period_yrs, - - - case - when obs regexp "!!9553=5006!!" then @breast_complaints_3moths := 1 - when obs regexp "!!9553=1068!!" then @breast_complaints_3moths := 2 - else @breast_complaints_3moths := null - end as breast_complaints_3moths, - - - case - when obs regexp "!!9694=1115!!" then @prev_exam_results := 1 - when obs regexp "!!9694=1116!!" then @prev_exam_results := 0 - when obs regexp "!!9694=1067!!" then @prev_exam_results := 2 - else @prev_exam_results := null - end as prev_exam_results, - - - case - when obs regexp "!!9631=1672!!" then @fam_brca_history_bf50 := 1 - when obs regexp "!!9631=1107!!" then @fam_brca_history_bf50 := 0 - when obs regexp "!!9631=978!!" then @fam_brca_history_bf50 := 2 - when obs regexp "!!9631=972!!" then @fam_brca_history_bf50 := 3 - when obs regexp "!!9631=1671!!" then @fam_brca_history_bf50 := 4 - when obs regexp "!!9631=1393!!" then @fam_brca_history_bf50 := 5 - when obs regexp "!!9631=1392!!" then @fam_brca_history_bf50 := 6 - when obs regexp "!!9631=1395!!" then @fam_brca_history_bf50 := 7 - when obs regexp "!!9631=1394!!" then @fam_brca_history_bf50 := 8 - else @fam_brca_history_bf50 := null - end as fam_brca_history_bf50, - - - case - when obs regexp "!!9632=978!!" then @fam_brca_history_aft50 := 1 - when obs regexp "!!9632=1672!!" then @fam_brca_history_aft50 := 2 - when obs regexp "!!9632=972!!" then @fam_brca_history_aft50 := 3 - when obs regexp "!!9632=1671!!" then @fam_brca_history_aft50 := 4 - when obs regexp "!!9632=1393!!" then @fam_brca_history_aft50 := 5 - when obs regexp "!!9632=1392!!" then @fam_brca_history_aft50 := 6 - when obs regexp "!!9632=1395!!" then @fam_brca_history_aft50 := 7 - when obs regexp "!!9632=1394!!" then @fam_brca_history_aft50 := 8 - else @fam_brca_history_aft50 := null - end as fam_brca_history_aft50, - - - case - when obs regexp "!!9633=978!!" then @fam_male_brca_history := 1 - when obs regexp "!!9633=1672!!" then @fam_male_brca_history := 2 - when obs regexp "!!9633=972!!" then @fam_male_brca_history := 3 - when obs regexp "!!9633=1671!!" then @fam_male_brca_history := 4 - when obs regexp "!!9633=1393!!" then @fam_male_brca_history := 5 - when obs regexp "!!9633=1392!!" then @fam_male_brca_history := 6 - when obs regexp "!!9633=1395!!" then @fam_male_brca_history := 7 - when obs regexp "!!9633=1394!!" then @fam_male_brca_history := 8 - else @fam_male_brca_history := null - end as fam_male_brca_history, - - - case - when obs regexp "!!9634=978!!" then @fam_ovarianca_history := 1 - when obs regexp "!!9634=1672!!" then @fam_ovarianca_history := 2 - when obs regexp "!!9634=972!!" then @fam_ovarianca_history := 3 - when obs regexp "!!9634=1671!!" then @fam_ovarianca_history := 4 - when obs regexp "!!9634=1393!!" then @fam_ovarianca_history := 5 - when obs regexp "!!9634=1392!!" then @fam_ovarianca_history := 6 - when obs regexp "!!9634=1395!!" then @fam_ovarianca_history := 7 - when obs regexp "!!9634=1394!!" then @fam_ovarianca_history := 8 - else @fam_ovarianca_history := null - end as fam_ovarianca_history, - - - case - when obs regexp "!!9635=978!!" then @fam_relatedca_history:= 1 - when obs regexp "!!9635=1672!!" then @fam_relatedca_history := 2 - when obs regexp "!!9635=972!!" then @fam_relatedca_history := 3 - when obs regexp "!!9635=1671!!" then @fam_relatedca_history := 4 - when obs regexp "!!9635=1393!!" then @fam_relatedca_history := 5 - when obs regexp "!!9635=1392!!" then @fam_relatedca_history := 6 - when obs regexp "!!9635=1395!!" then @fam_relatedca_history := 7 - when obs regexp "!!9635=1394!!" then @fam_relatedca_history := 8 - else @fam_relatedca_history := null - end as fam_relatedca_history, - - - case - when obs regexp "!!7176=6529!!" then @fam_otherca_specify := 1 - when obs regexp "!!7176=9636!!" then @fam_otherca_specify := 2 - when obs regexp "!!7176=9637!!" then @fam_otherca_specify := 3 - when obs regexp "!!7176=6485!!" then @fam_otherca_specify := 4 - when obs regexp "!!7176=9638!!" then @fam_otherca_specify := 5 - when obs regexp "!!7176=9639!!" then @fam_otherca_specify := 6 - when obs regexp "!!7176=6522!!" then @fam_otherca_specify := 5 - when obs regexp "!!7176=216!!" then @fam_otherca_specify := 6 - else @fam_otherca_specify := null - end as fam_otherca_specify, - - - case - when obs regexp "!!6251=1115!!" then @cur_physical_findings:= 0 - when obs regexp "!!6251=115!!" then @cur_physical_findings := 1 - when obs regexp "!!6251=6250!!" then @cur_physical_findings := 2 - when obs regexp "!!6251=6249!!" then @cur_physical_findings := 3 - when obs regexp "!!6251=5622!!" then @cur_physical_findings := 4 - when obs regexp "!!6251=582!!" then @cur_physical_findings := 5 - when obs regexp "!!6251=6493!!" then @cur_physical_findings := 6 - when obs regexp "!!6251=6499!!" then @cur_physical_findings := 7 - when obs regexp "!!6251=1118!!" then @cur_physical_findings := 8 - when obs regexp "!!6251=1481!!" then @cur_physical_findings := 9 - when obs regexp "!!6251=6729!!" then @cur_physical_findings := 10 - when obs regexp "!!6251=1116!!" then @cur_physical_findings := 11 - when obs regexp "!!6251=8188!!" then @cur_physical_findings := 12 - when obs regexp "!!6251=8189!!" then @cur_physical_findings := 13 - when obs regexp "!!6251=1067!!" then @cur_physical_findings := 14 - when obs regexp "!!6251=9689!!" then @cur_physical_findings := 15 - when obs regexp "!!6251=9690!!" then @cur_physical_findings := 16 - when obs regexp "!!6251=9687!!" then @cur_physical_findings := 17 - when obs regexp "!!6251=9688!!" then @cur_physical_findings := 18 - when obs regexp "!!6251=5313!!" then @cur_physical_findings := 19 - when obs regexp "!!6251=9691!!" then @cur_physical_findings := 20 - else @cur_physical_findings := null - end as cur_physical_findings, - - - case - when obs regexp "!!1121=1115!!" then @lymph_nodes_findings := 1 - when obs regexp "!!1121=161!!" then @lymph_nodes_findings := 2 - when obs regexp "!!1121=9675!!" then @lymph_nodes_findings:= 3 - when obs regexp "!!1121=9676!!" then @lymph_nodes_findings:= 4 - else @lymph_nodes_findings := null - end as lymph_nodes_findings, - - - case - when obs regexp "!!9748=1115!!" then @cur_screening_findings := 1 - when obs regexp "!!9748=9691!!" then @cur_screening_findings := 3 - when obs regexp "!!9748=1116!!" then @cur_screening_findings := 2 - else @cur_screening_findings := null - end as cur_screening_findings, - - - case - when obs regexp "!!6327=9651!!" then @patient_education := 1 - when obs regexp "!!6327=2345!!" then @patient_education := 2 - when obs regexp "!!6327=9692!!" then @patient_education:= 3 - when obs regexp "!!6327=5622!!" then @patient_education:= 4 - else @patient_education := null - end as patient_education, - - - case - when obs regexp "!!1915=" then @patient_education_other := GetValues(obs,1915) - else @patient_education_other := null - end as patient_education_other, - - - case - when obs regexp "!!1272=1107!!" then @referred_orderd := 0 - when obs regexp "!!1272=1496!!" then @referred_orderd:= 1 - else @referred_orderd := null - end as referred_orderd, - - case - when obs regexp "!!1272=9596!!" then @procedure_done := 1 - when obs regexp "!!1272=9595!!" then @procedure_done:= 2 - when obs regexp "!!1272=6510!!" then @procedure_done := 3 - when obs regexp "!!1272=7190!!" then @procedure_done:= 4 - when obs regexp "!!1272=6511!!" then @procedure_done := 5 - when obs regexp "!!1272=9997!!" then @procedure_done:= 6 - else @procedure_done := null - end as procedure_done, - - case - when obs regexp "!!9158=" then @referred_date := GetValues(obs,9158) - else @referred_date := null - end as referred_date, - - - case - when obs regexp "!!5096=" then @next_app_date := GetValues(obs,5096) - else @next_app_date := null - end as next_app_date, - - - case - when obs regexp "!!9702=9703!!" then @cbe_imaging_concordance := 1 - when obs regexp "!!9702=9704!!" then @cbe_imaging_concordance := 2 - when obs regexp "!!9702=1175!!" then @cbe_imaging_concordance := 3 - else @cbe_imaging_concordance := null - end as cbe_imaging_concordance, - - - case - when obs regexp "!!9595=1115!!" then @mammogram_results := 1 - when obs regexp "!!9595=1116!!" then @mammogram_results := 2 - when obs regexp "!!9595=1118!!" then @mammogram_results := 3 - when obs regexp "!!9595=1138!!" then @mammogram_results := 4 - when obs regexp "!!9595=1267!!" then @mammogram_results := 5 - when obs regexp "!!9595=1067!!" then @mammogram_results := 6 - else @mammogram_results := null - end as mammogram_results, - - - case - when obs regexp "!!9708=" then @mammogram_workup_date := GetValues(obs,9708) - else @mammogram_workup_date := null - end as mammogram_workup_date, - - - case - when obs regexp "!!9705=" then @date_patient_notified_of_mammogram_results := GetValues(obs,9705) - else @date_patient_notified_of_mammogram_results := null - end as date_patient_notified_of_mammogram_results, - - case - when obs regexp "!!9596=1115!!" then @Ultrasound_results := 1 - when obs regexp "!!9596=1116!!" then @Ultrasound_results := 2 - when obs regexp "!!9596=1067!!" then @Ultrasound_results := 3 - when obs regexp "!!9596=1118!!" then @Ultrasound_results := 4 - else @Ultrasound_results := null - end as Ultrasound_results, - - case - when obs regexp "!!9708=" then @ultrasound_workup_date := GetValues(obs,9708) - else @ultrasound_workup_date := null - end as ultrasound_workup_date, - - - case - when obs regexp "!!10047=" then @date_patient_notified_of_ultrasound_results := GetValues(obs,10047) - else @date_patient_notified_of_ultrasound_results := null - end as date_patient_notified_of_ultrasound_results, - - - case - when obs regexp "!!10051=9691!!" then @fna_results := 1 - when obs regexp "!!10051=10052!!" then @fna_results := 2 - when obs regexp "!!9596=1118!!" then @fna_results := 3 - else @fna_results := null - end as fna_results, - - - case - when obs regexp "!!10053=[0-9]" then @fna_tumor_size:= cast(GetValues(obs,10053) as unsigned) - else @fna_tumor_size := null - end as fna_tumor_size, - - - case - when obs regexp "!!10054=10055!!" then @fna_degree_of_malignancy := 1 - when obs regexp "!!10054=10056!!" then @fna_degree_of_malignancy := 2 - else @fna_degree_of_malignancy := null - end as fna_degree_of_malignancy, - - - case - when obs regexp "!!10057=" then @fna_workup_date := GetValues(obs,10057) - else @fna_workup_date := null - end as fna_workup_date, - - - case - when obs regexp "!!10059=" then @date_patient_notified_of_fna_results := GetValues(obs,10059) - else @date_patient_notified_of_fna_results := null - end as date_patient_notified_of_fna_results, - - - case - when obs regexp "!!8184=1115!!" then @breast_biopsy_results := 1 - when obs regexp "!!8184=1116!!" then @breast_biopsy_results := 2 - when obs regexp "!!8184=1118!!" then @breast_biopsy_results := 3 - when obs regexp "!!8184=1067!!" then @breast_biopsy_results := 4 - when obs regexp "!!8184=9691!!" then @breast_biopsy_results := 5 - when obs regexp "!!8184=10052!!" then @breast_biopsy_results := 6 - else @breast_biopsy_results := null - end as breast_biopsy_results, - - - case - when obs regexp "!!10053=[0-9]" then @biopsy_tumor_size:= cast(GetValues(obs,10053) as unsigned) - else @biopsy_tumor_size := null - end as biopsy_tumor_size, - - - case - when obs regexp "!!10054=10055!!" then @biopsy_degree_of_malignancy := 1 - when obs regexp "!!10054=10056!!" then @biopsy_degree_of_malignancy := 2 - else @biopsy_degree_of_malignancy := null - end as biopsy_degree_of_malignancy, - - case - when obs regexp "!!10060=" then @biopsy_workup_date := GetValues(obs,10060) - else @biopsy_workup_date := null - end as biopsy_workup_date, - - - case - when obs regexp "!!10061=" then @date_patient_notified_of_biopsy_resultse := GetValues(obs,10061) - else @date_patient_notified_of_biopsy_results := null - end as date_patient_notified_of_biopsy_results, - - - case - when obs regexp "!!9706=" then @date_patient_informed_and_referred_for_management := GetValues(obs,9706) - else @date_patient_informed_and_referred_for_management := null - end as date_patient_informed_and_referred_for_management, - - - case - when obs regexp "!!10068=9595!!" then @screening_mode := 1 - when obs regexp "!!10068=10067!!" then @screening_mode := 2 - when obs regexp "!!10068=9596!!" then @screening_mode := 3 - else @screening_mode := null - end as screening_mode, - - - case - when obs regexp "!!6042=" then @diagnosis := GetValues(obs,6042) - else @diagnosis := null - end as diagnosis, - - /**case - #when obs regexp "!!9728=" then @diagnosis_date := replace(replace((substring_index(substring(obs,locate("!!9728=",obs)),@sep,1)),"!!9728=",""),"!!","") - when obs regexp "!!9728=" then @diagnosis_date := encounter_datetime - else @diagnosis_date := null - end as diagnosis_date**/ - - - case - when obs regexp "!!9728=" then @diagnosis_date := GetValues(obs,9728) - else @diagnosis_date := null - end as diagnosis_date, - - - case - when obs regexp "!!9868=9852!!" then @cancer_staging := 1 - when obs regexp "!!9868=9856!!" then @cancer_staging := 2 - when obs regexp "!!9868=9860!!" then @cancer_staging := 3 - when obs regexp "!!9868=9864!!" then @cancer_staging := 4 - else @cancer_staging := null - end as cancer_staging, - - case - when obs regexp "!!6709=664!!" then @hiv_status := 1 - when obs regexp "!!6709=703!!" then @hiv_status := 2 - when obs regexp "!!6709=1067!!" then @hiv_status := 3 - else @hiv_status := null - end as hiv_status - - from flat_breast_cancer_screening_0 t1 - join amrs.person p using (person_id) - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - );# limit 100; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_breast_cancer_screening_1 drop prev_id, drop cur_id; - - drop table if exists flat_breast_cancer_screening_2; - create temporary table flat_breast_cancer_screening_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_breast_cancer_screening, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_breast_cancer_screening, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_breast_cancer_screening, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date - - from flat_breast_cancer_screening_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_breast_cancer_screening_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_breast_cancer_screening_3; - create temporary table flat_breast_cancer_screening_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_breast_cancer_screening, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_breast_cancer_screening, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_breast_cancer_screening, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_breast_cancer_screening_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - - select count(*) into @new_encounter_rows from flat_breast_cancer_screening_3; - - select @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; - select @total_rows_written; - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - encounter_id, - encounter_type, - encounter_datetime, - visit_id, - location_id, - t2.uuid as location_uuid, - gender, - age, - encounter_purpose, - other_encounter_purpose, - menstruation_before_12, - menses_stopped_permanently, - menses_stop_age, - hrt_use, - hrt_start_age, - hrt_end_age, - hrt_use_years, - hrt_type_used, - given_birth, - age_first_birth, - gravida, - parity, - cigarette_smoking, - cigarette_smoked_day, - tobacco_use, - tobacco_use_duration_yrs, - alcohol_drinking, - alcohol_type, - alcohol_use_period_yrs, - breast_complaints_3moths, - prev_exam_results, - fam_brca_history_bf50, - fam_brca_history_aft50, - fam_male_brca_history, - fam_ovarianca_history, - fam_relatedca_history, - fam_otherca_specify, - cur_physical_findings, - lymph_nodes_findings, - cur_screening_findings, - #cur_screening_findings_date, - patient_education, - patient_education_other, - referred_orderd, - referred_date, - procedure_done, - next_app_date, - cbe_imaging_concordance, - mammogram_results, - mammogram_workup_date, - date_patient_notified_of_mammogram_results, - Ultrasound_results, - ultrasound_workup_date, - date_patient_notified_of_ultrasound_results, - fna_results, - fna_tumor_size, - fna_degree_of_malignancy, - fna_workup_date, - date_patient_notified_of_fna_results, - breast_biopsy_results, - biopsy_tumor_size, - biopsy_degree_of_malignancy, - biopsy_workup_date, - date_patient_notified_of_biopsy_results, - date_patient_informed_and_referred_for_management, - screening_mode, - diagnosis, - diagnosis_date, - cancer_staging, - hiv_status, + when is_clinical_encounter then @cur_clinical_location_id := location_id + when @prev_id = @cur_id then @cur_clinical_location_id + else @cur_clinical_location_id := null + end as cur_clinic_location_id, + + case + when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date + else @prev_clinical_rtc_date := null + end as next_clinical_rtc_date_breast_cancer_screening, + + case + when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date + when @prev_id = @cur_id then @cur_clinical_rtc_date + else @cur_clinical_rtc_date:= null + end as cur_clinical_rtc_date + + from flat_breast_cancer_screening_1 + order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc + ); + + alter table flat_breast_cancer_screening_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; + + set @prev_id = null; + set @cur_id = null; + set @prev_encounter_type = null; + set @cur_encounter_type = null; + set @prev_encounter_datetime = null; + set @cur_encounter_datetime = null; + set @prev_clinical_datetime = null; + set @cur_clinical_datetime = null; + set @prev_clinical_location_id = null; + set @cur_clinical_location_id = null; + + drop temporary table if exists flat_breast_cancer_screening_3; + create temporary table flat_breast_cancer_screening_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) + (select + *, + @prev_id := @cur_id as prev_id, + @cur_id := t1.person_id as cur_id, + + case + when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type + else @prev_encounter_type:=null + end as prev_encounter_type_breast_cancer_screening, + @cur_encounter_type := encounter_type as cur_encounter_type, + + case + when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime + else @prev_encounter_datetime := null + end as prev_encounter_datetime_breast_cancer_screening, + + @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, + + case + when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime + else @prev_clinical_datetime := null + end as prev_clinical_datetime_breast_cancer_screening, + + case + when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id + else @prev_clinical_location_id := null + end as prev_clinical_location_id_breast_cancer_screening, + + case + when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime + when @prev_id = @cur_id then @cur_clinical_datetime + else @cur_clinical_datetime := null + end as cur_clinical_datetime, + + case + when is_clinical_encounter then @cur_clinical_location_id := location_id + when @prev_id = @cur_id then @cur_clinical_location_id + else @cur_clinical_location_id := null + end as cur_clinical_location_id, + + case + when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date + else @prev_clinical_rtc_date := null + end as prev_clinical_rtc_date_breast_cancer_screening, + + case + when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date + when @prev_id = @cur_id then @cur_clinical_rtc_date + else @cur_clinical_rtc_date:= null + end as cur_clinic_rtc_date + + from flat_breast_cancer_screening_2 t1 + order by person_id, date(encounter_datetime), encounter_type_sort_index + ); + + SELECT + COUNT(*) + INTO + @new_encounter_rows + FROM + flat_breast_cancer_screening_1; + + SELECT @new_encounter_rows; + SET @total_rows_written := @total_rows_written + @new_encounter_rows; + SELECT @total_rows_written; + + SET @dyn_sql := CONCAT('REPLACE INTO ', @write_table, + '(SELECT + null, + person_id, + encounter_id, + encounter_type, + encounter_datetime, + visit_id, + location_id, + t2.uuid as location_uuid, + gender, + age, + encounter_purpose, + other_encounter_purpose, + menstruation_before_12, + menses_stopped_permanently, + menses_stop_age, + hrt_use, + hrt_start_age, + hrt_end_age, + hrt_use_years, + hrt_type_used, + given_birth, + age_first_birth, + gravida, + parity, + cigarette_smoking, + cigarette_smoked_day, + tobacco_use, + tobacco_use_duration_yrs, + alcohol_drinking, + alcohol_type, + alcohol_use_period_yrs, + breast_complaints_3months, + breast_mass_location, + nipple_discharge_location, + nipple_retraction_location, + breast_erythrema_location, + breast_rash_location, + breast_pain_location, + other_changes_location, + history_of_mammogram, + mammogram_results, + breast_ultrasound_history, + breast_ultrasound_result, + history_of_breast_biopsy, + breast_biopsy_results, + number_of_biopsies, + biopsy_type, + prev_exam_results, + fam_brca_history_bf50, + fam_brca_history_aft50, + fam_male_brca_history, + fam_ovarianca_history, + fam_relatedca_history, + fam_otherca_specify, + cur_physical_findings, + lymph_nodes_findings, + cur_screening_findings, + #cur_screening_findings_date, + patient_education, + patient_education_other, + referrals_ordered, + referred_date, + procedure_done, + next_app_date, + cbe_imaging_concordance, + mammogram_workup_date, + date_patient_notified_of_mammogram_results, + ultrasound_results, + ultrasound_workup_date, + date_patient_notified_of_ultrasound_results, + fna_results, + fna_tumor_size, + fna_degree_of_malignancy, + fna_workup_date, + date_patient_notified_of_fna_results, + biopsy_tumor_size, + biopsy_degree_of_malignancy, + biopsy_workup_date, + date_patient_notified_of_biopsy_results, + biopsy_description, + diagnosis_date, + date_patient_informed_and_referred_for_management, + screening_mode, + diagnosis, + cancer_staging, + hiv_status, + prev_encounter_datetime_breast_cancer_screening, + next_encounter_datetime_breast_cancer_screening, + prev_encounter_type_breast_cancer_screening, + next_encounter_type_breast_cancer_screening, + prev_clinical_datetime_breast_cancer_screening, + next_clinical_datetime_breast_cancer_screening, + prev_clinical_location_id_breast_cancer_screening, + next_clinical_location_id_breast_cancer_screening, + prev_clinical_rtc_date_breast_cancer_screening, + next_clinical_rtc_date_breast_cancer_screening + FROM + flat_breast_cancer_screening_3 t1 + JOIN + amrs.location t2 USING (location_id))' + ); + + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + + SET @dyn_sql=CONCAT('DELETE t1 from ',@queue_table,' t1 JOIN flat_breast_cancer_screening_build_queue__0 t2 USING (person_id);'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; - prev_encounter_datetime_breast_cancer_screening, - next_encounter_datetime_breast_cancer_screening, - prev_encounter_type_breast_cancer_screening, - next_encounter_type_breast_cancer_screening, - prev_clinical_datetime_breast_cancer_screening, - next_clinical_datetime_breast_cancer_screening, - prev_clinical_location_id_breast_cancer_screening, - next_clinical_location_id_breast_cancer_screening, - prev_clinical_rtc_date_breast_cancer_screening, - next_clinical_rtc_date_breast_cancer_screening - - from flat_breast_cancer_screening_3 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_breast_cancer_screening_build_queue__0 t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #select @person_ids_count := (select count(*) from flat_breast_cancer_screening_build_queue_2); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; + SET @dyn_sql=CONCAT('SELECT COUNT(*) INTO @person_ids_count FROM ',@queue_table,';'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; - #select @person_ids_count as remaining_in_build_queue; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - #select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select @person_ids_count as 'persons remaining', @cycle_length as 'Cycle time (s)', ceil(@person_ids_count / cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - -#select 1; - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; + SET @cycle_length = TIMESTAMPDIFF(second,@loop_start_time,NOW()); + SET @total_time = @total_time + @cycle_length; + SET @cycle_number = @cycle_number + 1; + + SET @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); + + SELECT + @person_ids_count AS 'persons remaining', + @cycle_length AS 'Cycle time (s)', + CEIL(@person_ids_count / cycle_size) AS remaining_cycles, + @remaining_time AS 'Est time remaining (min)'; + END WHILE; + + IF (@query_type = 'build') THEN + SET @dyn_sql := CONCAT('drop table ', @queue_table, ';'); + PREPARE s1 FROM @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; + SET @total_rows_to_write := 0; + SET @dyn_sql := CONCAT('SELECT COUNT(*) INTO @total_rows_to_write FROM ', @write_table); + PREPARE s1 FROM @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; + SET @start_write := NOW(); + SELECT CONCAT(@start_write, ' : Writing ', @total_rows_to_write, ' to ', @primary_table); + + SET @dyn_sql := CONCAT('REPLACE INTO ', @primary_table, '(SELECT * FROM ', @write_table, ');'); + PREPARE s1 FROM @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ + SET @finish_write := NOW(); + SET @time_to_write := TIMESTAMPDIFF(SECOND, @start_write, @finish_write); + SELECT CONCAT(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); + + SET @dyn_sql := CONCAT('DROP TABLE ', @write_table, ';'); + PREPARE s1 FROM @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + END IF; + + SET @ave_cycle_length := CEIL(@total_time / @cycle_number); + SELECT CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); + + SET @end := NOW(); + INSERT INTO etl.flat_log VALUES (@start, @last_date_created, @table_version, TIMESTAMPDIFF(SECOND, @start, @end)); + SELECT CONCAT(@table_version, ': Time to complete: ', TIMESTAMPDIFF(MINUTE, @start, @end), ' minutes'); +END$$ DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening_v1_0.sql deleted file mode 100644 index c65b2ca..0000000 --- a/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening_v1_0.sql +++ /dev/null @@ -1,1219 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_breast_cancer_screening_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_breast_cancer_screening"; - set @query_type = query_type; -#set @query_type = "build"; - - set @total_rows_written = 0; - - set @encounter_types = "(86,145,146,160)"; - set @clinical_encounter_types = "(86,145,146,160)"; - set @non_clinical_encounter_types = "(-1)"; - set @other_encounter_types = "(-1)"; - - set @start = now(); - set @table_version = "flat_breast_cancer_screening_v1.0"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @boundary = "!!"; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - #delete from etl.flat_log where table_name like "%flat_breast_cancer_screening%"; - #drop table etl.flat_breast_cancer_screening; - - - #drop table if exists flat_breast_cancer_screening; - create table if not exists flat_breast_cancer_screening ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - encounter_id int, - encounter_type int, - encounter_datetime datetime, - visit_id int, - location_id int, - location_uuid varchar (100), - #location_name char (100), - gender char (100), - age int, - encounter_purpose int, - other_encounter_purpose varchar(1000), - menstruation_before_12 char, - menses_stopped_permanently char, - menses_stop_age int, - hrt_use char, - hrt_start_age int, - hrt_end_age int, - hrt_use_years int, - hrt_type_used int, - given_birth int, - age_first_birth int, - gravida int, - parity int, - cigarette_smoking int, - cigarette_smoked_day int, - tobacco_use int, - tobacco_use_duration_yrs int, - alcohol_drinking int, - alcohol_type int, - alcohol_use_period_yrs int, - breast_complaints_3moths int, - prev_exam_results int, - fam_brca_history_bf50 int, - fam_brca_history_aft50 int, - fam_male_brca_history int, - fam_ovarianca_history int, - fam_relatedca_history int, - fam_otherca_specify int, - cur_physical_findings int, - lymph_nodes_findings int, - cur_screening_findings int, - #cur_screening_findings_date int, - patient_education int, - patient_education_other varchar(1000), - referred_orderd int, - referred_date datetime, - procedure_done int, - next_app_date datetime, - - cbe_imaging_concordance int, - mammogram_results int, - mammogram_workup_date datetime, - date_patient_notified_of_mammogram_results datetime, - Ultrasound_results int, - ultrasound_workup_date datetime, - date_patient_notified_of_ultrasound_results datetime, - fna_results int, - fna_tumor_size int, - fna_degree_of_malignancy int, - fna_workup_date datetime, - date_patient_notified_of_fna_results datetime, - breast_biopsy_results int, - biopsy_tumor_size int, - biopsy_degree_of_malignancy int, - biopsy_workup_date datetime, - date_patient_notified_of_biopsy_results datetime, - date_patient_informed_and_referred_for_management datetime, - screening_mode int, - diagnosis int, - diagnosis_date datetime, - cancer_staging int, - - prev_encounter_datetime_breast_cancer_screening datetime, - next_encounter_datetime_breast_cancer_screening datetime, - prev_encounter_type_breast_cancer_screening mediumint, - next_encounter_type_breast_cancer_screening mediumint, - prev_clinical_datetime_breast_cancer_screening datetime, - next_clinical_datetime_breast_cancer_screening datetime, - prev_clinical_location_id_breast_cancer_screening mediumint, - next_clinical_location_id_breast_cancer_screening mediumint, - prev_clinical_rtc_date_breast_cancer_screening datetime, - next_clinical_rtc_date_breast_cancer_screening datetime, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,next_app_date), - index location_uuid_rtc_date (location_uuid,next_app_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_breast_cancer_screening), - index encounter_type (encounter_type), - index date_created (date_created) - - ); - - - - if(@query_type="build") then - select 'BUILDING..........................................'; - -#set @write_table = concat("flat_breast_cancer_screening_temp_",1); -#set @queue_table = concat("flat_breast_cancer_screening_build_queue_",1); - - set @write_table = concat("flat_breast_cancer_screening_temp_",queue_number); - set @queue_table = concat("flat_breast_cancer_screening_build_queue_",queue_number); - - -#drop table if exists flat_breast_cancer_screening_temp_1; - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_breast_cancer_screening_build_queue limit ', queue_size, ');'); -#SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_breast_cancer_screening_build_queue limit 500);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_breast_cancer_screening_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from flat_breast_cancer_screening_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_breast_cancer_screening"; - set @queue_table = "flat_breast_cancer_screening_sync_queue"; - create table if not exists flat_breast_cancer_screening_sync_queue (person_id int primary key); - - set @last_update = null; - - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - -#select max(date_created) into @last_update from etl.flat_log where table_name like "%breast_cancer_screening%"; - -#select @last_update; -select "Finding patients in amrs.encounters..."; - - replace into flat_breast_cancer_screening_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - -select "Finding patients in flat_obs..."; - - replace into flat_breast_cancer_screening_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - -select "Finding patients in flat_lab_obs..."; - replace into flat_breast_cancer_screening_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - -select "Finding patients in flat_orders..."; - - replace into flat_breast_cancer_screening_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_breast_cancer_screening_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_breast_cancer_screening_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - # Remove test patients - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - select @person_ids_count as 'num patients to update'; - - - #delete t1 from flat_breast_cancer_screening t1 join @queue_table t2 using (person_id); - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists flat_breast_cancer_screening_build_queue__0; - - #create temporary table flat_breast_cancer_screening_build_queue__0 (select * from flat_breast_cancer_screening_build_queue_2 limit 5000); #TODO - change this when data_fetch_size changes - -#SET @dyn_sql=CONCAT('create temporary table flat_breast_cancer_screening_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit 100);'); - SET @dyn_sql=CONCAT('create temporary table flat_breast_cancer_screening_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - drop temporary table if exists flat_breast_cancer_screening_0a; - SET @dyn_sql = CONCAT( - 'create temporary table flat_breast_cancer_screening_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - case - when t1.encounter_type in ',@clinical_encounter_types,' then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in ',@non_clinical_encounter_types,' then 20 - when t1.encounter_type in ',@clinical_encounter_types,' then 10 - when t1.encounter_type in', @other_encounter_types, ' then 5 - else 1 - end as encounter_type_sort_index, - t2.orders - from etl.flat_obs t1 - join flat_breast_cancer_screening_build_queue__0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_id) - where t1.encounter_type in ',@encounter_types,');'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - - insert into flat_breast_cancer_screening_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, #t1.location_id, - t1.obs, - null, #obs_datetimes - # in any visit, there many be multiple encounters. for this dataset, we want to include only clinical encounters (e.g. not lab or triage visit) - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_breast_cancer_screening_build_queue__0 t0 using (person_id) - ); - - - drop temporary table if exists flat_breast_cancer_screening_0; - create temporary table flat_breast_cancer_screening_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_breast_cancer_screening_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @encounter_purpose = null; - set @other_encounter_purpose = null; - set @menstruation_before_12 = null; - set @menses_stopped_permanently = null; - set @menses_stop_age = null; - set @hrt_use = null; - set @hrt_start_age = null; - set @hrt_end_age = null; - set @hrt_use_years = null; - set @hrt_type_used =null; - set @given_birth = null; - set @age_first_birth = null; - set @gravida = null; - set @parity = null; - set @cigarette_smoking = null; - set @cigarette_smoked_day =null; - set @tobacco_use =null; - set @tobacco_use_duration_yrs = null; - set @alcohol_drinking =null; - set @alcohol_type = null; - set @alcohol_use_period_yrs = null; - set @breast_complaints_3moths = null; - set @prev_exam_results = null; - set @fam_brca_history_bf50 = null; - set @fam_brca_history_aft50 = null; - set @fam_male_brca_history = null; - set @fam_ovarianca_history =null; - set @fam_relatedca_history =null; - set @fam_otherca_specify = null; - set @cur_physical_findings = null; - set @lymph_nodes_findings =null; - set @cur_screening_findings = null; - #set @cur_screening_findings_date = null; - set @patient_education = null; - set @patient_education_other = null; - set @referred_orderd = null; - set @procedure_done =null; - set @referred_date =null; - set @next_app_date = null; - - set @cbe_imaging_concordance =null; - set @mammogram_results =null; - set @mammogram_workup_date =null; - set @date_patient_notified_of_mammogram_results =null; - set @Ultrasound_results =null; - set @ultrasound_workup_date =null; - set @date_patient_notified_of_ultrasound_results =null; - set @fna_results =null; - set @fna_tumor_size =null; - set @fna_degree_of_malignancy =null; - set @fna_workup_date =null; - set @date_patient_notified_of_fna_results =null; - set @breast_biopsy_results =null; - set @biopsy_tumor_size =null; - set @biopsy_degree_of_malignancy =null; - set @biopsy_workup_date =null; - set @date_patient_notified_of_biopsy_results =null; - set @date_patient_informed_and_referred_for_management =null; - set @screening_mode =null; - set @diagnosis =null; - set @diagnosis_date =null; - set @cancer_staging =null; - - drop temporary table if exists flat_breast_cancer_screening_1; - create temporary table flat_breast_cancer_screening_1 #(index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - t1.encounter_id, - t1.encounter_type, - t1.encounter_datetime, - t1.visit_id, - #t4.name as location_name, - t1.location_id, - t1.is_clinical_encounter, - p.gender, - p.death_date, - - - case - when timestampdiff(year,birthdate,curdate()) > 0 then round(timestampdiff(year,birthdate,curdate()),0) - else round(timestampdiff(month,birthdate,curdate())/12,2) - end as age, - - case - when obs regexp "!!1834=9651!!" then @encounter_purpose := 1 - when obs regexp "!!1834=1154!!" then @encounter_purpose := 2 - when obs regexp "!!1834=1246!!" then @encounter_purpose := 3 - when obs regexp "!!1834=5622!!" then @encounter_purpose := 4 - else @encounter_purpose := null - end as encounter_purpose, - - case - when obs regexp "!!1915=" then @other_encounter_purpose := GetValues(obs,1915) - else @other_encounter_purpose := null - end as other_encounter_purpose, - - case - when obs regexp "!!9560=1065!!" then @menstruation_before_12 := 1 - when obs regexp "!!9560=1066!!" then @menstruation_before_12 := 0 - else @menstruation_before_12 := null - end as menstruation_before_12, - - case - when obs regexp "!!9561=1065!!" then @menstruation_before_12 := 1 - when obs regexp "!!9561=1066!!" then @menstruation_before_12 := 0 - when obs regexp "!!9561=9568!!" then @menstruation_before_12 := 2 - else @menses_stopped_permanently := null - end as menses_stopped_permanently, - - - case - when obs regexp "!!9562=[0-9]" and t1.encounter_type = @lab_encounter_type then @menses_stop_age:=cast(GetValues(obs,9562) as unsigned) - when @prev_id=@cur_id then@menses_stop_age - else @menses_stop_age:=null - end as menses_stop_age, - - case - when obs regexp "!!9626=1065!!" then @hrt_use := 1 - when obs regexp "!!9626=1066!!" then @hrt_use := 0 - when obs regexp "!!9626=9568!!" then @hrt_use := 2 - else @hrt_use := null - end as hrt_use, - - - case - when obs regexp "!!9627=[0-9]" then @hrt_start_age:=cast(GetValues(obs,9627) as unsigned) - else @hrt_start_age := null - end as hrt_start_age, - - - case - when obs regexp "!!9723=[0-9]" then @hrt_end_age:=cast(GetValues(obs,9723) as unsigned) - else @hrt_end_age := null - end as hrt_end_age, - - - case - when obs regexp "!!97629=[0-9]" then @hrt_use_years:=cast(GetValues(obs,9629) as unsigned) - else @hrt_use_years := null - end as hrt_use_years, - - - case - when obs regexp "!!9630=9573!!" then @hrt_type_used := 1 - when obs regexp "!!9630=6217!!" then @hrt_type_used := 2 - when obs regexp "!!9630=6218!!" then @hrt_type_used := 3 - else @hrt_type_used := null - end as hrt_type_used, - - case - when obs regexp "!!9563=1065!!" then @given_birth := 1 - when obs regexp "!!9563=1066!!" then @given_birth := 0 - else @given_birth := null - end as given_birth, - - - case - when obs regexp "!!5574=[0-9]" then @age_first_birth:=cast(GetValues(obs,5574) as unsigned) - else @age_first_birth := null - end as age_first_birth, - - - case - when obs regexp "!!5624=[0-9]" then @gravida:= cast(GetValues(obs,5624) as unsigned) - else @gravida := null - end as gravida, - - - case - when obs regexp "!!1053=[0-9]" then @parity:=cast(GetValues(obs,1053) as unsigned) - else @parity := null - end as parity, - - - case - when obs regexp "!!9333=" then @cigarette_smoking := GetValues(obs,9333) - else @cigarette_smoking := null - end as cigarette_smoking, - - - case - when obs regexp "!!2069=[0-9]" then @cigarette_smoked_day:=cast(GetValues(obs,2069) as unsigned) - else @cigarette_smoked_day := null - end as cigarette_smoked_day, - - - case - when obs regexp "!!7973=1065!!" then @tobacco_use := 1 - when obs regexp "!!7973=1066!!" then @tobacco_use := 0 - when obs regexp "!!7973=1679!!" then @tobacco_use := 2 - else @hrt_use := null - end as tobacco_use, - - - case - when obs regexp "!!8144=[0-9]" then @tobacco_use_duration_yrs:= cast(GetValues(obs,8144) as unsigned) - else @tobacco_use_duration_yrs := null - end as tobacco_use_duration_yrs, - - - case - when obs regexp "!!1684=1065!!" then @alcohol_drinking := 1 - when obs regexp "!!1684=1066!!" then @alcohol_drinking := 0 - when obs regexp "!!1684=1679!!" then @alcohol_drinking := 2 - else @alcohol_drinking := null - end as alcohol_drinking, - - - case - when obs regexp "!!1685=1682!!" then @alcohol_type := 1 - when obs regexp "!!1685=1681!!" then @alcohol_type := 2 - when obs regexp "!!1685=1680!!" then @alcohol_type := 3 - when obs regexp "!!1685=1683!!" then @alcohol_type := 4 - when obs regexp "!!1685=2059!!" then @alcohol_type := 5 - when obs regexp "!!1685=5622!!" then @alcohol_type := 6 - else @hrt_type_used := null - end as alcohol_type, - - - case - when obs regexp "!!8170=[0-9]" then @alcohol_use_period_yrs:= cast(GetValues(obs,8170) as unsigned) - else @alcohol_use_period_yrs := null - end as alcohol_use_period_yrs, - - - case - when obs regexp "!!9553=5006!!" then @breast_complaints_3moths := 1 - when obs regexp "!!9553=1068!!" then @breast_complaints_3moths := 2 - else @breast_complaints_3moths := null - end as breast_complaints_3moths, - - - case - when obs regexp "!!9694=1115!!" then @prev_exam_results := 1 - when obs regexp "!!9694=1116!!" then @prev_exam_results := 0 - when obs regexp "!!9694=1067!!" then @prev_exam_results := 2 - else @prev_exam_results := null - end as prev_exam_results, - - - case - when obs regexp "!!9631=1672!!" then @fam_brca_history_bf50 := 1 - when obs regexp "!!9631=1107!!" then @fam_brca_history_bf50 := 0 - when obs regexp "!!9631=978!!" then @fam_brca_history_bf50 := 2 - when obs regexp "!!9631=972!!" then @fam_brca_history_bf50 := 3 - when obs regexp "!!9631=1671!!" then @fam_brca_history_bf50 := 4 - when obs regexp "!!9631=1393!!" then @fam_brca_history_bf50 := 5 - when obs regexp "!!9631=1392!!" then @fam_brca_history_bf50 := 6 - when obs regexp "!!9631=1395!!" then @fam_brca_history_bf50 := 7 - when obs regexp "!!9631=1394!!" then @fam_brca_history_bf50 := 8 - else @fam_brca_history_bf50 := null - end as fam_brca_history_bf50, - - - case - when obs regexp "!!9632=978!!" then @fam_brca_history_aft50 := 1 - when obs regexp "!!9632=1672!!" then @fam_brca_history_aft50 := 2 - when obs regexp "!!9632=972!!" then @fam_brca_history_aft50 := 3 - when obs regexp "!!9632=1671!!" then @fam_brca_history_aft50 := 4 - when obs regexp "!!9632=1393!!" then @fam_brca_history_aft50 := 5 - when obs regexp "!!9632=1392!!" then @fam_brca_history_aft50 := 6 - when obs regexp "!!9632=1395!!" then @fam_brca_history_aft50 := 7 - when obs regexp "!!9632=1394!!" then @fam_brca_history_aft50 := 8 - else @fam_brca_history_aft50 := null - end as fam_brca_history_aft50, - - - case - when obs regexp "!!9633=978!!" then @fam_male_brca_history := 1 - when obs regexp "!!9633=1672!!" then @fam_male_brca_history := 2 - when obs regexp "!!9633=972!!" then @fam_male_brca_history := 3 - when obs regexp "!!9633=1671!!" then @fam_male_brca_history := 4 - when obs regexp "!!9633=1393!!" then @fam_male_brca_history := 5 - when obs regexp "!!9633=1392!!" then @fam_male_brca_history := 6 - when obs regexp "!!9633=1395!!" then @fam_male_brca_history := 7 - when obs regexp "!!9633=1394!!" then @fam_male_brca_history := 8 - else @fam_male_brca_history := null - end as fam_male_brca_history, - - - case - when obs regexp "!!9634=978!!" then @fam_ovarianca_history := 1 - when obs regexp "!!9634=1672!!" then @fam_ovarianca_history := 2 - when obs regexp "!!9634=972!!" then @fam_ovarianca_history := 3 - when obs regexp "!!9634=1671!!" then @fam_ovarianca_history := 4 - when obs regexp "!!9634=1393!!" then @fam_ovarianca_history := 5 - when obs regexp "!!9634=1392!!" then @fam_ovarianca_history := 6 - when obs regexp "!!9634=1395!!" then @fam_ovarianca_history := 7 - when obs regexp "!!9634=1394!!" then @fam_ovarianca_history := 8 - else @fam_ovarianca_history := null - end as fam_ovarianca_history, - - - case - when obs regexp "!!9635=978!!" then @fam_relatedca_history:= 1 - when obs regexp "!!9635=1672!!" then @fam_relatedca_history := 2 - when obs regexp "!!9635=972!!" then @fam_relatedca_history := 3 - when obs regexp "!!9635=1671!!" then @fam_relatedca_history := 4 - when obs regexp "!!9635=1393!!" then @fam_relatedca_history := 5 - when obs regexp "!!9635=1392!!" then @fam_relatedca_history := 6 - when obs regexp "!!9635=1395!!" then @fam_relatedca_history := 7 - when obs regexp "!!9635=1394!!" then @fam_relatedca_history := 8 - else @fam_relatedca_history := null - end as fam_relatedca_history, - - - case - when obs regexp "!!7176=6529!!" then @fam_otherca_specify := 1 - when obs regexp "!!7176=9636!!" then @fam_otherca_specify := 2 - when obs regexp "!!7176=9637!!" then @fam_otherca_specify := 3 - when obs regexp "!!7176=6485!!" then @fam_otherca_specify := 4 - when obs regexp "!!7176=9638!!" then @fam_otherca_specify := 5 - when obs regexp "!!7176=9639!!" then @fam_otherca_specify := 6 - when obs regexp "!!7176=6522!!" then @fam_otherca_specify := 5 - when obs regexp "!!7176=216!!" then @fam_otherca_specify := 6 - else @fam_otherca_specify := null - end as fam_otherca_specify, - - - case - when obs regexp "!!6251=1115!!" then @cur_physical_findings:= 0 - when obs regexp "!!6251=115!!" then @cur_physical_findings := 1 - when obs regexp "!!6251=6250!!" then @cur_physical_findings := 2 - when obs regexp "!!6251=6249!!" then @cur_physical_findings := 3 - when obs regexp "!!6251=5622!!" then @cur_physical_findings := 4 - when obs regexp "!!6251=582!!" then @cur_physical_findings := 5 - when obs regexp "!!6251=6493!!" then @cur_physical_findings := 6 - when obs regexp "!!6251=6499!!" then @cur_physical_findings := 7 - when obs regexp "!!6251=1118!!" then @cur_physical_findings := 8 - when obs regexp "!!6251=1481!!" then @cur_physical_findings := 9 - when obs regexp "!!6251=6729!!" then @cur_physical_findings := 10 - when obs regexp "!!6251=1116!!" then @cur_physical_findings := 11 - when obs regexp "!!6251=8188!!" then @cur_physical_findings := 12 - when obs regexp "!!6251=8189!!" then @cur_physical_findings := 13 - when obs regexp "!!6251=1067!!" then @cur_physical_findings := 14 - when obs regexp "!!6251=9689!!" then @cur_physical_findings := 15 - when obs regexp "!!6251=9690!!" then @cur_physical_findings := 16 - when obs regexp "!!6251=9687!!" then @cur_physical_findings := 17 - when obs regexp "!!6251=9688!!" then @cur_physical_findings := 18 - when obs regexp "!!6251=5313!!" then @cur_physical_findings := 19 - when obs regexp "!!6251=9691!!" then @cur_physical_findings := 20 - else @cur_physical_findings := null - end as cur_physical_findings, - - - case - when obs regexp "!!1121=1115!!" then @lymph_nodes_findings := 1 - when obs regexp "!!1121=161!!" then @lymph_nodes_findings := 2 - when obs regexp "!!1121=9675!!" then @lymph_nodes_findings:= 3 - when obs regexp "!!1121=9676!!" then @lymph_nodes_findings:= 4 - else @lymph_nodes_findings := null - end as lymph_nodes_findings, - - - case - when obs regexp "!!9748=1115!!" then @cur_screening_findings := 1 - when obs regexp "!!9748=9691!!" then @cur_screening_findings := 3 - when obs regexp "!!9748=1116!!" then @cur_screening_findings := 2 - else @cur_screening_findings := null - end as cur_screening_findings, - - - case - when obs regexp "!!6327=9651!!" then @patient_education := 1 - when obs regexp "!!6327=2345!!" then @patient_education := 2 - when obs regexp "!!6327=9692!!" then @patient_education:= 3 - when obs regexp "!!6327=5622!!" then @patient_education:= 4 - else @patient_education := null - end as patient_education, - - - case - when obs regexp "!!1915=" then @patient_education_other := GetValues(obs,1915) - else @patient_education_other := null - end as patient_education_other, - - - case - when obs regexp "!!1272=1107!!" then @referred_orderd := 0 - when obs regexp "!!1272=1496!!" then @referred_orderd:= 1 - else @referred_orderd := null - end as referred_orderd, - - case - when obs regexp "!!1272=9596!!" then @procedure_done := 1 - when obs regexp "!!1272=9595!!" then @procedure_done:= 2 - when obs regexp "!!1272=6510!!" then @procedure_done := 3 - when obs regexp "!!1272=7190!!" then @procedure_done:= 4 - when obs regexp "!!1272=6511!!" then @procedure_done := 5 - when obs regexp "!!1272=9997!!" then @procedure_done:= 6 - else @procedure_done := null - end as procedure_done, - - case - when obs regexp "!!9158=" then @referred_date := GetValues(obs,9158) - else @referred_date := null - end as referred_date, - - - case - when obs regexp "!!5096=" then @next_app_date := GetValues(obs,5096) - else @next_app_date := null - end as next_app_date, - - - case - when obs regexp "!!9702=9703!!" then @cbe_imaging_concordance := 1 - when obs regexp "!!9702=9704!!" then @cbe_imaging_concordance := 2 - when obs regexp "!!9702=1175!!" then @cbe_imaging_concordance := 3 - else @cbe_imaging_concordance := null - end as cbe_imaging_concordance, - - - case - when obs regexp "!!9595=1115!!" then @mammogram_results := 1 - when obs regexp "!!9595=1116!!" then @mammogram_results := 2 - when obs regexp "!!9595=1118!!" then @mammogram_results := 3 - when obs regexp "!!9595=1138!!" then @mammogram_results := 4 - when obs regexp "!!9595=1267!!" then @mammogram_results := 5 - when obs regexp "!!9595=1067!!" then @mammogram_results := 6 - else @mammogram_results := null - end as mammogram_results, - - - case - when obs regexp "!!9708=" then @mammogram_workup_date := GetValues(obs,9708) - else @mammogram_workup_date := null - end as mammogram_workup_date, - - - case - when obs regexp "!!9705=" then @date_patient_notified_of_mammogram_results := GetValues(obs,9705) - else @date_patient_notified_of_mammogram_results := null - end as date_patient_notified_of_mammogram_results, - - case - when obs regexp "!!9596=1115!!" then @Ultrasound_results := 1 - when obs regexp "!!9596=1116!!" then @Ultrasound_results := 2 - when obs regexp "!!9596=1067!!" then @Ultrasound_results := 3 - when obs regexp "!!9596=1118!!" then @Ultrasound_results := 4 - else @Ultrasound_results := null - end as Ultrasound_results, - - case - when obs regexp "!!9708=" then @ultrasound_workup_date := GetValues(obs,9708) - else @ultrasound_workup_date := null - end as ultrasound_workup_date, - - - case - when obs regexp "!!10047=" then @date_patient_notified_of_ultrasound_results := GetValues(obs,10047) - else @date_patient_notified_of_ultrasound_results := null - end as date_patient_notified_of_ultrasound_results, - - - case - when obs regexp "!!10051=9691!!" then @fna_results := 1 - when obs regexp "!!10051=10052!!" then @fna_results := 2 - when obs regexp "!!9596=1118!!" then @fna_results := 3 - else @fna_results := null - end as fna_results, - - - case - when obs regexp "!!10053=[0-9]" then @fna_tumor_size:= cast(GetValues(obs,10053) as unsigned) - else @fna_tumor_size := null - end as fna_tumor_size, - - - case - when obs regexp "!!10054=10055!!" then @fna_degree_of_malignancy := 1 - when obs regexp "!!10054=10056!!" then @fna_degree_of_malignancy := 2 - else @fna_degree_of_malignancy := null - end as fna_degree_of_malignancy, - - - case - when obs regexp "!!10057=" then @fna_workup_date := GetValues(obs,10057) - else @fna_workup_date := null - end as fna_workup_date, - - - case - when obs regexp "!!10059=" then @date_patient_notified_of_fna_results := GetValues(obs,10059) - else @date_patient_notified_of_fna_results := null - end as date_patient_notified_of_fna_results, - - - case - when obs regexp "!!8184=1115!!" then @breast_biopsy_results := 1 - when obs regexp "!!8184=1116!!" then @breast_biopsy_results := 2 - when obs regexp "!!8184=1118!!" then @breast_biopsy_results := 3 - when obs regexp "!!8184=1067!!" then @breast_biopsy_results := 4 - when obs regexp "!!8184=9691!!" then @breast_biopsy_results := 5 - when obs regexp "!!8184=10052!!" then @breast_biopsy_results := 6 - else @breast_biopsy_results := null - end as breast_biopsy_results, - - - case - when obs regexp "!!10053=[0-9]" then @biopsy_tumor_size:= cast(GetValues(obs,10053) as unsigned) - else @biopsy_tumor_size := null - end as biopsy_tumor_size, - - - case - when obs regexp "!!10054=10055!!" then @biopsy_degree_of_malignancy := 1 - when obs regexp "!!10054=10056!!" then @biopsy_degree_of_malignancy := 2 - else @biopsy_degree_of_malignancy := null - end as biopsy_degree_of_malignancy, - - - case - when obs regexp "!!10060=" then @biopsy_workup_date := GetValues(obs,10060) - else @biopsy_workup_date := null - end as biopsy_workup_date, - - - case - when obs regexp "!!10061=" then @date_patient_notified_of_biopsy_resultse := GetValues(obs,10061) - else @date_patient_notified_of_biopsy_results := null - end as date_patient_notified_of_biopsy_results, - - - case - when obs regexp "!!9706=" then @date_patient_informed_and_referred_for_management := GetValues(obs,10061) - else @date_patient_informed_and_referred_for_management := null - end as date_patient_informed_and_referred_for_management, - - - case - when obs regexp "!!10068=9595!!" then @screening_mode := 1 - when obs regexp "!!10068=10067!!" then @screening_mode := 2 - when obs regexp "!!10068=9596!!" then @screening_mode := 3 - else @screening_mode := null - end as screening_mode, - - - case - when obs regexp "!!6042=" then @diagnosis := GetValues(obs,6042) - else @diagnosis := null - end as diagnosis, - - /**case - #when obs regexp "!!9728=" then @diagnosis_date := replace(replace((substring_index(substring(obs,locate("!!9728=",obs)),@sep,1)),"!!9728=",""),"!!","") - when obs regexp "!!9728=" then @diagnosis_date := encounter_datetime - else @diagnosis_date := null - end as diagnosis_date**/ - - - case - when obs regexp "!!9728=" then @diagnosis_date := GetValues(obs,9728) - else @diagnosis_date := null - end as diagnosis_date, - - - case - when obs regexp "!!9868=9852!!" then @cancer_staging := 1 - when obs regexp "!!9868=9856!!" then @cancer_staging := 2 - when obs regexp "!!9868=9860!!" then @cancer_staging := 3 - when obs regexp "!!9868=9864!!" then @cancer_staging := 4 - else @cancer_staging := null - end as cancer_staging - - from flat_breast_cancer_screening_0 t1 - join amrs.person p using (person_id) - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - );# limit 100; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_breast_cancer_screening_1 drop prev_id, drop cur_id; - - drop table if exists flat_breast_cancer_screening_2; - create temporary table flat_breast_cancer_screening_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_breast_cancer_screening, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_breast_cancer_screening, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_breast_cancer_screening, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date - - from flat_breast_cancer_screening_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_breast_cancer_screening_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_breast_cancer_screening_3; - create temporary table flat_breast_cancer_screening_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_breast_cancer_screening, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_breast_cancer_screening, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_breast_cancer_screening, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_breast_cancer_screening_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - - select count(*) into @new_encounter_rows from flat_breast_cancer_screening_3; - - select @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; - select @total_rows_written; - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - encounter_id, - encounter_type, - encounter_datetime, - visit_id, - location_id, - t2.uuid as location_uuid, - gender, - age, - encounter_purpose, - other_encounter_purpose, - menstruation_before_12, - menses_stopped_permanently, - menses_stop_age, - hrt_use, - hrt_start_age, - hrt_end_age, - hrt_use_years, - hrt_type_used, - given_birth, - age_first_birth, - gravida, - parity, - cigarette_smoking, - cigarette_smoked_day, - tobacco_use, - tobacco_use_duration_yrs, - alcohol_drinking, - alcohol_type, - alcohol_use_period_yrs, - breast_complaints_3moths, - prev_exam_results, - fam_brca_history_bf50, - fam_brca_history_aft50, - fam_male_brca_history, - fam_ovarianca_history, - fam_relatedca_history, - fam_otherca_specify, - cur_physical_findings, - lymph_nodes_findings, - cur_screening_findings, - #cur_screening_findings_date, - patient_education, - patient_education_other, - referred_orderd, - referred_date, - procedure_done, - next_app_date, - cbe_imaging_concordance, - mammogram_results, - mammogram_workup_date, - date_patient_notified_of_mammogram_results, - Ultrasound_results, - ultrasound_workup_date, - date_patient_notified_of_ultrasound_results, - fna_results, - fna_tumor_size, - fna_degree_of_malignancy, - fna_workup_date, - date_patient_notified_of_fna_results, - breast_biopsy_results, - biopsy_tumor_size, - biopsy_degree_of_malignancy, - biopsy_workup_date, - date_patient_notified_of_biopsy_results, - date_patient_informed_and_referred_for_management, - screening_mode, - diagnosis, - diagnosis_date, - cancer_staging, - - prev_encounter_datetime_breast_cancer_screening, - next_encounter_datetime_breast_cancer_screening, - prev_encounter_type_breast_cancer_screening, - next_encounter_type_breast_cancer_screening, - prev_clinical_datetime_breast_cancer_screening, - next_clinical_datetime_breast_cancer_screening, - prev_clinical_location_id_breast_cancer_screening, - next_clinical_location_id_breast_cancer_screening, - prev_clinical_rtc_date_breast_cancer_screening, - next_clinical_rtc_date_breast_cancer_screening - - from flat_breast_cancer_screening_3 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_breast_cancer_screening_build_queue__0 t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #select @person_ids_count := (select count(*) from flat_breast_cancer_screening_build_queue_2); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count as remaining_in_build_queue; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - #select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select @person_ids_count as 'persons remaining', @cycle_length as 'Cycle time (s)', ceil(@person_ids_count / cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - -#select 1; - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening_v1_1.sql b/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening_v1_1.sql deleted file mode 100644 index 23fc386..0000000 --- a/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening_v1_1.sql +++ /dev/null @@ -1,1229 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_breast_cancer_screening_v1_1`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_breast_cancer_screening"; - set @query_type = query_type; -#set @query_type = "build"; - - set @total_rows_written = 0; - - set @encounter_types = "(86,145,146,160)"; - set @clinical_encounter_types = "(86,145,146,160)"; - set @non_clinical_encounter_types = "(-1)"; - set @other_encounter_types = "(-1)"; - - set @start = now(); - set @table_version = "flat_breast_cancer_screening_v1.1"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @boundary = "!!"; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - #delete from etl.flat_log where table_name like "%flat_breast_cancer_screening%"; - #drop table etl.flat_breast_cancer_screening; - - - #drop table if exists flat_breast_cancer_screening; - create table if not exists flat_breast_cancer_screening ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - encounter_id int, - encounter_type int, - encounter_datetime datetime, - visit_id int, - location_id int, - location_uuid varchar (100), - #location_name char (100), - gender char (100), - age int, - encounter_purpose int, - other_encounter_purpose varchar(1000), - menstruation_before_12 char, - menses_stopped_permanently char, - menses_stop_age int, - hrt_use char, - hrt_start_age int, - hrt_end_age int, - hrt_use_years int, - hrt_type_used int, - given_birth int, - age_first_birth int, - gravida int, - parity int, - cigarette_smoking int, - cigarette_smoked_day int, - tobacco_use int, - tobacco_use_duration_yrs int, - alcohol_drinking int, - alcohol_type int, - alcohol_use_period_yrs int, - breast_complaints_3moths int, - prev_exam_results int, - fam_brca_history_bf50 int, - fam_brca_history_aft50 int, - fam_male_brca_history int, - fam_ovarianca_history int, - fam_relatedca_history int, - fam_otherca_specify int, - cur_physical_findings int, - lymph_nodes_findings int, - cur_screening_findings int, - #cur_screening_findings_date int, - patient_education int, - patient_education_other varchar(1000), - referred_orderd int, - referred_date datetime, - procedure_done int, - next_app_date datetime, - - cbe_imaging_concordance int, - mammogram_results int, - mammogram_workup_date datetime, - date_patient_notified_of_mammogram_results datetime, - Ultrasound_results int, - ultrasound_workup_date datetime, - date_patient_notified_of_ultrasound_results datetime, - fna_results int, - fna_tumor_size int, - fna_degree_of_malignancy int, - fna_workup_date datetime, - date_patient_notified_of_fna_results datetime, - breast_biopsy_results int, - biopsy_tumor_size int, - biopsy_degree_of_malignancy int, - biopsy_workup_date datetime, - date_patient_notified_of_biopsy_results datetime, - date_patient_informed_and_referred_for_management datetime, - screening_mode int, - diagnosis int, - diagnosis_date datetime, - cancer_staging int, - hiv_status INT, - - prev_encounter_datetime_breast_cancer_screening datetime, - next_encounter_datetime_breast_cancer_screening datetime, - prev_encounter_type_breast_cancer_screening mediumint, - next_encounter_type_breast_cancer_screening mediumint, - prev_clinical_datetime_breast_cancer_screening datetime, - next_clinical_datetime_breast_cancer_screening datetime, - prev_clinical_location_id_breast_cancer_screening mediumint, - next_clinical_location_id_breast_cancer_screening mediumint, - prev_clinical_rtc_date_breast_cancer_screening datetime, - next_clinical_rtc_date_breast_cancer_screening datetime, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,next_app_date), - index location_uuid_rtc_date (location_uuid,next_app_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_breast_cancer_screening), - index encounter_type (encounter_type), - index date_created (date_created) - - ); - - - - if(@query_type="build") then - select 'BUILDING..........................................'; - -#set @write_table = concat("flat_breast_cancer_screening_temp_",1); -#set @queue_table = concat("flat_breast_cancer_screening_build_queue_",1); - - set @write_table = concat("flat_breast_cancer_screening_temp_",queue_number); - set @queue_table = concat("flat_breast_cancer_screening_build_queue_",queue_number); - - -#drop table if exists flat_breast_cancer_screening_temp_1; - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_breast_cancer_screening_build_queue limit ', queue_size, ');'); -#SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_breast_cancer_screening_build_queue limit 500);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_breast_cancer_screening_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from flat_breast_cancer_screening_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_breast_cancer_screening"; - set @queue_table = "flat_breast_cancer_screening_sync_queue"; - create table if not exists flat_breast_cancer_screening_sync_queue (person_id int primary key); - - set @last_update = null; - - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - -#select max(date_created) into @last_update from etl.flat_log where table_name like "%breast_cancer_screening%"; - -#select @last_update; -select "Finding patients in amrs.encounters..."; - - replace into flat_breast_cancer_screening_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - -select "Finding patients in flat_obs..."; - - replace into flat_breast_cancer_screening_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - -select "Finding patients in flat_lab_obs..."; - replace into flat_breast_cancer_screening_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - -select "Finding patients in flat_orders..."; - - replace into flat_breast_cancer_screening_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_breast_cancer_screening_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_breast_cancer_screening_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - # Remove test patients - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - select @person_ids_count as 'num patients to update'; - - - #delete t1 from flat_breast_cancer_screening t1 join @queue_table t2 using (person_id); - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists flat_breast_cancer_screening_build_queue__0; - - #create temporary table flat_breast_cancer_screening_build_queue__0 (select * from flat_breast_cancer_screening_build_queue_2 limit 5000); #TODO - change this when data_fetch_size changes - -#SET @dyn_sql=CONCAT('create temporary table flat_breast_cancer_screening_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit 100);'); - SET @dyn_sql=CONCAT('create temporary table flat_breast_cancer_screening_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - drop temporary table if exists flat_breast_cancer_screening_0a; - SET @dyn_sql = CONCAT( - 'create temporary table flat_breast_cancer_screening_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - case - when t1.encounter_type in ',@clinical_encounter_types,' then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in ',@non_clinical_encounter_types,' then 20 - when t1.encounter_type in ',@clinical_encounter_types,' then 10 - when t1.encounter_type in', @other_encounter_types, ' then 5 - else 1 - end as encounter_type_sort_index, - t2.orders - from etl.flat_obs t1 - join flat_breast_cancer_screening_build_queue__0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_id) - where t1.encounter_type in ',@encounter_types,');'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - - insert into flat_breast_cancer_screening_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, #t1.location_id, - t1.obs, - null, #obs_datetimes - # in any visit, there many be multiple encounters. for this dataset, we want to include only clinical encounters (e.g. not lab or triage visit) - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_breast_cancer_screening_build_queue__0 t0 using (person_id) - ); - - - drop temporary table if exists flat_breast_cancer_screening_0; - create temporary table flat_breast_cancer_screening_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_breast_cancer_screening_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @encounter_purpose = null; - set @other_encounter_purpose = null; - set @menstruation_before_12 = null; - set @menses_stopped_permanently = null; - set @menses_stop_age = null; - set @hrt_use = null; - set @hrt_start_age = null; - set @hrt_end_age = null; - set @hrt_use_years = null; - set @hrt_type_used =null; - set @given_birth = null; - set @age_first_birth = null; - set @gravida = null; - set @parity = null; - set @cigarette_smoking = null; - set @cigarette_smoked_day =null; - set @tobacco_use =null; - set @tobacco_use_duration_yrs = null; - set @alcohol_drinking =null; - set @alcohol_type = null; - set @alcohol_use_period_yrs = null; - set @breast_complaints_3moths = null; - set @prev_exam_results = null; - set @fam_brca_history_bf50 = null; - set @fam_brca_history_aft50 = null; - set @fam_male_brca_history = null; - set @fam_ovarianca_history =null; - set @fam_relatedca_history =null; - set @fam_otherca_specify = null; - set @cur_physical_findings = null; - set @lymph_nodes_findings =null; - set @cur_screening_findings = null; - #set @cur_screening_findings_date = null; - set @patient_education = null; - set @patient_education_other = null; - set @referred_orderd = null; - set @procedure_done =null; - set @referred_date =null; - set @next_app_date = null; - - set @cbe_imaging_concordance =null; - set @mammogram_results =null; - set @mammogram_workup_date =null; - set @date_patient_notified_of_mammogram_results =null; - set @Ultrasound_results =null; - set @ultrasound_workup_date =null; - set @date_patient_notified_of_ultrasound_results =null; - set @fna_results =null; - set @fna_tumor_size =null; - set @fna_degree_of_malignancy =null; - set @fna_workup_date =null; - set @date_patient_notified_of_fna_results =null; - set @breast_biopsy_results =null; - set @biopsy_tumor_size =null; - set @biopsy_degree_of_malignancy =null; - set @biopsy_workup_date =null; - set @date_patient_notified_of_biopsy_results =null; - set @date_patient_informed_and_referred_for_management =null; - set @screening_mode =null; - set @diagnosis =null; - set @diagnosis_date =null; - set @cancer_staging =null; - set @hiv_status = null; - - drop temporary table if exists flat_breast_cancer_screening_1; - create temporary table flat_breast_cancer_screening_1 #(index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - t1.encounter_id, - t1.encounter_type, - t1.encounter_datetime, - t1.visit_id, - #t4.name as location_name, - t1.location_id, - t1.is_clinical_encounter, - p.gender, - p.death_date, - - - case - when timestampdiff(year,birthdate,curdate()) > 0 then round(timestampdiff(year,birthdate,curdate()),0) - else round(timestampdiff(month,birthdate,curdate())/12,2) - end as age, - - case - when obs regexp "!!1834=9651!!" then @encounter_purpose := 1 - when obs regexp "!!1834=1154!!" then @encounter_purpose := 2 - when obs regexp "!!1834=1246!!" then @encounter_purpose := 3 - when obs regexp "!!1834=5622!!" then @encounter_purpose := 4 - else @encounter_purpose := null - end as encounter_purpose, - - case - when obs regexp "!!1915=" then @other_encounter_purpose := GetValues(obs,1915) - else @other_encounter_purpose := null - end as other_encounter_purpose, - - case - when obs regexp "!!9560=1065!!" then @menstruation_before_12 := 1 - when obs regexp "!!9560=1066!!" then @menstruation_before_12 := 0 - else @menstruation_before_12 := null - end as menstruation_before_12, - - case - when obs regexp "!!9561=1065!!" then @menstruation_before_12 := 1 - when obs regexp "!!9561=1066!!" then @menstruation_before_12 := 0 - when obs regexp "!!9561=9568!!" then @menstruation_before_12 := 2 - else @menses_stopped_permanently := null - end as menses_stopped_permanently, - - - case - when obs regexp "!!9562=[0-9]" and t1.encounter_type = @lab_encounter_type then @menses_stop_age:=cast(GetValues(obs,9562) as unsigned) - when @prev_id=@cur_id then@menses_stop_age - else @menses_stop_age:=null - end as menses_stop_age, - - case - when obs regexp "!!9626=1065!!" then @hrt_use := 1 - when obs regexp "!!9626=1066!!" then @hrt_use := 0 - when obs regexp "!!9626=9568!!" then @hrt_use := 2 - else @hrt_use := null - end as hrt_use, - - - case - when obs regexp "!!9627=[0-9]" then @hrt_start_age:=cast(GetValues(obs,9627) as unsigned) - else @hrt_start_age := null - end as hrt_start_age, - - - case - when obs regexp "!!9723=[0-9]" then @hrt_end_age:=cast(GetValues(obs,9723) as unsigned) - else @hrt_end_age := null - end as hrt_end_age, - - - case - when obs regexp "!!97629=[0-9]" then @hrt_use_years:=cast(GetValues(obs,9629) as unsigned) - else @hrt_use_years := null - end as hrt_use_years, - - - case - when obs regexp "!!9630=9573!!" then @hrt_type_used := 1 - when obs regexp "!!9630=6217!!" then @hrt_type_used := 2 - when obs regexp "!!9630=6218!!" then @hrt_type_used := 3 - else @hrt_type_used := null - end as hrt_type_used, - - case - when obs regexp "!!9563=1065!!" then @given_birth := 1 - when obs regexp "!!9563=1066!!" then @given_birth := 0 - else @given_birth := null - end as given_birth, - - - case - when obs regexp "!!5574=[0-9]" then @age_first_birth:=cast(GetValues(obs,5574) as unsigned) - else @age_first_birth := null - end as age_first_birth, - - - case - when obs regexp "!!5624=[0-9]" then @gravida:= cast(GetValues(obs,5624) as unsigned) - else @gravida := null - end as gravida, - - - case - when obs regexp "!!1053=[0-9]" then @parity:=cast(GetValues(obs,1053) as unsigned) - else @parity := null - end as parity, - - - case - when obs regexp "!!9333=" then @cigarette_smoking := GetValues(obs,9333) - else @cigarette_smoking := null - end as cigarette_smoking, - - - case - when obs regexp "!!2069=[0-9]" then @cigarette_smoked_day:=cast(GetValues(obs,2069) as unsigned) - else @cigarette_smoked_day := null - end as cigarette_smoked_day, - - - case - when obs regexp "!!7973=1065!!" then @tobacco_use := 1 - when obs regexp "!!7973=1066!!" then @tobacco_use := 0 - when obs regexp "!!7973=1679!!" then @tobacco_use := 2 - else @hrt_use := null - end as tobacco_use, - - - case - when obs regexp "!!8144=[0-9]" then @tobacco_use_duration_yrs:= cast(GetValues(obs,8144) as unsigned) - else @tobacco_use_duration_yrs := null - end as tobacco_use_duration_yrs, - - - case - when obs regexp "!!1684=1065!!" then @alcohol_drinking := 1 - when obs regexp "!!1684=1066!!" then @alcohol_drinking := 0 - when obs regexp "!!1684=1679!!" then @alcohol_drinking := 2 - else @alcohol_drinking := null - end as alcohol_drinking, - - - case - when obs regexp "!!1685=1682!!" then @alcohol_type := 1 - when obs regexp "!!1685=1681!!" then @alcohol_type := 2 - when obs regexp "!!1685=1680!!" then @alcohol_type := 3 - when obs regexp "!!1685=1683!!" then @alcohol_type := 4 - when obs regexp "!!1685=2059!!" then @alcohol_type := 5 - when obs regexp "!!1685=5622!!" then @alcohol_type := 6 - else @hrt_type_used := null - end as alcohol_type, - - - case - when obs regexp "!!8170=[0-9]" then @alcohol_use_period_yrs:= cast(GetValues(obs,8170) as unsigned) - else @alcohol_use_period_yrs := null - end as alcohol_use_period_yrs, - - - case - when obs regexp "!!9553=5006!!" then @breast_complaints_3moths := 1 - when obs regexp "!!9553=1068!!" then @breast_complaints_3moths := 2 - else @breast_complaints_3moths := null - end as breast_complaints_3moths, - - - case - when obs regexp "!!9694=1115!!" then @prev_exam_results := 1 - when obs regexp "!!9694=1116!!" then @prev_exam_results := 0 - when obs regexp "!!9694=1067!!" then @prev_exam_results := 2 - else @prev_exam_results := null - end as prev_exam_results, - - - case - when obs regexp "!!9631=1672!!" then @fam_brca_history_bf50 := 1 - when obs regexp "!!9631=1107!!" then @fam_brca_history_bf50 := 0 - when obs regexp "!!9631=978!!" then @fam_brca_history_bf50 := 2 - when obs regexp "!!9631=972!!" then @fam_brca_history_bf50 := 3 - when obs regexp "!!9631=1671!!" then @fam_brca_history_bf50 := 4 - when obs regexp "!!9631=1393!!" then @fam_brca_history_bf50 := 5 - when obs regexp "!!9631=1392!!" then @fam_brca_history_bf50 := 6 - when obs regexp "!!9631=1395!!" then @fam_brca_history_bf50 := 7 - when obs regexp "!!9631=1394!!" then @fam_brca_history_bf50 := 8 - else @fam_brca_history_bf50 := null - end as fam_brca_history_bf50, - - - case - when obs regexp "!!9632=978!!" then @fam_brca_history_aft50 := 1 - when obs regexp "!!9632=1672!!" then @fam_brca_history_aft50 := 2 - when obs regexp "!!9632=972!!" then @fam_brca_history_aft50 := 3 - when obs regexp "!!9632=1671!!" then @fam_brca_history_aft50 := 4 - when obs regexp "!!9632=1393!!" then @fam_brca_history_aft50 := 5 - when obs regexp "!!9632=1392!!" then @fam_brca_history_aft50 := 6 - when obs regexp "!!9632=1395!!" then @fam_brca_history_aft50 := 7 - when obs regexp "!!9632=1394!!" then @fam_brca_history_aft50 := 8 - else @fam_brca_history_aft50 := null - end as fam_brca_history_aft50, - - - case - when obs regexp "!!9633=978!!" then @fam_male_brca_history := 1 - when obs regexp "!!9633=1672!!" then @fam_male_brca_history := 2 - when obs regexp "!!9633=972!!" then @fam_male_brca_history := 3 - when obs regexp "!!9633=1671!!" then @fam_male_brca_history := 4 - when obs regexp "!!9633=1393!!" then @fam_male_brca_history := 5 - when obs regexp "!!9633=1392!!" then @fam_male_brca_history := 6 - when obs regexp "!!9633=1395!!" then @fam_male_brca_history := 7 - when obs regexp "!!9633=1394!!" then @fam_male_brca_history := 8 - else @fam_male_brca_history := null - end as fam_male_brca_history, - - - case - when obs regexp "!!9634=978!!" then @fam_ovarianca_history := 1 - when obs regexp "!!9634=1672!!" then @fam_ovarianca_history := 2 - when obs regexp "!!9634=972!!" then @fam_ovarianca_history := 3 - when obs regexp "!!9634=1671!!" then @fam_ovarianca_history := 4 - when obs regexp "!!9634=1393!!" then @fam_ovarianca_history := 5 - when obs regexp "!!9634=1392!!" then @fam_ovarianca_history := 6 - when obs regexp "!!9634=1395!!" then @fam_ovarianca_history := 7 - when obs regexp "!!9634=1394!!" then @fam_ovarianca_history := 8 - else @fam_ovarianca_history := null - end as fam_ovarianca_history, - - - case - when obs regexp "!!9635=978!!" then @fam_relatedca_history:= 1 - when obs regexp "!!9635=1672!!" then @fam_relatedca_history := 2 - when obs regexp "!!9635=972!!" then @fam_relatedca_history := 3 - when obs regexp "!!9635=1671!!" then @fam_relatedca_history := 4 - when obs regexp "!!9635=1393!!" then @fam_relatedca_history := 5 - when obs regexp "!!9635=1392!!" then @fam_relatedca_history := 6 - when obs regexp "!!9635=1395!!" then @fam_relatedca_history := 7 - when obs regexp "!!9635=1394!!" then @fam_relatedca_history := 8 - else @fam_relatedca_history := null - end as fam_relatedca_history, - - - case - when obs regexp "!!7176=6529!!" then @fam_otherca_specify := 1 - when obs regexp "!!7176=9636!!" then @fam_otherca_specify := 2 - when obs regexp "!!7176=9637!!" then @fam_otherca_specify := 3 - when obs regexp "!!7176=6485!!" then @fam_otherca_specify := 4 - when obs regexp "!!7176=9638!!" then @fam_otherca_specify := 5 - when obs regexp "!!7176=9639!!" then @fam_otherca_specify := 6 - when obs regexp "!!7176=6522!!" then @fam_otherca_specify := 5 - when obs regexp "!!7176=216!!" then @fam_otherca_specify := 6 - else @fam_otherca_specify := null - end as fam_otherca_specify, - - - case - when obs regexp "!!6251=1115!!" then @cur_physical_findings:= 0 - when obs regexp "!!6251=115!!" then @cur_physical_findings := 1 - when obs regexp "!!6251=6250!!" then @cur_physical_findings := 2 - when obs regexp "!!6251=6249!!" then @cur_physical_findings := 3 - when obs regexp "!!6251=5622!!" then @cur_physical_findings := 4 - when obs regexp "!!6251=582!!" then @cur_physical_findings := 5 - when obs regexp "!!6251=6493!!" then @cur_physical_findings := 6 - when obs regexp "!!6251=6499!!" then @cur_physical_findings := 7 - when obs regexp "!!6251=1118!!" then @cur_physical_findings := 8 - when obs regexp "!!6251=1481!!" then @cur_physical_findings := 9 - when obs regexp "!!6251=6729!!" then @cur_physical_findings := 10 - when obs regexp "!!6251=1116!!" then @cur_physical_findings := 11 - when obs regexp "!!6251=8188!!" then @cur_physical_findings := 12 - when obs regexp "!!6251=8189!!" then @cur_physical_findings := 13 - when obs regexp "!!6251=1067!!" then @cur_physical_findings := 14 - when obs regexp "!!6251=9689!!" then @cur_physical_findings := 15 - when obs regexp "!!6251=9690!!" then @cur_physical_findings := 16 - when obs regexp "!!6251=9687!!" then @cur_physical_findings := 17 - when obs regexp "!!6251=9688!!" then @cur_physical_findings := 18 - when obs regexp "!!6251=5313!!" then @cur_physical_findings := 19 - when obs regexp "!!6251=9691!!" then @cur_physical_findings := 20 - else @cur_physical_findings := null - end as cur_physical_findings, - - - case - when obs regexp "!!1121=1115!!" then @lymph_nodes_findings := 1 - when obs regexp "!!1121=161!!" then @lymph_nodes_findings := 2 - when obs regexp "!!1121=9675!!" then @lymph_nodes_findings:= 3 - when obs regexp "!!1121=9676!!" then @lymph_nodes_findings:= 4 - else @lymph_nodes_findings := null - end as lymph_nodes_findings, - - - case - when obs regexp "!!9748=1115!!" then @cur_screening_findings := 1 - when obs regexp "!!9748=9691!!" then @cur_screening_findings := 3 - when obs regexp "!!9748=1116!!" then @cur_screening_findings := 2 - else @cur_screening_findings := null - end as cur_screening_findings, - - - case - when obs regexp "!!6327=9651!!" then @patient_education := 1 - when obs regexp "!!6327=2345!!" then @patient_education := 2 - when obs regexp "!!6327=9692!!" then @patient_education:= 3 - when obs regexp "!!6327=5622!!" then @patient_education:= 4 - else @patient_education := null - end as patient_education, - - - case - when obs regexp "!!1915=" then @patient_education_other := GetValues(obs,1915) - else @patient_education_other := null - end as patient_education_other, - - - case - when obs regexp "!!1272=1107!!" then @referred_orderd := 0 - when obs regexp "!!1272=1496!!" then @referred_orderd:= 1 - else @referred_orderd := null - end as referred_orderd, - - case - when obs regexp "!!1272=9596!!" then @procedure_done := 1 - when obs regexp "!!1272=9595!!" then @procedure_done:= 2 - when obs regexp "!!1272=6510!!" then @procedure_done := 3 - when obs regexp "!!1272=7190!!" then @procedure_done:= 4 - when obs regexp "!!1272=6511!!" then @procedure_done := 5 - when obs regexp "!!1272=9997!!" then @procedure_done:= 6 - else @procedure_done := null - end as procedure_done, - - case - when obs regexp "!!9158=" then @referred_date := GetValues(obs,9158) - else @referred_date := null - end as referred_date, - - - case - when obs regexp "!!5096=" then @next_app_date := GetValues(obs,5096) - else @next_app_date := null - end as next_app_date, - - - case - when obs regexp "!!9702=9703!!" then @cbe_imaging_concordance := 1 - when obs regexp "!!9702=9704!!" then @cbe_imaging_concordance := 2 - when obs regexp "!!9702=1175!!" then @cbe_imaging_concordance := 3 - else @cbe_imaging_concordance := null - end as cbe_imaging_concordance, - - - case - when obs regexp "!!9595=1115!!" then @mammogram_results := 1 - when obs regexp "!!9595=1116!!" then @mammogram_results := 2 - when obs regexp "!!9595=1118!!" then @mammogram_results := 3 - when obs regexp "!!9595=1138!!" then @mammogram_results := 4 - when obs regexp "!!9595=1267!!" then @mammogram_results := 5 - when obs regexp "!!9595=1067!!" then @mammogram_results := 6 - else @mammogram_results := null - end as mammogram_results, - - - case - when obs regexp "!!9708=" then @mammogram_workup_date := GetValues(obs,9708) - else @mammogram_workup_date := null - end as mammogram_workup_date, - - - case - when obs regexp "!!9705=" then @date_patient_notified_of_mammogram_results := GetValues(obs,9705) - else @date_patient_notified_of_mammogram_results := null - end as date_patient_notified_of_mammogram_results, - - case - when obs regexp "!!9596=1115!!" then @Ultrasound_results := 1 - when obs regexp "!!9596=1116!!" then @Ultrasound_results := 2 - when obs regexp "!!9596=1067!!" then @Ultrasound_results := 3 - when obs regexp "!!9596=1118!!" then @Ultrasound_results := 4 - else @Ultrasound_results := null - end as Ultrasound_results, - - case - when obs regexp "!!9708=" then @ultrasound_workup_date := GetValues(obs,9708) - else @ultrasound_workup_date := null - end as ultrasound_workup_date, - - - case - when obs regexp "!!10047=" then @date_patient_notified_of_ultrasound_results := GetValues(obs,10047) - else @date_patient_notified_of_ultrasound_results := null - end as date_patient_notified_of_ultrasound_results, - - - case - when obs regexp "!!10051=9691!!" then @fna_results := 1 - when obs regexp "!!10051=10052!!" then @fna_results := 2 - when obs regexp "!!9596=1118!!" then @fna_results := 3 - else @fna_results := null - end as fna_results, - - - case - when obs regexp "!!10053=[0-9]" then @fna_tumor_size:= cast(GetValues(obs,10053) as unsigned) - else @fna_tumor_size := null - end as fna_tumor_size, - - - case - when obs regexp "!!10054=10055!!" then @fna_degree_of_malignancy := 1 - when obs regexp "!!10054=10056!!" then @fna_degree_of_malignancy := 2 - else @fna_degree_of_malignancy := null - end as fna_degree_of_malignancy, - - - case - when obs regexp "!!10057=" then @fna_workup_date := GetValues(obs,10057) - else @fna_workup_date := null - end as fna_workup_date, - - - case - when obs regexp "!!10059=" then @date_patient_notified_of_fna_results := GetValues(obs,10059) - else @date_patient_notified_of_fna_results := null - end as date_patient_notified_of_fna_results, - - - case - when obs regexp "!!8184=1115!!" then @breast_biopsy_results := 1 - when obs regexp "!!8184=1116!!" then @breast_biopsy_results := 2 - when obs regexp "!!8184=1118!!" then @breast_biopsy_results := 3 - when obs regexp "!!8184=1067!!" then @breast_biopsy_results := 4 - when obs regexp "!!8184=9691!!" then @breast_biopsy_results := 5 - when obs regexp "!!8184=10052!!" then @breast_biopsy_results := 6 - else @breast_biopsy_results := null - end as breast_biopsy_results, - - - case - when obs regexp "!!10053=[0-9]" then @biopsy_tumor_size:= cast(GetValues(obs,10053) as unsigned) - else @biopsy_tumor_size := null - end as biopsy_tumor_size, - - - case - when obs regexp "!!10054=10055!!" then @biopsy_degree_of_malignancy := 1 - when obs regexp "!!10054=10056!!" then @biopsy_degree_of_malignancy := 2 - else @biopsy_degree_of_malignancy := null - end as biopsy_degree_of_malignancy, - - - case - when obs regexp "!!10060=" then @biopsy_workup_date := GetValues(obs,10060) - else @biopsy_workup_date := null - end as biopsy_workup_date, - - - case - when obs regexp "!!10061=" then @date_patient_notified_of_biopsy_resultse := GetValues(obs,10061) - else @date_patient_notified_of_biopsy_results := null - end as date_patient_notified_of_biopsy_results, - - - case - when obs regexp "!!9706=" then @date_patient_informed_and_referred_for_management := GetValues(obs,10061) - else @date_patient_informed_and_referred_for_management := null - end as date_patient_informed_and_referred_for_management, - - - case - when obs regexp "!!10068=9595!!" then @screening_mode := 1 - when obs regexp "!!10068=10067!!" then @screening_mode := 2 - when obs regexp "!!10068=9596!!" then @screening_mode := 3 - else @screening_mode := null - end as screening_mode, - - - case - when obs regexp "!!6042=" then @diagnosis := GetValues(obs,6042) - else @diagnosis := null - end as diagnosis, - - /**case - #when obs regexp "!!9728=" then @diagnosis_date := replace(replace((substring_index(substring(obs,locate("!!9728=",obs)),@sep,1)),"!!9728=",""),"!!","") - when obs regexp "!!9728=" then @diagnosis_date := encounter_datetime - else @diagnosis_date := null - end as diagnosis_date**/ - - - case - when obs regexp "!!9728=" then @diagnosis_date := GetValues(obs,9728) - else @diagnosis_date := null - end as diagnosis_date, - - - case - when obs regexp "!!9868=9852!!" then @cancer_staging := 1 - when obs regexp "!!9868=9856!!" then @cancer_staging := 2 - when obs regexp "!!9868=9860!!" then @cancer_staging := 3 - when obs regexp "!!9868=9864!!" then @cancer_staging := 4 - else @cancer_staging := null - end as cancer_staging, - - case - when obs regexp "!!6709=664!!" then @hiv_status := 1 - when obs regexp "!!6709=703!!" then @hiv_status := 2 - when obs regexp "!!6709=1067!!" then @hiv_status := 3 - else @hiv_status := null - end as hiv_status - - from flat_breast_cancer_screening_0 t1 - join amrs.person p using (person_id) - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - );# limit 100; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_breast_cancer_screening_1 drop prev_id, drop cur_id; - - drop table if exists flat_breast_cancer_screening_2; - create temporary table flat_breast_cancer_screening_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_breast_cancer_screening, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_breast_cancer_screening, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_breast_cancer_screening, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date - - from flat_breast_cancer_screening_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_breast_cancer_screening_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_breast_cancer_screening_3; - create temporary table flat_breast_cancer_screening_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_breast_cancer_screening, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_breast_cancer_screening, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_breast_cancer_screening, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_breast_cancer_screening_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - - select count(*) into @new_encounter_rows from flat_breast_cancer_screening_3; - - select @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; - select @total_rows_written; - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - encounter_id, - encounter_type, - encounter_datetime, - visit_id, - location_id, - t2.uuid as location_uuid, - gender, - age, - encounter_purpose, - other_encounter_purpose, - menstruation_before_12, - menses_stopped_permanently, - menses_stop_age, - hrt_use, - hrt_start_age, - hrt_end_age, - hrt_use_years, - hrt_type_used, - given_birth, - age_first_birth, - gravida, - parity, - cigarette_smoking, - cigarette_smoked_day, - tobacco_use, - tobacco_use_duration_yrs, - alcohol_drinking, - alcohol_type, - alcohol_use_period_yrs, - breast_complaints_3moths, - prev_exam_results, - fam_brca_history_bf50, - fam_brca_history_aft50, - fam_male_brca_history, - fam_ovarianca_history, - fam_relatedca_history, - fam_otherca_specify, - cur_physical_findings, - lymph_nodes_findings, - cur_screening_findings, - #cur_screening_findings_date, - patient_education, - patient_education_other, - referred_orderd, - referred_date, - procedure_done, - next_app_date, - cbe_imaging_concordance, - mammogram_results, - mammogram_workup_date, - date_patient_notified_of_mammogram_results, - Ultrasound_results, - ultrasound_workup_date, - date_patient_notified_of_ultrasound_results, - fna_results, - fna_tumor_size, - fna_degree_of_malignancy, - fna_workup_date, - date_patient_notified_of_fna_results, - breast_biopsy_results, - biopsy_tumor_size, - biopsy_degree_of_malignancy, - biopsy_workup_date, - date_patient_notified_of_biopsy_results, - date_patient_informed_and_referred_for_management, - screening_mode, - diagnosis, - diagnosis_date, - cancer_staging, - hiv_status, - - prev_encounter_datetime_breast_cancer_screening, - next_encounter_datetime_breast_cancer_screening, - prev_encounter_type_breast_cancer_screening, - next_encounter_type_breast_cancer_screening, - prev_clinical_datetime_breast_cancer_screening, - next_clinical_datetime_breast_cancer_screening, - prev_clinical_location_id_breast_cancer_screening, - next_clinical_location_id_breast_cancer_screening, - prev_clinical_rtc_date_breast_cancer_screening, - next_clinical_rtc_date_breast_cancer_screening - - from flat_breast_cancer_screening_3 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_breast_cancer_screening_build_queue__0 t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #select @person_ids_count := (select count(*) from flat_breast_cancer_screening_build_queue_2); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count as remaining_in_build_queue; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - #select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select @person_ids_count as 'persons remaining', @cycle_length as 'Cycle time (s)', ceil(@person_ids_count / cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - -#select 1; - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening_v1_2.sql b/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening_v1_2.sql deleted file mode 100644 index cb493cd..0000000 --- a/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening_v1_2.sql +++ /dev/null @@ -1,1227 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_breast_cancer_screening_v1_2`(IN query_type VARCHAR(50), IN queue_number INT, IN queue_size INT, IN cycle_size INT) -BEGIN - SET @primary_table := "flat_breast_cancer_screening"; - SET @query_type := query_type; - - SET @total_rows_written := 0; - - SET @encounter_types = "(86, 145, 146, 160)"; - SET @clinical_encounter_types = "(86, 145, 146, 160)"; - SET @non_clinical_encounter_types = "(-1)"; - SET @other_encounter_types = "(-1)"; - - SET @start := NOW(); - SET @table_version := "flat_breast_cancer_screening_v1.2"; - - SET session sort_buffer_size := 512000000; - - SET @sep := " ## "; - SET @boundary := "!!"; - SET @last_date_created := (SELECT MAX(max_date_created) FROM etl.flat_obs); - - CREATE TABLE IF NOT EXISTS flat_breast_cancer_screening ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - encounter_id int, - encounter_type int, - encounter_datetime datetime, - visit_id int, - location_id int, - location_uuid varchar (100), - gender char (100), - age int, - encounter_purpose int, - other_encounter_purpose varchar(1000), - menstruation_before_12 char, - menses_stopped_permanently char, - menses_stop_age int, - hrt_use char, - hrt_start_age int, - hrt_end_age int, - hrt_use_years int, - hrt_type_used int, - given_birth int, - age_first_birth int, - gravida int, - parity int, - cigarette_smoking int, - cigarette_smoked_day int, - tobacco_use int, - tobacco_use_duration_yrs int, - alcohol_drinking int, - alcohol_type int, - alcohol_use_period_yrs int, - breast_complaints_3months int, - breast_mass_location tinyint, - nipple_discharge_location tinyint, - nipple_retraction_location tinyint, - breast_erythrema_location tinyint, - breast_rash_location tinyint, - breast_pain_location tinyint, - other_changes_location tinyint, - history_of_mammogram tinyint, - mammogram_results tinyint, - breast_ultrasound_history tinyint, - breast_ultrasound_result tinyint, - history_of_breast_biopsy tinyint, - breast_biopsy_results tinyint, - number_of_biopsies tinyint, - biopsy_type tinyint, - prev_exam_results int, - fam_brca_history_bf50 int, - fam_brca_history_aft50 int, - fam_male_brca_history int, - fam_ovarianca_history int, - fam_relatedca_history int, - fam_otherca_specify int, - cur_physical_findings int, - lymph_nodes_findings int, - cur_screening_findings int, - #cur_screening_findings_date int, - patient_education int, - patient_education_other varchar(1000), - referrals_ordered int, - referred_date datetime, - procedure_done int, - next_app_date datetime, - cbe_imaging_concordance int, - mammogram_workup_date datetime, - date_patient_notified_of_mammogram_results datetime, - ultrasound_results int, - ultrasound_workup_date datetime, - date_patient_notified_of_ultrasound_results datetime, - fna_results int, - fna_tumor_size int, - fna_degree_of_malignancy int, - fna_workup_date datetime, - date_patient_notified_of_fna_results datetime, - biopsy_tumor_size int, - biopsy_degree_of_malignancy int, - biopsy_workup_date datetime, - date_patient_notified_of_biopsy_results datetime, - biopsy_description varchar(1000), - diagnosis_date datetime, - date_patient_informed_and_referred_for_management datetime, - screening_mode int, - diagnosis int, - cancer_staging int, - hiv_status int, - prev_encounter_datetime_breast_cancer_screening datetime, - next_encounter_datetime_breast_cancer_screening datetime, - prev_encounter_type_breast_cancer_screening mediumint, - next_encounter_type_breast_cancer_screening mediumint, - prev_clinical_datetime_breast_cancer_screening datetime, - next_clinical_datetime_breast_cancer_screening datetime, - prev_clinical_location_id_breast_cancer_screening mediumint, - next_clinical_location_id_breast_cancer_screening mediumint, - prev_clinical_rtc_date_breast_cancer_screening datetime, - next_clinical_rtc_date_breast_cancer_screening datetime, - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,next_app_date), - index location_uuid_rtc_date (location_uuid,next_app_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_breast_cancer_screening), - index encounter_type (encounter_type), - index date_created (date_created) - ); - - -- Build step - IF (@query_type = "build") THEN - SELECT 'BUILDING..........................................'; - - SET @write_table := CONCAT('flat_breast_cancer_screening_temp_', queue_number); - SET @queue_table := CONCAT('flat_breast_cancer_screening_build_queue_', queue_number); - - SET @dyn_sql := CONCAT('CREATE TABLE IF NOT EXISTS ', @write_table, ' LIKE ', @primary_table); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql := CONCAT('CREATE TABLE IF NOT EXISTS ', @queue_table, ' (SELECT * FROM flat_breast_cancer_screening_build_queue LIMIT ', queue_size, ');'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql := CONCAT('DELETE t1 FROM flat_breast_cancer_screening_build_queue t1 join ', @queue_table, ' t2 using (person_id);'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - END IF; - - -- Sync step - IF (@query_type = 'sync') THEN - SELECT 'SYNCING.....................................'; - - SET @write_table := 'flat_breast_cancer_screening'; - SET @queue_table := 'flat_breast_cancer_screening_sync_queue'; - - CREATE TABLE IF NOT EXISTS flat_breast_cancer_screening_sync_queue (person_id INT PRIMARY KEY); - - SET @last_update := null; - - SELECT MAX(date_updated) INTO @last_update FROM etl.flat_log WHERE table_name = @table_version; - - SELECT 'Finding patients in amrs.encounters...'; - REPLACE INTO flat_breast_cancer_screening_sync_queue - (SELECT DISTINCT - patient_id - FROM - amrs.encounter - WHERE - date_changed > @last_update - ); - - SELECT 'Finding patients in flat_obs...'; - REPLACE INTO flat_breast_cancer_screening_sync_queue - (SELECT DISTINCT - person_id - FROM - etl.flat_obs - WHERE - max_date_created > @last_update - ); - - SELECT 'Finding patients in flat_lab_obs...'; - REPLACE INTO flat_breast_cancer_screening_sync_queue - (SELECT DISTINCT - person_id - FROM - etl.flat_lab_obs - WHERE - max_date_created > @last_update - ); - - SELECT 'Finding patients in flat_orders...'; - REPLACE INTO flat_breast_cancer_screening_sync_queue - (SELECT DISTINCT - person_id - FROM - etl.flat_orders - WHERE - max_date_created > @last_update - ); - - REPLACE INTO flat_breast_cancer_screening_sync_queue - (SELECT - person_id - FROM - amrs.person - WHERE - date_voided > @last_update - ); - - REPLACE INTO flat_breast_cancer_screening_sync_queue - (SELECT - person_id - FROM - amrs.person - WHERE - date_changed > @last_update - ); - END IF; - - -- Remove test patients - SET @dyn_sql := CONCAT('DELETE t1 FROM ', @queue_table, ' t1 - JOIN amrs.person_attribute t2 USING (person_id) - WHERE t2.person_attribute_type_id = 28 AND value = "true" AND voided = 0'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('SELECT COUNT(*) INTO @person_ids_count FROM ', @queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SELECT @person_ids_count AS 'num of patients to update'; - - SET @dyn_sql := CONCAT('DELETE t1 FROM ',@primary_table, ' t1 JOIN ', @queue_table,' t2 USING (person_id);'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql := CONCAT('SELECT COUNT(*) INTO @person_ids_count FROM ', @queue_table); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SELECT @person_ids_count AS 'num patients to update'; - - SET @dyn_sql := CONCAT('DELETE t1 FROM ', @primary_table, ' t1 join ', @queue_table,' t2 USING (person_id);'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_time = 0; - SET @cycle_number = 0; - - SET @total_time = 0; - SET @cycle_number = 0; - - WHILE @person_ids_count > 0 DO - SET @loop_start_time := NOW(); - - -- Create temporary table with a set of person ids - DROP TEMPORARY TABLE IF EXISTS flat_breast_cancer_screening_build_queue__0; - - SET @dyn_sql := CONCAT('CREATE TEMPORARY TABLE flat_breast_cancer_screening_build_queue__0 (person_id INT PRIMARY KEY) (SELECT * FROM ', @queue_table, ' LIMIT ', cycle_size, ');'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - DROP TEMPORARY TABLE IF EXISTS flat_breast_cancer_screening_0a; - SET @dyn_sql := CONCAT( - 'CREATE TEMPORARY TABLE flat_breast_cancer_screening_0a - (SELECT - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - case - when t1.encounter_type in ',@clinical_encounter_types,' then 1 - else null - end as is_clinical_encounter, - case - when t1.encounter_type in ',@non_clinical_encounter_types,' then 20 - when t1.encounter_type in ',@clinical_encounter_types,' then 10 - when t1.encounter_type in', @other_encounter_types, ' then 5 - else 1 - end as encounter_type_sort_index, - t2.orders - FROM - etl.flat_obs t1 - JOIN - flat_breast_cancer_screening_build_queue__0 t0 USING (person_id) - LEFT JOIN - etl.flat_orders t2 USING (encounter_id) - WHERE - t1.encounter_type IN ', @encounter_types, ');'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - INSERT INTO flat_breast_cancer_screening_0a - (SELECT - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, -- t1.location_id, - t1.obs, - null, -- obs_datetimes - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - FROM - etl.flat_lab_obs t1 - JOIN - flat_breast_cancer_screening_build_queue__0 t0 USING (person_id) - ); - - DROP TEMPORARY TABLE IF EXISTS flat_breast_cancer_screening_0; - CREATE TEMPORARY TABLE flat_breast_cancer_screening_0 (INDEX encounter_id (encounter_id), INDEX person_enc (person_id, encounter_datetime)) - (SELECT - * - FROM - flat_breast_cancer_screening_0a - ORDER BY - person_id, DATE(encounter_datetime) - ); - - SET @encounter_purpose := null; - SET @other_encounter_purpose := null; - SET @menstruation_before_12 := null; - SET @menses_stopped_permanently := null; - SET @menses_stop_age := null; - SET @hrt_use := null; - SET @hrt_start_age := null; - SET @hrt_end_age := null; - SET @hrt_use_years := null; - SET @hrt_type_used =null; - SET @given_birth := null; - SET @age_first_birth := null; - SET @gravida := null; - SET @parity := null; - SET @cigarette_smoking := null; - SET @cigarette_smoked_day := null; - SET @tobacco_use := null; - SET @tobacco_use_duration_yrs := null; - SET @alcohol_drinking := null; - SET @alcohol_type := null; - SET @alcohol_use_period_yrs := null; - SET @breast_complaints_3months := null; - SET @breast_mass_location := null; - SET @nipple_discharge_location := null; - SET @nipple_retraction_location := null; - SET @breast_erythrema_location := null; - SET @breast_rash_location := null; - SET @breast_pain_location := null; - SET @other_changes_location := null; - SET @history_of_mammogram := null; - SET @mammogram_results := null; - SET @breast_ultrasound_history := null; - SET @breast_ultrasound_result := null; - SET @history_of_breast_biopsy := null; - SET @breast_biopsy_results := null; - SET @number_of_biopsies := null; - SET @biopsy_type := null; - SET @prev_exam_results := null; - SET @fam_brca_history_bf50 := null; - SET @fam_brca_history_aft50 := null; - SET @fam_male_brca_history := null; - SET @fam_ovarianca_history := null; - SET @fam_relatedca_history := null; - SET @fam_otherca_specify := null; - SET @cur_physical_findings := null; - SET @lymph_nodes_findings := null; - SET @cur_screening_findings := null; - -- SET @cur_screening_findings_date := null; - SET @patient_education := null; - SET @patient_education_other := null; - SET @referrals_ordered := null; - SET @procedure_done := null; - SET @referred_date := null; - SET @next_app_date := null; - SET @cbe_imaging_concordance := null; - SET @mammogram_workup_date := null; - SET @date_patient_notified_of_mammogram_results := null; - SET @ultrasound_results := null; - SET @ultrasound_workup_date := null; - SET @date_patient_notified_of_ultrasound_results := null; - SET @fna_results := null; - SET @fna_tumor_size := null; - SET @fna_degree_of_malignancy := null; - SET @fna_workup_date := null; - SET @date_patient_notified_of_fna_results := null; - SET @biopsy_tumor_size := null; - SET @biopsy_degree_of_malignancy := null; - SET @biopsy_workup_date := null; - SET @date_patient_notified_of_biopsy_results := null; - SET @biopsy_description := null; - SET @diagnosis_date := null; - SET @date_patient_informed_and_referred_for_management := null; - SET @screening_mode := null; - SET @diagnosis := null; - SET @cancer_staging := null; - SET @hiv_status := null; - - DROP TEMPORARY TABLE IF EXISTS flat_breast_cancer_screening_1; - - CREATE TEMPORARY TABLE flat_breast_cancer_screening_1 - (SELECT - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - t1.encounter_id, - t1.encounter_type, - t1.encounter_datetime, - t1.visit_id, - -- t4.name as location_name, - t1.location_id, - t1.is_clinical_encounter, - p.gender, - p.death_date, - case - when timestampdiff(year,birthdate,curdate()) > 0 then round(timestampdiff(year,birthdate,curdate()),0) - else round(timestampdiff(month,birthdate,curdate())/12,2) - end as age, - case - when obs regexp "!!1834=9651!!" then @encounter_purpose := 1 - when obs regexp "!!1834=1154!!" then @encounter_purpose := 2 - when obs regexp "!!1834=1246!!" then @encounter_purpose := 3 - when obs regexp "!!1834=5622!!" then @encounter_purpose := 4 - else @encounter_purpose := null - end as encounter_purpose, - case - when obs regexp "!!1915=" then @other_encounter_purpose := GetValues(obs,1915) - else @other_encounter_purpose := null - end as other_encounter_purpose, - case - when obs regexp "!!9560=1065!!" then @menstruation_before_12 := 1 - when obs regexp "!!9560=1066!!" then @menstruation_before_12 := 0 - else @menstruation_before_12 := null - end as menstruation_before_12, - case - when obs regexp "!!9561=1065!!" then @menses_stopped_permanently := 1 - when obs regexp "!!9561=1066!!" then @menses_stopped_permanently := 0 - when obs regexp "!!9561=9568!!" then @menses_stopped_permanently := 2 - else @menses_stopped_permanently := null - end as menses_stopped_permanently, - case - when obs regexp "!!9562=[0-9]" and t1.encounter_type = @lab_encounter_type then @menses_stop_age:=cast(GetValues(obs,9562) as unsigned) - when @prev_id = @cur_id then @menses_stop_age - else @menses_stop_age := null - end as menses_stop_age, - case - when obs regexp "!!9626=1065!!" then @hrt_use := 1 - when obs regexp "!!9626=1066!!" then @hrt_use := 0 - when obs regexp "!!9626=9568!!" then @hrt_use := 2 - else @hrt_use := null - end as hrt_use, - case - when obs regexp "!!9627=[0-9]" then @hrt_start_age := cast(GetValues(obs,9627) as unsigned) - else @hrt_start_age := null - end as hrt_start_age, - case - when obs regexp "!!9723=[0-9]" then @hrt_end_age := cast(GetValues(obs,9723) as unsigned) - else @hrt_end_age := null - end as hrt_end_age, - case - when obs regexp "!!97629=[0-9]" then @hrt_use_years := cast(GetValues(obs,9629) as unsigned) - else @hrt_use_years := null - end as hrt_use_years, - case - when obs regexp "!!9630=9573!!" then @hrt_type_used := 1 - when obs regexp "!!9630=6217!!" then @hrt_type_used := 2 - when obs regexp "!!9630=6218!!" then @hrt_type_used := 3 - else @hrt_type_used := null - end as hrt_type_used, - case - when obs regexp "!!9563=1065!!" then @given_birth := 1 - when obs regexp "!!9563=1066!!" then @given_birth := 0 - else @given_birth := null - end as given_birth, - case - when obs regexp "!!5574=[0-9]" then @age_first_birth := cast(GetValues(obs,5574) as unsigned) - else @age_first_birth := null - end as age_first_birth, - case - when obs regexp "!!5624=[0-9]" then @gravida := cast(GetValues(obs,5624) as unsigned) - else @gravida := null - end as gravida, - case - when obs regexp "!!1053=[0-9]" then @parity := cast(GetValues(obs,1053) as unsigned) - else @parity := null - end as parity, - case - when obs regexp "!!9333=" then @cigarette_smoking := GetValues(obs,9333) - else @cigarette_smoking := null - end as cigarette_smoking, - case - when obs regexp "!!2069=[0-9]" then @cigarette_smoked_day := cast(GetValues(obs,2069) as unsigned) - else @cigarette_smoked_day := null - end as cigarette_smoked_day, - case - when obs regexp "!!7973=1065!!" then @tobacco_use := 1 - when obs regexp "!!7973=1066!!" then @tobacco_use := 0 - when obs regexp "!!7973=1679!!" then @tobacco_use := 2 - else @tobacco_use := null - end as tobacco_use, - case - when obs regexp "!!8144=[0-9]" then @tobacco_use_duration_yrs := cast(GetValues(obs, 8144) as unsigned) - else @tobacco_use_duration_yrs := null - end as tobacco_use_duration_yrs, - case - when obs regexp "!!1684=1065!!" then @alcohol_drinking := 1 - when obs regexp "!!1684=1066!!" then @alcohol_drinking := 0 - when obs regexp "!!1684=1679!!" then @alcohol_drinking := 2 - else @alcohol_drinking := null - end as alcohol_drinking, - case - when obs regexp "!!1685=1682!!" then @alcohol_type := 1 - when obs regexp "!!1685=1681!!" then @alcohol_type := 2 - when obs regexp "!!1685=1680!!" then @alcohol_type := 3 - when obs regexp "!!1685=1683!!" then @alcohol_type := 4 - when obs regexp "!!1685=2059!!" then @alcohol_type := 5 - when obs regexp "!!1685=5622!!" then @alcohol_type := 6 - else @alcohol_type := null - end as alcohol_type, - case - when obs regexp "!!8170=[0-9]" then @alcohol_use_period_yrs := cast(GetValues(obs,8170) as unsigned) - else @alcohol_use_period_yrs := null - end as alcohol_use_period_yrs, - case - when obs regexp "!!9553=5006!!" then @breast_complaints_3months := 1 - when obs regexp "!!9553=1068!!" then @breast_complaints_3months := 2 - else @breast_complaints_3months := null - end as breast_complaints_3months, - case - when obs regexp "!!6697=2399!!" then @breast_mass_location := 1 - when obs regexp "!!6697=5139!!" then @breast_mass_location := 2 - when obs regexp "!!6697=5141!!" then @breast_mass_location := 3 - when obs regexp "!!6697=6589!!" then @breast_mass_location := 4 - when obs regexp "!!6697=6590!!" then @breast_mass_location := 5 - when obs regexp "!!6697=6591!!" then @breast_mass_location := 6 - when obs regexp "!!6697=9625!!" then @breast_mass_location := 7 - else @breast_mass_location := null - end as breast_mass_location, - case - when obs regexp "!!9557=5139!!" then @nipple_discharge_location := 1 - when obs regexp "!!9557=5141!!" then @nipple_discharge_location := 2 - when obs regexp "!!9557=9625!!" then @nipple_discharge_location := 3 - else @nipple_discharge_location := null - end as nipple_discharge_location, - case - when obs regexp "!!9558=5139!!" then @nipple_retraction_location := 1 - when obs regexp "!!9558=5141!!" then @nipple_retraction_location := 2 - when obs regexp "!!9558=9625!!" then @nipple_retraction_location := 3 - else @nipple_retraction_location := null - end as nipple_retraction_location, - case - when obs regexp "!!9574=5139!!" then @breast_erythrema_location := 1 - when obs regexp "!!9574=5141!!" then @breast_erythrema_location := 2 - when obs regexp "!!9574=9625!!" then @breast_erythrema_location := 3 - else @breast_erythrema_location := null - end as breast_erythrema_location, - case - when obs regexp "!!9577=5139!!" then @breast_rash_location := 1 - when obs regexp "!!9577=5141!!" then @breast_rash_location := 2 - when obs regexp "!!9577=9625!!" then @breast_rash_location := 3 - else @breast_rash_location := null - end as breast_rash_location, - case - when obs regexp "!!9577=5139!!" then @breast_pain_location := 1 - when obs regexp "!!9577=5141!!" then @breast_pain_location := 2 - when obs regexp "!!9577=9625!!" then @breast_pain_location := 3 - else @breast_pain_location := null - end as breast_pain_location, - case - when obs regexp "!!9576=5139!!" then @other_changes_location := 1 - when obs regexp "!!9576=5141!!" then @other_changes_location := 2 - when obs regexp "!!9576=9625!!" then @other_changes_location := 3 - else @other_changes_location := null - end as other_changes_location, - case - when obs regexp "!!9594=1066!!" then @history_of_mammogram := 0 - when obs regexp "!!9594=1065!!" then @history_of_mammogram := 1 - else @history_of_mammogram := null - end as history_of_mammogram, - case - when obs regexp "!!9595=1115!!" then @mammogram_results := 1 - when obs regexp "!!9595=1116!!" then @mammogram_results := 2 - when obs regexp "!!9595=1118!!" then @mammogram_results := 3 - when obs regexp "!!9595=1138!!" then @mammogram_results := 4 - when obs regexp "!!9595=1267!!" then @mammogram_results := 5 - when obs regexp "!!9595=1067!!" then @mammogram_results := 6 - else @mammogram_results := null - end as mammogram_results, - case - when obs regexp "!!9597=1066!!" then @breast_ultrasound_history := 0 - when obs regexp "!!9597=1065!!" then @breast_ultrasound_history := 1 - else @breast_ultrasound_history := null - end as breast_ultrasound_history, - case - when obs regexp "!!9596=1115!!" then @breast_ultrasound_result := 1 - when obs regexp "!!9596=1116!!" then @breast_ultrasound_result := 2 - when obs regexp "!!9596=1067!!" then @breast_ultrasound_result := 3 - when obs regexp "!!9596=1118!!" then @breast_ultrasound_result := 4 - else @breast_ultrasound_result := null - end as breast_ultrasound_result, - case - when obs regexp "!!9598=1066!!" then @history_of_breast_biopsy := 0 - when obs regexp "!!9598=1065!!" then @history_of_breast_biopsy := 1 - else @history_of_breast_biopsy := null - end as history_of_breast_biopsy, - case - when obs regexp "!!8184=1115!!" then @breast_biopsy_results := 1 - when obs regexp "!!8184=1116!!" then @breast_biopsy_results := 2 - when obs regexp "!!8184=1118!!" then @breast_biopsy_results := 3 - when obs regexp "!!8184=1067!!" then @breast_biopsy_results := 4 - when obs regexp "!!8184=9691!!" then @breast_biopsy_results := 5 - when obs regexp "!!8184=10052!!" then @breast_biopsy_results := 6 - else @breast_biopsy_results := null - end as breast_biopsy_results, - case - when obs regexp "!!9599=" then @number_of_biopsies := GetValues(obs, 9599) - else @number_of_biopsies := null - end as number_of_biopsies, - case - when obs regexp "!!6509=6510!!" then @biopsy_type := 1 - when obs regexp "!!6509=6511!!" then @biopsy_type := 2 - when obs regexp "!!6509=6512!!" then @biopsy_type := 3 - when obs regexp "!!6509=6513!!" then @biopsy_type := 4 - when obs regexp "!!6509=7190!!" then @biopsy_type := 5 - when obs regexp "!!6509=8184!!" then @biopsy_type := 6 - when obs regexp "!!6509=10075!!" then @biopsy_type := 7 - when obs regexp "!!6509=10076!!" then @biopsy_type := 8 - else @biopsy_type := null - end as biopsy_type, - case - when obs regexp "!!9694=1115!!" then @prev_exam_results := 1 - when obs regexp "!!9694=1116!!" then @prev_exam_results := 0 - when obs regexp "!!9694=1067!!" then @prev_exam_results := 2 - else @prev_exam_results := null - end as prev_exam_results, - case - when obs regexp "!!9631=1672!!" then @fam_brca_history_bf50 := 1 - when obs regexp "!!9631=1107!!" then @fam_brca_history_bf50 := 0 - when obs regexp "!!9631=978!!" then @fam_brca_history_bf50 := 2 - when obs regexp "!!9631=972!!" then @fam_brca_history_bf50 := 3 - when obs regexp "!!9631=1671!!" then @fam_brca_history_bf50 := 4 - when obs regexp "!!9631=1393!!" then @fam_brca_history_bf50 := 5 - when obs regexp "!!9631=1392!!" then @fam_brca_history_bf50 := 6 - when obs regexp "!!9631=1395!!" then @fam_brca_history_bf50 := 7 - when obs regexp "!!9631=1394!!" then @fam_brca_history_bf50 := 8 - else @fam_brca_history_bf50 := null - end as fam_brca_history_bf50, - case - when obs regexp "!!9632=978!!" then @fam_brca_history_aft50 := 1 - when obs regexp "!!9632=1672!!" then @fam_brca_history_aft50 := 2 - when obs regexp "!!9632=972!!" then @fam_brca_history_aft50 := 3 - when obs regexp "!!9632=1671!!" then @fam_brca_history_aft50 := 4 - when obs regexp "!!9632=1393!!" then @fam_brca_history_aft50 := 5 - when obs regexp "!!9632=1392!!" then @fam_brca_history_aft50 := 6 - when obs regexp "!!9632=1395!!" then @fam_brca_history_aft50 := 7 - when obs regexp "!!9632=1394!!" then @fam_brca_history_aft50 := 8 - else @fam_brca_history_aft50 := null - end as fam_brca_history_aft50, - case - when obs regexp "!!9633=978!!" then @fam_male_brca_history := 1 - when obs regexp "!!9633=1672!!" then @fam_male_brca_history := 2 - when obs regexp "!!9633=972!!" then @fam_male_brca_history := 3 - when obs regexp "!!9633=1671!!" then @fam_male_brca_history := 4 - when obs regexp "!!9633=1393!!" then @fam_male_brca_history := 5 - when obs regexp "!!9633=1392!!" then @fam_male_brca_history := 6 - when obs regexp "!!9633=1395!!" then @fam_male_brca_history := 7 - when obs regexp "!!9633=1394!!" then @fam_male_brca_history := 8 - else @fam_male_brca_history := null - end as fam_male_brca_history, - case - when obs regexp "!!9634=978!!" then @fam_ovarianca_history := 1 - when obs regexp "!!9634=1672!!" then @fam_ovarianca_history := 2 - when obs regexp "!!9634=972!!" then @fam_ovarianca_history := 3 - when obs regexp "!!9634=1671!!" then @fam_ovarianca_history := 4 - when obs regexp "!!9634=1393!!" then @fam_ovarianca_history := 5 - when obs regexp "!!9634=1392!!" then @fam_ovarianca_history := 6 - when obs regexp "!!9634=1395!!" then @fam_ovarianca_history := 7 - when obs regexp "!!9634=1394!!" then @fam_ovarianca_history := 8 - else @fam_ovarianca_history := null - end as fam_ovarianca_history, - case - when obs regexp "!!9635=978!!" then @fam_relatedca_history:= 1 - when obs regexp "!!9635=1672!!" then @fam_relatedca_history := 2 - when obs regexp "!!9635=972!!" then @fam_relatedca_history := 3 - when obs regexp "!!9635=1671!!" then @fam_relatedca_history := 4 - when obs regexp "!!9635=1393!!" then @fam_relatedca_history := 5 - when obs regexp "!!9635=1392!!" then @fam_relatedca_history := 6 - when obs regexp "!!9635=1395!!" then @fam_relatedca_history := 7 - when obs regexp "!!9635=1394!!" then @fam_relatedca_history := 8 - else @fam_relatedca_history := null - end as fam_relatedca_history, - case - when obs regexp "!!7176=6529!!" then @fam_otherca_specify := 1 - when obs regexp "!!7176=9636!!" then @fam_otherca_specify := 2 - when obs regexp "!!7176=9637!!" then @fam_otherca_specify := 3 - when obs regexp "!!7176=6485!!" then @fam_otherca_specify := 4 - when obs regexp "!!7176=9638!!" then @fam_otherca_specify := 5 - when obs regexp "!!7176=9639!!" then @fam_otherca_specify := 6 - when obs regexp "!!7176=6522!!" then @fam_otherca_specify := 7 - when obs regexp "!!7176=216!!" then @fam_otherca_specify := 8 - else @fam_otherca_specify := null - end as fam_otherca_specify, - case - when obs regexp "!!6251=1115!!" then @cur_physical_findings:= 0 - when obs regexp "!!6251=115!!" then @cur_physical_findings := 1 - when obs regexp "!!6251=6250!!" then @cur_physical_findings := 2 - when obs regexp "!!6251=6249!!" then @cur_physical_findings := 3 - when obs regexp "!!6251=5622!!" then @cur_physical_findings := 4 - when obs regexp "!!6251=582!!" then @cur_physical_findings := 5 - when obs regexp "!!6251=6493!!" then @cur_physical_findings := 6 - when obs regexp "!!6251=6499!!" then @cur_physical_findings := 7 - when obs regexp "!!6251=1118!!" then @cur_physical_findings := 8 - when obs regexp "!!6251=1481!!" then @cur_physical_findings := 9 - when obs regexp "!!6251=6729!!" then @cur_physical_findings := 10 - when obs regexp "!!6251=1116!!" then @cur_physical_findings := 11 - when obs regexp "!!6251=8188!!" then @cur_physical_findings := 12 - when obs regexp "!!6251=8189!!" then @cur_physical_findings := 13 - when obs regexp "!!6251=1067!!" then @cur_physical_findings := 14 - when obs regexp "!!6251=9689!!" then @cur_physical_findings := 15 - when obs regexp "!!6251=9690!!" then @cur_physical_findings := 16 - when obs regexp "!!6251=9687!!" then @cur_physical_findings := 17 - when obs regexp "!!6251=9688!!" then @cur_physical_findings := 18 - when obs regexp "!!6251=5313!!" then @cur_physical_findings := 19 - when obs regexp "!!6251=9691!!" then @cur_physical_findings := 20 - else @cur_physical_findings := null - end as cur_physical_findings, - case - when obs regexp "!!1121=1115!!" then @lymph_nodes_findings := 1 - when obs regexp "!!1121=161!!" then @lymph_nodes_findings := 2 - when obs regexp "!!1121=9675!!" then @lymph_nodes_findings:= 3 - when obs regexp "!!1121=9676!!" then @lymph_nodes_findings:= 4 - else @lymph_nodes_findings := null - end as lymph_nodes_findings, - case - when obs regexp "!!9748=1115!!" then @cur_screening_findings := 1 - when obs regexp "!!9748=9691!!" then @cur_screening_findings := 3 - when obs regexp "!!9748=1116!!" then @cur_screening_findings := 2 - else @cur_screening_findings := null - end as cur_screening_findings, - case - when obs regexp "!!6327=9651!!" then @patient_education := 1 - when obs regexp "!!6327=2345!!" then @patient_education := 2 - when obs regexp "!!6327=9692!!" then @patient_education:= 3 - when obs regexp "!!6327=5622!!" then @patient_education:= 4 - else @patient_education := null - end as patient_education, - case - when obs regexp "!!1915=" then @patient_education_other := GetValues(obs,1915) - else @patient_education_other := null - end as patient_education_other, - case - when obs regexp "!!1272=1107!!" then @referrals_ordered := 0 - when obs regexp "!!1272=1496!!" then @referrals_ordered:= 1 - else @referrals_ordered := null - end as referrals_ordered, - case - when obs regexp "!!1272=9596!!" then @procedure_done := 1 - when obs regexp "!!1272=9595!!" then @procedure_done:= 2 - when obs regexp "!!1272=6510!!" then @procedure_done := 3 - when obs regexp "!!1272=7190!!" then @procedure_done:= 4 - when obs regexp "!!1272=6511!!" then @procedure_done := 5 - when obs regexp "!!1272=9997!!" then @procedure_done:= 6 - else @procedure_done := null - end as procedure_done, - case - when obs regexp "!!9158=" then @referred_date := GetValues(obs,9158) - else @referred_date := null - end as referred_date, - case - when obs regexp "!!5096=" then @next_app_date := GetValues(obs,5096) - else @next_app_date := null - end as next_app_date, - case - when obs regexp "!!9702=9703!!" then @cbe_imaging_concordance := 1 - when obs regexp "!!9702=9704!!" then @cbe_imaging_concordance := 2 - when obs regexp "!!9702=1175!!" then @cbe_imaging_concordance := 3 - else @cbe_imaging_concordance := null - end as cbe_imaging_concordance, - case - when obs regexp "!!9708=" then @mammogram_workup_date := GetValues(obs,9708) - else @mammogram_workup_date := null - end as mammogram_workup_date, - case - when obs regexp "!!9705=" then @date_patient_notified_of_mammogram_results := GetValues(obs,9705) - else @date_patient_notified_of_mammogram_results := null - end as date_patient_notified_of_mammogram_results, - case - when obs regexp "!!9596=1115!!" then @ultrasound_results := 1 - when obs regexp "!!9596=1116!!" then @ultrasound_results := 2 - when obs regexp "!!9596=1067!!" then @ultrasound_results := 3 - when obs regexp "!!9596=1118!!" then @ultrasound_results := 4 - else @ultrasound_results := null - end as ultrasound_results, - case - when obs regexp "!!9708=" then @ultrasound_workup_date := GetValues(obs,9708) - else @ultrasound_workup_date := null - end as ultrasound_workup_date, - case - when obs regexp "!!10047=" then @date_patient_notified_of_ultrasound_results := GetValues(obs,10047) - else @date_patient_notified_of_ultrasound_results := null - end as date_patient_notified_of_ultrasound_results, - case - when obs regexp "!!10051=9691!!" then @fna_results := 1 - when obs regexp "!!10051=10052!!" then @fna_results := 2 - when obs regexp "!!9596=1118!!" then @fna_results := 3 - else @fna_results := null - end as fna_results, - case - when obs regexp "!!10053=[0-9]" then @fna_tumor_size:= cast(GetValues(obs,10053) as unsigned) - else @fna_tumor_size := null - end as fna_tumor_size, - case - when obs regexp "!!10054=10055!!" then @fna_degree_of_malignancy := 1 - when obs regexp "!!10054=10056!!" then @fna_degree_of_malignancy := 2 - else @fna_degree_of_malignancy := null - end as fna_degree_of_malignancy, - case - when obs regexp "!!10057=" then @fna_workup_date := GetValues(obs,10057) - else @fna_workup_date := null - end as fna_workup_date, - case - when obs regexp "!!10059=" then @date_patient_notified_of_fna_results := GetValues(obs,10059) - else @date_patient_notified_of_fna_results := null - end as date_patient_notified_of_fna_results, - case - when obs regexp "!!10053=[0-9]" then @biopsy_tumor_size:= cast(GetValues(obs,10053) as unsigned) - else @biopsy_tumor_size := null - end as biopsy_tumor_size, - case - when obs regexp "!!10054=10055!!" then @biopsy_degree_of_malignancy := 1 - when obs regexp "!!10054=10056!!" then @biopsy_degree_of_malignancy := 2 - else @biopsy_degree_of_malignancy := null - end as biopsy_degree_of_malignancy, - case - when obs regexp "!!10060=" then @biopsy_workup_date := GetValues(obs,10060) - else @biopsy_workup_date := null - end as biopsy_workup_date, - case - when obs regexp "!!10061=" then @date_patient_notified_of_biopsy_resultse := GetValues(obs,10061) - else @date_patient_notified_of_biopsy_results := null - end as date_patient_notified_of_biopsy_results, - case - when obs regexp "!!7400=" then @biopsy_description := GetValues(obs,7400) - else @biopsy_description := null - end as biopsy_description, - case - when obs regexp "!!9728=" then @diagnosis_date := GetValues(obs, 9728) - else @diagnosis_date := null - end as diagnosis_date, - case - when obs regexp "!!9706=" then @date_patient_informed_and_referred_for_management := GetValues(obs, 9706) - else @date_patient_informed_and_referred_for_management := null - end as date_patient_informed_and_referred_for_management, - case - when obs regexp "!!10068=9595!!" then @screening_mode := 1 - when obs regexp "!!10068=10067!!" then @screening_mode := 2 - when obs regexp "!!10068=9596!!" then @screening_mode := 3 - else @screening_mode := null - end as screening_mode, - case - when obs regexp "!!6042=" then @diagnosis := GetValues(obs,6042) - else @diagnosis := null - end as diagnosis, - case - when obs regexp "!!9868=9852!!" then @cancer_staging := 1 - when obs regexp "!!9868=9856!!" then @cancer_staging := 2 - when obs regexp "!!9868=9860!!" then @cancer_staging := 3 - when obs regexp "!!9868=9864!!" then @cancer_staging := 4 - else @cancer_staging := null - end as cancer_staging, - case - when obs regexp "!!6709=664!!" then @hiv_status := 1 - when obs regexp "!!6709=703!!" then @hiv_status := 2 - when obs regexp "!!6709=1067!!" then @hiv_status := 3 - else @hiv_status := null - end as hiv_status - FROM - flat_breast_cancer_screening_0 t1 - JOIN amrs.person p using (person_id) - ORDER BY person_id, date(encounter_datetime) DESC, encounter_type_sort_index DESC - ); - - SET @prev_id = null; - SET @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - ALTER TABLE flat_breast_cancer_screening_1 DROP prev_id, DROP cur_id; - - drop table if exists flat_breast_cancer_screening_2; - create temporary table flat_breast_cancer_screening_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_breast_cancer_screening, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_breast_cancer_screening, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_breast_cancer_screening, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date - - from flat_breast_cancer_screening_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_breast_cancer_screening_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_breast_cancer_screening_3; - create temporary table flat_breast_cancer_screening_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_breast_cancer_screening, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_breast_cancer_screening, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_breast_cancer_screening, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_breast_cancer_screening_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - SELECT - COUNT(*) - INTO - @new_encounter_rows - FROM - flat_breast_cancer_screening_1; - - SELECT @new_encounter_rows; - SET @total_rows_written := @total_rows_written + @new_encounter_rows; - SELECT @total_rows_written; - - SET @dyn_sql := CONCAT('REPLACE INTO ', @write_table, - '(SELECT - null, - person_id, - encounter_id, - encounter_type, - encounter_datetime, - visit_id, - location_id, - t2.uuid as location_uuid, - gender, - age, - encounter_purpose, - other_encounter_purpose, - menstruation_before_12, - menses_stopped_permanently, - menses_stop_age, - hrt_use, - hrt_start_age, - hrt_end_age, - hrt_use_years, - hrt_type_used, - given_birth, - age_first_birth, - gravida, - parity, - cigarette_smoking, - cigarette_smoked_day, - tobacco_use, - tobacco_use_duration_yrs, - alcohol_drinking, - alcohol_type, - alcohol_use_period_yrs, - breast_complaints_3months, - breast_mass_location, - nipple_discharge_location, - nipple_retraction_location, - breast_erythrema_location, - breast_rash_location, - breast_pain_location, - other_changes_location, - history_of_mammogram, - mammogram_results, - breast_ultrasound_history, - breast_ultrasound_result, - history_of_breast_biopsy, - breast_biopsy_results, - number_of_biopsies, - biopsy_type, - prev_exam_results, - fam_brca_history_bf50, - fam_brca_history_aft50, - fam_male_brca_history, - fam_ovarianca_history, - fam_relatedca_history, - fam_otherca_specify, - cur_physical_findings, - lymph_nodes_findings, - cur_screening_findings, - #cur_screening_findings_date, - patient_education, - patient_education_other, - referrals_ordered, - referred_date, - procedure_done, - next_app_date, - cbe_imaging_concordance, - mammogram_workup_date, - date_patient_notified_of_mammogram_results, - ultrasound_results, - ultrasound_workup_date, - date_patient_notified_of_ultrasound_results, - fna_results, - fna_tumor_size, - fna_degree_of_malignancy, - fna_workup_date, - date_patient_notified_of_fna_results, - biopsy_tumor_size, - biopsy_degree_of_malignancy, - biopsy_workup_date, - date_patient_notified_of_biopsy_results, - biopsy_description, - diagnosis_date, - date_patient_informed_and_referred_for_management, - screening_mode, - diagnosis, - cancer_staging, - hiv_status, - prev_encounter_datetime_breast_cancer_screening, - next_encounter_datetime_breast_cancer_screening, - prev_encounter_type_breast_cancer_screening, - next_encounter_type_breast_cancer_screening, - prev_clinical_datetime_breast_cancer_screening, - next_clinical_datetime_breast_cancer_screening, - prev_clinical_location_id_breast_cancer_screening, - next_clinical_location_id_breast_cancer_screening, - prev_clinical_rtc_date_breast_cancer_screening, - next_clinical_rtc_date_breast_cancer_screening - FROM - flat_breast_cancer_screening_3 t1 - JOIN - amrs.location t2 USING (location_id))' - ); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('DELETE t1 from ',@queue_table,' t1 JOIN flat_breast_cancer_screening_build_queue__0 t2 USING (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('SELECT COUNT(*) INTO @person_ids_count FROM ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @cycle_length = TIMESTAMPDIFF(second,@loop_start_time,NOW()); - SET @total_time = @total_time + @cycle_length; - SET @cycle_number = @cycle_number + 1; - - SET @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - SELECT - @person_ids_count AS 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - END WHILE; - - IF (@query_type = 'build') THEN - SET @dyn_sql := CONCAT('drop table ', @queue_table, ';'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write := 0; - SET @dyn_sql := CONCAT('SELECT COUNT(*) INTO @total_rows_to_write FROM ', @write_table); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @start_write := NOW(); - SELECT CONCAT(@start_write, ' : Writing ', @total_rows_to_write, ' to ', @primary_table); - - SET @dyn_sql := CONCAT('REPLACE INTO ', @primary_table, '(SELECT * FROM ', @write_table, ');'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @finish_write := NOW(); - SET @time_to_write := TIMESTAMPDIFF(SECOND, @start_write, @finish_write); - SELECT CONCAT(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql := CONCAT('DROP TABLE ', @write_table, ';'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - END IF; - - SET @ave_cycle_length := CEIL(@total_time / @cycle_number); - SELECT CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - SET @end := NOW(); - INSERT INTO etl.flat_log VALUES (@start, @last_date_created, @table_version, TIMESTAMPDIFF(SECOND, @start, @end)); - SELECT CONCAT(@table_version, ': Time to complete: ', TIMESTAMPDIFF(MINUTE, @start, @end), ' minutes'); -END$$ -DELIMITER ; \ No newline at end of file diff --git a/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening_v1_3.sql b/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening_v1_3.sql deleted file mode 100644 index 54eea28..0000000 --- a/etl-scripts/stored-procedures/generate_flat_breast_cancer_screening_v1_3.sql +++ /dev/null @@ -1,1247 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_breast_cancer_screening_v1_3`(IN query_type VARCHAR(50), IN queue_number INT, IN queue_size INT, IN cycle_size INT) -BEGIN - -- v1.3: Rename `breast_complaints_3months` column to `breast_complaints_3months` - -- Rename `Ultrasound_results` to `ultrasound_results` - -- Add new columns: - -- breast_mass_location, - -- nipple_discharge_location, - -- nipple_retraction_location, - -- breast_erythrema_location, - -- breast_rash_location, - -- breast_pain_location, - -- other_changes_location, - -- history_of_mammogram, - -- mammogram_results, - -- breast_ultrasound_history, - -- breast_ultrasound_result, - -- history_of_breast_biopsy, - -- breast_biopsy_results, - -- number_of_biopsies, - -- biopsy_type, - -- prev_exam_results - - SET @primary_table := "flat_breast_cancer_screening"; - SET @query_type := query_type; - - SET @total_rows_written := 0; - - SET @encounter_types = "(86, 145, 146, 160)"; - SET @clinical_encounter_types = "(86, 145, 146, 160)"; - SET @non_clinical_encounter_types = "(-1)"; - SET @other_encounter_types = "(-1)"; - - SET @start := NOW(); - SET @table_version := "flat_breast_cancer_screening_v1.3"; - - SET session sort_buffer_size := 512000000; - - SET @sep := " ## "; - SET @boundary := "!!"; - SET @last_date_created := (SELECT MAX(max_date_created) FROM etl.flat_obs); - - CREATE TABLE IF NOT EXISTS flat_breast_cancer_screening ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - encounter_id int, - encounter_type int, - encounter_datetime datetime, - visit_id int, - location_id int, - location_uuid varchar (100), - gender char (100), - age int, - encounter_purpose int, - other_encounter_purpose varchar(1000), - menstruation_before_12 char, - menses_stopped_permanently char, - menses_stop_age int, - hrt_use char, - hrt_start_age int, - hrt_end_age int, - hrt_use_years int, - hrt_type_used int, - given_birth int, - age_first_birth int, - gravida int, - parity int, - cigarette_smoking int, - cigarette_smoked_day int, - tobacco_use int, - tobacco_use_duration_yrs int, - alcohol_drinking int, - alcohol_type int, - alcohol_use_period_yrs int, - breast_complaints_3months int, - breast_mass_location tinyint, - nipple_discharge_location tinyint, - nipple_retraction_location tinyint, - breast_erythrema_location tinyint, - breast_rash_location tinyint, - breast_pain_location tinyint, - other_changes_location tinyint, - history_of_mammogram tinyint, - mammogram_results tinyint, - breast_ultrasound_history tinyint, - breast_ultrasound_result tinyint, - history_of_breast_biopsy tinyint, - breast_biopsy_results tinyint, - number_of_biopsies tinyint, - biopsy_type tinyint, - prev_exam_results int, - fam_brca_history_bf50 int, - fam_brca_history_aft50 int, - fam_male_brca_history int, - fam_ovarianca_history int, - fam_relatedca_history int, - fam_otherca_specify int, - cur_physical_findings int, - lymph_nodes_findings int, - cur_screening_findings int, - #cur_screening_findings_date int, - patient_education int, - patient_education_other varchar(1000), - referrals_ordered int, - referred_date datetime, - procedure_done int, - next_app_date datetime, - cbe_imaging_concordance int, - mammogram_workup_date datetime, - date_patient_notified_of_mammogram_results datetime, - ultrasound_results int, - ultrasound_workup_date datetime, - date_patient_notified_of_ultrasound_results datetime, - fna_results int, - fna_tumor_size int, - fna_degree_of_malignancy int, - fna_workup_date datetime, - date_patient_notified_of_fna_results datetime, - biopsy_tumor_size int, - biopsy_degree_of_malignancy int, - biopsy_workup_date datetime, - date_patient_notified_of_biopsy_results datetime, - biopsy_description varchar(1000), - diagnosis_date datetime, - date_patient_informed_and_referred_for_management datetime, - screening_mode int, - diagnosis int, - cancer_staging int, - hiv_status int, - prev_encounter_datetime_breast_cancer_screening datetime, - next_encounter_datetime_breast_cancer_screening datetime, - prev_encounter_type_breast_cancer_screening mediumint, - next_encounter_type_breast_cancer_screening mediumint, - prev_clinical_datetime_breast_cancer_screening datetime, - next_clinical_datetime_breast_cancer_screening datetime, - prev_clinical_location_id_breast_cancer_screening mediumint, - next_clinical_location_id_breast_cancer_screening mediumint, - prev_clinical_rtc_date_breast_cancer_screening datetime, - next_clinical_rtc_date_breast_cancer_screening datetime, - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,next_app_date), - index location_uuid_rtc_date (location_uuid,next_app_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_breast_cancer_screening), - index encounter_type (encounter_type), - index date_created (date_created) - ); - - -- Build step - IF (@query_type = "build") THEN - SELECT 'BUILDING..........................................'; - - SET @write_table := CONCAT('flat_breast_cancer_screening_temp_', queue_number); - SET @queue_table := CONCAT('flat_breast_cancer_screening_build_queue_', queue_number); - - SET @dyn_sql := CONCAT('CREATE TABLE IF NOT EXISTS ', @write_table, ' LIKE ', @primary_table); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql := CONCAT('CREATE TABLE IF NOT EXISTS ', @queue_table, ' (SELECT * FROM flat_breast_cancer_screening_build_queue LIMIT ', queue_size, ');'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql := CONCAT('DELETE t1 FROM flat_breast_cancer_screening_build_queue t1 join ', @queue_table, ' t2 using (person_id);'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - END IF; - - -- Sync step - IF (@query_type = 'sync') THEN - SELECT 'SYNCING.....................................'; - - SET @write_table := 'flat_breast_cancer_screening'; - SET @queue_table := 'flat_breast_cancer_screening_sync_queue'; - - CREATE TABLE IF NOT EXISTS flat_breast_cancer_screening_sync_queue (person_id INT PRIMARY KEY); - - SET @last_update := null; - - SELECT MAX(date_updated) INTO @last_update FROM etl.flat_log WHERE table_name = @table_version; - - SELECT 'Finding patients in amrs.encounters...'; - REPLACE INTO flat_breast_cancer_screening_sync_queue - (SELECT DISTINCT - patient_id - FROM - amrs.encounter - WHERE - date_changed > @last_update - ); - - SELECT 'Finding patients in flat_obs...'; - REPLACE INTO flat_breast_cancer_screening_sync_queue - (SELECT DISTINCT - person_id - FROM - etl.flat_obs - WHERE - max_date_created > @last_update - ); - - SELECT 'Finding patients in flat_lab_obs...'; - REPLACE INTO flat_breast_cancer_screening_sync_queue - (SELECT DISTINCT - person_id - FROM - etl.flat_lab_obs - WHERE - max_date_created > @last_update - ); - - SELECT 'Finding patients in flat_orders...'; - REPLACE INTO flat_breast_cancer_screening_sync_queue - (SELECT DISTINCT - person_id - FROM - etl.flat_orders - WHERE - max_date_created > @last_update - ); - - REPLACE INTO flat_breast_cancer_screening_sync_queue - (SELECT - person_id - FROM - amrs.person - WHERE - date_voided > @last_update - ); - - REPLACE INTO flat_breast_cancer_screening_sync_queue - (SELECT - person_id - FROM - amrs.person - WHERE - date_changed > @last_update - ); - END IF; - - -- Remove test patients - SET @dyn_sql := CONCAT('DELETE t1 FROM ', @queue_table, ' t1 - JOIN amrs.person_attribute t2 USING (person_id) - WHERE t2.person_attribute_type_id = 28 AND value = "true" AND voided = 0'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('SELECT COUNT(*) INTO @person_ids_count FROM ', @queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SELECT @person_ids_count AS 'num of patients to update'; - - SET @dyn_sql := CONCAT('DELETE t1 FROM ',@primary_table, ' t1 JOIN ', @queue_table,' t2 USING (person_id);'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql := CONCAT('SELECT COUNT(*) INTO @person_ids_count FROM ', @queue_table); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SELECT @person_ids_count AS 'num patients to update'; - - SET @dyn_sql := CONCAT('DELETE t1 FROM ', @primary_table, ' t1 join ', @queue_table,' t2 USING (person_id);'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_time = 0; - SET @cycle_number = 0; - - SET @total_time = 0; - SET @cycle_number = 0; - - WHILE @person_ids_count > 0 DO - SET @loop_start_time := NOW(); - - -- Create temporary table with a set of person ids - DROP TEMPORARY TABLE IF EXISTS flat_breast_cancer_screening_build_queue__0; - - SET @dyn_sql := CONCAT('CREATE TEMPORARY TABLE flat_breast_cancer_screening_build_queue__0 (person_id INT PRIMARY KEY) (SELECT * FROM ', @queue_table, ' LIMIT ', cycle_size, ');'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - DROP TEMPORARY TABLE IF EXISTS flat_breast_cancer_screening_0a; - SET @dyn_sql := CONCAT( - 'CREATE TEMPORARY TABLE flat_breast_cancer_screening_0a - (SELECT - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - case - when t1.encounter_type in ',@clinical_encounter_types,' then 1 - else null - end as is_clinical_encounter, - case - when t1.encounter_type in ',@non_clinical_encounter_types,' then 20 - when t1.encounter_type in ',@clinical_encounter_types,' then 10 - when t1.encounter_type in', @other_encounter_types, ' then 5 - else 1 - end as encounter_type_sort_index, - t2.orders - FROM - etl.flat_obs t1 - JOIN - flat_breast_cancer_screening_build_queue__0 t0 USING (person_id) - LEFT JOIN - etl.flat_orders t2 USING (encounter_id) - WHERE - t1.encounter_type IN ', @encounter_types, ');'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - INSERT INTO flat_breast_cancer_screening_0a - (SELECT - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, -- t1.location_id, - t1.obs, - null, -- obs_datetimes - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - FROM - etl.flat_lab_obs t1 - JOIN - flat_breast_cancer_screening_build_queue__0 t0 USING (person_id) - ); - - DROP TEMPORARY TABLE IF EXISTS flat_breast_cancer_screening_0; - CREATE TEMPORARY TABLE flat_breast_cancer_screening_0 (INDEX encounter_id (encounter_id), INDEX person_enc (person_id, encounter_datetime)) - (SELECT - * - FROM - flat_breast_cancer_screening_0a - ORDER BY - person_id, DATE(encounter_datetime) - ); - - SET @encounter_purpose := null; - SET @other_encounter_purpose := null; - SET @menstruation_before_12 := null; - SET @menses_stopped_permanently := null; - SET @menses_stop_age := null; - SET @hrt_use := null; - SET @hrt_start_age := null; - SET @hrt_end_age := null; - SET @hrt_use_years := null; - SET @hrt_type_used =null; - SET @given_birth := null; - SET @age_first_birth := null; - SET @gravida := null; - SET @parity := null; - SET @cigarette_smoking := null; - SET @cigarette_smoked_day := null; - SET @tobacco_use := null; - SET @tobacco_use_duration_yrs := null; - SET @alcohol_drinking := null; - SET @alcohol_type := null; - SET @alcohol_use_period_yrs := null; - SET @breast_complaints_3months := null; - SET @breast_mass_location := null; - SET @nipple_discharge_location := null; - SET @nipple_retraction_location := null; - SET @breast_erythrema_location := null; - SET @breast_rash_location := null; - SET @breast_pain_location := null; - SET @other_changes_location := null; - SET @history_of_mammogram := null; - SET @mammogram_results := null; - SET @breast_ultrasound_history := null; - SET @breast_ultrasound_result := null; - SET @history_of_breast_biopsy := null; - SET @breast_biopsy_results := null; - SET @number_of_biopsies := null; - SET @biopsy_type := null; - SET @prev_exam_results := null; - SET @fam_brca_history_bf50 := null; - SET @fam_brca_history_aft50 := null; - SET @fam_male_brca_history := null; - SET @fam_ovarianca_history := null; - SET @fam_relatedca_history := null; - SET @fam_otherca_specify := null; - SET @cur_physical_findings := null; - SET @lymph_nodes_findings := null; - SET @cur_screening_findings := null; - -- SET @cur_screening_findings_date := null; - SET @patient_education := null; - SET @patient_education_other := null; - SET @referrals_ordered := null; - SET @procedure_done := null; - SET @referred_date := null; - SET @next_app_date := null; - SET @cbe_imaging_concordance := null; - SET @mammogram_workup_date := null; - SET @date_patient_notified_of_mammogram_results := null; - SET @ultrasound_results := null; - SET @ultrasound_workup_date := null; - SET @date_patient_notified_of_ultrasound_results := null; - SET @fna_results := null; - SET @fna_tumor_size := null; - SET @fna_degree_of_malignancy := null; - SET @fna_workup_date := null; - SET @date_patient_notified_of_fna_results := null; - SET @biopsy_tumor_size := null; - SET @biopsy_degree_of_malignancy := null; - SET @biopsy_workup_date := null; - SET @date_patient_notified_of_biopsy_results := null; - SET @biopsy_description := null; - SET @diagnosis_date := null; - SET @date_patient_informed_and_referred_for_management := null; - SET @screening_mode := null; - SET @diagnosis := null; - SET @cancer_staging := null; - SET @hiv_status := null; - - DROP TEMPORARY TABLE IF EXISTS flat_breast_cancer_screening_1; - - CREATE TEMPORARY TABLE flat_breast_cancer_screening_1 - (SELECT - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - t1.encounter_id, - t1.encounter_type, - t1.encounter_datetime, - t1.visit_id, - -- t4.name as location_name, - t1.location_id, - t1.is_clinical_encounter, - p.gender, - p.death_date, - case - when timestampdiff(year,birthdate,curdate()) > 0 then round(timestampdiff(year,birthdate,curdate()),0) - else round(timestampdiff(month,birthdate,curdate())/12,2) - end as age, - case - when obs regexp "!!1834=9651!!" then @encounter_purpose := 1 - when obs regexp "!!1834=1154!!" then @encounter_purpose := 2 - when obs regexp "!!1834=1246!!" then @encounter_purpose := 3 - when obs regexp "!!1834=5622!!" then @encounter_purpose := 4 - else @encounter_purpose := null - end as encounter_purpose, - case - when obs regexp "!!1915=" then @other_encounter_purpose := GetValues(obs,1915) - else @other_encounter_purpose := null - end as other_encounter_purpose, - case - when obs regexp "!!9560=1065!!" then @menstruation_before_12 := 1 - when obs regexp "!!9560=1066!!" then @menstruation_before_12 := 0 - else @menstruation_before_12 := null - end as menstruation_before_12, - case - when obs regexp "!!9561=1065!!" then @menses_stopped_permanently := 1 - when obs regexp "!!9561=1066!!" then @menses_stopped_permanently := 0 - when obs regexp "!!9561=9568!!" then @menses_stopped_permanently := 2 - else @menses_stopped_permanently := null - end as menses_stopped_permanently, - case - when obs regexp "!!9562=[0-9]" and t1.encounter_type = @lab_encounter_type then @menses_stop_age:=cast(GetValues(obs,9562) as unsigned) - when @prev_id = @cur_id then @menses_stop_age - else @menses_stop_age := null - end as menses_stop_age, - case - when obs regexp "!!9626=1065!!" then @hrt_use := 1 - when obs regexp "!!9626=1066!!" then @hrt_use := 0 - when obs regexp "!!9626=9568!!" then @hrt_use := 2 - else @hrt_use := null - end as hrt_use, - case - when obs regexp "!!9627=[0-9]" then @hrt_start_age := cast(GetValues(obs,9627) as unsigned) - else @hrt_start_age := null - end as hrt_start_age, - case - when obs regexp "!!9723=[0-9]" then @hrt_end_age := cast(GetValues(obs,9723) as unsigned) - else @hrt_end_age := null - end as hrt_end_age, - case - when obs regexp "!!97629=[0-9]" then @hrt_use_years := cast(GetValues(obs,9629) as unsigned) - else @hrt_use_years := null - end as hrt_use_years, - case - when obs regexp "!!9630=9573!!" then @hrt_type_used := 1 - when obs regexp "!!9630=6217!!" then @hrt_type_used := 2 - when obs regexp "!!9630=6218!!" then @hrt_type_used := 3 - else @hrt_type_used := null - end as hrt_type_used, - case - when obs regexp "!!9563=1065!!" then @given_birth := 1 - when obs regexp "!!9563=1066!!" then @given_birth := 0 - else @given_birth := null - end as given_birth, - case - when obs regexp "!!5574=[0-9]" then @age_first_birth := cast(GetValues(obs,5574) as unsigned) - else @age_first_birth := null - end as age_first_birth, - case - when obs regexp "!!5624=[0-9]" then @gravida := cast(GetValues(obs,5624) as unsigned) - else @gravida := null - end as gravida, - case - when obs regexp "!!1053=[0-9]" then @parity := cast(GetValues(obs,1053) as unsigned) - else @parity := null - end as parity, - case - when obs regexp "!!9333=" then @cigarette_smoking := GetValues(obs,9333) - else @cigarette_smoking := null - end as cigarette_smoking, - case - when obs regexp "!!2069=[0-9]" then @cigarette_smoked_day := cast(GetValues(obs,2069) as unsigned) - else @cigarette_smoked_day := null - end as cigarette_smoked_day, - case - when obs regexp "!!7973=1065!!" then @tobacco_use := 1 - when obs regexp "!!7973=1066!!" then @tobacco_use := 0 - when obs regexp "!!7973=1679!!" then @tobacco_use := 2 - else @tobacco_use := null - end as tobacco_use, - case - when obs regexp "!!8144=[0-9]" then @tobacco_use_duration_yrs := cast(GetValues(obs, 8144) as unsigned) - else @tobacco_use_duration_yrs := null - end as tobacco_use_duration_yrs, - case - when obs regexp "!!1684=1065!!" then @alcohol_drinking := 1 - when obs regexp "!!1684=1066!!" then @alcohol_drinking := 0 - when obs regexp "!!1684=1679!!" then @alcohol_drinking := 2 - else @alcohol_drinking := null - end as alcohol_drinking, - case - when obs regexp "!!1685=1682!!" then @alcohol_type := 1 - when obs regexp "!!1685=1681!!" then @alcohol_type := 2 - when obs regexp "!!1685=1680!!" then @alcohol_type := 3 - when obs regexp "!!1685=1683!!" then @alcohol_type := 4 - when obs regexp "!!1685=2059!!" then @alcohol_type := 5 - when obs regexp "!!1685=5622!!" then @alcohol_type := 6 - else @alcohol_type := null - end as alcohol_type, - case - when obs regexp "!!8170=[0-9]" then @alcohol_use_period_yrs := cast(GetValues(obs,8170) as unsigned) - else @alcohol_use_period_yrs := null - end as alcohol_use_period_yrs, - case - when obs regexp "!!9553=5006!!" then @breast_complaints_3months := 1 - when obs regexp "!!9553=1068!!" then @breast_complaints_3months := 2 - else @breast_complaints_3months := null - end as breast_complaints_3months, - case - when obs regexp "!!6697=2399!!" then @breast_mass_location := 1 - when obs regexp "!!6697=5139!!" then @breast_mass_location := 2 - when obs regexp "!!6697=5141!!" then @breast_mass_location := 3 - when obs regexp "!!6697=6589!!" then @breast_mass_location := 4 - when obs regexp "!!6697=6590!!" then @breast_mass_location := 5 - when obs regexp "!!6697=6591!!" then @breast_mass_location := 6 - when obs regexp "!!6697=9625!!" then @breast_mass_location := 7 - else @breast_mass_location := null - end as breast_mass_location, - case - when obs regexp "!!9557=5139!!" then @nipple_discharge_location := 1 - when obs regexp "!!9557=5141!!" then @nipple_discharge_location := 2 - when obs regexp "!!9557=9625!!" then @nipple_discharge_location := 3 - else @nipple_discharge_location := null - end as nipple_discharge_location, - case - when obs regexp "!!9558=5139!!" then @nipple_retraction_location := 1 - when obs regexp "!!9558=5141!!" then @nipple_retraction_location := 2 - when obs regexp "!!9558=9625!!" then @nipple_retraction_location := 3 - else @nipple_retraction_location := null - end as nipple_retraction_location, - case - when obs regexp "!!9574=5139!!" then @breast_erythrema_location := 1 - when obs regexp "!!9574=5141!!" then @breast_erythrema_location := 2 - when obs regexp "!!9574=9625!!" then @breast_erythrema_location := 3 - else @breast_erythrema_location := null - end as breast_erythrema_location, - case - when obs regexp "!!9577=5139!!" then @breast_rash_location := 1 - when obs regexp "!!9577=5141!!" then @breast_rash_location := 2 - when obs regexp "!!9577=9625!!" then @breast_rash_location := 3 - else @breast_rash_location := null - end as breast_rash_location, - case - when obs regexp "!!9577=5139!!" then @breast_pain_location := 1 - when obs regexp "!!9577=5141!!" then @breast_pain_location := 2 - when obs regexp "!!9577=9625!!" then @breast_pain_location := 3 - else @breast_pain_location := null - end as breast_pain_location, - case - when obs regexp "!!9576=5139!!" then @other_changes_location := 1 - when obs regexp "!!9576=5141!!" then @other_changes_location := 2 - when obs regexp "!!9576=9625!!" then @other_changes_location := 3 - else @other_changes_location := null - end as other_changes_location, - case - when obs regexp "!!9594=1066!!" then @history_of_mammogram := 0 - when obs regexp "!!9594=1065!!" then @history_of_mammogram := 1 - else @history_of_mammogram := null - end as history_of_mammogram, - case - when obs regexp "!!9595=1115!!" then @mammogram_results := 1 - when obs regexp "!!9595=1116!!" then @mammogram_results := 2 - when obs regexp "!!9595=1118!!" then @mammogram_results := 3 - when obs regexp "!!9595=1138!!" then @mammogram_results := 4 - when obs regexp "!!9595=1267!!" then @mammogram_results := 5 - when obs regexp "!!9595=1067!!" then @mammogram_results := 6 - else @mammogram_results := null - end as mammogram_results, - case - when obs regexp "!!9597=1066!!" then @breast_ultrasound_history := 0 - when obs regexp "!!9597=1065!!" then @breast_ultrasound_history := 1 - else @breast_ultrasound_history := null - end as breast_ultrasound_history, - case - when obs regexp "!!9596=1115!!" then @breast_ultrasound_result := 1 - when obs regexp "!!9596=1116!!" then @breast_ultrasound_result := 2 - when obs regexp "!!9596=1067!!" then @breast_ultrasound_result := 3 - when obs regexp "!!9596=1118!!" then @breast_ultrasound_result := 4 - else @breast_ultrasound_result := null - end as breast_ultrasound_result, - case - when obs regexp "!!9598=1066!!" then @history_of_breast_biopsy := 0 - when obs regexp "!!9598=1065!!" then @history_of_breast_biopsy := 1 - else @history_of_breast_biopsy := null - end as history_of_breast_biopsy, - case - when obs regexp "!!8184=1115!!" then @breast_biopsy_results := 1 - when obs regexp "!!8184=1116!!" then @breast_biopsy_results := 2 - when obs regexp "!!8184=1118!!" then @breast_biopsy_results := 3 - when obs regexp "!!8184=1067!!" then @breast_biopsy_results := 4 - when obs regexp "!!8184=9691!!" then @breast_biopsy_results := 5 - when obs regexp "!!8184=10052!!" then @breast_biopsy_results := 6 - else @breast_biopsy_results := null - end as breast_biopsy_results, - case - when obs regexp "!!9599=" then @number_of_biopsies := GetValues(obs, 9599) - else @number_of_biopsies := null - end as number_of_biopsies, - case - when obs regexp "!!6509=6510!!" then @biopsy_type := 1 - when obs regexp "!!6509=6511!!" then @biopsy_type := 2 - when obs regexp "!!6509=6512!!" then @biopsy_type := 3 - when obs regexp "!!6509=6513!!" then @biopsy_type := 4 - when obs regexp "!!6509=7190!!" then @biopsy_type := 5 - when obs regexp "!!6509=8184!!" then @biopsy_type := 6 - when obs regexp "!!6509=10075!!" then @biopsy_type := 7 - when obs regexp "!!6509=10076!!" then @biopsy_type := 8 - else @biopsy_type := null - end as biopsy_type, - case - when obs regexp "!!9694=1115!!" then @prev_exam_results := 1 - when obs regexp "!!9694=1116!!" then @prev_exam_results := 0 - when obs regexp "!!9694=1067!!" then @prev_exam_results := 2 - else @prev_exam_results := null - end as prev_exam_results, - case - when obs regexp "!!9631=1672!!" then @fam_brca_history_bf50 := 1 - when obs regexp "!!9631=1107!!" then @fam_brca_history_bf50 := 0 - when obs regexp "!!9631=978!!" then @fam_brca_history_bf50 := 2 - when obs regexp "!!9631=972!!" then @fam_brca_history_bf50 := 3 - when obs regexp "!!9631=1671!!" then @fam_brca_history_bf50 := 4 - when obs regexp "!!9631=1393!!" then @fam_brca_history_bf50 := 5 - when obs regexp "!!9631=1392!!" then @fam_brca_history_bf50 := 6 - when obs regexp "!!9631=1395!!" then @fam_brca_history_bf50 := 7 - when obs regexp "!!9631=1394!!" then @fam_brca_history_bf50 := 8 - else @fam_brca_history_bf50 := null - end as fam_brca_history_bf50, - case - when obs regexp "!!9632=978!!" then @fam_brca_history_aft50 := 1 - when obs regexp "!!9632=1672!!" then @fam_brca_history_aft50 := 2 - when obs regexp "!!9632=972!!" then @fam_brca_history_aft50 := 3 - when obs regexp "!!9632=1671!!" then @fam_brca_history_aft50 := 4 - when obs regexp "!!9632=1393!!" then @fam_brca_history_aft50 := 5 - when obs regexp "!!9632=1392!!" then @fam_brca_history_aft50 := 6 - when obs regexp "!!9632=1395!!" then @fam_brca_history_aft50 := 7 - when obs regexp "!!9632=1394!!" then @fam_brca_history_aft50 := 8 - else @fam_brca_history_aft50 := null - end as fam_brca_history_aft50, - case - when obs regexp "!!9633=978!!" then @fam_male_brca_history := 1 - when obs regexp "!!9633=1672!!" then @fam_male_brca_history := 2 - when obs regexp "!!9633=972!!" then @fam_male_brca_history := 3 - when obs regexp "!!9633=1671!!" then @fam_male_brca_history := 4 - when obs regexp "!!9633=1393!!" then @fam_male_brca_history := 5 - when obs regexp "!!9633=1392!!" then @fam_male_brca_history := 6 - when obs regexp "!!9633=1395!!" then @fam_male_brca_history := 7 - when obs regexp "!!9633=1394!!" then @fam_male_brca_history := 8 - else @fam_male_brca_history := null - end as fam_male_brca_history, - case - when obs regexp "!!9634=978!!" then @fam_ovarianca_history := 1 - when obs regexp "!!9634=1672!!" then @fam_ovarianca_history := 2 - when obs regexp "!!9634=972!!" then @fam_ovarianca_history := 3 - when obs regexp "!!9634=1671!!" then @fam_ovarianca_history := 4 - when obs regexp "!!9634=1393!!" then @fam_ovarianca_history := 5 - when obs regexp "!!9634=1392!!" then @fam_ovarianca_history := 6 - when obs regexp "!!9634=1395!!" then @fam_ovarianca_history := 7 - when obs regexp "!!9634=1394!!" then @fam_ovarianca_history := 8 - else @fam_ovarianca_history := null - end as fam_ovarianca_history, - case - when obs regexp "!!9635=978!!" then @fam_relatedca_history:= 1 - when obs regexp "!!9635=1672!!" then @fam_relatedca_history := 2 - when obs regexp "!!9635=972!!" then @fam_relatedca_history := 3 - when obs regexp "!!9635=1671!!" then @fam_relatedca_history := 4 - when obs regexp "!!9635=1393!!" then @fam_relatedca_history := 5 - when obs regexp "!!9635=1392!!" then @fam_relatedca_history := 6 - when obs regexp "!!9635=1395!!" then @fam_relatedca_history := 7 - when obs regexp "!!9635=1394!!" then @fam_relatedca_history := 8 - else @fam_relatedca_history := null - end as fam_relatedca_history, - case - when obs regexp "!!7176=6529!!" then @fam_otherca_specify := 1 - when obs regexp "!!7176=9636!!" then @fam_otherca_specify := 2 - when obs regexp "!!7176=9637!!" then @fam_otherca_specify := 3 - when obs regexp "!!7176=6485!!" then @fam_otherca_specify := 4 - when obs regexp "!!7176=9638!!" then @fam_otherca_specify := 5 - when obs regexp "!!7176=9639!!" then @fam_otherca_specify := 6 - when obs regexp "!!7176=6522!!" then @fam_otherca_specify := 5 - when obs regexp "!!7176=216!!" then @fam_otherca_specify := 6 - else @fam_otherca_specify := null - end as fam_otherca_specify, - case - when obs regexp "!!6251=1115!!" then @cur_physical_findings:= 0 - when obs regexp "!!6251=115!!" then @cur_physical_findings := 1 - when obs regexp "!!6251=6250!!" then @cur_physical_findings := 2 - when obs regexp "!!6251=6249!!" then @cur_physical_findings := 3 - when obs regexp "!!6251=5622!!" then @cur_physical_findings := 4 - when obs regexp "!!6251=582!!" then @cur_physical_findings := 5 - when obs regexp "!!6251=6493!!" then @cur_physical_findings := 6 - when obs regexp "!!6251=6499!!" then @cur_physical_findings := 7 - when obs regexp "!!6251=1118!!" then @cur_physical_findings := 8 - when obs regexp "!!6251=1481!!" then @cur_physical_findings := 9 - when obs regexp "!!6251=6729!!" then @cur_physical_findings := 10 - when obs regexp "!!6251=1116!!" then @cur_physical_findings := 11 - when obs regexp "!!6251=8188!!" then @cur_physical_findings := 12 - when obs regexp "!!6251=8189!!" then @cur_physical_findings := 13 - when obs regexp "!!6251=1067!!" then @cur_physical_findings := 14 - when obs regexp "!!6251=9689!!" then @cur_physical_findings := 15 - when obs regexp "!!6251=9690!!" then @cur_physical_findings := 16 - when obs regexp "!!6251=9687!!" then @cur_physical_findings := 17 - when obs regexp "!!6251=9688!!" then @cur_physical_findings := 18 - when obs regexp "!!6251=5313!!" then @cur_physical_findings := 19 - when obs regexp "!!6251=9691!!" then @cur_physical_findings := 20 - else @cur_physical_findings := null - end as cur_physical_findings, - case - when obs regexp "!!1121=1115!!" then @lymph_nodes_findings := 1 - when obs regexp "!!1121=161!!" then @lymph_nodes_findings := 2 - when obs regexp "!!1121=9675!!" then @lymph_nodes_findings:= 3 - when obs regexp "!!1121=9676!!" then @lymph_nodes_findings:= 4 - else @lymph_nodes_findings := null - end as lymph_nodes_findings, - case - when obs regexp "!!9748=1115!!" then @cur_screening_findings := 1 - when obs regexp "!!9748=9691!!" then @cur_screening_findings := 3 - when obs regexp "!!9748=1116!!" then @cur_screening_findings := 2 - else @cur_screening_findings := null - end as cur_screening_findings, - case - when obs regexp "!!6327=9651!!" then @patient_education := 1 - when obs regexp "!!6327=2345!!" then @patient_education := 2 - when obs regexp "!!6327=9692!!" then @patient_education:= 3 - when obs regexp "!!6327=5622!!" then @patient_education:= 4 - else @patient_education := null - end as patient_education, - case - when obs regexp "!!1915=" then @patient_education_other := GetValues(obs,1915) - else @patient_education_other := null - end as patient_education_other, - case - when obs regexp "!!1272=1107!!" then @referrals_ordered := 0 - when obs regexp "!!1272=1496!!" then @referrals_ordered:= 1 - else @referrals_ordered := null - end as referrals_ordered, - case - when obs regexp "!!1272=9596!!" then @procedure_done := 1 - when obs regexp "!!1272=9595!!" then @procedure_done:= 2 - when obs regexp "!!1272=6510!!" then @procedure_done := 3 - when obs regexp "!!1272=7190!!" then @procedure_done:= 4 - when obs regexp "!!1272=6511!!" then @procedure_done := 5 - when obs regexp "!!1272=9997!!" then @procedure_done:= 6 - else @procedure_done := null - end as procedure_done, - case - when obs regexp "!!9158=" then @referred_date := GetValues(obs,9158) - else @referred_date := null - end as referred_date, - case - when obs regexp "!!5096=" then @next_app_date := GetValues(obs,5096) - else @next_app_date := null - end as next_app_date, - case - when obs regexp "!!9702=9703!!" then @cbe_imaging_concordance := 1 - when obs regexp "!!9702=9704!!" then @cbe_imaging_concordance := 2 - when obs regexp "!!9702=1175!!" then @cbe_imaging_concordance := 3 - else @cbe_imaging_concordance := null - end as cbe_imaging_concordance, - case - when obs regexp "!!9708=" then @mammogram_workup_date := GetValues(obs,9708) - else @mammogram_workup_date := null - end as mammogram_workup_date, - case - when obs regexp "!!9705=" then @date_patient_notified_of_mammogram_results := GetValues(obs,9705) - else @date_patient_notified_of_mammogram_results := null - end as date_patient_notified_of_mammogram_results, - case - when obs regexp "!!9596=1115!!" then @ultrasound_results := 1 - when obs regexp "!!9596=1116!!" then @ultrasound_results := 2 - when obs regexp "!!9596=1067!!" then @ultrasound_results := 3 - when obs regexp "!!9596=1118!!" then @ultrasound_results := 4 - else @ultrasound_results := null - end as ultrasound_results, - case - when obs regexp "!!9708=" then @ultrasound_workup_date := GetValues(obs,9708) - else @ultrasound_workup_date := null - end as ultrasound_workup_date, - case - when obs regexp "!!10047=" then @date_patient_notified_of_ultrasound_results := GetValues(obs,10047) - else @date_patient_notified_of_ultrasound_results := null - end as date_patient_notified_of_ultrasound_results, - case - when obs regexp "!!10051=9691!!" then @fna_results := 1 - when obs regexp "!!10051=10052!!" then @fna_results := 2 - when obs regexp "!!9596=1118!!" then @fna_results := 3 - else @fna_results := null - end as fna_results, - case - when obs regexp "!!10053=[0-9]" then @fna_tumor_size:= cast(GetValues(obs,10053) as unsigned) - else @fna_tumor_size := null - end as fna_tumor_size, - case - when obs regexp "!!10054=10055!!" then @fna_degree_of_malignancy := 1 - when obs regexp "!!10054=10056!!" then @fna_degree_of_malignancy := 2 - else @fna_degree_of_malignancy := null - end as fna_degree_of_malignancy, - case - when obs regexp "!!10057=" then @fna_workup_date := GetValues(obs,10057) - else @fna_workup_date := null - end as fna_workup_date, - case - when obs regexp "!!10059=" then @date_patient_notified_of_fna_results := GetValues(obs,10059) - else @date_patient_notified_of_fna_results := null - end as date_patient_notified_of_fna_results, - case - when obs regexp "!!10053=[0-9]" then @biopsy_tumor_size:= cast(GetValues(obs,10053) as unsigned) - else @biopsy_tumor_size := null - end as biopsy_tumor_size, - case - when obs regexp "!!10054=10055!!" then @biopsy_degree_of_malignancy := 1 - when obs regexp "!!10054=10056!!" then @biopsy_degree_of_malignancy := 2 - else @biopsy_degree_of_malignancy := null - end as biopsy_degree_of_malignancy, - case - when obs regexp "!!10060=" then @biopsy_workup_date := GetValues(obs,10060) - else @biopsy_workup_date := null - end as biopsy_workup_date, - case - when obs regexp "!!10061=" then @date_patient_notified_of_biopsy_resultse := GetValues(obs,10061) - else @date_patient_notified_of_biopsy_results := null - end as date_patient_notified_of_biopsy_results, - case - when obs regexp "!!7400=" then @biopsy_description := GetValues(obs,7400) - else @biopsy_description := null - end as biopsy_description, - case - when obs regexp "!!9728=" then @diagnosis_date := GetValues(obs, 9728) - else @diagnosis_date := null - end as diagnosis_date, - case - when obs regexp "!!9706=" then @date_patient_informed_and_referred_for_management := GetValues(obs, 9706) - else @date_patient_informed_and_referred_for_management := null - end as date_patient_informed_and_referred_for_management, - case - when obs regexp "!!10068=9595!!" then @screening_mode := 1 - when obs regexp "!!10068=10067!!" then @screening_mode := 2 - when obs regexp "!!10068=9596!!" then @screening_mode := 3 - else @screening_mode := null - end as screening_mode, - case - when obs regexp "!!6042=" then @diagnosis := GetValues(obs,6042) - else @diagnosis := null - end as diagnosis, - case - when obs regexp "!!9868=9852!!" then @cancer_staging := 1 - when obs regexp "!!9868=9856!!" then @cancer_staging := 2 - when obs regexp "!!9868=9860!!" then @cancer_staging := 3 - when obs regexp "!!9868=9864!!" then @cancer_staging := 4 - else @cancer_staging := null - end as cancer_staging, - case - when obs regexp "!!6709=664!!" then @hiv_status := 1 - when obs regexp "!!6709=703!!" then @hiv_status := 2 - when obs regexp "!!6709=1067!!" then @hiv_status := 3 - else @hiv_status := null - end as hiv_status - FROM - flat_breast_cancer_screening_0 t1 - JOIN amrs.person p using (person_id) - ORDER BY person_id, date(encounter_datetime) DESC, encounter_type_sort_index DESC - ); - - SET @prev_id = null; - SET @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - ALTER TABLE flat_breast_cancer_screening_1 DROP prev_id, DROP cur_id; - - drop table if exists flat_breast_cancer_screening_2; - create temporary table flat_breast_cancer_screening_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_breast_cancer_screening, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_breast_cancer_screening, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_breast_cancer_screening, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date - - from flat_breast_cancer_screening_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_breast_cancer_screening_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_breast_cancer_screening_3; - create temporary table flat_breast_cancer_screening_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_breast_cancer_screening, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_breast_cancer_screening, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_breast_cancer_screening, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_breast_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_breast_cancer_screening_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - SELECT - COUNT(*) - INTO - @new_encounter_rows - FROM - flat_breast_cancer_screening_1; - - SELECT @new_encounter_rows; - SET @total_rows_written := @total_rows_written + @new_encounter_rows; - SELECT @total_rows_written; - - SET @dyn_sql := CONCAT('REPLACE INTO ', @write_table, - '(SELECT - null, - person_id, - encounter_id, - encounter_type, - encounter_datetime, - visit_id, - location_id, - t2.uuid as location_uuid, - gender, - age, - encounter_purpose, - other_encounter_purpose, - menstruation_before_12, - menses_stopped_permanently, - menses_stop_age, - hrt_use, - hrt_start_age, - hrt_end_age, - hrt_use_years, - hrt_type_used, - given_birth, - age_first_birth, - gravida, - parity, - cigarette_smoking, - cigarette_smoked_day, - tobacco_use, - tobacco_use_duration_yrs, - alcohol_drinking, - alcohol_type, - alcohol_use_period_yrs, - breast_complaints_3months, - breast_mass_location, - nipple_discharge_location, - nipple_retraction_location, - breast_erythrema_location, - breast_rash_location, - breast_pain_location, - other_changes_location, - history_of_mammogram, - mammogram_results, - breast_ultrasound_history, - breast_ultrasound_result, - history_of_breast_biopsy, - breast_biopsy_results, - number_of_biopsies, - biopsy_type, - prev_exam_results, - fam_brca_history_bf50, - fam_brca_history_aft50, - fam_male_brca_history, - fam_ovarianca_history, - fam_relatedca_history, - fam_otherca_specify, - cur_physical_findings, - lymph_nodes_findings, - cur_screening_findings, - #cur_screening_findings_date, - patient_education, - patient_education_other, - referrals_ordered, - referred_date, - procedure_done, - next_app_date, - cbe_imaging_concordance, - mammogram_workup_date, - date_patient_notified_of_mammogram_results, - ultrasound_results, - ultrasound_workup_date, - date_patient_notified_of_ultrasound_results, - fna_results, - fna_tumor_size, - fna_degree_of_malignancy, - fna_workup_date, - date_patient_notified_of_fna_results, - biopsy_tumor_size, - biopsy_degree_of_malignancy, - biopsy_workup_date, - date_patient_notified_of_biopsy_results, - biopsy_description, - diagnosis_date, - date_patient_informed_and_referred_for_management, - screening_mode, - diagnosis, - cancer_staging, - hiv_status, - prev_encounter_datetime_breast_cancer_screening, - next_encounter_datetime_breast_cancer_screening, - prev_encounter_type_breast_cancer_screening, - next_encounter_type_breast_cancer_screening, - prev_clinical_datetime_breast_cancer_screening, - next_clinical_datetime_breast_cancer_screening, - prev_clinical_location_id_breast_cancer_screening, - next_clinical_location_id_breast_cancer_screening, - prev_clinical_rtc_date_breast_cancer_screening, - next_clinical_rtc_date_breast_cancer_screening - FROM - flat_breast_cancer_screening_3 t1 - JOIN - amrs.location t2 USING (location_id))' - ); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('DELETE t1 from ',@queue_table,' t1 JOIN flat_breast_cancer_screening_build_queue__0 t2 USING (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('SELECT COUNT(*) INTO @person_ids_count FROM ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @cycle_length = TIMESTAMPDIFF(second,@loop_start_time,NOW()); - SET @total_time = @total_time + @cycle_length; - SET @cycle_number = @cycle_number + 1; - - SET @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - SELECT - @person_ids_count AS 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - END WHILE; - - IF (@query_type = 'build') THEN - SET @dyn_sql := CONCAT('drop table ', @queue_table, ';'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write := 0; - SET @dyn_sql := CONCAT('SELECT COUNT(*) INTO @total_rows_to_write FROM ', @write_table); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @start_write := NOW(); - SELECT CONCAT(@start_write, ' : Writing ', @total_rows_to_write, ' to ', @primary_table); - - SET @dyn_sql := CONCAT('REPLACE INTO ', @primary_table, '(SELECT * FROM ', @write_table, ');'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @finish_write := NOW(); - SET @time_to_write := TIMESTAMPDIFF(SECOND, @start_write, @finish_write); - SELECT CONCAT(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql := CONCAT('DROP TABLE ', @write_table, ';'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - END IF; - - SET @ave_cycle_length := CEIL(@total_time / @cycle_number); - SELECT CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - SET @end := NOW(); - INSERT INTO etl.flat_log VALUES (@start, @last_date_created, @table_version, TIMESTAMPDIFF(SECOND, @start, @end)); - SELECT CONCAT(@table_version, ': Time to complete: ', TIMESTAMPDIFF(MINUTE, @start, @end), ' minutes'); -END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_breast_cancer_treatment_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_breast_cancer_treatment.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_flat_breast_cancer_treatment_v1_0.sql rename to etl-scripts/stored-procedures/generate_flat_breast_cancer_treatment.sql index 6a8f91d..95ab9cd 100644 --- a/etl-scripts/stored-procedures/generate_flat_breast_cancer_treatment_v1_0.sql +++ b/etl-scripts/stored-procedures/generate_flat_breast_cancer_treatment.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_breast_cancer_treatment_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_breast_cancer_treatment`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN set @primary_table := "flat_breast_cancer_treatment"; set @query_type = query_type; diff --git a/etl-scripts/stored-procedures/generate_flat_cdm.sql b/etl-scripts/stored-procedures/generate_flat_cdm.sql index f7335d4..7a1899a 100644 --- a/etl-scripts/stored-procedures/generate_flat_cdm.sql +++ b/etl-scripts/stored-procedures/generate_flat_cdm.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_cdm_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_cdm`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN set @primary_table := "flat_cdm"; set @query_type = query_type; @@ -739,4 +739,4 @@ BEGIN select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); END$$ -DELIMITER ; +DELIMITER ; \ No newline at end of file diff --git a/etl-scripts/stored-procedures/generate_flat_cdm_peer_navigation_v1_2.sql b/etl-scripts/stored-procedures/generate_flat_cdm_peer_navigation.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_flat_cdm_peer_navigation_v1_2.sql rename to etl-scripts/stored-procedures/generate_flat_cdm_peer_navigation.sql index c8f6ee2..8b30f35 100644 --- a/etl-scripts/stored-procedures/generate_flat_cdm_peer_navigation_v1_2.sql +++ b/etl-scripts/stored-procedures/generate_flat_cdm_peer_navigation.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_cdm_peer_navigation_v1_2`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_cdm_peer_navigation`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN set @primary_table := "flat_cdm_peer_navigation"; set @total_rows_written = 0; diff --git a/etl-scripts/stored-procedures/generate_flat_cdm_peer_navigation_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_cdm_peer_navigation_v1_0.sql deleted file mode 100644 index 01b2928..0000000 --- a/etl-scripts/stored-procedures/generate_flat_cdm_peer_navigation_v1_0.sql +++ /dev/null @@ -1,614 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_cdm_peer_navigation_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_cdm_peer_navigation"; - set @total_rows_written = 0; - set @query_type = query_type; - set @clinical_encounter_types = "(-1)"; - - - set @start = now(); - set @table_version := "flat_cdm_peer_navigation_v1.0"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @boundary = "!!"; - set @lab_encounter_type := 99999; - set @death_encounter_type := 31; - set @last_date_created := (select max(max_date_created) from etl.flat_obs); - set @encounter_types = "(192,193)"; -SELECT 'variables initialized ...'; - -SET @dyn_sql=CONCAT('drop table ',@primary_table,';'); -PREPARE s1 from @dyn_sql; -EXECUTE s1; -DEALLOCATE PREPARE s1; - - - -CREATE TABLE IF NOT EXISTS flat_cdm_peer_navigation ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id INT, - encounter_datetime TIMESTAMP, - encounter_id INT, - encounter_type INT, - location_id INT, - PRIMARY KEY encounter_id (encounter_id), - INDEX date_created (date_created), - referal_category SMALLINT, - lifestyle_modification_plan INT, - current_medication_regimen INT, - understand_bp_meds_and_purpose INT, - understand_meds_frequency INT, - last_hypersenitive_regimen_change INT, - missed_bp_meds_7days INT, - reason_meds_missed INT, - reason_for_referal INT, - barries_to_complete_referral INT, - concerns_preventing_referral INT, - patient_referal_status INT, - is_referal_completed INT, - referal_preparation_discussed INT, - next_appointment_facility INT -); - -SELECT 'table created ...'; - - - - if(@query_type="build") then - select 'BUILDING..........................................'; - set @write_table = concat("flat_cdm_peer_navigation_temp_",queue_number); - set @queue_table = concat("flat_cdm_peer_navigation_build_queue_",queue_number); - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_cdm_peer_navigation_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from flat_cdm_peer_navigation_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_cdm_peer_navigation_test"; - set @queue_table = "flat_cdm_peer_navigation_test_sync_queue"; -CREATE TABLE IF NOT EXISTS flat_cdm_peer_navigation_test_sync_queue ( - person_id INT PRIMARY KEY -); - - set @last_update = null; - -SELECT - MAX(date_updated) -INTO @last_update FROM - etl.flat_log -WHERE - table_name = @table_version; - - - replace into flat_cdm_peer_navigation_test_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_cdm_peer_navigation_test_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - replace into flat_cdm_peer_navigation_test_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_cdm_peer_navigation_test_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_cdm_peer_navigation_test_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_cdm_peer_navigation_test_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - end if; - - - - - SELECT 'Working on test patients ...'; - - - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SELECT @person_ids_count AS 'num patients to sync'; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - - drop temporary table if exists flat_cdm_peer_navigation_build_queue__0; - - - - SET @dyn_sql=CONCAT('create temporary table flat_cdm_peer_navigation_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - drop temporary table if exists flat_cdm_peer_navigation_0a; - create temporary table flat_cdm_peer_navigation_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - l.name as `clinic`, - t1.obs, - t1.obs_datetimes, - - - case - when t1.encounter_type in (193,194) then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (3,4,115) then 10 - when t1.encounter_type in (129) then 5 - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join flat_cdm_peer_navigation_build_queue__0 t0 using (person_id) - join amrs.location l using (location_id) - left join etl.flat_orders t2 using(encounter_id) - where t1.encounter_type in (193,194) - ); - - insert into flat_cdm_peer_navigation_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_cdm_peer_navigation_build_queue__0 t0 using (person_id) - ); - - drop temporary table if exists flat_cdm_peer_navigation_0; - create temporary table flat_cdm_peer_navigation_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_cdm_peer_navigation_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @prev_id = null; - set @cur_id = null; - set @referal_category := null; - set @lifestyle_modification_plan := null; - set @current_medication_regimen := null; - set @understand_bp_meds_and_purpose :=null; - set @understand_meds_frequency := null ; - set @last_hypersenitive_regimen_change :=null ; - set @missed_bp_meds_7days :=null; - set @reason_meds_missed :=null; - set @reason_for_referal :=null; - set @barries_to_complete_referral :=null; - set @barries_prevent_referreal := null; - set @patient_referal_status := null ; - set @referal_preparation_discussed :=null ; - set @is_referal_completed := null ; - set @concerns_preventing_referral :=null ; - set @next_appointment_facility :=null ; - - drop temporary table if exists flat_cdm_peer_navigation_1; - create temporary table flat_cdm_peer_navigation_1 (index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - t1.encounter_id, - t1.encounter_type, - t1.encounter_datetime, - t1.location_id, - #t1.date_created, - case - when obs regexp "!!10494=8989!!" then @referal_category := 1 - when obs regexp "!!10494=7316!!" then @referal_category := 2 - else @referal_category := null - end as referal_category, - case - when obs regexp "!!9147=1065!!" then @lifestyle_modification_plan := 1 - when obs regexp "!!9147=1066!!" then @lifestyle_modification_plan := 2 - else @lifestyle_modification_plan := null - end as lifestyle_modification_plan, - case - when obs regexp "!!10241=1242!!" then @current_medication_regimen := 1 - when obs regexp "!!10241=1243!!" then @current_medication_regimen := 2 - when obs regexp "!!10241=2265!!" then @current_medication_regimen := 3 - when obs regexp "!!10241=250!!" then @current_medication_regimen := 4 - when obs regexp "!!10241=2267!!" then @current_medication_regimen := 5 - when obs regexp "!!10241=2276!!" then @current_medication_regimen := 6 - when obs regexp "!!10241=7303!!" then @current_medication_regimen := 7 - when obs regexp "!!10241=8834!!" then @current_medication_regimen := 8 - when obs regexp "!!10241=8836!!" then @current_medication_regimen := 9 - else @current_medication_regimen := null - end as current_medication_regimen, - case - when obs regexp "!!1654=1065!!" then @understand_bp_meds_and_purpose := 1 - when obs regexp "!!1654=1066!!" then @understand_bp_meds_and_purpose := 2 - else @understand_bp_meds_and_purpose := null - end as understand_bp_meds_and_purpose, - case - when obs regexp "!!10451=1065!!" then @understand_meds_frequency := 1 - when obs regexp "!!10451=1066!!" then @understand_meds_frequency := 2 - else @understand_meds_frequency := null - end as understand_meds_frequency, - case - when obs regexp "!!10495=1107!!" then @last_hypersenitive_regimen_change := 1 - when obs regexp "!!10495=1930!!" then @last_hypersenitive_regimen_change := 2 - when obs regexp "!!10495=981!!" then @last_hypersenitive_regimen_change := 1 - when obs regexp "!!10495=1260!!" then @last_hypersenitive_regimen_change := 2 - else @last_hypersenitive_regimen_change := null - end as last_hypersenitive_regimen_change, - case - when obs regexp "!!10452=1065!!" then @missed_bp_meds_7days := 1 - when obs regexp "!!10452=1066!!" then @missed_bp_meds_7days := 2 - else @missed_bp_meds_7days := null - end as missed_bp_meds_7days, - case - when obs regexp "!!1668=9148!!" then @reason_meds_missed := 1 - when obs regexp "!!1668=1648!!" then @reason_meds_missed := 2 - when obs regexp "!!1668=1664!!" then @reason_meds_missed := 1 - when obs regexp "!!1668=6295!!" then @reason_meds_missed := 2 - when obs regexp "!!1668=6100!!" then @reason_meds_missed := 1 - when obs regexp "!!1668=1546!!" then @reason_meds_missed := 2 - when obs regexp "!!1668=1647!!" then @reason_meds_missed := 1 - when obs regexp "!!1668=9126!!" then @reason_meds_missed := 2 - when obs regexp "!!1668=10453!!" then @reason_meds_missed := 2 - when obs regexp "!!1668=1548!!" then @reason_meds_missed := 1 - when obs regexp "!!1668=5622!!" then @reason_meds_missed := 2 - else @reason_meds_missed := null - end as reason_meds_missed, - case - when obs regexp "!!2327=6411!!" then @reason_for_referal := 1 - when obs regexp "!!2327=6583!!" then @reason_for_referal := 2 - when obs regexp "!!2327=10496!!" then @reason_for_referal := 3 - when obs regexp "!!2327=1664!!" then @reason_for_referal := 4 - when obs regexp "!!2327=6451!!" then @reason_for_referal := 5 - when obs regexp "!!2327=1486!!" then @reason_for_referal := 6 - when obs regexp "!!2327=7342!!" then @reason_for_referal := 7 - when obs regexp "!!2327=1578!!" then @reason_for_referal := 8 - when obs regexp "!!2327=5622!!" then @reason_for_referal := 9 - when obs regexp "!!2327=1548!!" then @reason_for_referal := 10 - when obs regexp "!!2327=2329!!" then @reason_for_referal := 11 - when obs regexp "!!2327=2330!!" then @reason_for_referal := 12 - when obs regexp "!!2327=5484!!" then @reason_for_referal := 13 - when obs regexp "!!2327=6510!!" then @reason_for_referal := 14 - when obs regexp "!!2327=7465!!" then @reason_for_referal := 15 - when obs regexp "!!2327=6511!!" then @reason_for_referal := 16 - when obs regexp "!!2327=6502!!" then @reason_for_referal := 17 - when obs regexp "!!2327=7190!!" then @reason_for_referal := 18 - when obs regexp "!!2327=8262!!" then @reason_for_referal := 19 - else @reason_for_referal := null - end as reason_for_referal, - case - when obs regexp "!!10459=1107!!" then @barries_to_complete_referral := 1 - when obs regexp "!!10459=10458!!" then @barries_to_complete_referral := 2 - when obs regexp "!!10459=10457!!" then @barries_to_complete_referral := 1 - when obs regexp "!!10459=1578!!" then @barries_to_complete_referral := 2 - when obs regexp "!!10459=10455!!" then @barries_to_complete_referral := 1 - when obs regexp "!!10459=820!!" then @barries_to_complete_referral := 2 - when obs regexp "!!10459=1576!!" then @barries_to_complete_referral := 1 - when obs regexp "!!10459=1577!!" then @barries_to_complete_referral := 2 - when obs regexp "!!10459=1666!!" then @barries_to_complete_referral := 2 - when obs regexp "!!10459=10456!!" then @barries_to_complete_referral := 1 - when obs regexp "!!10459=1915!!" then @barries_to_complete_referral := 2 - else @barries_to_complete_referral := null - end as barries_to_complete_referral, - case - when obs regexp "!!10460=1065!!" then @barries_prevent_referreal := 1 - when obs regexp "!!10460=1066!!" then @barries_prevent_referreal := 2 - else @barries_prevent_referreal := null - end as barries_prevent_referreal, - case - when obs regexp "!!9082=10461!!" then @patient_referal_status := 1 - when obs regexp "!!9082=10462!!" then @patient_referal_status := 2 - when obs regexp "!!9082=10463!!" then @patient_referal_status := 3 - when obs regexp "!!9082=9083!!" then @patient_referal_status := 4 - when obs regexp "!!9082=1593!!" then @patient_referal_status := 5 - when obs regexp "!!9082=9079!!" then @patient_referal_status := 6 - when obs regexp "!!9082=1915!!" then @patient_referal_status := 7 - else @patient_referal_status := null - end as patient_referal_status, - case - when obs regexp "!!10467=10464!!" then @referal_preparation_discussed := 1 - when obs regexp "!!10467=6468!!" then @referal_preparation_discussed := 2 - when obs regexp "!!10467=10465!!" then @referal_preparation_discussed := 1 - when obs regexp "!!10467=10466!!" then @referal_preparation_discussed := 2 - else @referal_preparation_discussed := null - end as referal_preparation_discussed, - case - when obs regexp "!!10462=1065!!" then @is_referal_completed := 1 - when obs regexp "!!10462=1066!!" then @is_referal_completed := 2 - else @is_referal_completed := null - end as is_referal_completed, - case - when obs regexp "!!10486=1065!!" then @concerns_preventing_referral := 1 - when obs regexp "!!10486=1066!!" then @concerns_preventing_referral := 2 - else @concerns_preventing_referral := null - end as concerns_preventing_referral, - case - when obs regexp "!!10486=1065!!" then @next_appointment_facility := 1 - when obs regexp "!!10486=1066!!" then @next_appointment_facility := 2 - else @next_appointment_facility := null - end as next_appointment_facility - - from flat_cdm_peer_navigation_0 t1 - join amrs.person p using (person_id) - ); - - - - set @prev_id = null; - set @cur_id = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_cdm_peer_navigation_1 drop prev_id, drop cur_id; - - drop table if exists flat_cdm_peer_navigation_2; - create temporary table flat_cdm_peer_navigation_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id - - from flat_cdm_peer_navigation_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_cdm_peer_navigation_2 drop prev_id, drop cur_id; - - - set @prev_id = null; - set @cur_id = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_cdm_peer_navigation_3; - create temporary table flat_cdm_peer_navigation_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id - - from flat_cdm_peer_navigation_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - alter table flat_cdm_peer_navigation_3 drop prev_id, drop cur_id; - - set @prev_id = null; - set @cur_id = null; - - #Handle transfers - - drop temporary table if exists flat_cdm_peer_navigation_4; - - create temporary table flat_cdm_peer_navigation_4 ( index person_enc (person_id, encounter_datetime)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id - - - - from flat_cdm_peer_navigation_3 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - -SELECT - COUNT(*) -INTO @new_encounter_rows FROM - flat_cdm_peer_navigation_4; - -SELECT @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; -SELECT @total_rows_written; - - - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - person_id, - encounter_id, - encounter_datetime, - encounter_type , - location_id , - date_created , - referal_category, - lifestyle_modification_plan, - current_medication_regimen , - understand_bp_meds_and_purpose, - understand_meds_frequency , - last_hypersenitive_regimen_change, - missed_bp_meds_7days , - reason_meds_missed , - reason_for_referal , - barries_to_complete_referral , - concerns_preventing_referral , - patient_referal_status, - is_referal_completed, - referal_preparation_discussed, - next_appointment_facility - - from flat_cdm_peer_navigation_4 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_cdm_peer_navigation_build_queue__0 t2 using (person_id);'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - -SELECT - @person_ids_count AS 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - - SET @dyn_sql=CONCAT('describe ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; -SELECT - CONCAT(@start_write, - ' : Writing ', - @total_rows_to_write, - ' to ', - @primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); -SELECT - CONCAT(@finish_write, - ' : Completed writing rows. Time to write to primary table: ', - @time_to_write, - ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); -SELECT - CONCAT('Average Cycle Length: ', - @ave_cycle_length, - ' second(s)'); - - set @end = now(); -SELECT - CONCAT(@table_version, - ' : Time to complete: ', - TIMESTAMPDIFF(MINUTE, @start, @end), - ' minutes'); - - - -END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_cdm_peer_navigation_v1_1.sql b/etl-scripts/stored-procedures/generate_flat_cdm_peer_navigation_v1_1.sql deleted file mode 100644 index b01530c..0000000 --- a/etl-scripts/stored-procedures/generate_flat_cdm_peer_navigation_v1_1.sql +++ /dev/null @@ -1,603 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_cdm_peer_navigation_v1_1`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_cdm_peer_navigation"; - set @total_rows_written = 0; - set @query_type = query_type; - set @clinical_encounter_types = "(-1)"; - - - set @start = now(); - set @table_version := "flat_cdm_peer_navigation_v1.1"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @boundary = "!!"; - set @lab_encounter_type := 99999; - set @death_encounter_type := 31; - set @last_date_created := (select max(max_date_created) from etl.flat_obs); - set @encounter_types = "(192,193)"; -SELECT 'variables initialized ...'; - -SET @dyn_sql=CONCAT('drop table ',@primary_table,';'); -PREPARE s1 from @dyn_sql; -EXECUTE s1; -DEALLOCATE PREPARE s1; - - - -CREATE TABLE IF NOT EXISTS flat_cdm_peer_navigation ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id INT, - encounter_datetime TIMESTAMP, - encounter_id INT, - encounter_type INT, - location_id INT, - referal_category SMALLINT, - referred_in_or_out SMALLINT, - lifestyle_modification_plan INT, - current_medication_regimen INT, - understand_bp_meds_and_purpose INT, - understand_meds_frequency INT, - last_hypersenitive_regimen_change INT, - missed_bp_meds_7days INT, - reason_meds_missed INT, - reason_for_referal INT, - barries_to_complete_referral INT, - concerns_preventing_referral INT, - patient_referal_status INT, - is_referal_completed INT, - referal_preparation_discussed INT, - next_appointment_facility INT, - next_peer_outreach INT -); - -SELECT 'table created ...'; - - if(@query_type="build") then - select 'BUILDING..........................................'; - - set @write_table = concat("flat_cdm_peer_navigation_temp_",queue_number); - set @queue_table = concat("flat_cdm_peer_navigation_build_queue_",queue_number); - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_cdm_peer_navigation_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - /*SET @dyn_sql=CONCAT('delete t1 from flat_cdm_peer_navigation_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; */ - - - end if; - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_cdm_peer_navigation"; - set @queue_table = "flat_cdm_peer_navigation_test_sync_queue"; -CREATE TABLE IF NOT EXISTS flat_cdm_peer_navigation_test_sync_queue ( - person_id INT -); - -set @last_update = null; - -SELECT - MAX(date_updated) -INTO @last_update FROM - etl.flat_log -WHERE - table_name = @table_version; - - - replace into flat_cdm_peer_navigation_test_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_cdm_peer_navigation_test_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - replace into flat_cdm_peer_navigation_test_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_cdm_peer_navigation_test_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_cdm_peer_navigation_test_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_cdm_peer_navigation_test_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - end if; - - - - SELECT 'Working on test patients ...'; - - - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SELECT @person_ids_count AS 'num patients to sync'; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - - drop temporary table if exists flat_cdm_peer_navigation_build_queue__0; - - - - SET @dyn_sql=CONCAT('create temporary table flat_cdm_peer_navigation_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - drop temporary table if exists flat_cdm_peer_navigation_0a; - create temporary table flat_cdm_peer_navigation_0a - (select - t1.person_id, - t1.obs, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id - from etl.flat_obs t1 - join flat_cdm_peer_navigation_build_queue__0 t0 using (person_id) - where t1.encounter_type in (192,193) - ); - - insert into flat_cdm_peer_navigation_0a - (select - t1.person_id, - t1.obs, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - t1.location_id - from etl.flat_lab_obs t1 - join flat_cdm_peer_navigation_build_queue__0 t0 using (person_id) - ); - - drop temporary table if exists flat_cdm_peer_navigation_0; - create temporary table flat_cdm_peer_navigation_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_cdm_peer_navigation_0a - order by person_id, date(encounter_datetime) - ); - - - set @prev_id = null; - set @cur_id = null; - set @referal_category := null; - set @lifestyle_modification_plan := null; - set @current_medication_regimen := null; - set @understand_bp_meds_and_purpose :=null; - set @understand_meds_frequency := null ; - set @last_hypersenitive_regimen_change :=null ; - set @missed_bp_meds_7days :=null; - set @reason_meds_missed :=null; - set @reason_for_referal :=null; - set @barries_to_complete_referral :=null; - set @barries_prevent_referreal := null; - set @patient_referal_status := null ; - set @referal_preparation_discussed :=null ; - set @is_referal_completed := null ; - set @concerns_preventing_referral :=null ; - set @next_appointment_facility :=null ; - set @referred_in_or_out :=null; - set @next_peer_outreach :=null; - - drop temporary table if exists flat_cdm_peer_navigation_1; - create temporary table flat_cdm_peer_navigation_1 (index encounter_id (encounter_id)) - (select - obs, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - t1.encounter_datetime, - t1.encounter_id, - t1.encounter_type, - t1.location_id, - case - when obs regexp "!!10494=8989!!" then @referal_category := 1 - when obs regexp "!!10494=7316!!" then @referal_category := 2 - else @referal_category := null - end as referal_category, - case - when obs regexp "!!10114=10497!!" then @referred_in_or_out:= 1 - when obs regexp "!!10114=10112!!" then @referred_in_or_out := 2 - else @referred_in_or_out := null - end as referred_in_or_out, - case - when obs regexp "!!9147=1065!!" then @lifestyle_modification_plan := 1 - when obs regexp "!!9147=1066!!" then @lifestyle_modification_plan := 2 - else @lifestyle_modification_plan := null - end as lifestyle_modification_plan, - case - when obs regexp "!!10241=1242!!" then @current_medication_regimen := 1 - when obs regexp "!!10241=1243!!" then @current_medication_regimen := 2 - when obs regexp "!!10241=2265!!" then @current_medication_regimen := 3 - when obs regexp "!!10241=250!!" then @current_medication_regimen := 4 - when obs regexp "!!10241=2267!!" then @current_medication_regimen := 5 - when obs regexp "!!10241=2276!!" then @current_medication_regimen := 6 - when obs regexp "!!10241=7303!!" then @current_medication_regimen := 7 - when obs regexp "!!10241=8834!!" then @current_medication_regimen := 8 - when obs regexp "!!10241=8836!!" then @current_medication_regimen := 9 - else @current_medication_regimen := null - end as current_medication_regimen, - case - when obs regexp "!!1654=1065!!" then @understand_bp_meds_and_purpose := 1 - when obs regexp "!!1654=1066!!" then @understand_bp_meds_and_purpose := 2 - else @understand_bp_meds_and_purpose := null - end as understand_bp_meds_and_purpose, - case - when obs regexp "!!10451=1065!!" then @understand_meds_frequency := 1 - when obs regexp "!!10451=1066!!" then @understand_meds_frequency := 2 - else @understand_meds_frequency := null - end as understand_meds_frequency, - case - when obs regexp "!!10495=1107!!" then @last_hypersenitive_regimen_change := 1 - when obs regexp "!!10495=1930!!" then @last_hypersenitive_regimen_change := 2 - when obs regexp "!!10495=981!!" then @last_hypersenitive_regimen_change := 1 - when obs regexp "!!10495=1260!!" then @last_hypersenitive_regimen_change := 2 - else @last_hypersenitive_regimen_change := null - end as last_hypersenitive_regimen_change, - case - when obs regexp "!!10452=1065!!" then @missed_bp_meds_7days := 1 - when obs regexp "!!10452=1066!!" then @missed_bp_meds_7days := 2 - else @missed_bp_meds_7days := null - end as missed_bp_meds_7days, - case - when obs regexp "!!1668=9148!!" then @reason_meds_missed := 1 - when obs regexp "!!1668=1648!!" then @reason_meds_missed := 2 - when obs regexp "!!1668=1664!!" then @reason_meds_missed := 1 - when obs regexp "!!1668=6295!!" then @reason_meds_missed := 2 - when obs regexp "!!1668=6100!!" then @reason_meds_missed := 1 - when obs regexp "!!1668=1546!!" then @reason_meds_missed := 2 - when obs regexp "!!1668=1647!!" then @reason_meds_missed := 1 - when obs regexp "!!1668=9126!!" then @reason_meds_missed := 2 - when obs regexp "!!1668=10453!!" then @reason_meds_missed := 2 - when obs regexp "!!1668=1548!!" then @reason_meds_missed := 1 - when obs regexp "!!1668=5622!!" then @reason_meds_missed := 2 - else @reason_meds_missed := null - end as reason_meds_missed, - case - when obs regexp "!!2327=6411!!" then @reason_for_referal := 1 - when obs regexp "!!2327=6583!!" then @reason_for_referal := 2 - when obs regexp "!!2327=10496!!" then @reason_for_referal := 3 - when obs regexp "!!2327=1664!!" then @reason_for_referal := 4 - when obs regexp "!!2327=6451!!" then @reason_for_referal := 5 - when obs regexp "!!2327=1486!!" then @reason_for_referal := 6 - when obs regexp "!!2327=7342!!" then @reason_for_referal := 7 - when obs regexp "!!2327=1578!!" then @reason_for_referal := 8 - when obs regexp "!!2327=5622!!" then @reason_for_referal := 9 - when obs regexp "!!2327=1548!!" then @reason_for_referal := 10 - when obs regexp "!!2327=2329!!" then @reason_for_referal := 11 - when obs regexp "!!2327=2330!!" then @reason_for_referal := 12 - when obs regexp "!!2327=5484!!" then @reason_for_referal := 13 - when obs regexp "!!2327=6510!!" then @reason_for_referal := 14 - when obs regexp "!!2327=7465!!" then @reason_for_referal := 15 - when obs regexp "!!2327=6511!!" then @reason_for_referal := 16 - when obs regexp "!!2327=6502!!" then @reason_for_referal := 17 - when obs regexp "!!2327=7190!!" then @reason_for_referal := 18 - when obs regexp "!!2327=8262!!" then @reason_for_referal := 19 - else @reason_for_referal := null - end as reason_for_referal, - case - when obs regexp "!!10459=1107!!" then @barries_to_complete_referral := 1 - when obs regexp "!!10459=10458!!" then @barries_to_complete_referral := 2 - when obs regexp "!!10459=10457!!" then @barries_to_complete_referral := 1 - when obs regexp "!!10459=1578!!" then @barries_to_complete_referral := 2 - when obs regexp "!!10459=10455!!" then @barries_to_complete_referral := 1 - when obs regexp "!!10459=820!!" then @barries_to_complete_referral := 2 - when obs regexp "!!10459=1576!!" then @barries_to_complete_referral := 1 - when obs regexp "!!10459=1577!!" then @barries_to_complete_referral := 2 - when obs regexp "!!10459=1666!!" then @barries_to_complete_referral := 2 - when obs regexp "!!10459=10456!!" then @barries_to_complete_referral := 1 - when obs regexp "!!10459=1915!!" then @barries_to_complete_referral := 2 - else @barries_to_complete_referral := null - end as barries_to_complete_referral, - case - when obs regexp "!!10460=1065!!" then @barries_prevent_referreal := 1 - when obs regexp "!!10460=1066!!" then @barries_prevent_referreal := 2 - else @barries_prevent_referreal := null - end as barries_prevent_referreal, - case - when obs regexp "!!9082=10461!!" then @patient_referal_status := 1 - when obs regexp "!!9082=10462!!" then @patient_referal_status := 2 - when obs regexp "!!9082=10463!!" then @patient_referal_status := 3 - when obs regexp "!!9082=9083!!" then @patient_referal_status := 4 - when obs regexp "!!9082=1593!!" then @patient_referal_status := 5 - when obs regexp "!!9082=9079!!" then @patient_referal_status := 6 - when obs regexp "!!9082=1915!!" then @patient_referal_status := 7 - else @patient_referal_status := null - end as patient_referal_status, - case - when obs regexp "!!10467=10464!!" then @referal_preparation_discussed := 1 - when obs regexp "!!10467=6468!!" then @referal_preparation_discussed := 2 - when obs regexp "!!10467=10465!!" then @referal_preparation_discussed := 1 - when obs regexp "!!10467=10466!!" then @referal_preparation_discussed := 2 - else @referal_preparation_discussed := null - end as referal_preparation_discussed, - case - when obs regexp "!!10462=1065!!" then @is_referal_completed := 1 - when obs regexp "!!10462=1066!!" then @is_referal_completed := 2 - else @is_referal_completed := null - end as is_referal_completed, - case - when obs regexp "!!10486=1065!!" then @concerns_preventing_referral := 1 - when obs regexp "!!10486=1066!!" then @concerns_preventing_referral := 2 - else @concerns_preventing_referral := null - end as concerns_preventing_referral, - case - when obs regexp "!!10486=1065!!" then @next_appointment_facility := 1 - when obs regexp "!!10486=1066!!" then @next_appointment_facility := 2 - else @next_appointment_facility := null - end as next_appointment_facility, - case - when obs regexp "!!9166= null!!" then @next_peer_outreach := 1 - else @next_peer_outreach := null - end as next_peer_outreach - - from flat_cdm_peer_navigation_0 t1 - join amrs.person p using (person_id) - ); - - - - set @prev_id = null; - set @cur_id = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_cdm_peer_navigation_1 drop prev_id, drop cur_id; - - drop table if exists flat_cdm_peer_navigation_2; - create temporary table flat_cdm_peer_navigation_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id - - from flat_cdm_peer_navigation_1 - order by person_id, date(encounter_datetime) desc - ); - - alter table flat_cdm_peer_navigation_2 drop prev_id, drop cur_id; - - - set @prev_id = null; - set @cur_id = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_cdm_peer_navigation_3; - create temporary table flat_cdm_peer_navigation_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id - - from flat_cdm_peer_navigation_2 t1 - order by person_id, date(encounter_datetime) - ); - - alter table flat_cdm_peer_navigation_3 drop prev_id, drop cur_id; - - set @prev_id = null; - set @cur_id = null; - - #Handle transfers - - drop temporary table if exists flat_cdm_peer_navigation_4; - - create temporary table flat_cdm_peer_navigation_4 ( index person_enc (person_id, encounter_datetime)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id - - - - from flat_cdm_peer_navigation_3 t1 - order by person_id, date(encounter_datetime) - ); - - -SELECT - COUNT(*) -INTO @new_encounter_rows FROM - flat_cdm_peer_navigation_4; - -SELECT @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; -SELECT @total_rows_written; - - - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - date_created, - person_id, - encounter_datetime, - encounter_id, - encounter_type, - location_id, - referal_category, - referred_in_or_out, - lifestyle_modification_plan, - current_medication_regimen, - understand_bp_meds_and_purpose, - understand_meds_frequency, - last_hypersenitive_regimen_change, - missed_bp_meds_7days, - reason_meds_missed, - reason_for_referal, - barries_to_complete_referral, - concerns_preventing_referral, - patient_referal_status, - is_referal_completed, - referal_preparation_discussed, - next_appointment_facility, - next_peer_outreach - - - - from flat_cdm_peer_navigation_4 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_cdm_peer_navigation_build_queue__0 t2 using (person_id);'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - -SELECT - @person_ids_count AS 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - - SET @dyn_sql=CONCAT('describe ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; -SELECT - CONCAT(@start_write, - ' : Writing ', - @total_rows_to_write, - ' to ', - @primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); -SELECT - CONCAT(@finish_write, - ' : Completed writing rows. Time to write to primary table: ', - @time_to_write, - ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - -SELECT - CONCAT('Average Cycle Length: ', - @ave_cycle_length, - ' second(s)'); - - set @end = now(); -insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); -SELECT - CONCAT(@table_version, - ' : Time to complete: ', - TIMESTAMPDIFF(MINUTE, @start, @end), - ' minutes'); - - - -END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_cdm_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_cdm_v1_0.sql deleted file mode 100644 index 0a7c86a..0000000 --- a/etl-scripts/stored-procedures/generate_flat_cdm_v1_0.sql +++ /dev/null @@ -1,742 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_cdm_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_cdm"; - set @query_type = query_type; -#set @query_type = "build"; - - - set @total_rows_written = 0; - - set @encounter_types = "(54,55,75,76,77,78,79,83,96,99,100,104,107,108,109,131,171,172)"; - set @clinical_encounter_types = "(54,55,75,76,77,78,79,83,96,104,107,108,109,171,172)"; - set @non_clinical_encounter_types = "(131)"; - set @other_encounter_types = "(-1)"; - - set @start = now(); - set @table_version = "flat_cdm_v1.0"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @boundary = "!!"; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - #delete from etl.flat_log where table_name like "%flat_cdm%"; - #drop table etl.flat_cdm; - create table if not exists flat_cdm ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - is_clinical_encounter int, - location_id int, - location_uuid varchar(100), - death_date datetime, - prev_rtc_date datetime, - rtc_date datetime, - - lmp date, - - sbp smallint, - dbp smallint, - pulse smallint, - - fbs decimal, - rbs decimal, - hb_a1c decimal, - hb_a1c_date datetime, - - creatinine decimal, - creatinine_date datetime, - - total_cholesterol decimal, - hdl decimal, - ldl decimal, - triglycerides decimal, - lipid_panel_date datetime, - - dm_status mediumint, - htn_status mediumint, - dm_meds varchar(500), - htn_meds varchar(500), - prescriptions text, - - problems text, - - prev_encounter_datetime_cdm datetime, - next_encounter_datetime_cdm datetime, - prev_encounter_type_cdm mediumint, - next_encounter_type_cdm mediumint, - prev_clinical_datetime_cdm datetime, - next_clinical_datetime_cdm datetime, - prev_clinical_location_id_cdm mediumint, - next_clinical_location_id_cdm mediumint, - prev_clinical_rtc_date_cdm datetime, - next_clinical_rtc_date_cdm datetime, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_cdm), - index encounter_type (encounter_type), - index date_created (date_created) - - ); - - - - if(@query_type="build") then - select 'BUILDING..........................................'; - -#set @write_table = concat("flat_cdm_temp_",1); -#set @queue_table = concat("flat_cdm_build_queue_",1); - - set @write_table = concat("flat_cdm_temp_",queue_number); - set @queue_table = concat("flat_cdm_build_queue_",queue_number); - - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_cdm_build_queue limit ', queue_size, ');'); -#SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_cdm_build_queue limit 500);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_cdm_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from flat_cdm_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_cdm"; - set @queue_table = "flat_cdm_sync_queue"; - create table if not exists flat_cdm_sync_queue (person_id int primary key); - - - - set @last_update = null; - - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - -#select max(date_created) into @last_update from etl.flat_log where table_name like "%cdm%"; - -#select @last_update; - - replace into flat_cdm_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_cdm_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - replace into flat_cdm_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_cdm_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_cdm_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_cdm_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - # Remove test patients - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - select @person_ids_count as 'num patients to update'; - - - #delete t1 from flat_cdm t1 join @queue_table t2 using (person_id); - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - drop temporary table if exists prescriptions; - create temporary table prescriptions (encounter_id int primary key, prescriptions text) - ( - select - encounter_id, - group_concat(obs separator ' $ ') as prescriptions - from - ( - select - t2.encounter_id, - obs_group_id, - group_concat( - case - when value_coded is not null then concat(@boundary,o.concept_id,'=',value_coded,@boundary) - when value_numeric is not null then concat(@boundary,o.concept_id,'=',value_numeric,@boundary) - when value_datetime is not null then concat(@boundary,o.concept_id,'=',date(value_datetime),@boundary) - when value_text is not null then concat(@boundary,o.concept_id,'=',value_text,@boundary) - when value_drug is not null then concat(@boundary,o.concept_id,'=',value_drug,@boundary) - when value_modifier is not null then concat(@boundary,o.concept_id,'=',value_modifier,@boundary) - end - order by o.concept_id,value_coded - separator ' ## ' - ) as obs - - from amrs.obs o - join (select encounter_id, obs_id, concept_id as grouping_concept from amrs.obs where concept_id in (7307,7334)) t2 on o.obs_group_id = t2.obs_id - group by obs_group_id - ) t - group by encounter_id - ); - - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists flat_cdm_build_queue__0; - - - #create temporary table flat_cdm_build_queue__0 (select * from flat_cdm_build_queue_2 limit 5000); #TODO - change this when data_fetch_size changes - -#SET @dyn_sql=CONCAT('create temporary table flat_cdm_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit 100);'); - SET @dyn_sql=CONCAT('create temporary table flat_cdm_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - - drop temporary table if exists flat_cdm_0a; - SET @dyn_sql = CONCAT( - 'create temporary table flat_cdm_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - case - when t1.encounter_type in ',@clinical_encounter_types,' then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in ',@non_clinical_encounter_types,' then 20 - when t1.encounter_type in ',@clinical_encounter_types,' then 10 - when t1.encounter_type in', @other_encounter_types, ' then 5 - else 1 - end as encounter_type_sort_index, - t2.orders - from etl.flat_obs t1 - join flat_cdm_build_queue__0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_id) - where t1.encounter_type in ',@encounter_types,');'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - insert into flat_cdm_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, #t1.location_id, - t1.obs, - null, #obs_datetimes - # in any visit, there many be multiple encounters. for this dataset, we want to include only clinical encounters (e.g. not lab or triage visit) - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_cdm_build_queue__0 t0 using (person_id) - ); - - drop temporary table if exists flat_cdm_0; - create temporary table flat_cdm_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_cdm_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_date = null; - set @cur_encounter_date = null; - set @enrollment_date = null; - set @cur_location = null; - set @cur_rtc_date = null; - set @prev_rtc_date = null; - - set @death_date = null; - - - #TO DO - # screened for cervical ca - # exposed infant - - drop temporary table if exists flat_cdm_1; - create temporary table flat_cdm_1 (index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.encounter_id, - @prev_encounter_date := date(@cur_encounter_date) as prev_encounter_date, - @cur_encounter_date := date(encounter_datetime) as cur_encounter_date, - t1.encounter_datetime, - t1.encounter_type, - t1.is_clinical_encounter, - - death_date, - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as location_id, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - # 5096 = return visit date - case - when obs regexp "!!5096=" then @cur_rtc_date := GetValues(obs,5096) - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - @lmp := GetValues(obs,1836) as lmp, - - case - when obs regexp "!!5085=" then @sbp := GetValues(obs,5085) - end as sbp, - - @dbp := GetValues(obs,5086) as dbp, - - @pulse := GetValues(obs,5087) as pulse, - - @fbs := GetValues(obs,6252) as fbs, - @rbs := GetValues(obs,887) as rbs, - - case - when obs regexp "!!6126=" then @hb_a1c := GetValues(obs,6126) - when @prev_id = @cur_id then @hb_a1c - else @hb_a1c := null - end as hb_a1c, - - case - when obs regexp "!!6126=" then @hb_a1c_date := ifnull(GetValues(obs_datetimes,6126),encounter_datetime) - when @prev_id=@cur_id then @hb_a1c_date - else @hb_a1c_date:=null - end as hb_a1c_date, - - case - when obs regexp "!!790=" then @creatinine := GetValues(obs,790) - when @prev_id = @cur_id then @creatinine - else @creatinine := null - end as creatinine, - - case - when obs regexp "!!790=" then @creatinine_date := ifnull(GetValues(obs_datetimes,790),encounter_datetime) - when @prev_id=@cur_id then @creatinine_date - else @creatinine_date:=null - end as creatinine_date, - - - case - when obs regexp "!!1006=" then @total_cholesterol := GetValues(obs,1006) - when @prev_id = @cur_id then @total_cholesterol - else @total_cholesterol := null - end as total_cholesterol, - - case - when obs regexp "!!1007=" then @hdl := GetValues(obs,1007) - when @prev_id = @cur_id then @hdl - else @hdl := null - end as hdl, - - case - when obs regexp "!!1008=" then @ldl := GetValues(obs,1008) - when @prev_id = @cur_id then @ldl - else @ldl := null - end as ldl, - - case - when obs regexp "!!1009=" then @triglycerides := GetValues(obs,1009) - when @prev_id = @cur_id then @triglycerides - else @triglycerides := null - end as triglycerides, - - case - when obs regexp "!!1006=" then @lipid_panel_date := ifnull(GetValues(obs_datetimes,1006),encounter_datetime) - when @prev_id=@cur_id then @lipid_panel_date - else @lipid_panel_date:=null - end as lipid_panel_date, - - - @dm_status := GetValues(obs,7287) as dm_status, - @htn_status := GetValues(obs,7288) as htn_status, - @dm_meds := GetValues(obs,7290) as dm_meds, - @htn_meds := GetValues(obs,10241) as htn_meds, - t2.prescriptions as prescriptions, - - @problems := GetValues(obs,6042 ) as problems - - - from flat_cdm_0 t1 - join amrs.person p using (person_id) - left outer join prescriptions t2 using (encounter_id) - ); - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_cdm_1 drop prev_id, drop cur_id; - - drop table if exists flat_cdm_2; - create temporary table flat_cdm_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_cdm, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_cdm, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_cdm, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id_cdm, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_cdm, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date - - from flat_cdm_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_cdm_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_cdm_3; - create temporary table flat_cdm_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_cdm, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_cdm, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_cdm, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id_cdm, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_cdm, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_cdm_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - - select count(*) into @new_encounter_rows from flat_cdm_3; - - select @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; - select @total_rows_written; - - - #add data to table - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - t1.uuid, - visit_id, - encounter_id, - encounter_datetime, - encounter_type, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - death_date, - prev_rtc_date, - cur_rtc_date, - lmp, - sbp, - dbp, - pulse, - fbs, - rbs, - hb_a1c, - hb_a1c_date, - creatinine, - creatinine_date, - total_cholesterol, - hdl, - ldl, - triglycerides, - lipid_panel_date, - dm_status, - htn_status, - dm_meds, - htn_meds, - prescriptions, - problems, - - - prev_encounter_datetime_cdm, - next_encounter_datetime_cdm, - prev_encounter_type_cdm, - next_encounter_type_cdm, - prev_clinical_datetime_cdm, - next_clinical_datetime_cdm, - prev_clinical_location_id_cdm, - next_clinical_location_id_cdm, - prev_clinical_rtc_date_cdm, - next_clinical_rtc_date_cdm - - from flat_cdm_3 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #delete from @queue_table where person_id in (select person_id from flat_cdm_build_queue__0); - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_cdm_build_queue__0 t2 using (person_id);'); -# select @dyn_sql; - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #select @person_ids_count := (select count(*) from flat_cdm_build_queue_2); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count as remaining_in_build_queue; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - #select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select @person_ids_count as 'persons remaining', @cycle_length as 'Cycle time (s)', ceil(@person_ids_count / cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; \ No newline at end of file diff --git a/etl-scripts/stored-procedures/generate_flat_cervical_cancer_screening_v1_1.sql b/etl-scripts/stored-procedures/generate_flat_cervical_cancer_screening.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_flat_cervical_cancer_screening_v1_1.sql rename to etl-scripts/stored-procedures/generate_flat_cervical_cancer_screening.sql index 55d2ace..d015901 100644 --- a/etl-scripts/stored-procedures/generate_flat_cervical_cancer_screening_v1_1.sql +++ b/etl-scripts/stored-procedures/generate_flat_cervical_cancer_screening.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_cervical_cancer_screening_v1_1`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_cervical_cancer_screening`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN SET @primary_table := "flat_cervical_cancer_screening"; SET @query_type := query_type; diff --git a/etl-scripts/stored-procedures/generate_flat_cervical_cancer_screening_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_cervical_cancer_screening_v1_0.sql deleted file mode 100644 index 54861e5..0000000 --- a/etl-scripts/stored-procedures/generate_flat_cervical_cancer_screening_v1_0.sql +++ /dev/null @@ -1,1139 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_cervical_cancer_screening_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_cervical_cancer_screening"; - set @query_type = query_type; -#set @query_type = "build"; - - set @total_rows_written = 0; - - set @encounter_types = "(69,70,147)"; - set @clinical_encounter_types = "(69,70,147)"; - set @non_clinical_encounter_types = "(-1)"; - set @other_encounter_types = "(-1)"; - - set @start = now(); - set @table_version = "flat_cervical_cancer_screening_v1.0"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @boundary = "!!"; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - -CREATE TABLE IF NOT EXISTS flat_cervical_cancer_screening ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id INT, - encounter_id INT, - encounter_type INT, - encounter_datetime DATETIME, - visit_id INT, - location_id INT, - location_uuid VARCHAR(100), - uuid VARCHAR(100), - age INT, - encounter_purpose INT, - cur_visit_type INT, - actual_scheduled_date DATETIME, - gravida INT, - parity INT, - Mensturation_status INT, - lmp DATETIME, - pregnancy_status INT, - pregnant_edd DATETIME, - reason_not_pregnant INT, - hiv_status INT, - viral_load FLOAT, - viral_load_date DATETIME, - prior_via_done INT, - prior_via_result INT, - prior_via_date DATETIME, - cur_via_result INT, - visual_impression_cervix INT, - visual_impression_vagina INT, - visual_impression_vulva INT, - via_procedure_done INT, - other_via_procedure_done VARCHAR(1000), - via_management_plan INT, - other_via_management_plan VARCHAR(1000), - via_assessment_notes TEXT, - via_rtc_date DATETIME, - prior_dysplasia_done INT, - previous_via_result INT, - previous_via_result_date DATETIME, - prior_papsmear_result INT, - prior_biopsy_result INT, - prior_biopsy_result_date DATETIME, - prior_biopsy_result_other VARCHAR(1000), - prior_biopsy_result_date_other DATETIME, - past_dysplasia_treatment INT, - treatment_specimen_pathology INT, - satisfactory_colposcopy INT, - colposcopy_findings INT, - cervica_lesion_size INT, - dysplasia_cervix_impression INT, - dysplasia_vagina_impression INT, - dysplasia_vulva_impression INT, - dysplasia_procedure_done INT, - other_dysplasia_procedure_done VARCHAR(1000), - dysplasia_mgmt_plan INT, - other_dysplasia_mgmt_plan VARCHAR(1000), - dysplasia_assesment_notes TEXT, - dysplasia_rtc_date DATETIME, - Pap_smear_results INT, - leep_location INT, - Cervix_biopsy_results INT, - Vagina_biopsy_results INT, - Vulva_biopsy_result INT, - endometrium_biopsy INT, - ECC INT, - biopsy_results_mngmt_plan INT, - cancer_staging INT, - next_app_date DATETIME, - prev_encounter_datetime_cervical_cancer_screening DATETIME, - next_encounter_datetime_cervical_cancer_screening DATETIME, - prev_encounter_type_cervical_cancer_screening MEDIUMINT, - next_encounter_type_cervical_cancer_screening MEDIUMINT, - prev_clinical_datetime_cervical_cancer_screening DATETIME, - next_clinical_datetime_cervical_cancer_screening DATETIME, - prev_clinical_location_id_cervical_cancer_screening MEDIUMINT, - next_clinical_location_id_cervical_cancer_screening MEDIUMINT, - prev_clinical_rtc_date_cervical_cancer_screening DATETIME, - next_clinical_rtc_date_cervical_cancer_screening DATETIME, - PRIMARY KEY encounter_id (encounter_id), - INDEX person_date (person_id , encounter_datetime), - INDEX location_enc_date (location_uuid , encounter_datetime), - INDEX enc_date_location (encounter_datetime , location_uuid), - INDEX location_id_rtc_date (location_id , next_app_date), - INDEX location_uuid_rtc_date (location_uuid , next_app_date), - INDEX loc_id_enc_date_next_clinical (location_id , encounter_datetime , next_clinical_datetime_cervical_cancer_screening), - INDEX encounter_type (encounter_type), - INDEX date_created (date_created) -); - - - - if(@query_type="build") then - select 'BUILDING..........................................'; - -#set @write_table = concat("flat_cervical_cancer_screening_temp_",1); -#set @queue_table = concat("flat_cervical_cancer_screening_build_queue_",1); - - set @write_table = concat("flat_cervical_cancer_screening_temp_",queue_number); - set @queue_table = concat("flat_cervical_cancer_screening_build_queue_",queue_number); - - -#drop table if exists flat_cervical_cancer_screening_temp_1; - SET @dyn_sql=CONCAT('create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_cervical_cancer_screening_build_queue limit ', queue_size, ');'); -#SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_cervical_cancer_screening_build_queue limit 500);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_cervical_cancer_screening_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from flat_cervical_cancer_screening_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_cervical_cancer_screening"; - set @queue_table = "flat_cervical_cancer_screening_sync_queue"; -CREATE TABLE IF NOT EXISTS flat_cervical_cancer_screening_sync_queue ( - person_id INT PRIMARY KEY -); - - set @last_update = null; - -SELECT - MAX(date_updated) -INTO @last_update FROM - etl.flat_log -WHERE - table_name = @table_version; - -#select max(date_created) into @last_update from etl.flat_log where table_name like "%cervical_cancer_screening%"; - -#select @last_update; - - replace into flat_cervical_cancer_screening_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_cervical_cancer_screening_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - replace into flat_cervical_cancer_screening_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_cervical_cancer_screening_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_cervical_cancer_screening_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_cervical_cancer_screening_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - # Remove test patients - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SELECT @person_ids_count AS 'num patients to update'; - - - #delete t1 from flat_cervical_cancer_screening t1 join @queue_table t2 using (person_id); - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists flat_cervical_cancer_screening_build_queue__0; - - #create temporary table flat_cervical_cancer_screening_build_queue__0 (select * from flat_cervical_cancer_screening_build_queue_2 limit 5000); #TODO - change this when data_fetch_size changes - -#SET @dyn_sql=CONCAT('create temporary table flat_cervical_cancer_screening_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit 100);'); - SET @dyn_sql=CONCAT('create temporary table flat_cervical_cancer_screening_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - drop temporary table if exists flat_cervical_cancer_screening_0a; - SET @dyn_sql = CONCAT( - 'create temporary table flat_cervical_cancer_screening_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - case - when t1.encounter_type in ',@clinical_encounter_types,' then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in ',@non_clinical_encounter_types,' then 20 - when t1.encounter_type in ',@clinical_encounter_types,' then 10 - when t1.encounter_type in', @other_encounter_types, ' then 5 - else 1 - end as encounter_type_sort_index, - t2.orders - from etl.flat_obs t1 - join flat_cervical_cancer_screening_build_queue__0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_id) - where t1.encounter_type in ',@encounter_types,');'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - - insert into flat_cervical_cancer_screening_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, #t1.location_id, - t1.obs, - null, #obs_datetimes - # in any visit, there many be multiple encounters. for this dataset, we want to include only clinical encounters (e.g. not lab or triage visit) - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_cervical_cancer_screening_build_queue__0 t0 using (person_id) - ); - - - drop temporary table if exists flat_cervical_cancer_screening_0; - create temporary table flat_cervical_cancer_screening_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_cervical_cancer_screening_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @prev_id = null; - set @cur_id = null; - set @encounter_purpose =null; - set @cur_visit_type = null; - set @actual_scheduled_date = null; - set @gravida = null; - set @parity = null; - set @Mensturation_status = null; - set @lmp = null; - set @pregnancy_status = null; - set @pregnant_edd = null; - set @preason_not_pregnant = null; - set @hiv_status = null; - set @viral_load = null; - set @viral_load_date = null; - set @prior_via_done = null; - set @prior_via_result = null; - set @prior_via_date = null; - set @cur_via_result = null; - set @visual_impression_cervix = null; - set @visual_impression_vagina = null; - set @visual_impression_vulva = null; - set @via_procedure_done = null; - set @other_via_procedure_done = null; - set @via_management_plan = null; - set @other_via_management_plan = null; - set @via_assessment_notes = null; - set @via_rtc_date = null; - set @prior_dysplasia_done = null; - set @previous_via_result = null; - set @previous_via_result_date = null; - set @prior_papsmear_result = null; - set @prior_biopsy_result = null; - set @prior_biopsy_result_date = null; - set @prior_biopsy_result_other = null; - set @prior_biopsy_result_date_other = null; - set @past_dysplasia_treatment = null; - set @treatment_specimen_pathology = null; - set @satisfactory_colposcopy = null; - set @colposcopy_findings = null; - set @cervica_lesion_size = null; - set @dysplasia_cervix_impression = null; - set @dysplasia_vagina_impression = null; - set @dysplasia_vulva_impression = null; - set @dysplasia_procedure_done = null; - set @other_dysplasia_procedure_done = null; - set @dysplasia_mgmt_plan = null; - set @other_dysplasia_mgmt_plan = null; - set @dysplasia_assesment_notes = null; - set @dysplasia_rtc_date = null; - set @Pap_smear_results = null; - set @leep_location =null; - set @Cervix_biopsy_results = null; - set @Vagina_biopsy_results =null; - set @Vulva_biopsy_result =null; - set @endometrium_biopsy =null; - set @ECC =null; - set @biopsy_results_mngmt_plan =null; - set @cancer_staging =null; - - drop temporary table if exists flat_cervical_cancer_screening_1; - create temporary table flat_cervical_cancer_screening_1 #(index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - t1.encounter_id, - t1.encounter_type, - t1.encounter_datetime, - t1.visit_id, - #t4.name as location_name, - t1.location_id, - t1.is_clinical_encounter, - p.gender, - p.death_date, - - - case - when timestampdiff(year,birthdate,curdate()) > 0 then round(timestampdiff(year,birthdate,curdate()),0) - else round(timestampdiff(month,birthdate,curdate())/12,2) - end as age, - - case - when obs regexp "!!1834=9651!!" then @encounter_purpose := 1 - when obs regexp "!!1834=1154!!" then @encounter_purpose := 2 - when obs regexp "!!1834=1246!!" then @encounter_purpose := 3 - when obs regexp "!!1834=5622!!" then @encounter_purpose := 4 - else @encounter_purpose := null - end as encounter_purpose, - case - when obs regexp "!!1839=1838!!" then @cur_visit_type := 1 - when obs regexp "!!1839=1837!!" then @cur_visit_type := 2 - when obs regexp "!!1839=1246!!" then @cur_visit_type := 3 - when obs regexp "!!1839=7850!!" then @cur_visit_type := 4 - when obs regexp "!!1839=9569!!" then @cur_visit_type := 5 - else @cur_visit_type := null - end as cur_visit_type, - case - when obs regexp "!!7029=" then @actual_scheduled_date := replace(replace((substring_index(substring(obs,locate("!!7029=",obs)),@sep,1)),"!!7029=",""),"!!","") - else @actual_scheduled_date := null - end as actual_scheduled_date, - case - when obs regexp "!!5624=[0-9]" then @gravida:=cast(replace(replace((substring_index(substring(obs,locate("!!5624=",obs)),@sep,1)),"!!5624=",""),"!!","") as unsigned) - else @gravida := null - end as gravida, - case - when obs regexp "!!1053=[0-9]" then @parity:=cast(replace(replace((substring_index(substring(obs,locate("!!1053=",obs)),@sep,1)),"!!1053=",""),"!!","") as unsigned) - else @parity := null - end as parity, - case - when obs regexp "!!2061=5989!!" then @Mensturation_status := 1 - when obs regexp "!!2061=6496!!" then @prev_exam_results := 2 - else @Mensturation_status := null - end as Mensturation_status, - case - when obs regexp "!!1836=" then @lmp := replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!","") - else @lmp := null - end as lmp, - case - when obs regexp "!!8351=1065!!" then @pregnancy_status := 1 - when obs regexp "!!8351=1066!!" then @pregnancy_status := 0 - else @pregnancy_status := null - end as pregnancy_status, - case - when obs regexp "!!5596=" then @pregnant_edd := replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @pregnant_edd := null - end as pregnant_edd, - case - when obs regexp "!!9733=9729!!" then @reason_not_pregnant := 1 - when obs regexp "!!9733=9730!!" then @reason_not_pregnant := 2 - when obs regexp "!!9733=9731!!" then @reason_not_pregnant := 3 - when obs regexp "!!9733=9732!!" then @reason_not_pregnant := 4 - else @reason_not_pregnant := null - end as reason_not_pregnant, - case - when obs regexp "!!6709=664!!" then @hiv_status := 1 - when obs regexp "!!6709=703!!" then @hiv_status := 2 - when obs regexp "!!6709=1067!!" then @hiv_status := 3 - else @hiv_status := null - end as hiv_status, - case - when obs regexp "!!856=[0-9]" then @viral_load:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - else @viral_load := null - end as viral_load, - case - when obs regexp "!!000=" then @viral_load_date := replace(replace((substring_index(substring(obs,locate("!!000=",obs)),@sep,1)),"!!000=",""),"!!","") - else @viral_load_date := null - end as viral_load_date, - case - when obs regexp "!!9589=1065!!" then @prior_via_done := 1 - when obs regexp "!!9589=1066!!" then @prior_via_done := 0 - else @prior_via_done := null - end as prior_via_done, - case - when obs regexp "!!7381=664!!" then @prior_via_result := 1 - when obs regexp "!!7381=703!!" then @prior_via_result := 2 - else @prior_via_result := null - end as prior_via_result, - case - when obs regexp "!!000=" then @prior_via_date := replace(replace((substring_index(substring(obs,locate("!!000=",obs)),@sep,1)),"!!000=",""),"!!","") - else @prior_via_date := null - end as prior_via_date, - case - when obs regexp "!!9590=1115!!" then @cur_via_result := 1 - when obs regexp "!!9590=7469!!" then @cur_via_result := 2 - when obs regexp "!!9590=9593!!" then @cur_via_result := 3 - when obs regexp "!!9590=7472!!" then @cur_via_result := 4 - when obs regexp "!!9590=7293!!" then @cur_via_result := 5 - when obs regexp "!!9590=7470!!" then @cur_via_result := 6 - when obs regexp "!!9590=6497!!" then @cur_via_result := 7 - when obs regexp "!!9590=5245!!" then @cur_via_result := 8 - when obs regexp "!!9590=9591!!" then @cur_via_result := 9 - when obs regexp "!!9590=9592!!" then @cur_via_result := 10 - when obs regexp "!!9590=6497!!" then @cur_via_result := 11 - else @cur_via_resul := null - end as cur_via_result, - case - when obs regexp "!!7484=1115!!" then @visual_impression_cervix := 1 - when obs regexp "!!7484=7507!!" then @visual_impression_cervix := 2 - when obs regexp "!!7484=7508!!" then @visual_impression_cervix := 3 - else @visual_impression_cervix := null - end as visual_impression_cervix, - case - when obs regexp "!!7490=1115!!" then @visual_impression_vagina := 1 - when obs regexp "!!7490=1447!!" then @visual_impression_vagina := 2 - when obs regexp "!!7490=9181!!" then @visual_impression_vagina := 3 - else @visual_impression_vagina := null - end as visual_impression_vagina, - case - when obs regexp "!!7490=1115!!" then @visual_impression_vulva := 1 - when obs regexp "!!7490=1447!!" then @visual_impression_vulva := 2 - when obs regexp "!!7490=9177!!" then @visual_impression_vulva := 3 - else @visual_impression_vulva := null - end as visual_impression_vulva, - case - when obs regexp "!!7479=1107!!" then @via_procedure_done := 1 - when obs regexp "!!7479=6511!!" then @via_procedure_done := 2 - when obs regexp "!!7479=7466!!" then @via_procedure_done := 3 - when obs regexp "!!7479=7147!!" then @via_procedure_done := 4 - when obs regexp "!!7479=9724!!" then @via_procedure_done := 5 - when obs regexp "!!7479=6510!!" then @via_procedure_done := 6 - when obs regexp "!!7479=5622!!" then @via_procedure_done := 7 - else @via_procedure_done := null - end as via_procedure_done, - case - when obs regexp "!!1915=" then @other_via_procedure_done := replace(replace((substring_index(substring(obs,locate("!!1915=",obs)),@sep,1)),"!!1915=",""),"!!","") - else @other_via_procedure_done := null - end as other_via_procedure_done, - case - when obs regexp "!!7500=9725!!" then @via_management_plan := 1 - when obs regexp "!!7500=9178!!" then @via_management_plan := 2 - when obs regexp "!!7500=7497!!" then @via_management_plan := 3 - when obs regexp "!!7500=7383!!" then @via_management_plan := 4 - when obs regexp "!!7500=7499!!" then @via_management_plan := 5 - when obs regexp "!!7500=5622!!" then @via_management_plan := 6 - else @via_management_plan := null - end as via_management_plan, - case - when obs regexp "!!1915=" then @other_via_management_plan := replace(replace((substring_index(substring(obs,locate("!!1915=",obs)),@sep,1)),"!!1915=",""),"!!","") - else @other_via_management_plan := null - end as other_via_management_plan, - case - when obs regexp "!!7222=" then @via_assessment_notes := replace(replace((substring_index(substring(obs,locate("!!7222=",obs)),@sep,1)),"!!7222=",""),"!!","") - else @via_assessment_notes := null - end as via_assessment_notes, - case - when obs regexp "!!5096=" then @via_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - else @via_rtc_date := null - end as via_rtc_date, - case - when obs regexp "!!7379=1065!!" then @prior_dysplasia_done := 1 - when obs regexp "!!7379=1066!!" then @prior_dysplasia_done := 0 - else @prior_dysplasia_done := null - end as prior_dysplasia_done, - case - when obs regexp "!!7381=664!!" then @previous_via_result := 1 - when obs regexp "!!7381=703!!" then @previous_via_result := 2 - else @previous_via_result := null - end as previous_via_result, - case - when obs regexp "!!000=" then @previous_via_result_date := replace(replace((substring_index(substring(obs,locate("!!000=",obs)),@sep,1)),"!!000=",""),"!!","") - else @previous_via_result_date := null - end as previous_via_result_date, - case - when obs regexp "!!7423=1115!!" then @prior_papsmear_result := 1 - when obs regexp "!!7423=7417!!" then @prior_papsmear_result := 2 - when obs regexp "!!7423=7418!!" then @prior_papsmear_result := 3 - when obs regexp "!!7423=7419!!" then @prior_papsmear_result := 4 - when obs regexp "!!7423=7420!!" then @prior_papsmear_result := 5 - when obs regexp "!!7423=7421!!" then @prior_papsmear_result := 6 - when obs regexp "!!7423=7422!!" then @prior_papsmear_result := 7 - else @prior_papsmear_result := null - end as prior_papsmear_result, - case - when obs regexp "!!7426=1115!!" then @prior_biopsy_result := 1 - when obs regexp "!!7426=7424!!" then @prior_biopsy_result := 2 - when obs regexp "!!7426=7425!!" then @prior_biopsy_result := 3 - when obs regexp "!!7426=7216!!" then @prior_biopsy_result := 4 - when obs regexp "!!7426=7421!!" then @prior_biopsy_result := 5 - else @prior_biopsy_result := null - end as prior_biopsy_result, - case - when obs regexp "!!000=" then @prior_biopsy_result_date := replace(replace((substring_index(substring(obs,locate("!!000=",obs)),@sep,1)),"!!000=",""),"!!","") - else @prior_biopsy_result_date := null - end as prior_biopsy_result_date, - case - when obs regexp "!!7400=" then @prior_biopsy_result_other := replace(replace((substring_index(substring(obs,locate("!!7400=",obs)),@sep,1)),"!!7400=",""),"!!","") - else @prior_biopsy_result_other := null - end as prior_biopsy_result_other, - case - when obs regexp "!!000=" then @prior_biopsy_result_date_other := replace(replace((substring_index(substring(obs,locate("!!000=",obs)),@sep,1)),"!!000=",""),"!!","") - else @prior_biopsy_result_date_other := null - end as prior_biopsy_result_date_other, - case - when obs regexp "!!7467=7466!!" then @past_dysplasia_treatment := 1 - when obs regexp "!!7467=7147!!" then @past_dysplasia_treatment := 2 - when obs regexp "!!7467=7465!!" then @past_dysplasia_treatment := 3 - when obs regexp "!!7467=5622!!" then @past_dysplasia_treatment := 4 - else @past_dysplasia_treatment := null - end as past_dysplasia_treatment, - case - when obs regexp "!!7579=1115!!" then @treatment_specimen_pathology := 1 - when obs regexp "!!7579=149!!" then @treatment_specimen_pathology := 2 - when obs regexp "!!7579=9620!!" then @treatment_specimen_pathology := 3 - when obs regexp "!!7579=7424!!" then @treatment_specimen_pathology := 4 - when obs regexp "!!7579=7425!!" then @treatment_specimen_pathology := 5 - when obs regexp "!!7579=7216!!" then @treatment_specimen_pathology := 6 - when obs regexp "!!7579=7421!!" then @treatment_specimen_pathology := 7 - when obs regexp "!!7579=9618!!" then @treatment_specimen_pathology := 8 - else @treatment_specimen_pathology := null - end as treatment_specimen_pathology, - case - when obs regexp "!!7428=1065!!" then @satisfactory_colposcopy := 1 - when obs regexp "!!7428=1066!!" then @satisfactory_colposcopy := 0 - when obs regexp "!!7428=1118!!" then @satisfactory_colposcopy := 2 - else @satisfactory_colposcopy := null - end as satisfactory_colposcopy, - case - when obs regexp "!!7383=1115!!" then @colposcopy_findings := 1 - when obs regexp "!!7383=7469!!" then @colposcopy_findings := 2 - when obs regexp "!!7383=7473!!" then @colposcopy_findings := 3 - when obs regexp "!!7383=7470!!" then @colposcopy_findings := 4 - when obs regexp "!!7383=7471!!" then @colposcopy_findings := 5 - when obs regexp "!!7383=7472!!" then @colposcopy_findings := 6 - else @colposcopy_findings := null - end as colposcopy_findings, - case - when obs regexp "!!7477=7474!!" then @cervica_lesion_size := 1 - when obs regexp "!!7477=9619!!" then @cervica_lesion_size := 2 - when obs regexp "!!7477=7476!!" then @cervica_lesion_size := 3 - else @cervica_lesion_size := null - end as cervica_lesion_size, - case - when obs regexp "!!7484=1115!!" then @dysplasia_cervix_impression := 1 - when obs regexp "!!7484=7424!!" then @dysplasia_cervix_impression := 2 - when obs regexp "!!7484=7425!!" then @dysplasia_cervix_impression := 3 - when obs regexp "!!7484=7216!!" then @dysplasia_cervix_impression := 4 - when obs regexp "!!7484=7421!!" then @dysplasia_cervix_impression := 5 - else @dysplasia_cervix_impression := null - end as dysplasia_cervix_impression, - case - when obs regexp "!!7490=1115!!" then @dysplasia_vagina_impression := 1 - when obs regexp "!!7490=1447!!" then @dysplasia_vagina_impression := 2 - when obs regexp "!!7490=9177!!" then @dysplasia_vagina_impression := 3 - else @dysplasia_vagina_impression := null - end as dysplasia_vagina_impression, - case - when obs regexp "!!7487=1115!!" then @dysplasia_vulva_impression := 1 - when obs regexp "!!7487=1447!!" then @dysplasia_vulva_impression := 2 - when obs regexp "!!7487=9177!!" then @dysplasia_vulva_impression := 3 - else @dysplasia_vulva_impression := null - end as dysplasia_vulva_impression, - case - when obs regexp "!!7479=1107!!" then @dysplasia_procedure_done := 1 - when obs regexp "!!7479=6511!!" then @dysplasia_procedure_done := 2 - when obs regexp "!!7479=7466!!" then @dysplasia_procedure_done := 3 - when obs regexp "!!7479=7147!!" then @dysplasia_procedure_done := 4 - when obs regexp "!!7479=9724!!" then @dysplasia_procedure_done := 5 - when obs regexp "!!7479=6510!!" then @dysplasia_procedure_done := 6 - when obs regexp "!!7479=5622!!" then @dysplasia_procedure_done := 7 - else @dysplasia_procedure_done := null - end as dysplasia_procedure_done, - case - when obs regexp "!!1915=" then @other_dysplasia_procedure_done := replace(replace((substring_index(substring(obs,locate("!!1915=",obs)),@sep,1)),"!!1915=",""),"!!","") - else @other_dysplasia_procedure_done := null - end as other_dysplasia_procedure_done, - case - when obs regexp "!!7500=9725!!" then @dysplasia_mgmt_plan := 1 - when obs regexp "!!7500=9178!!" then @dysplasia_mgmt_plan := 2 - when obs regexp "!!7500=7497!!" then @dysplasia_mgmt_plan := 3 - when obs regexp "!!7500=7383!!" then @dysplasia_mgmt_plan := 4 - when obs regexp "!!7500=7499!!" then @dysplasia_mgmt_plan := 5 - when obs regexp "!!7500=5622!!" then @dysplasia_mgmt_plan:= 6 - else @dysplasia_mgmt_plan := null - end as dysplasia_mgmt_plan, - case - when obs regexp "!!1915=" then @other_dysplasia_mgmt_plan := replace(replace((substring_index(substring(obs,locate("!!1915=",obs)),@sep,1)),"!!1915=",""),"!!","") - else @other_dysplasia_mgmt_plan := null - end as other_dysplasia_mgmt_plan, - case - when obs regexp "!!7222=" then @dysplasia_assesment_notes := replace(replace((substring_index(substring(obs,locate("!!7222=",obs)),@sep,1)),"!!7222=",""),"!!","") - else @dysplasia_assesment_notes := null - end as dysplasia_assesment_notes, - case - when obs regexp "!!5096=" then @dysplasia_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - else @dysplasia_rtc_date := null - end as dysplasia_rtc_date, - case - when obs regexp "!!7423=1115!!" then @Pap_smear_results := 1 - when obs regexp "!!7423=7417!!" then @Pap_smear_results := 2 - when obs regexp "!!7423=7418!!" then @Pap_smear_results := 3 - when obs regexp "!!7423=7419!!" then @Pap_smear_results := 4 - when obs regexp "!!7423=7420!!" then @Pap_smear_results := 5 - when obs regexp "!!7423=7422!!" then @Pap_smear_results := 6 - else @Pap_smear_results := null - end as Pap_smear_results, - case - when obs regexp "!!8268=8266!!" then @leep_location := 1 - when obs regexp "!!8268=8267!!" then @leep_location := 2 - else @leep_location := null - end as leep_location, - case - when obs regexp "!!7645=1115!!" then @Cervix_biopsy_results := 1 - when obs regexp "!!7645=7424!!" then @Cervix_biopsy_results := 2 - when obs regexp "!!7645=7425!!" then @Cervix_biopsy_results := 3 - when obs regexp "!!7645=7216!!" then @Cervix_biopsy_results := 4 - when obs regexp "!!7645=1447!!" then @Cervix_biopsy_results := 5 - when obs regexp "!!7645=149!!" then @Cervix_biopsy_results := 6 - when obs regexp "!!7645=8282!!" then @Cervix_biopsy_results := 7 - when obs regexp "!!7645=9620!!" then @Cervix_biopsy_results := 8 - when obs regexp "!!7645=8276!!" then @Cervix_biopsy_results := 9 - when obs regexp "!!7645=9617!!" then @Cervix_biopsy_results := 10 - when obs regexp "!!7645=9621!!" then @Cervix_biopsy_results := 11 - when obs regexp "!!7645=7421!!" then @Cervix_biopsy_results := 12 - when obs regexp "!!7645=7422!!" then @Cervix_biopsy_results := 13 - when obs regexp "!!7645=9618!!" then @Cervix_biopsy_results := 14 - else @Cervix_biopsy_results := null - end as Cervix_biopsy_results, - case - when obs regexp "!!7647=1115!!" then @Vagina_biopsy_results := 1 - when obs regexp "!!7647=7492!!" then @Vagina_biopsy_results := 2 - when obs regexp "!!7647=7491!!" then @Vagina_biopsy_results := 3 - when obs regexp "!!7647=7435!!" then @Vagina_biopsy_results := 4 - when obs regexp "!!7647=6537!!" then @Vagina_biopsy_results := 5 - when obs regexp "!!7647=1447!!" then @Vagina_biopsy_results := 6 - when obs regexp "!!7647=8282!!" then @Vagina_biopsy_results := 7 - when obs regexp "!!7647=9620!!" then @Vagina_biopsy_results := 8 - when obs regexp "!!7647=8276!!" then @Vagina_biopsy_results := 9 - when obs regexp "!!7647=9617!!" then @Vagina_biopsy_results := 10 - when obs regexp "!!7647=9621!!" then @Vagina_biopsy_results := 11 - when obs regexp "!!7647=7421!!" then @Vagina_biopsy_results := 12 - when obs regexp "!!7647=7422!!" then @Vagina_biopsy_results := 13 - when obs regexp "!!7647=9618!!" then @Vagina_biopsy_results := 14 - else @Vagina_biopsy_results := null - end as Vagina_biopsy_results, - case - when obs regexp "!!7646=1115!!" then @Vulva_biopsy_result := 1 - when obs regexp "!!7646=7489!!" then @Vulva_biopsy_result := 2 - when obs regexp "!!7646=7488!!" then @Vulva_biopsy_result := 3 - when obs regexp "!!7646=7483!!" then @Vulva_biopsy_result := 4 - when obs regexp "!!7646=9618!!" then @Vulva_biopsy_result := 5 - when obs regexp "!!7646=1447!!" then @Vulva_biopsy_result := 6 - when obs regexp "!!7646=8282!!" then @Vulva_biopsy_result := 7 - when obs regexp "!!7646=9620!!" then @Vulva_biopsy_result := 8 - when obs regexp "!!7646=8276!!" then @Vulva_biopsy_result := 9 - when obs regexp "!!7646=9617!!" then @Vulva_biopsy_result := 10 - when obs regexp "!!7646=9621!!" then @Vulva_biopsy_result := 11 - when obs regexp "!!7646=7421!!" then @Vulva_biopsy_result := 12 - when obs regexp "!!7646=7422!!" then @Vulva_biopsy_result := 13 - else @Vulva_biopsy_result := null - end as Vulva_biopsy_result, - case - when obs regexp "!!10207=1115!!" then @endometrium_biopsy := 1 - when obs regexp "!!10207=8276!!" then @endometrium_biopsy := 2 - when obs regexp "!!10207=9617!!" then @endometrium_biopsy := 3 - when obs regexp "!!10207=9621!!" then @endometrium_biopsy := 4 - when obs regexp "!!10207=9618!!" then @endometrium_biopsy := 5 - when obs regexp "!!10207=7421!!" then @endometrium_biopsy := 6 - when obs regexp "!!10207=8282!!" then @endometrium_biopsy := 7 - when obs regexp "!!10207=9620!!" then @endometrium_biopsy := 8 - when obs regexp "!!10207=7422!!" then @endometrium_biopsy := 9 - else @endometrium_biopsy := null - end as endometrium_biopsy, - case - when obs regexp "!!10204=1115!!" then @ECC := 1 - when obs regexp "!!10204=7424!!" then @ECC := 2 - when obs regexp "!!10204=7425!!" then @ECC := 3 - when obs regexp "!!10204=7216!!" then @ECC := 4 - when obs regexp "!!10204=1447!!" then @ECC := 5 - when obs regexp "!!10204=149!!" then @ECC := 6 - when obs regexp "!!10204=8282!!" then @ECC := 7 - when obs regexp "!!10204=9620!!" then @ECC := 8 - when obs regexp "!!10204=8276!!" then @ECC := 9 - when obs regexp "!!10204=9617!!" then @ECC := 10 - when obs regexp "!!10204=9621!!" then @ECC := 11 - when obs regexp "!!10204=7421!!" then @ECC := 12 - when obs regexp "!!10204=7422!!" then @ECC := 13 - when obs regexp "!!10204=9618!!" then @ECC := 14 - else @ECC := null - end as ECC, - case - when obs regexp "!!7500=9725!!" then @biopsy_results_mngmt_plan := 1 - when obs regexp "!!7500=9178!!" then @biopsy_results_mngmt_plan := 2 - when obs regexp "!!7500=9498!!" then @biopsy_results_mngmt_plan := 2 - when obs regexp "!!7500=7496!!" then @biopsy_results_mngmt_plan := 3 - when obs regexp "!!7500=7497!!" then @biopsy_results_mngmt_plan := 4 - when obs regexp "!!7500=7499!!" then @biopsy_results_mngmt_plan := 5 - when obs regexp "!!7500=6105!!" then @biopsy_results_mngmt_plan := 6 - when obs regexp "!!7500=7147!!" then @biopsy_results_mngmt_plan := 7 - when obs regexp "!!7500=7466!!" then @biopsy_results_mngmt_plan := 8 - when obs regexp "!!7500=10200!!" then @biopsy_results_mngmt_plan := 9 - else @biopsy_results_mngmt_plan := null - end as biopsy_results_mngmt_plan, - case - when obs regexp "!!9868=9852!!" then @cancer_staging := 1 - when obs regexp "!!9868=9856!!" then @cancer_staging := 2 - when obs regexp "!!9868=9860!!" then @cancer_staging := 3 - when obs regexp "!!9868=9864!!" then @cancer_staging := 4 - else @cancer_staging := null - end as cancer_staging, - null as next_app_date - - from flat_cervical_cancer_screening_0 t1 - join amrs.person p using (person_id) - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - );# limit 100; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_cervical_cancer_screening_1 drop prev_id, drop cur_id; - - drop table if exists flat_cervical_cancer_screening_2; - create temporary table flat_cervical_cancer_screening_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_cervical_cancer_screening, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_cervical_cancer_screening, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_cervical_cancer_screening, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id_cervical_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_cervical_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date - - from flat_cervical_cancer_screening_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_cervical_cancer_screening_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_cervical_cancer_screening_3; - create temporary table flat_cervical_cancer_screening_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_cervical_cancer_screening, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_cervical_cancer_screening, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_cervical_cancer_screening, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id_cervical_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_cervical_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_cervical_cancer_screening_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - - SELECT - COUNT(*) -INTO @new_encounter_rows FROM - flat_cervical_cancer_screening_3; - -SELECT @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; -SELECT @total_rows_written; - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id , - encounter_id, - encounter_type, - encounter_datetime, - visit_id, - location_id, - t2.uuid as location_uuid, - uuid, - age, - encounter_purpose, - cur_visit_type, - actual_scheduled_date, - gravida, - parity, - Mensturation_status, - lmp, - pregnancy_status, - pregnant_edd, - reason_not_pregnant, - hiv_status, - viral_load, - viral_load_date, - prior_via_done, - prior_via_result, - prior_via_date, - cur_via_result, - visual_impression_cervix, - visual_impression_vagina, - visual_impression_vulva, - via_procedure_done, - other_via_procedure_done, - via_management_plan, - other_via_management_plan, - via_assessment_notes, - via_rtc_date, - prior_dysplasia_done, - previous_via_result, - previous_via_result_date, - prior_papsmear_result, - prior_biopsy_result, - prior_biopsy_result_date, - prior_biopsy_result_other, - prior_biopsy_result_date_other, - past_dysplasia_treatment, - treatment_specimen_pathology, - satisfactory_colposcopy, - colposcopy_findings, - cervica_lesion_size, - dysplasia_cervix_impression, - dysplasia_vagina_impression, - dysplasia_vulva_impression, - dysplasia_procedure_done, - other_dysplasia_procedure_done, - dysplasia_mgmt_plan, - other_dysplasia_mgmt_plan, - dysplasia_assesment_notes, - dysplasia_rtc_date, - Pap_smear_results, - leep_location, - Cervix_biopsy_results, - Vagina_biopsy_results, - Vulva_biopsy_result, - endometrium_biopsy, - ECC, - biopsy_results_mngmt_plan, - cancer_staging, - next_app_date, - - prev_encounter_datetime_cervical_cancer_screening, - next_encounter_datetime_cervical_cancer_screening, - prev_encounter_type_cervical_cancer_screening, - next_encounter_type_cervical_cancer_screening, - prev_clinical_datetime_cervical_cancer_screening, - next_clinical_datetime_cervical_cancer_screening, - prev_clinical_location_id_cervical_cancer_screening, - next_clinical_location_id_cervical_cancer_screening, - prev_clinical_rtc_date_cervical_cancer_screening, - next_clinical_rtc_date_cervical_cancer_screening - - from flat_cervical_cancer_screening_3 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_cervical_cancer_screening_build_queue__0 t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #select @person_ids_count := (select count(*) from flat_cervical_cancer_screening_build_queue_2); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count as remaining_in_build_queue; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - #select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); -SELECT - @person_ids_count AS 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - -#select 1; - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - SELECT - CONCAT(@start_write, - ' : Writing ', - @total_rows_to_write, - ' to ', - @primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); -SELECT - CONCAT(@finish_write, - ' : Completed writing rows. Time to write to primary table: ', - @time_to_write, - ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); -SELECT - CONCAT('Average Cycle Length: ', - @ave_cycle_length, - ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - SELECT - CONCAT(@table_version, - ' : Time to complete: ', - TIMESTAMPDIFF(MINUTE, @start, @end), - ' minutes'); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_covid_screening_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_covid_screening.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_flat_covid_screening_v1_0.sql rename to etl-scripts/stored-procedures/generate_flat_covid_screening.sql index 24ea628..1cb856e 100644 --- a/etl-scripts/stored-procedures/generate_flat_covid_screening_v1_0.sql +++ b/etl-scripts/stored-procedures/generate_flat_covid_screening.sql @@ -1,6 +1,6 @@ use etl; DELIMITER $$ -CREATE PROCEDURE `generate_flat_covid_screening_v1_0`() +CREATE PROCEDURE `generate_flat_covid_screening`() BEGIN select @start:= now(); diff --git a/etl-scripts/stored-procedures/generate_flat_general_oncology_treatment_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_general_oncology_treatment.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_flat_general_oncology_treatment_v1_0.sql rename to etl-scripts/stored-procedures/generate_flat_general_oncology_treatment.sql index 537d410..a0394e1 100644 --- a/etl-scripts/stored-procedures/generate_flat_general_oncology_treatment_v1_0.sql +++ b/etl-scripts/stored-procedures/generate_flat_general_oncology_treatment.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_general_oncology_treatment_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_general_oncology_treatment`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN set @primary_table := "flat_general_oncology_treatment"; set @query_type = query_type; diff --git a/etl-scripts/stored-procedures/generate_flat_hei_summary_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_hei_summary.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_flat_hei_summary_v1_0.sql rename to etl-scripts/stored-procedures/generate_flat_hei_summary.sql index 9a3e51b..de3d775 100644 --- a/etl-scripts/stored-procedures/generate_flat_hei_summary_v1_0.sql +++ b/etl-scripts/stored-procedures/generate_flat_hei_summary.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_hei_summary_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_hei_summary`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN set @primary_table := "flat_hei_summary"; set @total_rows_written = 0; diff --git a/etl-scripts/stored-procedures/generate_flat_hemophilia_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_hemophilia.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_flat_hemophilia_v1_0.sql rename to etl-scripts/stored-procedures/generate_flat_hemophilia.sql index 9161fe9..6842aea 100644 --- a/etl-scripts/stored-procedures/generate_flat_hemophilia_v1_0.sql +++ b/etl-scripts/stored-procedures/generate_flat_hemophilia.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_hemophilia_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_hemophilia`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN set @primary_table := "flat_hemophilia"; set @query_type = query_type; diff --git a/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v3_0.sql b/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v3_0.sql deleted file mode 100644 index d0bb923..0000000 --- a/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v3_0.sql +++ /dev/null @@ -1,369 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_labs_and_imaging_v3_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set session sort_buffer_size=512000000; - set session group_concat_max_len=100000; - set @start = now(); - set @primary_table := "flat_labs_and_imaging"; - select @table_version := 'flat_labs_and_imaging_v3.0'; - set @total_rows_written = 0; - set @query_type = query_type; - set @queue_number = queue_number; - set @queue_size = queue_size; - set @cycle_size = cycle_size; - - - - - - - - - - - - - - - - - - - set @concept_ids = '(1030, 1040, 856, 5497, 730, 21,653,790,12,6126,887,6252,1537,1271,9239,9020,857)'; - - - - - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - set @boundary = "!!"; - set @sep = " ## "; - - SET @dyn_sql = CONCAT('create table if not exists ', @primary_table, - ' (date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - encounter_id int, - test_datetime datetime, - encounter_type int, - hiv_dna_pcr int, - hiv_rapid_test int, - hiv_viral_load int, - cd4_count int, - cd4_percent decimal, - hemoglobin decimal, - ast int, - creatinine decimal, - chest_xray int, - hba1c decimal, - rbs decimal, - fbs decimal, - ecg int, - urea decimal, - has_errors text, - vl_error boolean, - cd4_error boolean, - hiv_dna_pcr_error boolean, - - tests_ordered varchar(1000), - primary key encounter_id (encounter_id), - index person_date (person_id, test_datetime), - index person_uuid (uuid) - )'); - - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - select CONCAT('BUILDING ',@primary_table,'..........................................'); - set @write_table = concat(@primary_table,"_temp_",@queue_number); - set @build_queue = concat(@primary_table,'_build_queue'); - set @queue_table = concat(@build_queue,'_',@queue_number); - - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(encounter_id int primary key) (select * from ',@build_queue,' limit ', @queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from ',@build_queue,' t1 join ',@queue_table, ' t2 using (encounter_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - - end if; - - - if (@query_type="sync") then - select CONCAT('SYNCING ',@primary_table,'..........................................'); - set @write_table = @primary_table; - set @queue_table = concat(@primary_table,'_sync_queue'); - - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - -create table flat_labs_and_imaging_sync_queue (person_id int, obs_datetime datetime, index test_datetime (person_id, obs_datetime)); - - - SET @dyn_sql=CONCAT( - 'replace into ',@queue_table, - '(select person_id, test_datetime - from flat_lab_obs - where date_created > @last_update - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - SET @dyn_sql=CONCAT('replace into ',@queue_table, - '(select person_id, obs_datetime - from amrs.obs t1 - where t1.date_voided > @last_updated - and concept_id in ',@concept_ids,' - group by person_id, obs_datetime)' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table,' t1 join ',@queue_table, ' t2 using (encounter_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - while @queue_count > 0 do - - set @loop_start_time = now(); - - - drop temporary table if exists temp_queue_table; - - SET @dyn_sql = CONCAT('create temporary table temp_queue_table like ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql= CONCAT('replace into temp_queue_table (select * from ',@queue_table,' limit ', @cycle_size,')'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - select count(*) as '# rows to write' from flat_vitals_0; - - SET @dyn_sql = CONCAT('replace into ',@write_table, - '( - - - )'); - - - -replace into flat_labs_and_imaging_sync_queue -(select person_id, test_datetime from flat_test_obs); - - -drop table if exists flat_labs_and_imaging_0; -set @queue_table = 'flat_labs_and_imaging_sync_queue'; - - SET @dyn_sql= CONCAT( - 'create temporary table flat_labs_and_imaging_0(primary key (encounter_id), index encounter_id (encounter_id), index person_test (person_id,test_datetime)) - (select t1.person_id, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t3.orders, - if(obs regexp "!!1030=",cast(replace(replace((substring_index(substring(obs,locate("1030=",obs)),@sep,1)),"1030=",""),"!!","") as unsigned),null) as hiv_dna_pcr, - if(obs regexp "!!1040=",cast(replace(replace((substring_index(substring(obs,locate("1040=",obs)),@sep,1)),"1040=",""),"!!","") as unsigned),null) as hiv_rapid_test, - case - when obs regexp "!!856=" then cast(replace(replace((substring_index(substring(obs,locate("856=",obs)),@sep,1)),"856=",""),"!!","") as unsigned) - end as hiv_viral_load, - if(obs regexp "!!5497=",cast(replace(replace((substring_index(substring(obs,locate("5497=",obs)),@sep,1)),"5497=",""),"!!","") as unsigned),null) as cd4_count, - if(obs regexp "!!730=",cast(replace(replace((substring_index(substring(obs,locate("730=",obs)),@sep,1)),"730=",""),"!!","") as decimal(3,1)),null) as cd4_percent, - if(obs regexp "!!21=",cast(replace(replace((substring_index(substring(obs,locate("21=",obs)),@sep,1)),"21=",""),"!!","") as decimal(4,1)),null) as hemoglobin, - if(obs regexp "!!653=",cast(replace(replace((substring_index(substring(obs,locate("653=",obs)),@sep,1)),"653=",""),"!!","") as unsigned),null) as ast, - if(obs regexp "!!790=",cast(replace(replace((substring_index(substring(obs,locate("790=",obs)),@sep,1)),"790=",""),"!!","") as decimal(4,1)),null) as creatinine, - if(obs regexp "!!12=" and not obs regexp "!!12=1107",cast(replace(replace((substring_index(substring(obs,locate("12=",obs)),@sep,1)),"12=",""),"!!","") as unsigned),null) as chest_xray, - if(obs regexp "!!6126=",cast(replace(replace((substring_index(substring(obs,locate("6126=",obs)),@sep,1)),"6126=",""),"!!","") as decimal(4,1)),null) as hba1c, - if(obs regexp "!!887=",cast(replace(replace((substring_index(substring(obs,locate("887=",obs)),@sep,1)),"887=",""),"!!","") as decimal(4,1)),null) as rbs, - if(obs regexp "!!6252=",cast(replace(replace((substring_index(substring(obs,locate("6252=",obs)),@sep,1)),"6252=",""),"!!","") as decimal(4,1)),null) as fbs, - if(obs regexp "!!1537=",cast(replace(replace((substring_index(substring(obs,locate("1537=",obs)),@sep,1)),"1537=",""),"!!","") as unsigned),null) as ecg, - if(obs regexp "!!857=",cast(replace(replace((substring_index(substring(obs,locate("857=",obs)),@sep,1)),"857=",""),"!!","") as decimal(4,1)),null) as urea, - if(obs regexp "!!9239=",obs,null) as has_errors, - if(obs regexp "!!9239=856!!",1,null) as vl_error, - if(obs regexp "!!9239=5497!!",1,null) as cd4_error, - if(obs regexp "!!9239=1030",1,null) as hiv_dna_pcr_error, - CONCAT( - case - when obs regexp "!!1271=" then - replace(replace((substring_index(substring(obs,locate("!!1271=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1271=", "") ) ) / LENGTH("!!1271=") ))),"!!1271=",""),"!!","") - else "" - end, - ifnull(orders,"") - ) as tests_ordered - - from flat_test_obs t1 ##NEED TO UPDATE - join ', @queue_table,' t2 on t1.person_id = t2.person_id and t1.test_datetime = t2.obs_datetime - left join flat_orders t3 on t1.test_datetime = t3.encounter_datetime and t1.person_id = t3.person_id - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - set @last_update = '2016-03-01'; - SET @dyn_sql= CONCAT( - 'insert into flat_labs_and_imaging_0 (person_id, encounter_id, test_datetime, encounter_type, location_id, orders,tests_ordered) - ( select t1.person_id, - t1.encounter_id, - t1.encounter_datetime as test_datetime, - t3.encounter_type, - t3.location_id, - t1.orders, - 0 as tests_ordered - from flat_orders t1 - join amrs.encounter t2 using (encounter_id) - left outer join flat_test_obs t3 ON t1.person_id = t3.person_id AND t1.encounter_datetime = t3.test_datetime - where t1.encounter_datetime >= @last_update and t3.person_id is null - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql = CONCAT( - 'insert into ',@write_table, - '(select - person_id, - t1.uuid, - encounter_id, - test_datetime, - encounter_type, - hiv_dna_pcr, - hiv_rapid_test, - hiv_viral_load, - cd4_count, - cd4_percent, - hemoglobin, - ast, - creatinine, - chest_xray, - hba1c, - rbs, - fbs, - ecg, - urea, - has_errors, - vl_error, - cd4_error, - hiv_dna_pcr_error, - tests_ordered - from flat_labs_and_imaging_0 - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join temp_queue_table t2 using (person_id, obs_datetime);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@queue_count / @cycle_size) / 60); - - - select @queue_count as '# in queue', @cycle_length as 'Cycle Time (s)', ceil(@queue_count / @cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - SET @dyn_sql = CONCAT( - 'delete t1 - from ',@primary_table, ' t1 - join amrs.person t2 using (person_id) - where t2.voided=1;'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - select @end := now(); - insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v4_0.sql b/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v4_0.sql deleted file mode 100644 index 079d78e..0000000 --- a/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v4_0.sql +++ /dev/null @@ -1,490 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_labs_and_imaging_v4_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set session sort_buffer_size=512000000; - set session group_concat_max_len=100000; - set @start = now(); - set @primary_table := "flat_labs_and_imaging"; - select @table_version := 'flat_labs_and_imaging_v4.0'; - set @total_rows_written = 0; - set @query_type = query_type; - set @queue_number = queue_number; - set @queue_size = queue_size; - set @cycle_size = cycle_size; - - # 1030 = HIV DNA PCR - # 1040 = HIV Rapid test - # 856 = HIV VIRAL LOAD, QUANTITATIVE - # 5497 = CD4, BY FACS - # 730 = CD4%, BY FACS - # 21 = HEMOGLOBIN - # 653 = AST - # 790 = SERUM CREATININE - # 12 = X-RAY, CHEST, PRELIMINARY FINDINGS - # 6126 = hba1c - # 887 = rbs - # 6252 = fbs - # 1537 = ecg - # 857 = SERUM BLOOD UREA NITROGEN - # 1271 = TESTS ORDERED - # 9239 = LABORATORY TEST WITH EXCEPTION - # 9020 = LAB ERROR - /* - 679 RBC - 21 HGB - 851 MCV - 1018 MCH - 1017 MCHC - 1016 RDW - 729 PLT - 678 SERUM WBC - 1330 ANC - 6134 Uric acid - 790 Creatinine - 1132 Sodium - 1133 Potassium - 1134 Chloride - 655 Total Bili - 1297 Direct Bili - 6123 GGT - 653 AST - 654 ALT - 717 Total Protein - 848 Albumin - 785 ALP - 1014 LDH - 10249 Total PSA - 10250 CEA - 10251 (CA 19-9) - 9010 HBF - 9011 HBA - 9699 HbS - 9012 HBA2 - */ - - set @concept_ids = '(1030, 1040, 856, 5497, 730, 21,653,790,12,6126,887,6252,1537,1271,9239,9020,857 - 679,21,851,1018,1017,1016,729,678,1330,6134,790,1132,1133,1134,655,1297,6123, - 653,654,717,848,785,1014,10249,10250,10251,9010,9011,9699,9012 - )'; - -#set @queue_number = 1; -#set @queue_size = 10000; -#set @cycle_size = 1000; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - set @boundary = "!!"; - set @sep = " ## "; - - SET @dyn_sql = CONCAT('create table if not exists ', @primary_table, - ' (date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - encounter_id int, - test_datetime datetime, - encounter_type int, - hiv_dna_pcr int, - hiv_rapid_test int, - hiv_viral_load int, - cd4_count int, - cd4_percent decimal, - chest_xray int, - hba1c decimal, - rbs decimal, - fbs decimal, - ecg int, - urea decimal, - has_errors text, - vl_error boolean, - cd4_error boolean, - hiv_dna_pcr_error boolean, - rbc decimal, - hemoglobin decimal, - mcv int, - mch decimal, - mchc decimal, - rdw decimal, - plt int, - wbc decimal, - anc decimal, - uric_acid decimal, - creatinine decimal, - na decimal, - k decimal, - cl decimal, - total_bili decimal, - direct_bili decimal, - ggt decimal, - ast decimal, - alt decimal, - total_protein decimal, - albumin decimal, - alk_phos decimal, - ldh decimal, - total_psa decimal, - cea decimal, - ca_19_9 decimal, - hbf decimal, - hba decimal, - hbs decimal, - hba2 decimal, - - tests_ordered varchar(1000), - primary key encounter_id (encounter_id), - index person_date (person_id, test_datetime), - index person_uuid (uuid) - )'); - - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - select CONCAT('BUILDING ',@primary_table,'..........................................'); - set @write_table = concat(@primary_table,"_temp_",@queue_number); -# set @build_queue = concat(@primary_table,'_build_queue'); - set @build_queue = concat('flat_labs_and_imaging_build_queue'); - set @queue_table = concat(@build_queue,'_',@queue_number); - - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(person_id int primary key) (select * from ',@build_queue,' limit ', @queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_obs_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from ',@build_queue,' t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - - end if; - - - if (@query_type="sync") then - select CONCAT('SYNCING ',@primary_table,'..........................................'); - set @write_table = @primary_table; - set @queue_table = concat(@primary_table,'_sync_queue'); - - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; -#set @last_update = "2018-01-26"; - create table if not exists flat_labs_and_imaging_sync_queue (person_id int primary key); - - - set @last_update = null; - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - - replace into flat_labs_and_imaging_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_labs_and_imaging_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - - replace into flat_labs_and_imaging_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_labs_and_imaging_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - end if; - - # delete all rows in primary table in the queue - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table,' t1 join ',@queue_table, ' t2 using (person_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - while @queue_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists temp_queue_table; - - SET @dyn_sql = CONCAT('create temporary table temp_queue_table like ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql= CONCAT('replace into temp_queue_table (select * from ',@queue_table,' limit ', @cycle_size,')'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - drop table if exists flat_labs_and_imaging_0; - - SET @dyn_sql= CONCAT( - 'create temporary table flat_labs_and_imaging_0(primary key (encounter_id), index encounter_id (encounter_id), index person_test (person_id,test_datetime)) - (select t1.person_id, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t3.orders, - if(obs regexp "!!1030=",cast(replace(replace((substring_index(substring(obs,locate("1030=",obs)),@sep,1)),"1030=",""),"!!","") as unsigned),null) as hiv_dna_pcr, - if(obs regexp "!!1040=",cast(replace(replace((substring_index(substring(obs,locate("1040=",obs)),@sep,1)),"1040=",""),"!!","") as unsigned),null) as hiv_rapid_test, - case - when obs regexp "!!856=" then cast(replace(replace((substring_index(substring(obs,locate("856=",obs)),@sep,1)),"856=",""),"!!","") as unsigned) - end as hiv_viral_load, - if(obs regexp "!!5497=",cast(replace(replace((substring_index(substring(obs,locate("5497=",obs)),@sep,1)),"5497=",""),"!!","") as unsigned),null) as cd4_count, - if(obs regexp "!!730=",cast(replace(replace((substring_index(substring(obs,locate("730=",obs)),@sep,1)),"730=",""),"!!","") as decimal(3,1)),null) as cd4_percent, - if(obs regexp "!!12=" and not obs regexp "!!12=1107",cast(replace(replace((substring_index(substring(obs,locate("12=",obs)),@sep,1)),"12=",""),"!!","") as unsigned),null) as chest_xray, - if(obs regexp "!!6126=",cast(replace(replace((substring_index(substring(obs,locate("6126=",obs)),@sep,1)),"6126=",""),"!!","") as decimal(4,1)),null) as hba1c, - if(obs regexp "!!887=",cast(replace(replace((substring_index(substring(obs,locate("887=",obs)),@sep,1)),"887=",""),"!!","") as decimal(4,1)),null) as rbs, - if(obs regexp "!!6252=",cast(replace(replace((substring_index(substring(obs,locate("6252=",obs)),@sep,1)),"6252=",""),"!!","") as decimal(4,1)),null) as fbs, - if(obs regexp "!!1537=",cast(replace(replace((substring_index(substring(obs,locate("1537=",obs)),@sep,1)),"1537=",""),"!!","") as unsigned),null) as ecg, - if(obs regexp "!!857=",cast(replace(replace((substring_index(substring(obs,locate("857=",obs)),@sep,1)),"857=",""),"!!","") as decimal(4,1)),null) as urea, - if(obs regexp "!!9239=",obs,null) as has_errors, - if(obs regexp "!!9239=856!!",1,null) as vl_error, - if(obs regexp "!!9239=5497!!",1,null) as cd4_error, - if(obs regexp "!!9239=1030",1,null) as hiv_dna_pcr_error, - if(obs regexp "!!679=",cast(getValues(obs,679) as decimal(6,2)),null) as rbc, - if(obs regexp "!!21=",cast(getValues(obs,21) as decimal(6,2)),null) as hemoglobin, - if(obs regexp "!!851=",cast(getValues(obs,851) as unsigned),null) as mcv, - if(obs regexp "!!1018=",cast(getValues(obs,1018) as decimal(6,2)),null) as mch, - if(obs regexp "!!1017=",cast(getValues(obs,1017) as decimal(6,2)),null) as mchc, - if(obs regexp "!!1016=",cast(getValues(obs,1016) as decimal(6,2)),null) as rdw, - if(obs regexp "!!729=",cast(getValues(obs,729) as unsigned),null) as plt, - if(obs regexp "!!678=",cast(getValues(obs,678) as decimal(6,2)),null) as wbc, - if(obs regexp "!!1330=",cast(getValues(obs,1330) as decimal(6,2)),null) as anc, - - if(obs regexp "!!6134=",cast(getValues(obs,6134) as decimal(6,2)),null) as uric_acid, - if(obs regexp "!!790=",cast(getValues(obs,790) as decimal(6,2)),null) as creatinine, - if(obs regexp "!!1132=",cast(getValues(obs,1132) as decimal(6,2)),null) as na, - if(obs regexp "!!1133=",cast(getValues(obs,1133) as decimal(6,2)),null) as k, - if(obs regexp "!!1134=",cast(getValues(obs,1134) as decimal(6,2)),null) as cl, - - if(obs regexp "!!655=",cast(getValues(obs,655) as decimal(6,2)),null) as total_bili, - if(obs regexp "!!1297=",cast(getValues(obs,1297) as decimal(6,2)),null) as direct_bili, - if(obs regexp "!!6123=",cast(getValues(obs,6123) as decimal(6,2)),null) as ggt, - if(obs regexp "!!653=",cast(getValues(obs,653) as decimal(6,2)),null) as ast, - if(obs regexp "!!654=",cast(getValues(obs,654) as decimal(6,2)),null) as alt, - if(obs regexp "!!717=",cast(getValues(obs,717) as decimal(6,2)),null) as total_protein, - if(obs regexp "!!848=",cast(getValues(obs,848) as decimal(6,2)),null) as albumin, - if(obs regexp "!!785=",cast(getValues(obs,785) as decimal(6,2)),null) as alk_phos, - if(obs regexp "!!1014=",cast(getValues(obs,1014) as decimal(6,2)),null) as ldh, - - if(obs regexp "!!10249=",cast(getValues(obs,10249) as decimal(6,2)),null) as total_psa, - - if(obs regexp "!!10250=",cast(getValues(obs,10250) as decimal(6,2)),null) as cea, - - if(obs regexp "!!10251=",cast(getValues(obs,10251) as decimal(6,2)),null) as ca_19_9, - - if(obs regexp "!!9010=",cast(getValues(obs,9010) as decimal(6,2)),null) as hbf, - if(obs regexp "!!9011=",cast(getValues(obs,9011) as decimal(6,2)),null) as hba, - if(obs regexp "!!9699=",cast(getValues(obs,9699) as decimal(6,2)),null) as hbs, - if(obs regexp "!!9012=",cast(getValues(obs,9012) as decimal(6,2)),null) as hba2, - - - CONCAT( - case - when obs regexp "!!1271=" then getValues(obs,1271) - # replace(replace((substring_index(substring(obs,locate("!!1271=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1271=", "") ) ) / LENGTH("!!1271=") ))),"!!1271=",""),"!!","") - else "" - end, - ifnull(orders,"") - ) as tests_ordered - - from flat_lab_obs t1 - join temp_queue_table t2 using(person_id) - left outer join flat_orders t3 using(encounter_id) - )' - ); - - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - -/* -# set @last_update = '2016-03-01'; - SET @dyn_sql= CONCAT( - 'insert into flat_labs_and_imaging_0 (person_id, encounter_id, test_datetime, encounter_type, location_id, orders,tests_ordered) - ( select t1.person_id, - t1.encounter_id, - t1.encounter_datetime as test_datetime, - t3.encounter_type, - t3.location_id, - t1.orders, - 0 as tests_ordered - from flat_orders t1 - join ', @queue_table,' t2 using(person_id) - join amrs.encounter t2 using (encounter_id) - left outer join flat_lab_obs t3 ON t1.person_id = t3.person_id AND t1.encounter_datetime = t3.test_datetime - where - t3.person_id is null - # and t1.encounter_datetime >= @last_update and - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; -*/ - - SET @dyn_sql = CONCAT( - 'insert into ',@write_table, - '(select - null, - person_id, - uuid, - encounter_id, - test_datetime, - encounter_type , - hiv_dna_pcr, - hiv_rapid_test, - hiv_viral_load, - cd4_count, - cd4_percent, - chest_xray, - hba1c, - rbs, - fbs, - ecg, - urea, - has_errors, - vl_error, - cd4_error, - hiv_dna_pcr_error, - rbc, - hemoglobin, - mcv, - mch, - mchc, - rdw, - plt, - wbc, - anc, - uric_acid, - creatinine, - na, - k, - cl, - total_bili, - direct_bili, - ggt, - ast, - alt, - total_protein, - albumin, - alk_phos, - ldh, - total_psa, - cea, - ca_19_9, - hbf, - hba, - hbs, - hba2, - tests_ordered - from flat_labs_and_imaging_0 t1 - join amrs.person t2 using (person_id) - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('delete t1.* from ',@queue_table,' t1 join temp_queue_table t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@queue_count / @cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select @queue_count as '# in queue', @cycle_length as 'Cycle Time (s)', ceil(@queue_count / @cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - SET @dyn_sql = CONCAT( - 'delete t1 - from ',@primary_table, ' t1 - join amrs.person t2 using (person_id) - where t2.voided=1;'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - select @end := now(); - insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v4_1.sql b/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v4_1.sql deleted file mode 100644 index 9b65317..0000000 --- a/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v4_1.sql +++ /dev/null @@ -1,493 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_labs_and_imaging_v4_1`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set session sort_buffer_size=512000000; - set session group_concat_max_len=100000; - set @start = now(); - set @primary_table := "flat_labs_and_imaging"; - select @table_version := 'flat_labs_and_imaging_v4.1'; - set @total_rows_written = 0; - set @query_type = query_type; - set @queue_number = queue_number; - set @queue_size = queue_size; - set @cycle_size = cycle_size; - - # 1030 = HIV DNA PCR - # 1040 = HIV Rapid test - # 856 = HIV VIRAL LOAD, QUANTITATIVE - # 5497 = CD4, BY FACS - # 730 = CD4%, BY FACS - # 21 = HEMOGLOBIN - # 653 = AST - # 790 = SERUM CREATININE - # 12 = X-RAY, CHEST, PRELIMINARY FINDINGS - # 6126 = hba1c - # 887 = rbs - # 6252 = fbs - # 1537 = ecg - # 857 = SERUM BLOOD UREA NITROGEN - # 1271 = TESTS ORDERED - # 9239 = LABORATORY TEST WITH EXCEPTION - # 9020 = LAB ERROR - /* - 679 RBC - 21 HGB - 851 MCV - 1018 MCH - 1017 MCHC - 1016 RDW - 729 PLT - 678 SERUM WBC - 1330 ANC - 6134 Uric acid - 790 Creatinine - 1132 Sodium - 1133 Potassium - 1134 Chloride - 655 Total Bili - 1297 Direct Bili - 6123 GGT - 653 AST - 654 ALT - 717 Total Protein - 848 Albumin - 785 ALP - 1014 LDH - 10249 Total PSA - 10250 CEA - 10251 (CA 19-9) - 9010 HBF - 9011 HBA - 9699 HbS - 9012 HBA2 - 9812 SERUM CRAG - */ - - set @concept_ids = '(1030, 1040, 856, 5497, 730, 21,653,790,12,6126,887,6252,1537,1271,9239,9020,857 - 679,21,851,1018,1017,1016,729,678,1330,6134,790,1132,1133,1134,655,1297,6123, - 653,654,717,848,785,1014,10249,10250,10251,9010,9011,9699,9012,9812 - )'; - -#set @queue_number = 1; -#set @queue_size = 10000; -#set @cycle_size = 1000; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - set @boundary = "!!"; - set @sep = " ## "; - - SET @dyn_sql = CONCAT('create table if not exists ', @primary_table, - ' (date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - encounter_id int, - test_datetime datetime, - encounter_type int, - hiv_dna_pcr int, - hiv_rapid_test int, - hiv_viral_load int, - cd4_count int, - cd4_percent decimal, - chest_xray int, - hba1c decimal, - rbs decimal, - fbs decimal, - ecg int, - urea decimal, - has_errors text, - vl_error boolean, - cd4_error boolean, - hiv_dna_pcr_error boolean, - rbc decimal, - hemoglobin decimal, - mcv int, - mch decimal, - mchc decimal, - rdw decimal, - plt int, - wbc decimal, - anc decimal, - uric_acid decimal, - creatinine decimal, - na decimal, - k decimal, - cl decimal, - total_bili decimal, - direct_bili decimal, - ggt decimal, - ast decimal, - alt decimal, - total_protein decimal, - albumin decimal, - alk_phos decimal, - ldh decimal, - total_psa decimal, - cea decimal, - ca_19_9 decimal, - hbf decimal, - hba decimal, - hbs decimal, - hba2 decimal, - serum_crag decimal, - tests_ordered varchar(1000), - primary key encounter_id (encounter_id), - index person_date (person_id, test_datetime), - index person_uuid (uuid) - )'); - - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - select CONCAT('BUILDING ',@primary_table,'..........................................'); - set @write_table = concat(@primary_table,"_temp_",@queue_number); -# set @build_queue = concat(@primary_table,'_build_queue'); - set @build_queue = concat('flat_labs_and_imaging_build_queue'); - set @queue_table = concat(@build_queue,'_',@queue_number); - - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(person_id int primary key) (select * from ',@build_queue,' limit ', @queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_obs_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from ',@build_queue,' t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - - end if; - - - if (@query_type="sync") then - select CONCAT('SYNCING ',@primary_table,'..........................................'); - set @write_table = @primary_table; - set @queue_table = concat(@primary_table,'_sync_queue'); - - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; -#set @last_update = "2018-01-26"; - create table if not exists flat_labs_and_imaging_sync_queue (person_id int primary key); - - - set @last_update = null; - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - - replace into flat_labs_and_imaging_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_labs_and_imaging_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - - replace into flat_labs_and_imaging_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_labs_and_imaging_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - end if; - - # delete all rows in primary table in the queue - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table,' t1 join ',@queue_table, ' t2 using (person_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - while @queue_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists temp_queue_table; - - SET @dyn_sql = CONCAT('create temporary table temp_queue_table like ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql= CONCAT('replace into temp_queue_table (select * from ',@queue_table,' limit ', @cycle_size,')'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - drop table if exists flat_labs_and_imaging_0; - - SET @dyn_sql= CONCAT( - 'create temporary table flat_labs_and_imaging_0(primary key (encounter_id), index encounter_id (encounter_id), index person_test (person_id,test_datetime)) - (select t1.person_id, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t3.orders, - if(obs regexp "!!1030=",cast(replace(replace((substring_index(substring(obs,locate("1030=",obs)),@sep,1)),"1030=",""),"!!","") as unsigned),null) as hiv_dna_pcr, - if(obs regexp "!!1040=",cast(replace(replace((substring_index(substring(obs,locate("1040=",obs)),@sep,1)),"1040=",""),"!!","") as unsigned),null) as hiv_rapid_test, - case - when obs regexp "!!856=" then cast(replace(replace((substring_index(substring(obs,locate("856=",obs)),@sep,1)),"856=",""),"!!","") as unsigned) - end as hiv_viral_load, - if(obs regexp "!!5497=",cast(replace(replace((substring_index(substring(obs,locate("5497=",obs)),@sep,1)),"5497=",""),"!!","") as unsigned),null) as cd4_count, - if(obs regexp "!!730=",cast(replace(replace((substring_index(substring(obs,locate("730=",obs)),@sep,1)),"730=",""),"!!","") as decimal(3,1)),null) as cd4_percent, - if(obs regexp "!!12=" and not obs regexp "!!12=1107",cast(replace(replace((substring_index(substring(obs,locate("12=",obs)),@sep,1)),"12=",""),"!!","") as unsigned),null) as chest_xray, - if(obs regexp "!!6126=",cast(replace(replace((substring_index(substring(obs,locate("6126=",obs)),@sep,1)),"6126=",""),"!!","") as decimal(4,1)),null) as hba1c, - if(obs regexp "!!887=",cast(replace(replace((substring_index(substring(obs,locate("887=",obs)),@sep,1)),"887=",""),"!!","") as decimal(4,1)),null) as rbs, - if(obs regexp "!!6252=",cast(replace(replace((substring_index(substring(obs,locate("6252=",obs)),@sep,1)),"6252=",""),"!!","") as decimal(4,1)),null) as fbs, - if(obs regexp "!!1537=",cast(replace(replace((substring_index(substring(obs,locate("1537=",obs)),@sep,1)),"1537=",""),"!!","") as unsigned),null) as ecg, - if(obs regexp "!!857=",cast(replace(replace((substring_index(substring(obs,locate("857=",obs)),@sep,1)),"857=",""),"!!","") as decimal(4,1)),null) as urea, - if(obs regexp "!!9239=",obs,null) as has_errors, - if(obs regexp "!!9239=856!!",1,null) as vl_error, - if(obs regexp "!!9239=5497!!",1,null) as cd4_error, - if(obs regexp "!!9239=1030",1,null) as hiv_dna_pcr_error, - if(obs regexp "!!679=",cast(getValues(obs,679) as decimal(6,2)),null) as rbc, - if(obs regexp "!!21=",cast(getValues(obs,21) as decimal(6,2)),null) as hemoglobin, - if(obs regexp "!!851=",cast(getValues(obs,851) as unsigned),null) as mcv, - if(obs regexp "!!1018=",cast(getValues(obs,1018) as decimal(6,2)),null) as mch, - if(obs regexp "!!1017=",cast(getValues(obs,1017) as decimal(6,2)),null) as mchc, - if(obs regexp "!!1016=",cast(getValues(obs,1016) as decimal(6,2)),null) as rdw, - if(obs regexp "!!729=",cast(getValues(obs,729) as unsigned),null) as plt, - if(obs regexp "!!678=",cast(getValues(obs,678) as decimal(6,2)),null) as wbc, - if(obs regexp "!!1330=",cast(getValues(obs,1330) as decimal(6,2)),null) as anc, - - if(obs regexp "!!6134=",cast(getValues(obs,6134) as decimal(6,2)),null) as uric_acid, - if(obs regexp "!!790=",cast(getValues(obs,790) as decimal(6,2)),null) as creatinine, - if(obs regexp "!!1132=",cast(getValues(obs,1132) as decimal(6,2)),null) as na, - if(obs regexp "!!1133=",cast(getValues(obs,1133) as decimal(6,2)),null) as k, - if(obs regexp "!!1134=",cast(getValues(obs,1134) as decimal(6,2)),null) as cl, - - if(obs regexp "!!655=",cast(getValues(obs,655) as decimal(6,2)),null) as total_bili, - if(obs regexp "!!1297=",cast(getValues(obs,1297) as decimal(6,2)),null) as direct_bili, - if(obs regexp "!!6123=",cast(getValues(obs,6123) as decimal(6,2)),null) as ggt, - if(obs regexp "!!653=",cast(getValues(obs,653) as decimal(6,2)),null) as ast, - if(obs regexp "!!654=",cast(getValues(obs,654) as decimal(6,2)),null) as alt, - if(obs regexp "!!717=",cast(getValues(obs,717) as decimal(6,2)),null) as total_protein, - if(obs regexp "!!848=",cast(getValues(obs,848) as decimal(6,2)),null) as albumin, - if(obs regexp "!!785=",cast(getValues(obs,785) as decimal(6,2)),null) as alk_phos, - if(obs regexp "!!1014=",cast(getValues(obs,1014) as decimal(6,2)),null) as ldh, - - if(obs regexp "!!10249=",cast(getValues(obs,10249) as decimal(6,2)),null) as total_psa, - - if(obs regexp "!!10250=",cast(getValues(obs,10250) as decimal(6,2)),null) as cea, - - if(obs regexp "!!10251=",cast(getValues(obs,10251) as decimal(6,2)),null) as ca_19_9, - - if(obs regexp "!!9010=",cast(getValues(obs,9010) as decimal(6,2)),null) as hbf, - if(obs regexp "!!9011=",cast(getValues(obs,9011) as decimal(6,2)),null) as hba, - if(obs regexp "!!9699=",cast(getValues(obs,9699) as decimal(6,2)),null) as hbs, - if(obs regexp "!!9012=",cast(getValues(obs,9012) as decimal(6,2)),null) as hba2, - if(obs regexp "!!9812=",cast(getValues(obs,9812) as decimal(6,2)),null) as serum_crag, - - - CONCAT( - case - when obs regexp "!!1271=" then getValues(obs,1271) - # replace(replace((substring_index(substring(obs,locate("!!1271=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1271=", "") ) ) / LENGTH("!!1271=") ))),"!!1271=",""),"!!","") - else "" - end, - ifnull(orders,"") - ) as tests_ordered - - from flat_lab_obs t1 - join temp_queue_table t2 using(person_id) - left outer join flat_orders t3 using(encounter_id) - )' - ); - - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - -/* -# set @last_update = '2016-03-01'; - SET @dyn_sql= CONCAT( - 'insert into flat_labs_and_imaging_0 (person_id, encounter_id, test_datetime, encounter_type, location_id, orders,tests_ordered) - ( select t1.person_id, - t1.encounter_id, - t1.encounter_datetime as test_datetime, - t3.encounter_type, - t3.location_id, - t1.orders, - 0 as tests_ordered - from flat_orders t1 - join ', @queue_table,' t2 using(person_id) - join amrs.encounter t2 using (encounter_id) - left outer join flat_lab_obs t3 ON t1.person_id = t3.person_id AND t1.encounter_datetime = t3.test_datetime - where - t3.person_id is null - # and t1.encounter_datetime >= @last_update and - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; -*/ - - SET @dyn_sql = CONCAT( - 'insert into ',@write_table, - '(select - null, - person_id, - uuid, - encounter_id, - test_datetime, - encounter_type , - hiv_dna_pcr, - hiv_rapid_test, - hiv_viral_load, - cd4_count, - cd4_percent, - chest_xray, - hba1c, - rbs, - fbs, - ecg, - urea, - has_errors, - vl_error, - cd4_error, - hiv_dna_pcr_error, - rbc, - hemoglobin, - mcv, - mch, - mchc, - rdw, - plt, - wbc, - anc, - uric_acid, - creatinine, - na, - k, - cl, - total_bili, - direct_bili, - ggt, - ast, - alt, - total_protein, - albumin, - alk_phos, - ldh, - total_psa, - cea, - ca_19_9, - hbf, - hba, - hbs, - hba2, - serum_crag, - tests_ordered - from flat_labs_and_imaging_0 t1 - join amrs.person t2 using (person_id) - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('delete t1.* from ',@queue_table,' t1 join temp_queue_table t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@queue_count / @cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select @queue_count as '# in queue', @cycle_length as 'Cycle Time (s)', ceil(@queue_count / @cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - SET @dyn_sql = CONCAT( - 'delete t1 - from ',@primary_table, ' t1 - join amrs.person t2 using (person_id) - where t2.voided=1;'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - select @end := now(); - insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v4_2.sql b/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v4_2.sql deleted file mode 100644 index e4dec44..0000000 --- a/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v4_2.sql +++ /dev/null @@ -1,496 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_labs_and_imaging_v4_2`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set session sort_buffer_size=512000000; - set session group_concat_max_len=100000; - set @start = now(); - set @primary_table := "flat_labs_and_imaging"; - select @table_version := 'flat_labs_and_imaging_v4.2'; - set @total_rows_written = 0; - set @query_type = query_type; - set @queue_number = queue_number; - set @queue_size = queue_size; - set @cycle_size = cycle_size; - - # 1030 = HIV DNA PCR - # 1040 = HIV Rapid test - # 856 = HIV VIRAL LOAD, QUANTITATIVE - # 5497 = CD4, BY FACS - # 730 = CD4%, BY FACS - # 21 = HEMOGLOBIN - # 653 = AST - # 790 = SERUM CREATININE - # 12 = X-RAY, CHEST, PRELIMINARY FINDINGS - # 6126 = hba1c - # 887 = rbs - # 6252 = fbs - # 1537 = ecg - # 857 = SERUM BLOOD UREA NITROGEN - # 1271 = TESTS ORDERED - # 9239 = LABORATORY TEST WITH EXCEPTION - # 9020 = LAB ERROR - /* - 679 RBC - 21 HGB - 851 MCV - 1018 MCH - 1017 MCHC - 1016 RDW - 729 PLT - 678 SERUM WBC - 1330 ANC - 6134 Uric acid - 790 Creatinine - 1132 Sodium - 1133 Potassium - 1134 Chloride - 655 Total Bili - 1297 Direct Bili - 6123 GGT - 653 AST - 654 ALT - 717 Total Protein - 848 Albumin - 785 ALP - 1014 LDH - 10249 Total PSA - 10250 CEA - 10251 (CA 19-9) - 9010 HBF - 9011 HBA - 9699 HbS - 9012 HBA2 - 9812 SERUM CRAG - 10304 GENEXPERT, IMAGE - 10313 DRUG SENSITIVITY TEST, IMAGE - */ - - set @concept_ids = '(1030, 1040, 856, 5497, 730, 21,653,790,12,6126,887,6252,1537,1271,9239,9020,857 - 679,21,851,1018,1017,1016,729,678,1330,6134,790,1132,1133,1134,655,1297,6123, - 653,654,717,848,785,1014,10249,10250,10251,9010,9011,9699,9012,9812,10304,10313 - )'; - -#set @queue_number = 1; -#set @queue_size = 10000; -#set @cycle_size = 1000; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - set @boundary = "!!"; - set @sep = " ## "; - - SET @dyn_sql = CONCAT('create table if not exists ', @primary_table, - ' (date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - encounter_id int, - test_datetime datetime, - encounter_type int, - hiv_dna_pcr int, - hiv_rapid_test int, - hiv_viral_load int, - cd4_count int, - cd4_percent decimal, - chest_xray int, - hba1c decimal, - rbs decimal, - fbs decimal, - ecg int, - urea decimal, - has_errors text, - vl_error boolean, - cd4_error boolean, - hiv_dna_pcr_error boolean, - rbc decimal, - hemoglobin decimal, - mcv int, - mch decimal, - mchc decimal, - rdw decimal, - plt int, - wbc decimal, - anc decimal, - uric_acid decimal, - creatinine decimal, - na decimal, - k decimal, - cl decimal, - total_bili decimal, - direct_bili decimal, - ggt decimal, - ast decimal, - alt decimal, - total_protein decimal, - albumin decimal, - alk_phos decimal, - ldh decimal, - total_psa decimal, - cea decimal, - ca_19_9 decimal, - hbf decimal, - hba decimal, - hbs decimal, - hba2 decimal, - serum_crag decimal, - gene_expert_image varchar(255), - dst_image varchar(255), - tests_ordered varchar(1000), - primary key encounter_id (encounter_id), - index person_date (person_id, test_datetime), - index person_uuid (uuid) - )'); - - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - select CONCAT('BUILDING ',@primary_table,'..........................................'); - set @write_table = concat(@primary_table,"_temp_",@queue_number); -# set @build_queue = concat(@primary_table,'_build_queue'); - set @build_queue = concat('flat_labs_and_imaging_build_queue'); - set @queue_table = concat(@build_queue,'_',@queue_number); - - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(person_id int primary key) (select * from ',@build_queue,' limit ', @queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_obs_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from ',@build_queue,' t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - - end if; - - - if (@query_type="sync") then - select CONCAT('SYNCING ',@primary_table,'..........................................'); - set @write_table = @primary_table; - set @queue_table = concat(@primary_table,'_sync_queue'); - - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; -#set @last_update = "2018-01-26"; - create table if not exists flat_labs_and_imaging_sync_queue (person_id int primary key); - - - set @last_update = null; - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - - replace into flat_labs_and_imaging_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_labs_and_imaging_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - - replace into flat_labs_and_imaging_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_labs_and_imaging_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - end if; - - # delete all rows in primary table in the queue - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table,' t1 join ',@queue_table, ' t2 using (person_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - while @queue_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists temp_queue_table; - - SET @dyn_sql = CONCAT('create temporary table temp_queue_table like ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql= CONCAT('replace into temp_queue_table (select * from ',@queue_table,' limit ', @cycle_size,')'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - drop table if exists flat_labs_and_imaging_0; - - SET @dyn_sql= CONCAT( - 'create temporary table flat_labs_and_imaging_0(primary key (encounter_id), index encounter_id (encounter_id), index person_test (person_id,test_datetime)) - (select t1.person_id, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t3.orders, - if(obs regexp "!!1030=",cast(replace(replace((substring_index(substring(obs,locate("1030=",obs)),@sep,1)),"1030=",""),"!!","") as unsigned),null) as hiv_dna_pcr, - if(obs regexp "!!1040=",cast(replace(replace((substring_index(substring(obs,locate("1040=",obs)),@sep,1)),"1040=",""),"!!","") as unsigned),null) as hiv_rapid_test, - case - when obs regexp "!!856=" then cast(replace(replace((substring_index(substring(obs,locate("856=",obs)),@sep,1)),"856=",""),"!!","") as unsigned) - end as hiv_viral_load, - if(obs regexp "!!5497=",cast(replace(replace((substring_index(substring(obs,locate("5497=",obs)),@sep,1)),"5497=",""),"!!","") as unsigned),null) as cd4_count, - if(obs regexp "!!730=",cast(replace(replace((substring_index(substring(obs,locate("730=",obs)),@sep,1)),"730=",""),"!!","") as decimal(3,1)),null) as cd4_percent, - if(obs regexp "!!12=" and not obs regexp "!!12=1107",cast(replace(replace((substring_index(substring(obs,locate("12=",obs)),@sep,1)),"12=",""),"!!","") as unsigned),null) as chest_xray, - if(obs regexp "!!6126=",cast(replace(replace((substring_index(substring(obs,locate("6126=",obs)),@sep,1)),"6126=",""),"!!","") as decimal(4,1)),null) as hba1c, - if(obs regexp "!!887=",cast(replace(replace((substring_index(substring(obs,locate("887=",obs)),@sep,1)),"887=",""),"!!","") as decimal(4,1)),null) as rbs, - if(obs regexp "!!6252=",cast(replace(replace((substring_index(substring(obs,locate("6252=",obs)),@sep,1)),"6252=",""),"!!","") as decimal(4,1)),null) as fbs, - if(obs regexp "!!1537=",cast(replace(replace((substring_index(substring(obs,locate("1537=",obs)),@sep,1)),"1537=",""),"!!","") as unsigned),null) as ecg, - if(obs regexp "!!857=",cast(replace(replace((substring_index(substring(obs,locate("857=",obs)),@sep,1)),"857=",""),"!!","") as decimal(4,1)),null) as urea, - if(obs regexp "!!9239=",obs,null) as has_errors, - if(obs regexp "!!9239=856!!",1,null) as vl_error, - if(obs regexp "!!9239=5497!!",1,null) as cd4_error, - if(obs regexp "!!9239=1030",1,null) as hiv_dna_pcr_error, - if(obs regexp "!!679=",cast(getValues(obs,679) as decimal(6,2)),null) as rbc, - if(obs regexp "!!21=",cast(getValues(obs,21) as decimal(6,2)),null) as hemoglobin, - if(obs regexp "!!851=",cast(getValues(obs,851) as unsigned),null) as mcv, - if(obs regexp "!!1018=",cast(getValues(obs,1018) as decimal(6,2)),null) as mch, - if(obs regexp "!!1017=",cast(getValues(obs,1017) as decimal(6,2)),null) as mchc, - if(obs regexp "!!1016=",cast(getValues(obs,1016) as decimal(6,2)),null) as rdw, - if(obs regexp "!!729=",cast(getValues(obs,729) as unsigned),null) as plt, - if(obs regexp "!!678=",cast(getValues(obs,678) as decimal(6,2)),null) as wbc, - if(obs regexp "!!1330=",cast(getValues(obs,1330) as decimal(6,2)),null) as anc, - if(obs regexp "!!6134=",cast(getValues(obs,6134) as decimal(6,2)),null) as uric_acid, - if(obs regexp "!!790=",cast(getValues(obs,790) as decimal(6,2)),null) as creatinine, - if(obs regexp "!!1132=",cast(getValues(obs,1132) as decimal(6,2)),null) as na, - if(obs regexp "!!1133=",cast(getValues(obs,1133) as decimal(6,2)),null) as k, - if(obs regexp "!!1134=",cast(getValues(obs,1134) as decimal(6,2)),null) as cl, - if(obs regexp "!!655=",cast(getValues(obs,655) as decimal(6,2)),null) as total_bili, - if(obs regexp "!!1297=",cast(getValues(obs,1297) as decimal(6,2)),null) as direct_bili, - if(obs regexp "!!6123=",cast(getValues(obs,6123) as decimal(6,2)),null) as ggt, - if(obs regexp "!!653=",cast(getValues(obs,653) as decimal(6,2)),null) as ast, - if(obs regexp "!!654=",cast(getValues(obs,654) as decimal(6,2)),null) as alt, - if(obs regexp "!!717=",cast(getValues(obs,717) as decimal(6,2)),null) as total_protein, - if(obs regexp "!!848=",cast(getValues(obs,848) as decimal(6,2)),null) as albumin, - if(obs regexp "!!785=",cast(getValues(obs,785) as decimal(6,2)),null) as alk_phos, - if(obs regexp "!!1014=",cast(getValues(obs,1014) as decimal(6,2)),null) as ldh, - if(obs regexp "!!10249=",cast(getValues(obs,10249) as decimal(6,2)),null) as total_psa, - if(obs regexp "!!10250=",cast(getValues(obs,10250) as decimal(6,2)),null) as cea, - if(obs regexp "!!10251=",cast(getValues(obs,10251) as decimal(6,2)),null) as ca_19_9, - if(obs regexp "!!9010=",cast(getValues(obs,9010) as decimal(6,2)),null) as hbf, - if(obs regexp "!!9011=",cast(getValues(obs,9011) as decimal(6,2)),null) as hba, - if(obs regexp "!!9699=",cast(getValues(obs,9699) as decimal(6,2)),null) as hbs, - if(obs regexp "!!9012=",cast(getValues(obs,9012) as decimal(6,2)),null) as hba2, - if(obs regexp "!!9812=",cast(getValues(obs,9812) as decimal(6,2)),null) as serum_crag, - if(obs regexp "!!10304=",getValues(obs,10304),null) as gene_expert_image, - if(obs regexp "!!10313=",getValues(obs,10313),null) as dst_image, - - - - CONCAT( - case - when obs regexp "!!1271=" then getValues(obs,1271) - # replace(replace((substring_index(substring(obs,locate("!!1271=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1271=", "") ) ) / LENGTH("!!1271=") ))),"!!1271=",""),"!!","") - else "" - end, - ifnull(orders,"") - ) as tests_ordered - - from flat_lab_obs t1 - join temp_queue_table t2 using(person_id) - left outer join flat_orders t3 using(encounter_id) - )' - ); - - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - -/* -# set @last_update = '2016-03-01'; - SET @dyn_sql= CONCAT( - 'insert into flat_labs_and_imaging_0 (person_id, encounter_id, test_datetime, encounter_type, location_id, orders,tests_ordered) - ( select t1.person_id, - t1.encounter_id, - t1.encounter_datetime as test_datetime, - t3.encounter_type, - t3.location_id, - t1.orders, - 0 as tests_ordered - from flat_orders t1 - join ', @queue_table,' t2 using(person_id) - join amrs.encounter t2 using (encounter_id) - left outer join flat_lab_obs t3 ON t1.person_id = t3.person_id AND t1.encounter_datetime = t3.test_datetime - where - t3.person_id is null - # and t1.encounter_datetime >= @last_update and - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; -*/ - - SET @dyn_sql = CONCAT( - 'insert into ',@write_table, - '(select - null, - person_id, - uuid, - encounter_id, - test_datetime, - encounter_type , - hiv_dna_pcr, - hiv_rapid_test, - hiv_viral_load, - cd4_count, - cd4_percent, - chest_xray, - hba1c, - rbs, - fbs, - ecg, - urea, - has_errors, - vl_error, - cd4_error, - hiv_dna_pcr_error, - rbc, - hemoglobin, - mcv, - mch, - mchc, - rdw, - plt, - wbc, - anc, - uric_acid, - creatinine, - na, - k, - cl, - total_bili, - direct_bili, - ggt, - ast, - alt, - total_protein, - albumin, - alk_phos, - ldh, - total_psa, - cea, - ca_19_9, - hbf, - hba, - hbs, - hba2, - serum_crag, - gene_expert_image, - dst_image, - tests_ordered - from flat_labs_and_imaging_0 t1 - join amrs.person t2 using (person_id) - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('delete t1.* from ',@queue_table,' t1 join temp_queue_table t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@queue_count / @cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select @queue_count as '# in queue', @cycle_length as 'Cycle Time (s)', ceil(@queue_count / @cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - SET @dyn_sql = CONCAT( - 'delete t1 - from ',@primary_table, ' t1 - join amrs.person t2 using (person_id) - where t2.voided=1;'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - select @end := now(); - insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - -END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v4_3.sql b/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v4_3.sql deleted file mode 100644 index 8292c94..0000000 --- a/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v4_3.sql +++ /dev/null @@ -1,503 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_labs_and_imaging_v4_3`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set session sort_buffer_size=512000000; - set session group_concat_max_len=100000; - set @start = now(); - set @primary_table := "flat_labs_and_imaging"; - select @table_version := 'flat_labs_and_imaging_v4.3'; - set @total_rows_written = 0; - set @query_type = query_type; - set @queue_number = queue_number; - set @queue_size = queue_size; - set @cycle_size = cycle_size; - - # 1030 = HIV DNA PCR - # 1040 = HIV Rapid test - # 856 = HIV VIRAL LOAD, QUANTITATIVE - # 5497 = CD4, BY FACS - # 730 = CD4%, BY FACS - # 21 = HEMOGLOBIN - # 653 = AST - # 790 = SERUM CREATININE - # 12 = X-RAY, CHEST, PRELIMINARY FINDINGS - # 6126 = hba1c - # 887 = rbs - # 6252 = fbs - # 1537 = ecg - # 857 = SERUM BLOOD UREA NITROGEN - # 1271 = TESTS ORDERED - # 9239 = LABORATORY TEST WITH EXCEPTION - # 9020 = LAB ERROR - # 8731 = SERUM M PROTEIN - # 8595 = SPEP - - /* - 679 RBC - 21 HGB - 851 MCV - 1018 MCH - 1017 MCHC - 1016 RDW - 729 PLT - 678 SERUM WBC - 1330 ANC - 6134 Uric acid - 790 Creatinine - 1132 Sodium - 1133 Potassium - 1134 Chloride - 655 Total Bili - 1297 Direct Bili - 6123 GGT - 653 AST - 654 ALT - 717 Total Protein - 848 Albumin - 785 ALP - 1014 LDH - 10249 Total PSA - 10250 CEA - 10251 (CA 19-9) - 9010 HBF - 9011 HBA - 9699 HbS - 9012 HBA2 - 9812 SERUM CRAG - 10304 GENEXPERT, IMAGE - 10313 DRUG SENSITIVITY TEST, IMAGE - */ - - set @concept_ids = '(1030, 1040, 856, 5497, 730, 21,653,790,12,6126,887,6252,1537,1271,9239,9020,857 - 679,21,851,1018,1017,1016,729,678,1330,6134,790,1132,1133,1134,655,1297,6123, - 653,654,717,848,785,1014,10249,10250,10251,9010,9011,9699,9012,9812,10304,10313,8731,8595 - )'; - -#set @queue_number = 1; -#set @queue_size = 10000; -#set @cycle_size = 1000; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - set @boundary = "!!"; - set @sep = " ## "; - - SET @dyn_sql = CONCAT('create table if not exists ', @primary_table, - ' (date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - encounter_id int, - test_datetime datetime, - encounter_type int, - hiv_dna_pcr int, - hiv_rapid_test int, - hiv_viral_load int, - cd4_count int, - cd4_percent decimal, - chest_xray int, - hba1c decimal, - rbs decimal, - fbs decimal, - ecg int, - urea decimal, - has_errors text, - vl_error boolean, - cd4_error boolean, - hiv_dna_pcr_error boolean, - rbc decimal, - hemoglobin decimal, - mcv int, - mch decimal, - mchc decimal, - rdw decimal, - plt int, - wbc decimal, - anc decimal, - uric_acid decimal, - creatinine decimal, - na decimal, - k decimal, - cl decimal, - total_bili decimal, - direct_bili decimal, - ggt decimal, - ast decimal, - alt decimal, - total_protein decimal, - albumin decimal, - alk_phos decimal, - ldh decimal, - total_psa decimal, - cea decimal, - ca_19_9 decimal, - hbf decimal, - hba decimal, - hbs decimal, - hba2 decimal, - serum_crag decimal, - gene_expert_image varchar(255), - dst_image varchar(255), - serum_m_protein int, - spep int, - tests_ordered varchar(1000), - primary key encounter_id (encounter_id), - index person_date (person_id, test_datetime), - index person_uuid (uuid) - )'); - - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - select CONCAT('BUILDING ',@primary_table,'..........................................'); - set @write_table = concat(@primary_table,"_temp_",@queue_number); -# set @build_queue = concat(@primary_table,'_build_queue'); - set @build_queue = concat('flat_labs_and_imaging_build_queue'); - set @queue_table = concat(@build_queue,'_',@queue_number); - - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(person_id int primary key) (select * from ',@build_queue,' limit ', @queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_obs_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from ',@build_queue,' t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - - end if; - - - if (@query_type="sync") then - select CONCAT('SYNCING ',@primary_table,'..........................................'); - set @write_table = @primary_table; - set @queue_table = concat(@primary_table,'_sync_queue'); - - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; -#set @last_update = "2018-01-26"; - create table if not exists flat_labs_and_imaging_sync_queue (person_id int primary key); - - - set @last_update = null; - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - - replace into flat_labs_and_imaging_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_labs_and_imaging_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - - replace into flat_labs_and_imaging_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_labs_and_imaging_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - end if; - - # delete all rows in primary table in the queue - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table,' t1 join ',@queue_table, ' t2 using (person_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - while @queue_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists temp_queue_table; - - SET @dyn_sql = CONCAT('create temporary table temp_queue_table like ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql= CONCAT('replace into temp_queue_table (select * from ',@queue_table,' limit ', @cycle_size,')'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - drop table if exists flat_labs_and_imaging_0; - - SET @dyn_sql= CONCAT( - 'create temporary table flat_labs_and_imaging_0(primary key (encounter_id), index encounter_id (encounter_id), index person_test (person_id,test_datetime)) - (select t1.person_id, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t3.orders, - if(obs regexp "!!1030=",cast(replace(replace((substring_index(substring(obs,locate("1030=",obs)),@sep,1)),"1030=",""),"!!","") as unsigned),null) as hiv_dna_pcr, - if(obs regexp "!!1040=",cast(replace(replace((substring_index(substring(obs,locate("1040=",obs)),@sep,1)),"1040=",""),"!!","") as unsigned),null) as hiv_rapid_test, - case - when obs regexp "!!856=" then cast(replace(replace((substring_index(substring(obs,locate("856=",obs)),@sep,1)),"856=",""),"!!","") as unsigned) - end as hiv_viral_load, - if(obs regexp "!!5497=",cast(replace(replace((substring_index(substring(obs,locate("5497=",obs)),@sep,1)),"5497=",""),"!!","") as unsigned),null) as cd4_count, - if(obs regexp "!!730=",cast(replace(replace((substring_index(substring(obs,locate("730=",obs)),@sep,1)),"730=",""),"!!","") as decimal(3,1)),null) as cd4_percent, - if(obs regexp "!!12=" and not obs regexp "!!12=1107",cast(replace(replace((substring_index(substring(obs,locate("12=",obs)),@sep,1)),"12=",""),"!!","") as unsigned),null) as chest_xray, - if(obs regexp "!!6126=",cast(replace(replace((substring_index(substring(obs,locate("6126=",obs)),@sep,1)),"6126=",""),"!!","") as decimal(4,1)),null) as hba1c, - if(obs regexp "!!887=",cast(replace(replace((substring_index(substring(obs,locate("887=",obs)),@sep,1)),"887=",""),"!!","") as decimal(4,1)),null) as rbs, - if(obs regexp "!!6252=",cast(replace(replace((substring_index(substring(obs,locate("6252=",obs)),@sep,1)),"6252=",""),"!!","") as decimal(4,1)),null) as fbs, - if(obs regexp "!!1537=",cast(replace(replace((substring_index(substring(obs,locate("1537=",obs)),@sep,1)),"1537=",""),"!!","") as unsigned),null) as ecg, - if(obs regexp "!!857=",cast(replace(replace((substring_index(substring(obs,locate("857=",obs)),@sep,1)),"857=",""),"!!","") as decimal(4,1)),null) as urea, - if(obs regexp "!!9239=",obs,null) as has_errors, - if(obs regexp "!!9239=856!!",1,null) as vl_error, - if(obs regexp "!!9239=5497!!",1,null) as cd4_error, - if(obs regexp "!!9239=1030",1,null) as hiv_dna_pcr_error, - if(obs regexp "!!679=",cast(getValues(obs,679) as decimal(6,2)),null) as rbc, - if(obs regexp "!!21=",cast(getValues(obs,21) as decimal(6,2)),null) as hemoglobin, - if(obs regexp "!!851=",cast(getValues(obs,851) as unsigned),null) as mcv, - if(obs regexp "!!1018=",cast(getValues(obs,1018) as decimal(6,2)),null) as mch, - if(obs regexp "!!1017=",cast(getValues(obs,1017) as decimal(6,2)),null) as mchc, - if(obs regexp "!!1016=",cast(getValues(obs,1016) as decimal(6,2)),null) as rdw, - if(obs regexp "!!729=",cast(getValues(obs,729) as unsigned),null) as plt, - if(obs regexp "!!678=",cast(getValues(obs,678) as decimal(6,2)),null) as wbc, - if(obs regexp "!!1330=",cast(getValues(obs,1330) as decimal(6,2)),null) as anc, - if(obs regexp "!!6134=",cast(getValues(obs,6134) as decimal(6,2)),null) as uric_acid, - if(obs regexp "!!790=",cast(getValues(obs,790) as decimal(6,2)),null) as creatinine, - if(obs regexp "!!1132=",cast(getValues(obs,1132) as decimal(6,2)),null) as na, - if(obs regexp "!!1133=",cast(getValues(obs,1133) as decimal(6,2)),null) as k, - if(obs regexp "!!1134=",cast(getValues(obs,1134) as decimal(6,2)),null) as cl, - if(obs regexp "!!655=",cast(getValues(obs,655) as decimal(6,2)),null) as total_bili, - if(obs regexp "!!1297=",cast(getValues(obs,1297) as decimal(6,2)),null) as direct_bili, - if(obs regexp "!!6123=",cast(getValues(obs,6123) as decimal(6,2)),null) as ggt, - if(obs regexp "!!653=",cast(getValues(obs,653) as decimal(6,2)),null) as ast, - if(obs regexp "!!654=",cast(getValues(obs,654) as decimal(6,2)),null) as alt, - if(obs regexp "!!717=",cast(getValues(obs,717) as decimal(6,2)),null) as total_protein, - if(obs regexp "!!848=",cast(getValues(obs,848) as decimal(6,2)),null) as albumin, - if(obs regexp "!!785=",cast(getValues(obs,785) as decimal(6,2)),null) as alk_phos, - if(obs regexp "!!1014=",cast(getValues(obs,1014) as decimal(6,2)),null) as ldh, - if(obs regexp "!!10249=",cast(getValues(obs,10249) as decimal(6,2)),null) as total_psa, - if(obs regexp "!!10250=",cast(getValues(obs,10250) as decimal(6,2)),null) as cea, - if(obs regexp "!!10251=",cast(getValues(obs,10251) as decimal(6,2)),null) as ca_19_9, - if(obs regexp "!!9010=",cast(getValues(obs,9010) as decimal(6,2)),null) as hbf, - if(obs regexp "!!9011=",cast(getValues(obs,9011) as decimal(6,2)),null) as hba, - if(obs regexp "!!9699=",cast(getValues(obs,9699) as decimal(6,2)),null) as hbs, - if(obs regexp "!!9012=",cast(getValues(obs,9012) as decimal(6,2)),null) as hba2, - if(obs regexp "!!9812=",cast(getValues(obs,9812) as decimal(6,2)),null) as serum_crag, - if(obs regexp "!!10304=",getValues(obs,10304),null) as gene_expert_image, - if(obs regexp "!!10313=",getValues(obs,10313),null) as dst_image, - if(obs regexp "!!8595=",getValues(obs,8595),null) as serum_m_protein, - if(obs regexp "!!8731=",getValues(obs,8731),null) as spep, - CONCAT( - case - when obs regexp "!!1271=" then getValues(obs,1271) - # replace(replace((substring_index(substring(obs,locate("!!1271=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1271=", "") ) ) / LENGTH("!!1271=") ))),"!!1271=",""),"!!","") - else "" - end, - ifnull(orders,"") - ) as tests_ordered - from flat_lab_obs t1 - join temp_queue_table t2 using(person_id) - left outer join flat_orders t3 using(encounter_id) - )' - ); - - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - -/* -# set @last_update = '2016-03-01'; - SET @dyn_sql= CONCAT( - 'insert into flat_labs_and_imaging_0 (person_id, encounter_id, test_datetime, encounter_type, location_id, orders,tests_ordered) - ( select t1.person_id, - t1.encounter_id, - t1.encounter_datetime as test_datetime, - t3.encounter_type, - t3.location_id, - t1.orders, - 0 as tests_ordered - from flat_orders t1 - join ', @queue_table,' t2 using(person_id) - join amrs.encounter t2 using (encounter_id) - left outer join flat_lab_obs t3 ON t1.person_id = t3.person_id AND t1.encounter_datetime = t3.test_datetime - where - t3.person_id is null - # and t1.encounter_datetime >= @last_update and - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; -*/ - - SET @dyn_sql = CONCAT( - 'insert into ',@write_table, - '(select - null, - person_id, - uuid, - encounter_id, - test_datetime, - encounter_type , - hiv_dna_pcr, - hiv_rapid_test, - hiv_viral_load, - cd4_count, - cd4_percent, - chest_xray, - hba1c, - rbs, - fbs, - ecg, - urea, - has_errors, - vl_error, - cd4_error, - hiv_dna_pcr_error, - rbc, - hemoglobin, - mcv, - mch, - mchc, - rdw, - plt, - wbc, - anc, - uric_acid, - creatinine, - na, - k, - cl, - total_bili, - direct_bili, - ggt, - ast, - alt, - total_protein, - albumin, - alk_phos, - ldh, - total_psa, - cea, - ca_19_9, - hbf, - hba, - hbs, - hba2, - serum_crag, - gene_expert_image, - dst_image, - serum_m_protein, - spep, - tests_ordered - from flat_labs_and_imaging_0 t1 - join amrs.person t2 using (person_id) - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('delete t1.* from ',@queue_table,' t1 join temp_queue_table t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@queue_count / @cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select @queue_count as '# in queue', @cycle_length as 'Cycle Time (s)', ceil(@queue_count / @cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - SET @dyn_sql = CONCAT( - 'delete t1 - from ',@primary_table, ' t1 - join amrs.person t2 using (person_id) - where t2.voided=1;'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - select @end := now(); - if (@query_type="sync") then - insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - end if; - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - -END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v4_4.sql b/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v4_4.sql deleted file mode 100644 index 6e07be6..0000000 --- a/etl-scripts/stored-procedures/generate_flat_labs_and_imaging_v4_4.sql +++ /dev/null @@ -1,581 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_labs_and_imaging_v4_4`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set session sort_buffer_size=512000000; - set session group_concat_max_len=100000; - set @start = now(); - set @primary_table := "flat_labs_and_imaging"; - SELECT @table_version:='flat_labs_and_imaging_v4.4'; - set @total_rows_written = 0; - set @query_type = query_type; - set @queue_number = queue_number; - set @queue_size = queue_size; - set @cycle_size = cycle_size; - - # 1030 = HIV DNA PCR - # 1040 = HIV Rapid test - # 856 = HIV VIRAL LOAD, QUANTITATIVE - # 5497 = CD4, BY FACS - # 730 = CD4%, BY FACS - # 21 = HEMOGLOBIN - # 653 = AST - # 790 = SERUM CREATININE - # 12 = X-RAY, CHEST, PRELIMINARY FINDINGS - # 6126 = hba1c - # 887 = rbs - # 6252 = fbs - # 1537 = ecg - # 857 = SERUM BLOOD UREA NITROGEN - # 1271 = TESTS ORDERED - # 9239 = LABORATORY TEST WITH EXCEPTION - # 9020 = LAB ERROR - # 8731 = SERUM M PROTEIN - # 8595 = SPEP - - # 1984 = PRESENCE OF PUS CELLS URINE - # 2339 = PRESENCE OF PROTEIN URINE - # 6337 = PRESENCE OF LEUCOCYTES - # 7276 = PRESENCE OF KETONE - # 2340 = PRESENCE OF SUGAR URINE - # 9307 = PRESENCE OF NITRITES - # 1327 = RETICULOCYTES - # 8732 = SERUM ALPHA-1 GLOBULIN - # 8733 = SERUM ALPHA-2 GLOBULIN - # 8734 = SERUM BETA GLOBULIN - # 8735 = SERUM GAMMA GLOBULIN - # 10195 = KAPPA LIGHT CHAINS - # 10196 = LAMBDA LIGHT CHAINS - # 10197 = RATIO OF KAPPA LAMBDA - - /* - 679 RBC - 21 HGB - 851 MCV - 1018 MCH - 1017 MCHC - 1016 RDW - 729 PLT - 678 SERUM WBC - 1330 ANC - 790 Creatinine - 1132 Sodium - 1133 Potassium - 1134 Chloride - 655 Total Bili - 1297 Direct Bili - 6123 GGT - 653 AST - 654 ALT - 717 Total Protein - 848 Albumin - 785 ALP - 1014 LDH - 10249 Total PSA - 10250 CEA - 10251 (CA 19-9) - 9010 HBF - 9011 HBA - 9699 HbS - 9012 HBA2 - 9812 SERUM CRAG - 10304 GENEXPERT, IMAGE - 10313 DRUG SENSITIVITY TEST, IMAGE - */ - - set @concept_ids = '(1030, 1040, 856, 5497, 730, 21,653,790,12,6126,887,6252,1537,1271,9239,9020,857 - 679,21,851,1018,1017,1016,729,678,1330,790,1132,1133,1134,655,1297,6123, - 653,654,717,848,785,1014,10249,10250,10251,9010,9011,9699,9012,9812,10304,10313,8731,8595 - 1984,2339,6337,7276,2340,9307,1327,8732,8733,8734,8735,10195,10196,10197 - )'; - -#set @queue_number = 1; -#set @queue_size = 10000; -#set @cycle_size = 1000; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - set @boundary = "!!"; - set @sep = " ## "; - - SET @dyn_sql = CONCAT('create table if not exists ', @primary_table, - ' (date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - encounter_id int, - test_datetime datetime, - encounter_type int, - hiv_dna_pcr int, - hiv_rapid_test int, - hiv_viral_load int, - cd4_count int, - cd4_percent decimal, - chest_xray int, - hba1c decimal, - rbs decimal, - fbs decimal, - ecg int, - urea decimal, - has_errors text, - vl_error boolean, - cd4_error boolean, - hiv_dna_pcr_error boolean, - rbc decimal, - hemoglobin decimal, - mcv int, - mch decimal, - mchc decimal, - rdw decimal, - plt int, - wbc decimal, - anc decimal, - creatinine decimal, - na decimal, - k decimal, - cl decimal, - total_bili decimal, - direct_bili decimal, - ggt decimal, - ast decimal, - alt decimal, - total_protein decimal, - albumin decimal, - alk_phos decimal, - ldh decimal, - total_psa decimal, - cea decimal, - ca_19_9 decimal, - hbf decimal, - hba decimal, - hbs decimal, - hba2 decimal, - serum_crag decimal, - gene_expert_image varchar(255), - dst_image varchar(255), - serum_m_protein int, - spep int, - pus_c_urine int, - protein_urine int, - leuc int, - ketone int, - sugar_urine int, - nitrites int, - retic decimal, - a_1_glob decimal, - a_2_glob decimal, - beta_glob decimal, - gamma_glob decimal, - kappa_l_c decimal, - lambda_l_c decimal, - ratio_l_c decimal, - tests_ordered varchar(1000), - primary key encounter_id (encounter_id), - index person_date (person_id, test_datetime), - index person_uuid (uuid) - )'); - - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - select CONCAT('BUILDING ',@primary_table,'..........................................'); - set @write_table = concat(@primary_table,"_temp_",@queue_number); -# set @build_queue = concat(@primary_table,'_build_queue'); - set @build_queue = concat('flat_labs_and_imaging_build_queue'); - set @queue_table = concat(@build_queue,'_',@queue_number); - - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(person_id int primary key) (select * from ',@build_queue,' limit ', @queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_obs_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from ',@build_queue,' t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - - end if; - - - if (@query_type="sync") then - select CONCAT('SYNCING ',@primary_table,'..........................................'); - set @write_table = @primary_table; - set @queue_table = concat(@primary_table,'_sync_queue'); - - SELECT - MAX(date_updated) -INTO @last_update FROM - etl.flat_log -WHERE - table_name = @table_version; -CREATE TABLE IF NOT EXISTS flat_labs_and_imaging_sync_queue ( - person_id INT PRIMARY KEY -); - - - set @last_update = null; - SELECT - MAX(date_updated) -INTO @last_update FROM - etl.flat_log -WHERE - table_name = @table_version; - - replace into flat_labs_and_imaging_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_labs_and_imaging_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - - replace into flat_labs_and_imaging_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_labs_and_imaging_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - end if; - - # delete all rows in primary table in the queue - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table,' t1 join ',@queue_table, ' t2 using (person_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - while @queue_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists temp_queue_table; - - SET @dyn_sql = CONCAT('create temporary table temp_queue_table like ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql= CONCAT('replace into temp_queue_table (select * from ',@queue_table,' limit ', @cycle_size,')'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - drop table if exists flat_labs_and_imaging_0; - - SET @dyn_sql= CONCAT( - 'create temporary table flat_labs_and_imaging_0(primary key (encounter_id), index encounter_id (encounter_id), index person_test (person_id,test_datetime)) - (select t1.person_id, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t3.orders, - if(obs regexp "!!1030=",cast(replace(replace((substring_index(substring(obs,locate("1030=",obs)),@sep,1)),"1030=",""),"!!","") as unsigned),null) as hiv_dna_pcr, - if(obs regexp "!!1040=",cast(replace(replace((substring_index(substring(obs,locate("1040=",obs)),@sep,1)),"1040=",""),"!!","") as unsigned),null) as hiv_rapid_test, - case - when obs regexp "!!856=" then cast(replace(replace((substring_index(substring(obs,locate("856=",obs)),@sep,1)),"856=",""),"!!","") as unsigned) - end as hiv_viral_load, - if(obs regexp "!!5497=",cast(replace(replace((substring_index(substring(obs,locate("5497=",obs)),@sep,1)),"5497=",""),"!!","") as unsigned),null) as cd4_count, - if(obs regexp "!!730=",cast(replace(replace((substring_index(substring(obs,locate("730=",obs)),@sep,1)),"730=",""),"!!","") as decimal(3,1)),null) as cd4_percent, - if(obs regexp "!!12=" and not obs regexp "!!12=1107",cast(replace(replace((substring_index(substring(obs,locate("12=",obs)),@sep,1)),"12=",""),"!!","") as unsigned),null) as chest_xray, - if(obs regexp "!!6126=",cast(replace(replace((substring_index(substring(obs,locate("6126=",obs)),@sep,1)),"6126=",""),"!!","") as decimal(4,1)),null) as hba1c, - if(obs regexp "!!887=",cast(replace(replace((substring_index(substring(obs,locate("887=",obs)),@sep,1)),"887=",""),"!!","") as decimal(4,1)),null) as rbs, - if(obs regexp "!!6252=",cast(replace(replace((substring_index(substring(obs,locate("6252=",obs)),@sep,1)),"6252=",""),"!!","") as decimal(4,1)),null) as fbs, - if(obs regexp "!!1537=",cast(replace(replace((substring_index(substring(obs,locate("1537=",obs)),@sep,1)),"1537=",""),"!!","") as unsigned),null) as ecg, - if(obs regexp "!!857=",cast(replace(replace((substring_index(substring(obs,locate("857=",obs)),@sep,1)),"857=",""),"!!","") as decimal(4,1)),null) as urea, - if(obs regexp "!!9239=",obs,null) as has_errors, - if(obs regexp "!!9239=856!!",1,null) as vl_error, - if(obs regexp "!!9239=5497!!",1,null) as cd4_error, - if(obs regexp "!!9239=1030",1,null) as hiv_dna_pcr_error, - if(obs regexp "!!679=",cast(getValues(obs,679) as decimal(6,2)),null) as rbc, - if(obs regexp "!!21=",cast(getValues(obs,21) as decimal(6,2)),null) as hemoglobin, - if(obs regexp "!!851=",cast(getValues(obs,851) as unsigned),null) as mcv, - if(obs regexp "!!1018=",cast(getValues(obs,1018) as decimal(6,2)),null) as mch, - if(obs regexp "!!1017=",cast(getValues(obs,1017) as decimal(6,2)),null) as mchc, - if(obs regexp "!!1016=",cast(getValues(obs,1016) as decimal(6,2)),null) as rdw, - if(obs regexp "!!729=",cast(getValues(obs,729) as unsigned),null) as plt, - if(obs regexp "!!678=",cast(getValues(obs,678) as decimal(6,2)),null) as wbc, - if(obs regexp "!!1330=",cast(getValues(obs,1330) as decimal(6,2)),null) as anc, - if(obs regexp "!!790=",cast(getValues(obs,790) as decimal(6,2)),null) as creatinine, - if(obs regexp "!!1132=",cast(getValues(obs,1132) as decimal(6,2)),null) as na, - if(obs regexp "!!1133=",cast(getValues(obs,1133) as decimal(6,2)),null) as k, - if(obs regexp "!!1134=",cast(getValues(obs,1134) as decimal(6,2)),null) as cl, - if(obs regexp "!!655=",cast(getValues(obs,655) as decimal(6,2)),null) as total_bili, - if(obs regexp "!!1297=",cast(getValues(obs,1297) as decimal(6,2)),null) as direct_bili, - if(obs regexp "!!6123=",cast(getValues(obs,6123) as decimal(6,2)),null) as ggt, - if(obs regexp "!!653=",cast(getValues(obs,653) as decimal(6,2)),null) as ast, - if(obs regexp "!!654=",cast(getValues(obs,654) as decimal(6,2)),null) as alt, - if(obs regexp "!!717=",cast(getValues(obs,717) as decimal(6,2)),null) as total_protein, - if(obs regexp "!!848=",cast(getValues(obs,848) as decimal(6,2)),null) as albumin, - if(obs regexp "!!785=",cast(getValues(obs,785) as decimal(6,2)),null) as alk_phos, - if(obs regexp "!!1014=",cast(getValues(obs,1014) as decimal(6,2)),null) as ldh, - if(obs regexp "!!10249=",cast(getValues(obs,10249) as decimal(6,2)),null) as total_psa, - if(obs regexp "!!10250=",cast(getValues(obs,10250) as decimal(6,2)),null) as cea, - if(obs regexp "!!10251=",cast(getValues(obs,10251) as decimal(6,2)),null) as ca_19_9, - if(obs regexp "!!9010=",cast(getValues(obs,9010) as decimal(6,2)),null) as hbf, - if(obs regexp "!!9011=",cast(getValues(obs,9011) as decimal(6,2)),null) as hba, - if(obs regexp "!!9699=",cast(getValues(obs,9699) as decimal(6,2)),null) as hbs, - if(obs regexp "!!9012=",cast(getValues(obs,9012) as decimal(6,2)),null) as hba2, - if(obs regexp "!!9812=",cast(getValues(obs,9812) as decimal(6,2)),null) as serum_crag, - if(obs regexp "!!10304=",getValues(obs,10304),null) as gene_expert_image, - if(obs regexp "!!10313=",getValues(obs,10313),null) as dst_image, - if(obs regexp "!!8595=",getValues(obs,8595),null) as serum_m_protein, - if(obs regexp "!!8731=",getValues(obs,8731),null) as spep, - if(obs regexp "!!1984=",getValues(obs,1984),null) as pus_c_urine, - if(obs regexp "!!2339=",cast(replace(replace((substring_index(substring(obs,locate("2339=",obs)),@sep,1)),"2339=",""),"!!","") as unsigned),null) as protein_urine, - if(obs regexp "!!6337=",getValues(obs,6337),null) as leuc, - if(obs regexp "!!7276=",getValues(obs,7276),null) as ketone, - if(obs regexp "!!2340=",getValues(obs,2340),null) as sugar_urine, - if(obs regexp "!!9307=",getValues(obs,9307),null) as nitrites, - if(obs regexp "!!1327=",cast(getValues(obs,1327) as decimal(5,2)),null) as retic, - if(obs regexp "!!8732=",cast(getValues(obs,8732) as decimal(5,2)),null) as a_1_glob, - if(obs regexp "!!8733=",cast(getValues(obs,8733) as decimal(5,2)),null) as a_2_glob, - if(obs regexp "!!8734=",cast(getValues(obs,8734) as decimal(5,2)),null) as beta_glob, - if(obs regexp "!!8735=",cast(getValues(obs,8735) as decimal(5,2)),null) as gamma_glob, - if(obs regexp "!!10195=",cast(getValues(obs,10195) as decimal(5,2)),null) as kappa_l_c, - if(obs regexp "!!10196=",cast(getValues(obs,10196) as decimal(5,2)),null) as lambda_l_c, - if(obs regexp "!!10197=",cast(getValues(obs,10197) as decimal(5,2)),null) as ratio_l_c, - CONCAT( - case - when obs regexp "!!1271=" then getValues(obs,1271) - # replace(replace((substring_index(substring(obs,locate("!!1271=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1271=", "") ) ) / LENGTH("!!1271=") ))),"!!1271=",""),"!!","") - else "" - end, - ifnull(orders,"") - ) as tests_ordered - from flat_lab_obs t1 - join temp_queue_table t2 using(person_id) - left outer join flat_orders t3 using(encounter_id) - )' - ); - - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - -/* -# set @last_update = '2016-03-01'; - SET @dyn_sql= CONCAT( - 'insert into flat_labs_and_imaging_0 (person_id, encounter_id, test_datetime, encounter_type, location_id, orders,tests_ordered) - ( select t1.person_id, - t1.encounter_id, - t1.encounter_datetime as test_datetime, - t3.encounter_type, - t3.location_id, - t1.orders, - 0 as tests_ordered - from flat_orders t1 - join ', @queue_table,' t2 using(person_id) - join amrs.encounter t2 using (encounter_id) - left outer join flat_lab_obs t3 ON t1.person_id = t3.person_id AND t1.encounter_datetime = t3.test_datetime - where - t3.person_id is null - # and t1.encounter_datetime >= @last_update and - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; -*/ - - SET @dyn_sql = CONCAT( - 'insert into ',@write_table, - '(select - null, - person_id, - uuid, - encounter_id, - test_datetime, - encounter_type , - hiv_dna_pcr, - hiv_rapid_test, - hiv_viral_load, - cd4_count, - cd4_percent, - chest_xray, - hba1c, - rbs, - fbs, - ecg, - urea, - has_errors, - vl_error, - cd4_error, - hiv_dna_pcr_error, - rbc, - hemoglobin, - mcv, - mch, - mchc, - rdw, - plt, - wbc, - anc, - creatinine, - na, - k, - cl, - total_bili, - direct_bili, - ggt, - ast, - alt, - total_protein, - albumin, - alk_phos, - ldh, - total_psa, - cea, - ca_19_9, - hbf, - hba, - hbs, - hba2, - serum_crag, - gene_expert_image, - dst_image, - serum_m_protein, - spep, - pus_c_urine, - protein_urine, - leuc, - ketone, - sugar_urine, - nitrites, - retic, - a_1_glob, - a_2_glob, - beta_glob, - gamma_glob, - kappa_l_c, - lambda_l_c, - ratio_l_c, - tests_ordered - from flat_labs_and_imaging_0 t1 - join amrs.person t2 using (person_id) - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('delete t1.* from ',@queue_table,' t1 join temp_queue_table t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@queue_count / @cycle_size) / 60); -SELECT - @queue_count AS '# in queue', - @cycle_length AS 'Cycle Time (s)', - CEIL(@queue_count / @cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - - end while; - - SET @dyn_sql = CONCAT( - 'delete t1 - from ',@primary_table, ' t1 - join amrs.person t2 using (person_id) - where t2.voided=1;'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - SELECT - CONCAT(@start_write, - ' : Writing ', - @total_rows_to_write, - ' to ', - @primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); -SELECT - CONCAT(@finish_write, - ' : Completed writing rows. Time to write to primary table: ', - @time_to_write, - ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - SELECT @end:=NOW(); - insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - SELECT - CONCAT(@table_version, - ' : Time to complete: ', - TIMESTAMPDIFF(MINUTE, @start, @end), - ' minutes'); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_lung_cancer_screening.sql b/etl-scripts/stored-procedures/generate_flat_lung_cancer_screening.sql index a3a49cf..91461c9 100644 --- a/etl-scripts/stored-procedures/generate_flat_lung_cancer_screening.sql +++ b/etl-scripts/stored-procedures/generate_flat_lung_cancer_screening.sql @@ -1,43 +1,37 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_lung_cancer_screening_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_lung_cancer_screening`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN - set @primary_table := "flat_lung_cancer_screening"; - set @query_type = query_type; - - set @total_rows_written = 0; - - set @encounter_types = "(177,185)"; - set @clinical_encounter_types = "(-1)"; - set @non_clinical_encounter_types = "(-1)"; - set @other_encounter_types = "(-1)"; - - set @start = now(); - set @table_version = "flat_lung_cancer_screening_v1.0"; - - set session sort_buffer_size=512000000; + set @primary_table := "flat_lung_cancer_screening"; + set @query_type = query_type; + + set @total_rows_written = 0; - set @sep = " ## "; - set @boundary = "!!"; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); + set @encounter_types = "(177,185)"; + set @clinical_encounter_types = "(-1)"; + set @non_clinical_encounter_types = "(-1)"; + set @other_encounter_types = "(-1)"; + + set @start = now(); + set @table_version = "flat_lung_cancer_screening_v1.0"; - #delete from etl.flat_log where table_name like "%flat_lung_cancer_screening%"; - #drop table etl.flat_lung_cancer_screening; + set session sort_buffer_size = 512000000; + set @sep = " ## "; + set @boundary = "!!"; + set @last_date_created = (select max(max_date_created) from etl.flat_obs); - #drop table if exists flat_lung_cancer_screening; - create table if not exists flat_lung_cancer_screening ( + create table if not exists flat_lung_cancer_screening ( date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, + person_id int, encounter_id int, encounter_type int, encounter_datetime datetime, visit_id int, location_id int, location_uuid varchar (100), - #location_name char (100), gender char (100), age int, - cur_visit_type INT, + cur_visit_type INT, referral_from INT, chief_complains INT, complain_duration INT, @@ -69,11 +63,11 @@ BEGIN rtc_date DATETIME, imaging_results INT, ct_findings INT, - Imaging_results_description varchar(500), + Imaging_results_description varchar(500), biospy_procedure_done INT, - biopsy_workup_date DATETIME, + biopsy_workup_date DATETIME, biospy_results INT, - other_condition varchar(500), + other_condition varchar(500), type_of_malignancy INT, other_malignancy VARCHAR(1000), lung_cancer_type INT, @@ -82,7 +76,6 @@ BEGIN diagnosis_date DATETIME, referral_ordered INT, return_date DATETIME, - prev_encounter_datetime_lung_cancer_screening datetime, next_encounter_datetime_lung_cancer_screening datetime, prev_encounter_type_lung_cancer_screening mediumint, @@ -93,7 +86,6 @@ BEGIN next_clinical_location_id_lung_cancer_screening mediumint, prev_clinical_rtc_date_lung_cancer_screening datetime, next_clinical_rtc_date_lung_cancer_screening datetime, - primary key encounter_id (encounter_id), index person_date (person_id, encounter_datetime), index location_enc_date (location_uuid,encounter_datetime), @@ -101,7 +93,6 @@ BEGIN index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_lung_cancer_screening), index encounter_type (encounter_type), index date_created (date_created) - ); diff --git a/etl-scripts/stored-procedures/generate_flat_lung_cancer_screening_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_lung_cancer_screening_v1_0.sql deleted file mode 100644 index 39e1dd6..0000000 --- a/etl-scripts/stored-procedures/generate_flat_lung_cancer_screening_v1_0.sql +++ /dev/null @@ -1,974 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_lung_cancer_screening_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_lung_cancer_screening"; - set @query_type = query_type; - - set @total_rows_written = 0; - - set @encounter_types = "(177,185)"; - set @clinical_encounter_types = "(-1)"; - set @non_clinical_encounter_types = "(-1)"; - set @other_encounter_types = "(-1)"; - - set @start = now(); - set @table_version = "flat_lung_cancer_screening_v1.0"; - - set session sort_buffer_size = 512000000; - - set @sep = " ## "; - set @boundary = "!!"; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - create table if not exists flat_lung_cancer_screening ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - encounter_id int, - encounter_type int, - encounter_datetime datetime, - visit_id int, - location_id int, - location_uuid varchar (100), - gender char (100), - age int, - cur_visit_type INT, - referral_from INT, - chief_complains INT, - complain_duration INT, - number_of_days INT, - number_of_weeks INT, - number_of_months INT, - number_of_years INT, - ever_smoked_cigarettes INT, - number_of_sticks INT, - cigarette_duration INT, - tobacco_use INT, - tobacco_duration INT, - main_occupation INT, - chemical_exposure INT, - asbestos_exposure INT, - other_cancer_in_family INT, - hiv_status INT, - previous_tb_treatment INT, - treatment_duration INT, - number_of_tb_treatment INT, - previous_test_ordered INT, - sputum_aafbs_results INT, - gene_expert_results INT, - x_ray_results INT, - procedure_ordered INT, - imaging_test_ordered INT, - radiology_test VARCHAR(1000), - follow_up_care_plan INT, - rtc_date DATETIME, - imaging_results INT, - ct_findings INT, - Imaging_results_description varchar(500), - biospy_procedure_done INT, - biopsy_workup_date DATETIME, - biospy_results INT, - other_condition varchar(500), - type_of_malignancy INT, - other_malignancy VARCHAR(1000), - lung_cancer_type INT, - non_small_cancer INT, - repository_disease INT, - diagnosis_date DATETIME, - referral_ordered INT, - return_date DATETIME, - prev_encounter_datetime_lung_cancer_screening datetime, - next_encounter_datetime_lung_cancer_screening datetime, - prev_encounter_type_lung_cancer_screening mediumint, - next_encounter_type_lung_cancer_screening mediumint, - prev_clinical_datetime_lung_cancer_screening datetime, - next_clinical_datetime_lung_cancer_screening datetime, - prev_clinical_location_id_lung_cancer_screening mediumint, - next_clinical_location_id_lung_cancer_screening mediumint, - prev_clinical_rtc_date_lung_cancer_screening datetime, - next_clinical_rtc_date_lung_cancer_screening datetime, - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_lung_cancer_screening), - index encounter_type (encounter_type), - index date_created (date_created) - ); - - - - if(@query_type="build") then - select 'BUILDING..........................................'; - -#set @write_table = concat("flat_lung_cancer_screening_temp_",1); -#set @queue_table = concat("flat_lung_cancer_screening_build_queue_",1); - - set @write_table = concat("flat_lung_cancer_screening_temp_",queue_number); - set @queue_table = concat("flat_lung_cancer_screening_build_queue_",queue_number); - - -#drop table if exists flat_lung_cancer_screening_temp_1; - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_lung_cancer_screening_build_queue limit ', queue_size, ');'); -#SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_lung_cancer_screening_build_queue limit 500);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_lung_cancer_screening_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from flat_lung_cancer_screening_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_lung_cancer_screening"; - set @queue_table = "flat_lung_cancer_screening_sync_queue"; - create table if not exists flat_lung_cancer_screening_sync_queue (person_id int primary key); - - set @last_update = null; - - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - -#select max(date_created) into @last_update from etl.flat_log where table_name like "%lung_cancer_screening%"; - -#select @last_update; -select "Finding patients in amrs.encounters..."; - - replace into flat_lung_cancer_screening_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - -select "Finding patients in flat_obs..."; - - replace into flat_lung_cancer_screening_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - -select "Finding patients in flat_lab_obs..."; - replace into flat_lung_cancer_screening_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - -select "Finding patients in flat_orders..."; - - replace into flat_lung_cancer_screening_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_lung_cancer_screening_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_lung_cancer_screening_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - # Remove test patients - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - select @person_ids_count as 'num patients to update'; - - - #delete t1 from flat_lung_cancer_screening t1 join @queue_table t2 using (person_id); - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists flat_lung_cancer_screening_build_queue__0; - - #create temporary table flat_lung_cancer_screening_build_queue__0 (select * from flat_lung_cancer_screening_build_queue_2 limit 5000); #TODO - change this when data_fetch_size changes - -#SET @dyn_sql=CONCAT('create temporary table flat_lung_cancer_screening_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit 100);'); - SET @dyn_sql=CONCAT('create temporary table flat_lung_cancer_screening_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - drop temporary table if exists flat_lung_cancer_screening_0a; - SET @dyn_sql = CONCAT( - 'create temporary table flat_lung_cancer_screening_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - case - when t1.encounter_type in ',@clinical_encounter_types,' then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in ',@non_clinical_encounter_types,' then 20 - when t1.encounter_type in ',@clinical_encounter_types,' then 10 - when t1.encounter_type in', @other_encounter_types, ' then 5 - else 1 - end as encounter_type_sort_index, - t2.orders - from etl.flat_obs t1 - join flat_lung_cancer_screening_build_queue__0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_id) - where t1.encounter_type in ',@encounter_types,');'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - - insert into flat_lung_cancer_screening_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, #t1.location_id, - t1.obs, - null, #obs_datetimes - # in any visit, there many be multiple encounters. for this dataset, we want to include only clinical encounters (e.g. not lab or triage visit) - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_lung_cancer_screening_build_queue__0 t0 using (person_id) - ); - - - drop temporary table if exists flat_lung_cancer_screening_0; - create temporary table flat_lung_cancer_screening_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_lung_cancer_screening_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @cur_visit_type = null; - set @referral_from = null; - set @chief_complains = null; - set @complain_duration = null; - set @number_of_days = null; - set @number_of_weeks = null; - set @number_of_months = null; - set @number_of_years = null; - set @ever_smoked_cigarettes = null; - set @number_of_sticks = null; - set @cigarette_duration = null; - set @tobacco_use = null; - set @tobacco_duration = null; - set @main_occupation = null; - set @chemical_exposure = null; - set @asbestos_exposure = null; - set @other_cancer_in_family = null; - set @hiv_status = null; - set @previous_tb_treatment = null; - set @treatment_duration = null; - set @number_of_tb_treatment= null; - set @previous_test_ordered = null; - set @sputum_aafbs_results = null; - set @gene_expert_results = null; - set @x_ray_results = null; - set @procedure_ordered= null; - set @imaging_test_ordered = null; - set @radiology_test = null; - set @follow_up_care_plan = null; - set @rtc_date = null; - set @imaging_results= null; - set @ct_findings = null; - set @Imaging_results_description = null; - set @biospy_procedure_done = null; - set @biopsy_workup_date = null; - set @biospy_results = null; - set @other_condition = null; - set @type_of_malignancy = null; - set @other_malignancy = null; - set @lung_cancer_type = null; - set @non_small_cancer = null; - set @repository_disease = null; - set @diagnosis_date = null; - set @referral_ordered = null; - set @return_date = null; - - - drop temporary table if exists flat_lung_cancer_screening_1; - create temporary table flat_lung_cancer_screening_1 #(index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - t1.encounter_id, - t1.encounter_type, - t1.encounter_datetime, - t1.visit_id, - #t4.name as location_name, - t1.location_id, - t1.is_clinical_encounter, - p.gender, - p.death_date, - case - when timestampdiff(year,birthdate,curdate()) > 0 then round(timestampdiff(year,birthdate,curdate()),0) - else round(timestampdiff(month,birthdate,curdate())/12,2) - end as age, - case - when obs regexp "!!1839=7037!!" then @cur_visit_type := 1 - when obs regexp "!!1839=7875!!" then @cur_visit_type := 2 - else @cur_visit_type := null - end as cur_visit_type, - case - - when obs regexp "!!6749=6572!!" then @referral_from := 1 - - when obs regexp "!!6749=8161!!" then @referral_from := 2 - - when obs regexp "!!6749=2242!!" then @referral_from := 3 - - when obs regexp "!!6749=978!!" then @referral_from := 4 - - when obs regexp "!!6749=5487!!" then @referral_from := 5 - - when obs regexp "!!6749=1275!!" then @referral_from := 6 - - when obs regexp "!!6749=5622!!" then @referral_from := 7 - - else @referral_from = null - - end as referral_from, - - case - when obs regexp "!!5219=107!!" then @chief_complains := 1 - - when obs regexp "!!5219=5960!!" then @chief_complains := 2 - - when obs regexp "!!5219=6786!!" then @chief_complains := 3 - - when obs regexp "!!5219=136!!" then @chief_complains := 4 - - when obs regexp "!!5219=7225!!" then @chief_complains := 5 - - when obs regexp "!!5219=456!!" then @chief_complains := 6 - - when obs regexp "!!5219=832!!" then @chief_complains := 7 - - when obs regexp "!!5219=5622!!" then @chief_complains := 8 - - else @chief_complains = null - - end as chief_complains, - - case - when obs regexp "!!8777=1072!!" then @complain_duration := 1 - when obs regexp "!!8777=1073!!" then @complain_duration := 2 - when obs regexp "!!8777=1074!!" then @complain_duration := 3 - when obs regexp "!!8777=8787!!" then @complain_duration := 4 - else @complain_duration = null - end as complain_duration, - case - when obs regexp "!!1892=" then @number_of_days := GetValues(obs,1892) - else @number_of_days = null - end as number_of_days, - case - when obs regexp "!!1893=" then @number_of_weeks := GetValues(obs,1893) - else @number_of_weeks = null - end as number_of_weeks, - case - when obs regexp "!!1894=" then @number_of_months := GetValues(obs,1894) - else @number_of_months = null - end as number_of_months, - case - when obs regexp "!!7953=" then @number_of_years := GetValues(obs,7953) - else @number_of_years = null - end as number_of_years, - case - when obs regexp "!!6473=1065!!" then @ever_smoked_cigarettes := 1 - when obs regexp "!!6473=1066!!" then @ever_smoked_cigarettes := 2 - when obs regexp "!!6473=1679!!" then @ever_smoked_cigarettes := 3 - else @ever_smoked_cigarettes = null - end as ever_smoked_cigarettes, - case - when obs regexp "!!2069=" then @number_of_sticks := GetValues(obs,2069) - else @number_of_sticks = null - end as number_of_sticks, - case - when obs regexp "!!2070=" then @cigarette_duration := GetValues(obs,2070) - else @cigarette_duration = null - end as cigarette_duration, - - case - when obs regexp "!!7973=1065!!" then @tobacco_use := 1 - when obs regexp "!!7973=1066!!" then @tobacco_use := 2 - when obs regexp "!!7973=1679!!" then @tobacco_use := 3 - else @tobacco_use = null - end as tobacco_use, - case - when obs regexp "!!8144=" then @tobacco_duration := GetValues(obs,8144) - else @tobacco_duration = null - end as tobacco_duration, - - case - when obs regexp "!!1972=1967!!" then @main_occupation := 1 - when obs regexp "!!1972=1966!!" then @main_occupation := 2 - when obs regexp "!!1972=1971!!" then @main_occupation := 3 - when obs regexp "!!1972=8710!!" then @main_occupation := 4 - when obs regexp "!!1972=10369!!" then @main_occupation := 5 - when obs regexp "!!1972=10368!!" then @main_occupation := 6 - when obs regexp "!!1972=5622!!" then @main_occupation := 7 - else @main_occupation = null - end as main_occupation, - case - when obs regexp "!!10310=1107!!" then @chemical_exposure := 1 - when obs regexp "!!10310=10308!!" then @chemical_exposure := 2 - when obs regexp "!!10310=10370!!" then @chemical_exposure := 3 - when obs regexp "!!10310=10309!!" then @chemical_exposure := 4 - when obs regexp "!!10310=5622!!" then @chemical_exposure := 5 - else @chemical_exposure = null - end as chemical_exposure, - case - when obs regexp "!!10312=1065!!" then @asbestos_exposure := 1 - when obs regexp "!!10312=1066!!" then @asbestos_exposure := 2 - else @asbestos_exposure = null - end as asbestos_exposure, - - case - when obs regexp "!!9635=1107!!" then @other_cancer_in_family := 1 - when obs regexp "!!9635=978!!" then @other_cancer_in_family := 2 - when obs regexp "!!9635=1692!!" then @other_cancer_in_family := 3 - when obs regexp "!!9635=972!!" then @other_cancer_in_family := 4 - when obs regexp "!!9635=1671!!" then @other_cancer_in_family := 5 - when obs regexp "!!9635=1393!!" then @other_cancer_in_family := 6 - when obs regexp "!!9635=1392!!" then @other_cancer_in_family := 7 - when obs regexp "!!9635=1395!!" then @other_cancer_in_family := 8 - when obs regexp "!!9635=1394!!" then @other_cancer_in_family := 9 - when obs regexp "!!9635=1673!!" then @other_cancer_in_family := 10 - else @other_cancer_in_family = null - end as other_cancer_in_family, - case - when obs regexp "!!6709=1067!!" then @hiv_status := 1 - when obs regexp "!!6709=664!!" then @hiv_status := 2 - when obs regexp "!!6709=703!!" then @hiv_status := 3 - else @hiv_status = null - end as hiv_status, - case - when obs regexp "!!10242=1065!!" then @previous_tb_treatment := 1 - when obs regexp "!!10242=1066!!" then @previous_tb_treatment := 2 - else @previous_tb_treatment = null - end as previous_tb_treatment, - case - when obs regexp "!!1894=" then @treatment_duration := GetValues(obs,1894) - else @treatment_duration = null - end as treatment_duration, - case - when obs regexp "!!9304=" then @number_of_tb_treatment := GetValues(obs,9304) - else @number_of_tb_treatment = null - end as number_of_tb_treatment, - case - when obs regexp "!!2028=1107!!" then @previous_test_ordered := 1 - when obs regexp "!!2028=8064!!" then @previous_test_ordered := 2 - when obs regexp "!!2028=9543!!" then @previous_test_ordered := 3 - when obs regexp "!!2028=12!!" then @previous_test_ordered := 4 - when obs regexp "!!2028=5622!!" then @previous_test_ordered := 5 - else @previous_test_ordered = null - end as previous_test_ordered, - - case - when obs regexp "!!307=703!!" then @sputum_aafbs_results := 1 - when obs regexp "!!307=664!!" then @sputum_aafbs_results := 2 - when obs regexp "!!307=1118!!" then @sputum_aafbs_results := 3 - else @sputum_aafbs_results = null - end as sputum_aafbs_results, - - case - when obs regexp "!!8070=703!!" then @gene_expert_results := 1 - when obs regexp "!!8070=664!!" then @gene_expert_results := 2 - when obs regexp "!!8070=1138!!" then @gene_expert_results := 3 - else @gene_expert_results = null - end as gene_expert_results, - - case - when obs regexp "!!12=1115!!" then @x_ray_results := 1 - when obs regexp "!!12=1116!!" then @x_ray_results := 2 - when obs regexp "!!12=1118!!" then @x_ray_results := 3 - else @x_ray_results = null - end as x_ray_results, - - case - when obs regexp "!!10127=1107!!" then @procedure_ordered := 1 - when obs regexp "!!10127=10075!!" then @procedure_ordered := 2 - when obs regexp "!!10127=10076!!" then @procedure_ordered := 3 - when obs regexp "!!10127=10126!!" then @procedure_ordered := 4 - when obs regexp "!!10127=5622!!" then @procedure_ordered := 5 - else @procedure_ordered = null - end as procedure_ordered, - - case - when obs regexp "!!1271=12!!" then @imaging_test_ordered := 1 - when obs regexp "!!1271=7113!!" then @imaging_test_ordered := 2 - when obs regexp "!!1271=5622!!" then @imaging_test_ordered := 3 - else @imaging_test_ordered = null - end as imaging_test_ordered, - case - when obs regexp "!!8190=" then @radiology_test := GetValues(obs,8190) - else @radiology_test = null - end as radiology_test, - case - when obs regexp "!!9930=1107!!" then @follow_up_care_plan := 1 - when obs regexp "!!9930=9725!!" then @follow_up_care_plan := 2 - when obs regexp "!!9930=5622!!" then @follow_up_care_plan := 3 - else @follow_up_care_plan = null - end as follow_up_care_plan, - case - when obs regexp "!!5096=" then @rtc_date := GetValues(obs,5096) - else @rtc_date = null - end as rtc_date, - - case - when obs regexp "!!12=1115!!" then @imaging_results := 1 - when obs regexp "!!12=1116!!" then @imaging_results := 2 - when obs regexp "!!12=5622!!" then @imaging_results := 3 - else @imaging_results = null - end as imaging_results, - - case - when obs regexp "!!7113=1115!!" then @ct_findings := 1 - when obs regexp "!!7113=10318!!" then @ct_findings := 2 - when obs regexp "!!7113=2418!!" then @ct_findings := 3 - when obs regexp "!!7113=1136!!" then @ct_findings := 4 - when obs regexp "!!7113=5622!!" then @ct_findings := 5 - else @ct_findings = null - end as ct_findings, - case - when obs regexp "!!10077=" then @Imaging_results_description := GetValues(obs,10077) - else @Imaging_results_description = null - end as Imaging_results_description, - case - when obs regexp "!!10391=1065!!" then @biospy_procedure_done := 1 - when obs regexp "!!10391=1066!!" then @biospy_procedure_done := 2 - else @biospy_procedure_done = null - end as biospy_procedure_done, - case - when obs regexp "!!10060=" then @biopsy_workup_date := GetValues(obs,10060) - else @biopsy_workup_date = null - end as biopsy_workup_date, - case - when obs regexp "!!10231=10052!!" then @biospy_results := 1 - when obs regexp "!!10231=10212!!" then @biospy_results := 2 - when obs regexp "!!10231=5622!!" then @biospy_results := 3 - else @biospy_results = null - end as biospy_results, - - - case - when obs regexp "!!1915=" then @other_condition := GetValues(obs,1915) - else @other_condition = null - end as other_condition, - case - when obs regexp "!!9846=" then @type_of_malignancy := GetValues(obs,9846) - else @type_of_malignancy = null - end as type_of_malignancy, - case - when obs regexp "!!10390=" then @other_malignancy := GetValues(obs,10390) - else @other_malignancy = null - end as other_malignancy, - case - when obs regexp "!!7176=10129!!" then @lung_cancer_type := 1 - when obs regexp "!!7176=10130!!" then @lung_cancer_type := 2 - when obs regexp "!!7176=5622!!" then @lung_cancer_type := 3 - else @lung_cancer_type = null - end as lung_cancer_type, - case - when obs regexp "!!10132=7421!!" then @non_small_cancer := 1 - when obs regexp "!!10132=7422!!" then @non_small_cancer := 2 - when obs regexp "!!10132=10131!!" then @non_small_cancer := 3 - when obs regexp "!!10132=10209!!" then @non_small_cancer := 4 - else @non_small_cancer = null - end as non_small_cancer, - - case - when obs regexp "!!10319=1295!!" then @repository_disease := 1 - when obs regexp "!!10319=58!!" then @repository_disease := 2 - when obs regexp "!!10319=10211!!" then @repository_disease := 3 - when obs regexp "!!10319=10210!!" then @repository_disease := 4 - when obs regexp "!!10319=5622!!" then @repository_disease := 5 - else @repository_disease = null - end as repository_disease, - case - when obs regexp "!!9728=" then @diagnosis_date := GetValues(obs,9728) - else @diagnosis_date = null - end as diagnosis_date, - case - when obs regexp "!!1272=10299!!" then @referral_ordered := 1 - when obs regexp "!!1272=8161!!" then @referral_ordered := 2 - when obs regexp "!!1272=8724!!" then @referral_ordered := 3 - when obs regexp "!!1272=5486!!" then @referral_ordered := 4 - when obs regexp "!!1272=10200!!" then @referral_ordered := 5 - when obs regexp "!!1272=6570!!" then @referral_ordered := 6 - when obs regexp "!!1272=5622!!" then @referral_ordered := 7 - else @referral_ordered = null - end as referral_ordered, - - case - when obs regexp "!!5096=" then @return_date := GetValues(obs,5096) - else @return_date = null - end as return_date - - from flat_lung_cancer_screening_0 t1 - join amrs.person p using (person_id) - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - );# limit 100; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_lung_cancer_screening_1 drop prev_id, drop cur_id; - - drop table if exists flat_lung_cancer_screening_2; - create temporary table flat_lung_cancer_screening_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_lung_cancer_screening, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_lung_cancer_screening, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_lung_cancer_screening, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id_lung_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_lung_cancer_screening, - - case - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date - - from flat_lung_cancer_screening_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_lung_cancer_screening_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_lung_cancer_screening_3; - create temporary table flat_lung_cancer_screening_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_lung_cancer_screening, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_lung_cancer_screening, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_lung_cancer_screening, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id_lung_cancer_screening, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_lung_cancer_screening, - - case - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_lung_cancer_screening_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - - select count(*) into @new_encounter_rows from flat_lung_cancer_screening_3; - - select @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; - select @total_rows_written; - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - encounter_id, - encounter_type, - encounter_datetime, - visit_id, - location_id, - t2.uuid as location_uuid, - gender, - age, - cur_visit_type, - referral_from, - chief_complains, - complain_duration, - number_of_days, - number_of_weeks, - number_of_months, - number_of_years, - ever_smoked_cigarettes, - number_of_sticks, - cigarette_duration, - tobacco_use, - tobacco_duration, - main_occupation, - chemical_exposure, - asbestos_exposure, - other_cancer_in_family, - hiv_status, - previous_tb_treatment, - treatment_duration, - number_of_tb_treatment, - previous_test_ordered, - sputum_aafbs_results, - gene_expert_results, - x_ray_results, - procedure_ordered, - imaging_test_ordered, - radiology_test, - follow_up_care_plan, - rtc_date, - imaging_results, - ct_findings, - Imaging_results_description, - biospy_procedure_done, - biopsy_workup_date, - biospy_results, - other_condition, - type_of_malignancy, - other_malignancy, - lung_cancer_type, - non_small_cancer, - repository_disease, - diagnosis_date, - referral_ordered, - return_date, - - - prev_encounter_datetime_lung_cancer_screening, - next_encounter_datetime_lung_cancer_screening, - prev_encounter_type_lung_cancer_screening, - next_encounter_type_lung_cancer_screening, - prev_clinical_datetime_lung_cancer_screening, - next_clinical_datetime_lung_cancer_screening, - prev_clinical_location_id_lung_cancer_screening, - next_clinical_location_id_lung_cancer_screening, - prev_clinical_rtc_date_lung_cancer_screening, - next_clinical_rtc_date_lung_cancer_screening - - from flat_lung_cancer_screening_3 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_lung_cancer_screening_build_queue__0 t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #select @person_ids_count := (select count(*) from flat_lung_cancer_screening_build_queue_2); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count as remaining_in_build_queue; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - #select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select @person_ids_count as 'persons remaining', @cycle_length as 'Cycle time (s)', ceil(@person_ids_count / cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - -#select 1; - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_lung_cancer_treatment.sql b/etl-scripts/stored-procedures/generate_flat_lung_cancer_treatment.sql index 915ac5f..2424d79 100644 --- a/etl-scripts/stored-procedures/generate_flat_lung_cancer_treatment.sql +++ b/etl-scripts/stored-procedures/generate_flat_lung_cancer_treatment.sql @@ -1,1290 +1,1256 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_lung_cancer_treatment_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_lung_cancer_treatment`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN - set @primary_table := "flat_lung_cancer_treatment"; - set @query_type = query_type; - - set @total_rows_written = 0; - - set @encounter_types = "(170,169)"; - set @clinical_encounter_types = "(-1)"; - set @non_clinical_encounter_types = "(-1)"; - set @other_encounter_types = "(-1)"; - - set @start = now(); - set @table_version = "flat_lung_cancer_treatment_v1_0"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @boundary = "!!"; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - CREATE TABLE IF NOT EXISTS flat_lung_cancer_treatment ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id INT, - encounter_id INT, - encounter_type INT, - encounter_datetime DATETIME, - visit_id INT, - location_id INT, - location_uuid VARCHAR(100), - gender CHAR(100), - age INT, - cur_visit_type INT, - referred_from INT, - facility_name VARCHAR(100), - referral_date DATETIME, - referred_by INT, - marital_status INT, - main_occupation INT, - patient_level_education INT, - smoke_cigarettes INT, - number_of_sticks INT, - cigga_smoke_duration INT, - duration_since_last_use INT, - tobacco_use INT, - tobacco_use_duration INT, - drink_alcohol INT, - chemical_exposure INT, - asbestos_exposure INT, - any_family_mem_with_cancer INT, - family_member INT, - chief_complaints INT, - complain_duration INT, - number_days INT, - number_weeks INT, - number_months INT, - number_years INT, - ecog_performance INT, - general_exam INT, - heent INT, - chest INT, - heart INT, - abdomen_exam INT, - urogenital INT, - extremities INT, - testicular_exam INT, - nodal_survey INT, - musculo_skeletal INT, - neurologic INT, - previous_tb_treatment INT, - ever_diagnosed INT, - disease_diagnosis_year INT, - hiv_status INT, - chest_xray_result INT, - chest_ct_scan_results INT, - pet_scan_results INT, - abdominal_ultrasound_result INT, - other_imaging_results INT, - test_ordered INT, - other_radiology VARCHAR(500), - other_laboratory VARCHAR(500), - procedure_ordered INT, - procedure_performed INT, - other_procedures VARCHAR(500), - procedure_date DATETIME, - lung_cancer_type INT, - small_cell_lung_ca_staging INT, - non_small_cell_ca_type INT, - diagnosis_date DATETIME, - staging INT, - cancer_staging_date DATETIME, - metastasis_region VARCHAR(500), - treatment_plan INT, - other_treatment_plan INT, - radiotherapy_sessions INT, - surgery_date DATETIME, - chemotherapy_plan INT, - chemo_start_date DATETIME, - treatment_intent INT, - chemo_regimen VARCHAR(500), - chemo_cycle INT, - total_planned_chemo_cycle INT, - chemo_drug INT, - dosage_in_milligrams INT, - drug_route INT, - other_drugs INT, - purpose INT, - referral_ordered INT, - rtc DATETIME, - prev_encounter_datetime_lung_cancer_treatment DATETIME, - next_encounter_datetime_lung_cancer_treatment DATETIME, - prev_encounter_type_lung_cancer_treatment MEDIUMINT, - next_encounter_type_lung_cancer_treatment MEDIUMINT, - prev_clinical_datetime_lung_cancer_treatment DATETIME, - next_clinical_datetime_lung_cancer_treatment DATETIME, - prev_clinical_location_id_lung_cancer_treatment MEDIUMINT, - next_clinical_location_id_lung_cancer_treatment MEDIUMINT, - prev_clinical_rtc_date_lung_cancer_treatment DATETIME, - next_clinical_rtc_date_lung_cancer_treatment DATETIME, - PRIMARY KEY encounter_id (encounter_id), - INDEX person_date (person_id , encounter_datetime), - INDEX location_enc_date (location_uuid , encounter_datetime), - INDEX enc_date_location (encounter_datetime , location_uuid), - INDEX loc_id_enc_date_next_clinical (location_id , encounter_datetime , next_clinical_datetime_lung_cancer_treatment), - INDEX encounter_type (encounter_type), - INDEX date_created (date_created) -); - - - - if(@query_type="build") then - select 'BUILDING..........................................'; - -#set @write_table = concat("flat_lung_cancer_treatment_temp_",1); -#set @queue_table = concat("flat_lung_cancer_treatment_build_queue_",1); - - set @write_table = concat("flat_lung_cancer_treatment_temp_",queue_number); - set @queue_table = concat("flat_lung_cancer_treatment_build_queue_",queue_number); - - -#drop table if exists flat_lung_cancer_treatment_temp_1; - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_lung_cancer_treatment_build_queue limit ', queue_size, ');'); -#SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_lung_cancer_treatment_build_queue limit 500);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_lung_cancer_treatment_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from flat_lung_cancer_treatment_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_lung_cancer_treatment"; - set @queue_table = "flat_lung_cancer_treatment_sync_queue"; -CREATE TABLE IF NOT EXISTS flat_lung_cancer_treatment_sync_queue ( - person_id INT PRIMARY KEY -); - - set @last_update = null; - -SELECT - MAX(date_updated) -INTO @last_update FROM - etl.flat_log -WHERE - table_name = @table_version; - -SELECT 'Finding patients in amrs.encounters...'; - - replace into flat_lung_cancer_treatment_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - + SET @primary_table := "flat_lung_cancer_treatment"; + SET @query_type := query_type; -SELECT 'Finding patients in flat_obs...'; - - replace into flat_lung_cancer_treatment_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - -SELECT 'Finding patients in flat_lab_obs...'; - replace into flat_lung_cancer_treatment_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); + SET @total_rows_written := 0; -SELECT 'Finding patients in flat_orders...'; - - replace into flat_lung_cancer_treatment_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_lung_cancer_treatment_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_lung_cancer_treatment_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - # Remove test patients - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SELECT @person_ids_count AS 'num patients to update'; - - - #delete t1 from flat_lung_cancer_treatment t1 join @queue_table t2 using (person_id); - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); + SET @encounter_types := "(169,170)"; + SET @clinical_encounter_types := "(-1)"; + SET @non_clinical_encounter_types := "(-1)"; + SET @other_encounter_types := "(-1)"; - #create temp table with a set of person ids - drop temporary table if exists flat_lung_cancer_treatment_build_queue__0; - - #create temporary table flat_lung_cancer_treatment_build_queue__0 (select * from flat_lung_cancer_treatment_build_queue_2 limit 5000); #TODO - change this when data_fetch_size changes - -#SET @dyn_sql=CONCAT('create temporary table flat_lung_cancer_treatment_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit 100);'); - SET @dyn_sql=CONCAT('create temporary table flat_lung_cancer_treatment_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - drop temporary table if exists flat_lung_cancer_treatment_0a; - SET @dyn_sql = CONCAT( - 'create temporary table flat_lung_cancer_treatment_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - case - when t1.encounter_type in ',@clinical_encounter_types,' then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in ',@non_clinical_encounter_types,' then 20 - when t1.encounter_type in ',@clinical_encounter_types,' then 10 - when t1.encounter_type in', @other_encounter_types, ' then 5 - else 1 - end as encounter_type_sort_index, - t2.orders - from etl.flat_obs t1 - join flat_lung_cancer_treatment_build_queue__0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_id) - where t1.encounter_type in ',@encounter_types,');'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - + SET @start := now(); + SET @table_version := "flat_lung_cancer_treatment_v1_0"; + + set session sort_buffer_size := 512000000; + + SET @sep := " ## "; + SET @boundary := "!!"; + SET @last_date_created := (select max(max_date_created) from etl.flat_obs); + + CREATE TABLE IF NOT EXISTS flat_lung_cancer_treatment ( + date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + person_id INT, + encounter_id INT, + encounter_type INT, + encounter_datetime DATETIME, + visit_id INT, + location_id INT, + location_uuid VARCHAR(100), + gender CHAR(100), + age INT, + birthdate DATE, + death_date DATE, + cur_visit_type INT, + referred_from INT, + facility_name VARCHAR(100), + referral_date DATETIME, + referred_by INT, + marital_status INT, + main_occupation INT, + patient_level_education INT, + smoke_cigarettes INT, + number_of_sticks INT, + cigga_smoke_duration INT, + duration_since_last_use INT, + tobacco_use INT, + tobacco_use_duration INT, + drink_alcohol INT, + chemical_exposure INT, + asbestos_exposure INT, + any_family_mem_with_cancer INT, + family_member INT, + chief_complaints INT, + complain_duration INT, + number_days INT, + number_weeks INT, + number_months INT, + number_years INT, + ecog_performance INT, + general_exam INT, + heent INT, + chest INT, + heart INT, + abdomen_exam INT, + urogenital INT, + extremities INT, + testicular_exam INT, + nodal_survey INT, + musculo_skeletal INT, + neurologic INT, + previous_tb_treatment INT, + ever_diagnosed INT, + year_of_diagnosis INT, + date_of_diagnosis DATE, + hiv_status INT, + chest_xray_result INT, + chest_ct_scan_results INT, + pet_scan_results INT, + abdominal_ultrasound_result INT, + other_imaging_results INT, + test_ordered INT, + other_radiology VARCHAR(500), + other_laboratory VARCHAR(500), + procedure_ordered INT, + procedure_performed INT, + other_procedures VARCHAR(500), + procedure_date DATETIME, + lung_cancer_type INT, + small_cell_lung_ca_staging INT, + non_small_cell_ca_type INT, + staging INT, + cancer_staging_date DATETIME, + metastasis_region VARCHAR(500), + treatment_plan INT, + other_treatment_plan INT, + radiotherapy_sessions INT, + surgery_date DATETIME, + chemotherapy_plan INT, + chemo_start_date DATETIME, + chemo_stop_date DATETIME, + treatment_intent INT, + chemo_regimen VARCHAR(500), + chemo_cycle INT, + total_planned_chemo_cycle INT, + chemo_drug INT, + dosage_in_milligrams INT, + drug_route INT, + other_drugs INT, + purpose INT, + referral_ordered INT, + rtc DATETIME, + prev_encounter_datetime_lung_cancer_treatment DATETIME, + next_encounter_datetime_lung_cancer_treatment DATETIME, + prev_encounter_type_lung_cancer_treatment MEDIUMINT, + next_encounter_type_lung_cancer_treatment MEDIUMINT, + prev_clinical_datetime_lung_cancer_treatment DATETIME, + next_clinical_datetime_lung_cancer_treatment DATETIME, + prev_clinical_location_id_lung_cancer_treatment MEDIUMINT, + next_clinical_location_id_lung_cancer_treatment MEDIUMINT, + prev_clinical_rtc_date_lung_cancer_treatment DATETIME, + next_clinical_rtc_date_lung_cancer_treatment DATETIME, + PRIMARY KEY encounter_id (encounter_id), + INDEX person_date (person_id, encounter_datetime), + INDEX location_enc_date (location_uuid, encounter_datetime), + INDEX enc_date_location (encounter_datetime, location_uuid), + INDEX loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_lung_cancer_treatment), + INDEX encounter_type (encounter_type), + INDEX date_created (date_created) + ); + + if (@query_type="build") then + select 'BUILDING..........................................'; + + set @write_table = concat("flat_lung_cancer_treatment_temp_",queue_number); + set @queue_table = concat("flat_lung_cancer_treatment_build_queue_",queue_number); + + SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_lung_cancer_treatment_build_queue limit ', queue_size, ');'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SET @dyn_sql=CONCAT('delete t1 from flat_lung_cancer_treatment_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + end if; + + if (@query_type="sync") then + select 'SYNCING..........................................'; + set @write_table = "flat_lung_cancer_treatment"; + set @queue_table = "flat_lung_cancer_treatment_sync_queue"; + CREATE TABLE IF NOT EXISTS flat_lung_cancer_treatment_sync_queue ( + person_id INT PRIMARY KEY + ); + + set @last_update = null; + + SELECT + MAX(date_updated) + INTO @last_update FROM + etl.flat_log + WHERE + table_name = @table_version; + + SELECT 'Finding patients in amrs.encounters...'; + + replace into flat_lung_cancer_treatment_sync_queue + (select distinct patient_id + from amrs.encounter + where date_changed > @last_update + ); + + + SELECT 'Finding patients in flat_obs...'; + + replace into flat_lung_cancer_treatment_sync_queue + (select distinct person_id + from etl.flat_obs + where max_date_created > @last_update + ); + + + SELECT 'Finding patients in flat_lab_obs...'; + replace into flat_lung_cancer_treatment_sync_queue + (select distinct person_id + from etl.flat_lab_obs + where max_date_created > @last_update + ); + + SELECT 'Finding patients in flat_orders...'; + replace into flat_lung_cancer_treatment_sync_queue + (select distinct person_id + from etl.flat_orders + where max_date_created > @last_update + ); + + replace into flat_lung_cancer_treatment_sync_queue + (select person_id from + amrs.person + where date_voided > @last_update); + + replace into flat_lung_cancer_treatment_sync_queue + (select person_id from + amrs.person + where date_changed > @last_update); + + end if; + + -- Remove test patients + SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 + join amrs.person_attribute t2 using (person_id) + where t2.person_attribute_type_id=28 and value="true" and voided=0'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SET @person_ids_count = 0; + SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SELECT @person_ids_count AS 'num patients to update'; + + SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + set @total_time=0; + set @cycle_number = 0; + + while @person_ids_count > 0 do + set @loop_start_time = now(); + drop temporary table if exists flat_lung_cancer_treatment_build_queue__0; + + SET @dyn_sql=CONCAT('create temporary table flat_lung_cancer_treatment_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + drop temporary table if exists flat_lung_cancer_treatment_0a; + SET @dyn_sql = CONCAT( + 'create temporary table flat_lung_cancer_treatment_0a + (select + t1.person_id, + t1.visit_id, + t1.encounter_id, + t1.encounter_datetime, + t1.encounter_type, + t1.location_id, + t1.obs, + t1.obs_datetimes, + case + when t1.encounter_type in ',@clinical_encounter_types,' then 1 + else null + end as is_clinical_encounter, + + case + when t1.encounter_type in ',@non_clinical_encounter_types,' then 20 + when t1.encounter_type in ',@clinical_encounter_types,' then 10 + when t1.encounter_type in', @other_encounter_types, ' then 5 + else 1 + end as encounter_type_sort_index, + t2.orders + from etl.flat_obs t1 + join flat_lung_cancer_treatment_build_queue__0 t0 using (person_id) + left join etl.flat_orders t2 using(encounter_id) + where t1.encounter_type in ',@encounter_types,');'); + + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + insert into flat_lung_cancer_treatment_0a + (select + t1.person_id, + null, + t1.encounter_id, + t1.test_datetime, + t1.encounter_type, + null, #t1.location_id, + t1.obs, + null, #obs_datetimes + # in any visit, there many be multiple encounters. for this dataset, we want to include only clinical encounters (e.g. not lab or triage visit) + 0 as is_clinical_encounter, + 1 as encounter_type_sort_index, + null + from etl.flat_lab_obs t1 + join flat_lung_cancer_treatment_build_queue__0 t0 using (person_id) + ); + + + drop temporary table if exists flat_lung_cancer_treatment_0; + create temporary table flat_lung_cancer_treatment_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) + (select * from flat_lung_cancer_treatment_0a + order by person_id, date(encounter_datetime), encounter_type_sort_index + ); + + set @birthdate = null; + set @death_date = null; + set @cur_visit_type = null; + set @referred_from = null; + set @facility_name = null; + set @referral_date = null; + set @referred_by = null; + set @marital_status = null; + set @main_occupation = null; + set @patient_level_education = null; + set @smoke_cigarettes = null; + set @number_of_sticks = null; + set @cigga_smoke_duration = null; + set @duration_since_last_use = null; + set @tobacco_use = null; + set @tobacco_use_duration = null; + set @drink_alcohol = null; + set @chemical_exposure = null; + set @asbestos_exposure = null; + set @any_family_mem_with_cancer = null; + set @family_member = null; + set @chief_complaints = null; + set @complain_duration = null; + set @number_days = null; + set @number_weeks = null; + set @number_months = null; + set @number_years = null; + set @ecog_performance = null; + set @general_exam = null; + set @heent = null; + set @chest = null; + set @heart = null; + set @abdomen_exam = null; + set @urogenital = null; + set @extremities = null; + set @testicular_exam = null; + set @nodal_survey = null; + set @musculo_skeletal = null; + set @neurologic = null; + set @previous_tb_treatment = null; + set @ever_diagnosed = null; + set @year_of_diagnosis = null; + set @date_of_diagnosis = null; + set @hiv_status = null; + set @chest_xray_result= null; + set @chest_ct_scan_results = null; + set @pet_scan_results = null; + set @abdominal_ultrasound_result = null; + set @other_imaging_results = null; + set @test_ordered = null; + set @other_radiology = null; + set @other_laboratory = null; + set @procedure_ordered= null; + set @procedure_performed = null; + set @other_procedures = null; + set @procedure_date = null; + set @lung_cancer_type = null; + set @small_cell_lung_ca_staging = null; + set @non_small_cell_ca_type = null; + set @staging = null; + set @cancer_staging_date= null; + set @metastasis_region = null; + set @treatment_plan = null; + set @other_treatment_plan = null; + set @radiotherapy_sessions= null; + set @surgery_date = null; + set @chemotherapy_plan = null; + set @chemo_start_date = null; + set @chemo_stop_date = null; + set @treatment_intent = null; + set @chemo_regimen = null; + set @chemo_cycle = null; + set @total_planned_chemo_cycle = null; + set @chemo_drug = null; + set @dosage_in_milligrams = null; + set @drug_route = null; + set @other_drugs = null; + set @purpose = null; + set @referral_ordered = null; + set @rtc = null; + + drop temporary table if exists flat_lung_cancer_treatment_1; + create temporary table flat_lung_cancer_treatment_1 #(index encounter_id (encounter_id)) + (select + obs, + encounter_type_sort_index, + @prev_id := @cur_id as prev_id, + @cur_id := t1.person_id as cur_id, + t1.person_id, + t1.encounter_id, + t1.encounter_type, + t1.encounter_datetime, + t1.visit_id, + t1.location_id, + t1.is_clinical_encounter, + p.gender, + p.birthdate, + p.death_date, + case + when timestampdiff(year,birthdate,curdate()) > 0 then round(timestampdiff(year,birthdate,curdate()),0) + else round(timestampdiff(month,birthdate,curdate())/12,2) + end as age, + case + when obs regexp "!!1839=7037!!" then @cur_visit_type := 1 + when obs regexp "!!1839=7875!!" then @cur_visit_type := 2 + else @cur_visit_type := null + end as cur_visit_type, + case + when obs regexp "!!6749=8161!!" then @referred_from := 1 + when obs regexp "!!6749=5487!!" then @referred_from := 2 + when obs regexp "!!6749=1275!!" then @referred_from := 3 + when obs regexp "!!6749=2242!!" then @referred_from := 4 + when obs regexp "!!6749=6572!!" then @referred_from := 5 + when obs regexp "!!6749=978!!" then @referred_from := 6 + when obs regexp "!!6749=5622!!" then @referred_from := 7 + else @referred_from := null + end as referred_from, + case + when obs regexp "!!6748=" then @facility_name := GetValues(obs,6748) + else @facility_name = null + end as facility_name, + case + when obs regexp "!!9158=" then @referral_date := GetValues(obs,9158) + else @referral_date = null + end as referral_date, + case + when obs regexp "!!10135=1496!!" then @referred_by := 1 + when obs regexp "!!10135=6280!!" then @referred_by := 2 + when obs regexp "!!10135=5507!!" then @referred_by := 3 + when obs regexp "!!10135=5622!!" then @referred_by := 4 + else @referred_by := null + end as referred_by, + case + when obs regexp "!!1054=1175!!" then @marital_status := 1 + when obs regexp "!!1054=1057!!" then @marital_status := 2 + when obs regexp "!!1054=5555!!" then @marital_status := 3 + when obs regexp "!!1054=6290!!" then @marital_status := 4 + when obs regexp "!!1054=1060!!" then @marital_status := 5 + when obs regexp "!!1054=1056!!" then @marital_status := 6 + when obs regexp "!!1054=1058!!" then @marital_status := 7 + when obs regexp "!!1054=1059!!" then @marital_status := 8 + else @marital_status := null + end as marital_status, + case + when obs regexp "!!1972=1967!!" then @main_occupation := 1 + when obs regexp "!!1972=6401!!" then @main_occupation := 2 + when obs regexp "!!1972=6408!!" then @main_occupation := 3 + when obs regexp "!!1972=6966!!" then @main_occupation := 4 + when obs regexp "!!1972=1969!!" then @main_occupation := 5 + when obs regexp "!!1972=1968!!" then @main_occupation := 6 + when obs regexp "!!1972=1966!!" then @main_occupation := 7 + when obs regexp "!!1972=8407!!" then @main_occupation := 8 + when obs regexp "!!1972=6284!!" then @main_occupation := 9 + when obs regexp "!!1972=8589!!" then @main_occupation := 10 + when obs regexp "!!1972=5619!!" then @main_occupation := 11 + when obs regexp "!!1972=5622!!" then @main_occupation := 12 + else @main_occupation = null + end as main_occupation, + case + when obs regexp "!!1605=1107!!" then @patient_level_education := 1 + when obs regexp "!!1605=6214!!" then @patient_level_education := 2 + when obs regexp "!!1605=6215!!" then @patient_level_education := 3 + when obs regexp "!!1605=1604!!" then @patient_level_education := 4 + else @patient_level_education := null + end as patient_level_education, + + case + when obs regexp "!!6473=1065!!" then @smoke_cigarettes := 1 + when obs regexp "!!6473=1066!!" then @smoke_cigarettes := 2 + when obs regexp "!!6473=1679!!" then @smoke_cigarettes := 3 + else @smoke_cigarettes := null + end as smoke_cigarettes, + case + when obs regexp "!!2069=" then @number_of_sticks := GetValues(obs,2069) + else @number_of_sticks = null + end as number_of_sticks, + case + when obs regexp "!!2070=" then @cigga_smoke_duration := GetValues(obs,2070) + else @cigga_smoke_duration = null + end as cigga_smoke_duration, + case + when obs regexp "!!2068=" then @duration_since_last_use := GetValues(obs,2068) + else @duration_since_last_use = null + end as duration_since_last_use, + case + when obs regexp "!!7973=1065!!" then @tobacco_use := 1 + when obs regexp "!!7973=1066!!" then @tobacco_use := 2 + when obs regexp "!!7973=1679!!" then @tobacco_use := 3 + else @tobacco_use = null + end as tobacco_use, + case + when obs regexp "!!8144=" then @tobacco_use_duration := GetValues(obs,8144) + else @tobacco_use_duration = null + end as tobacco_use_duration, + case + when obs regexp "!!1684=1065!!" then @drink_alcohol := 1 + when obs regexp "!!1684=1066!!" then @drink_alcohol := 2 + when obs regexp "!!1684=1679!!" then @drink_alcohol := 3 + else @drink_alcohol := null + end as drink_alcohol, + case + when obs regexp "!!10310=1107!!" then @chemical_exposure := 1 + when obs regexp "!!10310=10308!!" then @chemical_exposure := 2 + when obs regexp "!!10310=10370!!" then @chemical_exposure := 3 + when obs regexp "!!10310=10309!!" then @chemical_exposure := 4 + when obs regexp "!!10310=5622!!" then @chemical_exposure := 5 + else @chemical_exposure = null + end as chemical_exposure, + case + when obs regexp "!!10312=1065!!" then @asbestos_exposure := 1 + when obs regexp "!!10312=1066!!" then @asbestos_exposure := 2 + else @asbestos_exposure = null + end as asbestos_exposure, + + case + when obs regexp "!!6802=1065!!" then @any_family_mem_with_cancer := 1 + when obs regexp "!!6802=1067!!" then @any_family_mem_with_cancer := 2 + else @any_family_mem_with_cancer = null + end as any_family_mem_with_cancer, + case + when obs regexp "!!9635=1107!!" then @family_member := 1 + when obs regexp "!!9635=978!!" then @family_member := 2 + when obs regexp "!!9635=1692!!" then @family_member := 3 + when obs regexp "!!9635=972!!" then @family_member := 4 + when obs regexp "!!9635=1671!!" then @family_member := 5 + when obs regexp "!!9635=1393!!" then @family_member := 6 + when obs regexp "!!9635=1392!!" then @family_member := 7 + when obs regexp "!!9635=1395!!" then @family_member := 8 + when obs regexp "!!9635=1394!!" then @family_member := 9 + when obs regexp "!!9635=1673!!" then @family_member := 10 + else @family_member := null + end as family_member, + case + when obs regexp "!!5219=136!!" then @chief_complaints := 1 + when obs regexp "!!5219=107!!" then @chief_complaints := 2 + when obs regexp "!!5219=6786!!" then @chief_complaints := 3 + when obs regexp "!!5219=5954!!" then @chief_complaints := 4 + when obs regexp "!!5219=832!!" then @chief_complaints := 5 + when obs regexp "!!5219=5960!!" then @chief_complaints := 6 + when obs regexp "!!5219=10121!!" then @chief_complaints := 7 + when obs regexp "!!5219=5622!!" then @chief_complaints := 8 + else @chief_complaints := null + end as chief_complaints, + case + when obs regexp "!!8777=1072!!" then @complain_duration := 1 + when obs regexp "!!8777=1073!!" then @complain_duration := 2 + when obs regexp "!!8777=1074!!" then @complain_duration := 3 + when obs regexp "!!8777=8787!!" then @complain_duration := 4 + else @complain_duration = null + end as complain_duration, + case + when obs regexp "!!1892=" then @number_days := GetValues(obs,1892) + else @number_days = null + end as number_days, + case + when obs regexp "!!1893=" then @number_weeks := GetValues(obs,1893) + else @number_weeks = null + end as number_weeks, + case + when obs regexp "!!1894=" then @number_months := GetValues(obs,1894) + else @number_months = null + end as number_months, + case + when obs regexp "!!7953=" then @number_years := GetValues(obs,7953) + else @number_years = null + end as number_years, + case + when obs regexp "!!6584=1115!!" then @ecog_performance := 1 + when obs regexp "!!6584=6585!!" then @ecog_performance := 2 + when obs regexp "!!6584=6586!!" then @ecog_performance := 3 + when obs regexp "!!6584=6587!!" then @ecog_performance := 4 + when obs regexp "!!6584=6588!!" then @ecog_performance := 5 + else @ecog_performance = null + end as ecog_performance, + case + when obs regexp "!!1119=1115!!" then @general_exam := 1 + when obs regexp "!!1119=5201!!" then @general_exam := 2 + when obs regexp "!!1119=5245!!" then @general_exam := 3 + when obs regexp "!!1119=215!!" then @general_exam := 4 + when obs regexp "!!1119=589!!" then @general_exam := 5 + else @general_exam = null + end as general_exam, + case + when obs regexp "!!1122=1118!!" then @heent := 1 + when obs regexp "!!1122=1115!!" then @heent := 2 + when obs regexp "!!1122=5192!!" then @heent := 3 + when obs regexp "!!1122=516!!" then @heent := 4 + when obs regexp "!!1122=513!!" then @heent := 5 + when obs regexp "!!1122=5170!!" then @heent := 6 + when obs regexp "!!1122=5334!!" then @heent := 7 + when obs regexp "!!1122=6672!!" then @heent := 8 + else @heent = null + end as heent, + case + when obs regexp "!!1123=1118!!" then @chest := 1 + when obs regexp "!!1123=1115!!" then @chest := 2 + when obs regexp "!!1123=5138!!" then @chest := 3 + when obs regexp "!!1123=5115!!" then @chest := 4 + when obs regexp "!!1123=5116!!" then @chest := 5 + when obs regexp "!!1123=5181!!" then @chest := 6 + when obs regexp "!!1123=5127!!" then @chest := 7 + else @chest = null + end as chest, + case + when obs regexp "!!1124=1118!!" then @heart := 1 + when obs regexp "!!1124=1115!!" then @heart := 2 + when obs regexp "!!1124=1117!!" then @heart := 3 + when obs regexp "!!1124=550!!" then @heart := 4 + when obs regexp "!!1124=5176!!" then @heart := 5 + when obs regexp "!!1124=5162!!" then @heart := 6 + when obs regexp "!!1124=5164!!" then @heart := 7 + else @heart = null + end as heart, + case + when obs regexp "!!1125=1118!!" then @abdomen_exam := 1 + when obs regexp "!!1125=1115!!" then @abdomen_exam := 2 + when obs regexp "!!1125=5105!!" then @abdomen_exam := 3 + when obs regexp "!!1125=5008!!" then @abdomen_exam := 4 + when obs regexp "!!1125=5009!!" then @abdomen_exam := 5 + when obs regexp "!!1125=581!!" then @abdomen_exam := 6 + when obs regexp "!!1125=5103!!" then @abdomen_exam := 7 + else @abdomen_exam = null + end as abdomen_exam, + case + when obs regexp "!!1126=1118!!" then @urogenital := 1 + when obs regexp "!!1126=1115!!" then @urogenital := 2 + when obs regexp "!!1126=1116!!" then @urogenital := 3 + when obs regexp "!!1126=2186!!" then @urogenital := 4 + when obs regexp "!!1126=864!!" then @urogenital := 5 + when obs regexp "!!1126=6334!!" then @urogenital := 6 + when obs regexp "!!1126=1447!!" then @urogenital := 7 + when obs regexp "!!1126=5993!!" then @urogenital := 8 + when obs regexp "!!1126=8998!!" then @urogenital := 9 + when obs regexp "!!1126=1489!!" then @urogenital := 10 + when obs regexp "!!1126=8417!!" then @urogenital := 11 + when obs regexp "!!1126=8261!!" then @urogenital := 12 + else @urogenital = null + end as urogenital, + case + when obs regexp "!!1127=1118!!" then @extremities := 1 + when obs regexp "!!1127=1115!!" then @extremities := 2 + when obs regexp "!!1127=590!!" then @extremities := 3 + when obs regexp "!!1127=7293!!" then @extremities := 4 + when obs regexp "!!1127=134!!" then @extremities := 5 + else @extremities = null + end as extremities, + case + when obs regexp "!!8420=1118!!" then @testicular_exam := 1 + when obs regexp "!!8420=1115!!" then @testicular_exam := 2 + when obs regexp "!!8420=1116!!" then @testicular_exam := 3 + else @testicular_exam = null + end as testicular_exam, + case + when obs regexp "!!1121=1118!!" then @nodal_survey := 1 + when obs regexp "!!1121=1115!!" then @nodal_survey := 2 + when obs regexp "!!1121=1116!!" then @nodal_survey := 3 + when obs regexp "!!1121=8261!!" then @nodal_survey := 4 + else @nodal_survey = null + end as nodal_survey, + case + when obs regexp "!!1128=1118!!" then @musculo_skeletal := 1 + when obs regexp "!!1128=1115!!" then @musculo_skeletal := 2 + when obs regexp "!!1128=1116!!" then @musculo_skeletal := 3 + else @musculo_skeletal = null + end as musculo_skeletal, + case + when obs regexp "!!1129=1118!!" then @neurologic := 1 + when obs regexp "!!1129=1115!!" then @neurologic := 2 + when obs regexp "!!1129=599!!" then @neurologic := 3 + when obs regexp "!!1129=497!!" then @neurologic := 4 + when obs regexp "!!1129=5108!!" then @neurologic := 5 + else @neurologic = null + end as neurologic, + case + when obs regexp "!!5704=" then @previous_tb_treatment := GetValues(obs,5704) + else @previous_tb_treatment = null + end as previous_tb_treatment, + case + when obs regexp "!!6245=142!!" then @ever_diagnosed := 1 + when obs regexp "!!6245=5!!" then @ever_diagnosed := 2 + when obs regexp "!!6245=6237!!" then @ever_diagnosed := 3 + when obs regexp "!!6245=903!!" then @ever_diagnosed := 4 + when obs regexp "!!6245=6033!!" then @ever_diagnosed := 5 + when obs regexp "!!6245=43!!" then @ever_diagnosed := 6 + when obs regexp "!!6245=58!!" then @ever_diagnosed := 7 + else @ever_diagnosed = null + end as ever_diagnosed, + case + when obs regexp "!!9281=" then @year_of_diagnosis := GetValues(obs,9281) + else @year_of_diagnosis = null + end as year_of_diagnosis, + case + when obs regexp "!!9728=" then @date_of_diagnosis := DATE(replace(replace((substring_index(substring(obs, locate("!!9728=", obs)), @sep, 1)), "!!9728=", ""), "!!", "")) + else @date_of_diagnosis = null + end as date_of_diagnosis, + case + when obs regexp "!!6709=1067!!" then @hiv_status := 1 + when obs regexp "!!6709=664!!" then @hiv_status := 2 + when obs regexp "!!6709=703!!" then @hiv_status := 3 + else @hiv_status = null + end as hiv_status, + case + when obs regexp "!!12=1115!!" then @chest_xray_result := 1 + when obs regexp "!!12=1116!!" then @chest_xray_result := 2 + else @chest_xray_result = null + end as chest_xray_result, + case + when obs regexp "!!7113=1115!!" then @chest_ct_scan_results := 1 + when obs regexp "!!7113=1117!!" then @chest_ct_scan_results := 2 + else @chest_ct_scan_results = null + end as chest_ct_scan_results, + case + when obs regexp "!!10123=1115!!" then @pet_scan_results := 1 + when obs regexp "!!10123=1118!!" then @pet_scan_results := 2 + else @pet_scan_results = null + end as pet_scan_results, + case + when obs regexp "!!845=1115!!" then @abdominal_ultrasound_result := 1 + when obs regexp "!!845=1119!!" then @abdominal_ultrasound_result := 2 + else @abdominal_ultrasound_result = null + end as abdominal_ultrasound_result, + case + when obs regexp "!!10124=" then @other_imaging_results = GetValues(obs,10124) + else @other_imaging_results = null + end as other_imaging_results, + + case + when obs regexp "!!1271=1107!!" then @test_ordered := 1 + when obs regexp "!!1271=1019!!" then @test_ordered := 2 + when obs regexp "!!1271=790!!" then @test_ordered := 3 + when obs regexp "!!1271=953!!" then @test_ordered := 4 + when obs regexp "!!1271=6898!!" then @test_ordered := 5 + when obs regexp "!!1271=9009!!" then @test_ordered := 6 + else @test_ordered = null + end as test_ordered, + case + when obs regexp "!!8190=" then @other_radiology := GetValues(obs,8190) + else @other_radiology = null + end as other_radiology, + case + when obs regexp "!!9538=" then @other_laboratory := GetValues(obs,9538) + else @other_laboratory = null + end as other_laboratory, + case + when obs regexp "!!10127=1107!!" then @procedure_ordered := 1 + when obs regexp "!!10127=10075!!" then @procedure_ordered := 2 + when obs regexp "!!10127=10076!!" then @procedure_ordered := 3 + when obs regexp "!!10127=10126!!" then @procedure_ordered := 4 + when obs regexp "!!10127=5622!!" then @procedure_ordered := 5 + else @procedure_ordered = null + end as procedure_ordered, + case + when obs regexp "!!10442=1107!!" then @procedure_performed := 1 + when obs regexp "!!10442=10075!!" then @procedure_performed := 2 + when obs regexp "!!10442=10076!!" then @procedure_performed := 3 + when obs regexp "!!10442=10126!!" then @procedure_performed := 4 + when obs regexp "!!10442=5622!!" then @procedure_performed := 5 + else @procedure_performed = null + end as procedure_performed, + case + when obs regexp "!!1915=" then @other_procedures := GetValues(obs,1915) + else @other_procedures = null + end as other_procedures, + case + when obs regexp "!!10443=" then @procedure_date := GetValues(obs,10443) + else @procedure_date = null + end as procedure_date, + + case + when obs regexp "!!7176=10129!!" then @lung_cancer_type := 1 + when obs regexp "!!7176=10130!!" then @lung_cancer_type := 2 + when obs regexp "!!7176=5622!!" then @lung_cancer_type := 3 + else @lung_cancer_type = null + end as lung_cancer_type, + case + when obs regexp "!!10137=6563!!" then @small_cell_lung_ca_staging := 1 + when obs regexp "!!10137=6564!!" then @small_cell_lung_ca_staging := 2 + else @small_cell_lung_ca_staging = null + end as small_cell_lung_ca_staging, + case + when obs regexp "!!10132=7421!!" then @non_small_cell_ca_type := 1 + when obs regexp "!!10132=7422!!" then @non_small_cell_ca_type := 2 + when obs regexp "!!10132=10131!!" then @non_small_cell_ca_type := 3 + when obs regexp "!!10132=10209!!" then @non_small_cell_ca_type := 3 + else @non_small_cell_ca_type = null + end as non_small_cell_ca_type, + case + when obs regexp "!!9868=9852!!" then @staging := 1 + when obs regexp "!!9868=9856!!" then @staging := 2 + when obs regexp "!!9868=9860!!" then @staging := 3 + when obs regexp "!!9868=9864!!" then @staging := 4 + when obs regexp "!!9868=6563!!" then @staging := 5 + when obs regexp "!!9868=6564!!" then @staging := 6 + else @staging = null + end as staging, + case + when obs regexp "!!10441=" then @cancer_staging_date := GetValues(obs,10441) + else @cancer_staging_date = null + end as cancer_staging_date, + case + when obs regexp "!!10445=" then @metastasis_region := GetValues(obs,10445) + else @metastasis_region = null + end as metastasis_region, + case + when obs regexp "!!8723=6575!!" then @treatment_plan := 1 + when obs regexp "!!8723=8427!!" then @treatment_plan := 2 + when obs regexp "!!8723=7465!!" then @treatment_plan := 3 + when obs regexp "!!8723=5622!!" then @treatment_plan := 4 + else @treatment_plan = null + end as treatment_plan, + case + when obs regexp "!!10039=" then @other_treatment_plan := GetValues(obs,10039) + else @other_treatment_plan = null + end as other_treatment_plan, + case + when obs regexp "!!10133=" then @radiotherapy_sessions := GetValues(obs,10133) + else @radiotherapy_sessions = null + end as radiotherapy_sessions, + case + when obs regexp "!!10134=" then @surgery_date := GetValues(obs,10134) + else @surgery_date = null + end as surgery_date, + case + when obs regexp "!!9869=1256!!" then @chemotherapy_plan := 1 + when obs regexp "!!9869=1259!!" then @chemotherapy_plan := 2 + when obs regexp "!!9869=1260!!" then @chemotherapy_plan := 3 + when obs regexp "!!9869=1257!!" then @chemotherapy_plan := 4 + else @chemotherapy_plan = null + end as chemotherapy_plan, + case + when obs regexp "!!1190=" then @chemo_start_date := GetValues(obs,1190) + else @chemo_start_date = null + end as chemo_start_date, + case + when obs regexp "!!9869=1260!!" then @chemo_start_date := GetValues(obs_datetimes, 9869) + else @chemo_stop_date = null + end as chemo_stop_date, + case + when obs regexp "!!2206=9218!!" then @treatment_intent := 1 + when obs regexp "!!2206=8428!!" then @treatment_intent := 2 + when obs regexp "!!2206=8724!!" then @treatment_intent := 3 + when obs regexp "!!2206=9219!!" then @treatment_intent := 4 + else @treatment_intent = null + end as treatment_intent, + case + when obs regexp "!!9946=" then @chemo_regimen := GetValues(obs,9946) + else @chemo_regimen = null + end as chemo_regimen, + case + when obs regexp "!!6643=" then @chemo_cycle := GetValues(obs,6643) + else @chemo_cycle = null + end as chemo_cycle, + case + when obs regexp "!!6644=" then @total_planned_chemo_cycle := GetValues(obs,6644) + else @total_planned_chemo_cycle = null + end as total_planned_chemo_cycle, + case + when obs regexp "!!9918=7202!!" then @chemo_drug := 1 + when obs regexp "!!9918=8506!!" then @chemo_drug := 2 + when obs regexp "!!9918=7210!!" then @chemo_drug := 3 + when obs regexp "!!9918=8492!!" then @chemo_drug := 4 + when obs regexp "!!9918=8507!!" then @chemo_drug := 5 + else @chemo_drug = null + end as chemo_drug, + case + when obs regexp "!!1899=" then @dosage_in_milligrams := GetValues(obs,1899) + else @dosage_in_milligrams = null + end as dosage_in_milligrams, + case + when obs regexp "!!7463=7458!!" then @drug_route := 1 + when obs regexp "!!7463=10078!!" then @drug_route := 2 + when obs regexp "!!7463=10079!!" then @drug_route := 3 + when obs regexp "!!7463=7597!!" then @drug_route := 4 + when obs regexp "!!7463=7581!!" then @drug_route := 5 + when obs regexp "!!7463=7609!!" then @drug_route := 6 + when obs regexp "!!7463=7616!!" then @drug_route := 7 + when obs regexp "!!7463=7447!!" then @drug_route := 8 + else @drug_route = null + end as drug_route, + case + when obs regexp "!!1895=" then @other_drugs := GetValues(obs,1895) + else @other_drugs = null + end as other_drugs, + case + when obs regexp "!!2206=9220!!" then @purpose := 1 + when obs regexp "!!2206=8428!!" then @purpose := 2 + else @purpose = null + end as purpose, + case + when obs regexp "!!1272=1107!!" then @referral_ordered := 1 + when obs regexp "!!1272=8724!!" then @referral_ordered := 2 + when obs regexp "!!1272=5486!!" then @referral_ordered := 3 + when obs regexp "!!1272=5884!!" then @referral_ordered := 4 + when obs regexp "!!1272=6570!!" then @referral_ordered := 5 + when obs regexp "!!1272=5622!!" then @referral_ordered := 6 + else @referral_ordered = null + end as referral_ordered, + case + when obs regexp "!!5096=" then @rtc := GetValues(obs,5096) + else @rtc = null + end as rtc + + from flat_lung_cancer_treatment_0 t1 + join amrs.person p using (person_id) + order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc + ); + + set @prev_id = null; + set @cur_id = null; + set @prev_encounter_datetime = null; + set @cur_encounter_datetime = null; + + set @prev_clinical_datetime = null; + set @cur_clinical_datetime = null; + + set @next_encounter_type = null; + set @cur_encounter_type = null; + + set @prev_clinical_location_id = null; + set @cur_clinical_location_id = null; + + alter table flat_lung_cancer_treatment_1 drop prev_id, drop cur_id; + + drop table if exists flat_lung_cancer_treatment_2; + create temporary table flat_lung_cancer_treatment_2 + (select *, + @prev_id := @cur_id as prev_id, + @cur_id := person_id as cur_id, + + case + when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime + else @prev_encounter_datetime := null + end as next_encounter_datetime_lung_cancer_treatment, + + @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, + + case + when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type + else @next_encounter_type := null + end as next_encounter_type_lung_cancer_treatment, + + @cur_encounter_type := encounter_type as cur_encounter_type, + + case + when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime + else @prev_clinical_datetime := null + end as next_clinical_datetime_lung_cancer_treatment, + + case + when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id + else @prev_clinical_location_id := null + end as next_clinical_location_id_lung_cancer_treatment, + + case + when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime + when @prev_id = @cur_id then @cur_clinical_datetime + else @cur_clinical_datetime := null + end as cur_clinic_datetime, + + case + when is_clinical_encounter then @cur_clinical_location_id := location_id + when @prev_id = @cur_id then @cur_clinical_location_id + else @cur_clinical_location_id := null + end as cur_clinic_location_id, + + case + when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date + else @prev_clinical_rtc_date := null + end as next_clinical_rtc_date_lung_cancer_treatment, + + case + when @prev_id = @cur_id then @cur_clinical_rtc_date + else @cur_clinical_rtc_date:= null + end as cur_clinical_rtc_date + + from flat_lung_cancer_treatment_1 + order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc + ); + + alter table flat_lung_cancer_treatment_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; + + + set @prev_id = null; + set @cur_id = null; + set @prev_encounter_type = null; + set @cur_encounter_type = null; + set @prev_encounter_datetime = null; + set @cur_encounter_datetime = null; + set @prev_clinical_datetime = null; + set @cur_clinical_datetime = null; + set @prev_clinical_location_id = null; + set @cur_clinical_location_id = null; + + drop temporary table if exists flat_lung_cancer_treatment_3; + create temporary table flat_lung_cancer_treatment_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) + (select + *, + @prev_id := @cur_id as prev_id, + @cur_id := t1.person_id as cur_id, + + case + when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type + else @prev_encounter_type:=null + end as prev_encounter_type_lung_cancer_treatment, + @cur_encounter_type := encounter_type as cur_encounter_type, + + case + when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime + else @prev_encounter_datetime := null + end as prev_encounter_datetime_lung_cancer_treatment, + + @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, + + case + when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime + else @prev_clinical_datetime := null + end as prev_clinical_datetime_lung_cancer_treatment, + + case + when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id + else @prev_clinical_location_id := null + end as prev_clinical_location_id_lung_cancer_treatment, + + case + when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime + when @prev_id = @cur_id then @cur_clinical_datetime + else @cur_clinical_datetime := null + end as cur_clinical_datetime, + + case + when is_clinical_encounter then @cur_clinical_location_id := location_id + when @prev_id = @cur_id then @cur_clinical_location_id + else @cur_clinical_location_id := null + end as cur_clinical_location_id, + + case + when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date + else @prev_clinical_rtc_date := null + end as prev_clinical_rtc_date_lung_cancer_treatment, + + case + when @prev_id = @cur_id then @cur_clinical_rtc_date + else @cur_clinical_rtc_date:= null + end as cur_clinic_rtc_date + + from flat_lung_cancer_treatment_2 t1 + order by person_id, date(encounter_datetime), encounter_type_sort_index + ); + + SELECT + COUNT(*) + INTO @new_encounter_rows FROM + flat_lung_cancer_treatment_3; - - - - insert into flat_lung_cancer_treatment_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, #t1.location_id, - t1.obs, - null, #obs_datetimes - # in any visit, there many be multiple encounters. for this dataset, we want to include only clinical encounters (e.g. not lab or triage visit) - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_lung_cancer_treatment_build_queue__0 t0 using (person_id) - ); - - - drop temporary table if exists flat_lung_cancer_treatment_0; - create temporary table flat_lung_cancer_treatment_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_lung_cancer_treatment_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - set @cur_visit_type = null; - set @referred_from = null; - set @facility_name = null; - set @referral_date = null; - set @referred_by = null; - set @marital_status = null; - set @main_occupation = null; - set @patient_level_education = null; - set @smoke_cigarettes = null; - set @number_of_sticks = null; - set @cigga_smoke_duration = null; - set @duration_since_last_use = null; - set @tobacco_use = null; - set @tobacco_use_duration = null; - set @drink_alcohol = null; - set @chemical_exposure = null; - set @asbestos_exposure = null; - set @any_family_mem_with_cancer = null; - set @family_member = null; - set @chief_complaints = null; - set @complain_duration = null; - set @number_days = null; - set @number_weeks = null; - set @number_months = null; - set @number_years = null; - set @ecog_performance = null; - set @general_exam = null; - set @heent = null; - set @chest = null; - set @heart = null; - set @abdomen_exam = null; - set @urogenital = null; - set @extremities = null; - set @testicular_exam = null; - set @nodal_survey = null; - set @musculo_skeletal = null; - set @neurologic = null; - set @previous_tb_treatment = null; - set @ever_diagnosed = null; - set @disease_diagnosis_year = null; - set @hiv_status = null; - set @chest_xray_result= null; - set @chest_ct_scan_results = null; - set @pet_scan_results = null; - set @abdominal_ultrasound_result = null; - set @other_imaging_results = null; - set @test_ordered = null; - set @other_radiology = null; - set @other_laboratory = null; - set @procedure_ordered= null; - set @procedure_performed = null; - set @other_procedures = null; - set @procedure_date = null; - set @lung_cancer_type = null; - set @small_cell_lung_ca_staging = null; - set @non_small_cell_ca_type = null; - set @diagnosis_date = null; - set @staging = null; - set @cancer_staging_date= null; - set @metastasis_region = null; - set @treatment_plan = null; - set @other_treatment_plan = null; - set @radiotherapy_sessions= null; - set @surgery_date = null; - set @chemotherapy_plan = null; - set @chemo_start_date = null; - set @treatment_intent = null; - set @chemo_regimen = null; - set @chemo_cycle = null; - set @total_planned_chemo_cycle = null; - set @chemo_drug = null; - set @dosage_in_milligrams = null; - set @drug_route = null; - set @other_drugs = null; - set @purpose = null; - set @referral_ordered = null; - set @rtc = null; - - - drop temporary table if exists flat_lung_cancer_treatment_1; - create temporary table flat_lung_cancer_treatment_1 #(index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - t1.encounter_id, - t1.encounter_type, - t1.encounter_datetime, - t1.visit_id, - #t4.name as location_name, - t1.location_id, - t1.is_clinical_encounter, - p.gender, - p.death_date, - + SELECT @new_encounter_rows; + set @total_rows_written = @total_rows_written + @new_encounter_rows; + SELECT @total_rows_written; + + SET @dyn_sql=CONCAT('replace into ',@write_table, + '(select + null, + person_id, + encounter_id, + encounter_type, + encounter_datetime, + visit_id, + location_id, + t2.uuid as location_uuid, + gender, + age, + birthdate, + death_date, + cur_visit_type, + referred_from, + facility_name, + referral_date, + referred_by, + marital_status, + main_occupation, + patient_level_education, + smoke_cigarettes, + number_of_sticks, + cigga_smoke_duration, + duration_since_last_use, + tobacco_use, + tobacco_use_duration, + drink_alcohol, + chemical_exposure, + asbestos_exposure, + any_family_mem_with_cancer, + family_member, + chief_complaints, + complain_duration, + number_days, + number_weeks, + number_months, + number_years, + ecog_performance, + general_exam, + heent, + chest, + heart, + abdomen_exam, + urogenital, + extremities, + testicular_exam, + nodal_survey, + musculo_skeletal, + neurologic, + previous_tb_treatment, + ever_diagnosed, + year_of_diagnosis, + date_of_diagnosis, + hiv_status, + chest_xray_result, + chest_ct_scan_results, + pet_scan_results, + abdominal_ultrasound_result, + other_imaging_results, + test_ordered, + other_radiology, + other_laboratory, + procedure_ordered, + procedure_performed, + other_procedures, + procedure_date, + lung_cancer_type, + small_cell_lung_ca_staging, + non_small_cell_ca_type, + staging, + cancer_staging_date, + metastasis_region, + treatment_plan, + other_treatment_plan, + radiotherapy_sessions, + surgery_date, + chemotherapy_plan, + chemo_start_date, + chemo_stop_date, + treatment_intent, + chemo_regimen, + chemo_cycle, + total_planned_chemo_cycle, + chemo_drug, + dosage_in_milligrams, + drug_route, + other_drugs, + purpose, + referral_ordered, + rtc, + prev_encounter_datetime_lung_cancer_treatment, + next_encounter_datetime_lung_cancer_treatment, + prev_encounter_type_lung_cancer_treatment, + next_encounter_type_lung_cancer_treatment, + prev_clinical_datetime_lung_cancer_treatment, + next_clinical_datetime_lung_cancer_treatment, + prev_clinical_location_id_lung_cancer_treatment, + next_clinical_location_id_lung_cancer_treatment, + prev_clinical_rtc_date_lung_cancer_treatment, + next_clinical_rtc_date_lung_cancer_treatment + + from flat_lung_cancer_treatment_3 t1 + join amrs.location t2 using (location_id))'); + + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_lung_cancer_treatment_build_queue__0 t2 using (person_id);'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; - case - when timestampdiff(year,birthdate,curdate()) > 0 then round(timestampdiff(year,birthdate,curdate()),0) - else round(timestampdiff(month,birthdate,curdate())/12,2) - end as age, - case - when obs regexp "!!1839=7037!!" then @cur_visit_type := 1 - when obs regexp "!!1839=7875!!" then @cur_visit_type := 2 - else @cur_visit_type := null - end as cur_visit_type, - case - when obs regexp "!!6749=8161!!" then @referred_from := 1 - when obs regexp "!!6749=5487!!" then @referred_from := 2 - when obs regexp "!!6749=1275!!" then @referred_from := 3 - when obs regexp "!!6749=2242!!" then @referred_from := 4 - when obs regexp "!!6749=6572!!" then @referred_from := 5 - when obs regexp "!!6749=978!!" then @referred_from := 6 - when obs regexp "!!6749=5622!!" then @referred_from := 7 - else @referred_from := null - end as referred_from, - case - when obs regexp "!!6748=" then @facility_name = GetValues(obs,6748) - else @facility_name = null - end as facility_name, - case - when obs regexp "!!9158=" then @referral_date = GetValues(obs,9158) - else @referral_date = null - end as referral_date, - case - when obs regexp "!!10135=1496!!" then @referred_by := 1 - when obs regexp "!!10135=6280!!" then @referred_by := 2 - when obs regexp "!!10135=5507!!" then @referred_by := 3 - when obs regexp "!!10135=5622!!" then @referred_by := 4 - else @referred_by := null - end as referred_by, - case - when obs regexp "!!1054=1175!!" then @marital_status := 1 - when obs regexp "!!1054=1057!!" then @marital_status := 2 - when obs regexp "!!1054=5555!!" then @marital_status := 3 - when obs regexp "!!1054=6290!!" then @marital_status := 4 - when obs regexp "!!1054=1060!!" then @marital_status := 5 - when obs regexp "!!1054=1056!!" then @marital_status := 6 - when obs regexp "!!1054=1058!!" then @marital_status := 7 - when obs regexp "!!1054=1059!!" then @marital_status := 8 - else @marital_status := null - end as marital_status, - case - when obs regexp "!!1972=1967!!" then @main_occupation := 1 - when obs regexp "!!1972=6401!!" then @main_occupation := 2 - when obs regexp "!!1972=6408!!" then @main_occupation := 3 - when obs regexp "!!1972=6966!!" then @main_occupation := 4 - when obs regexp "!!1972=1969!!" then @main_occupation := 5 - when obs regexp "!!1972=1968!!" then @main_occupation := 6 - when obs regexp "!!1972=1966!!" then @main_occupation := 7 - when obs regexp "!!1972=8407!!" then @main_occupation := 8 - when obs regexp "!!1972=6284!!" then @main_occupation := 9 - when obs regexp "!!1972=8589!!" then @main_occupation := 10 - when obs regexp "!!1972=5619!!" then @main_occupation := 11 - when obs regexp "!!1972=5622!!" then @main_occupation := 12 - else @main_occupation = null - end as main_occupation, - case - when obs regexp "!!1605=1107!!" then @patient_level_education := 1 - when obs regexp "!!1605=6214!!" then @patient_level_education := 2 - when obs regexp "!!1605=6215!!" then @patient_level_education := 3 - when obs regexp "!!1605=1604!!" then @patient_level_education := 4 - else @patient_level_education := null - end as patient_level_education, - - case - when obs regexp "!!6473=1065!!" then @smoke_cigarettes := 1 - when obs regexp "!!6473=1066!!" then @smoke_cigarettes := 2 - when obs regexp "!!6473=1679!!" then @smoke_cigarettes := 3 - else @smoke_cigarettes := null - end as smoke_cigarettes, - case - when obs regexp "!!2069=" then @number_of_sticks = GetValues(obs,2069) - else @number_of_sticks = null - end as number_of_sticks, - case - when obs regexp "!!2070=" then @cigga_smoke_duration = GetValues(obs,2070) - else @cigga_smoke_duration = null - end as cigga_smoke_duration, - case - when obs regexp "!!2068=" then @duration_since_last_use = GetValues(obs,2068) - else @duration_since_last_use = null - end as duration_since_last_use, - case - when obs regexp "!!7973=1065!!" then @tobacco_use := 1 - when obs regexp "!!7973=1066!!" then @tobacco_use := 2 - when obs regexp "!!7973=1679!!" then @tobacco_use := 3 - else @tobacco_use = null - end as tobacco_use, - case - when obs regexp "!!8144=" then @tobacco_use_duration = GetValues(obs,8144) - else @tobacco_use_duration = null - end as tobacco_use_duration, - case - when obs regexp "!!1684=1065!!" then @drink_alcohol := 1 - when obs regexp "!!1684=1066!!" then @drink_alcohol := 2 - when obs regexp "!!1684=1679!!" then @drink_alcohol := 3 - else @drink_alcohol := null - end as drink_alcohol, - case - when obs regexp "!!10310=1107!!" then @chemical_exposure := 1 - when obs regexp "!!10310=10308!!" then @chemical_exposure := 2 - when obs regexp "!!10310=10370!!" then @chemical_exposure := 3 - when obs regexp "!!10310=10309!!" then @chemical_exposure := 4 - when obs regexp "!!10310=5622!!" then @chemical_exposure := 5 - else @chemical_exposure = null - end as chemical_exposure, - case - when obs regexp "!!10312=1065!!" then @asbestos_exposure := 1 - when obs regexp "!!10312=1066!!" then @asbestos_exposure := 2 - else @asbestos_exposure = null - end as asbestos_exposure, - - case - when obs regexp "!!6802=1065!!" then @any_family_mem_with_cancer := 1 - when obs regexp "!!6802=1067!!" then @any_family_mem_with_cancer := 2 - else @any_family_mem_with_cancer = null - end as any_family_mem_with_cancer, - case - when obs regexp "!!9635=1107!!" then @family_member := 1 - when obs regexp "!!9635=978!!" then @family_member := 2 - when obs regexp "!!9635=1692!!" then @family_member := 3 - when obs regexp "!!9635=972!!" then @family_member := 4 - when obs regexp "!!9635=1671!!" then @family_member := 5 - when obs regexp "!!9635=1393!!" then @family_member := 6 - when obs regexp "!!9635=1392!!" then @family_member := 7 - when obs regexp "!!9635=1395!!" then @family_member := 8 - when obs regexp "!!9635=1394!!" then @family_member := 9 - when obs regexp "!!9635=1673!!" then @family_member := 10 - else @family_member := null - end as family_member, - case - when obs regexp "!!5219=136!!" then @chief_complaints := 1 - when obs regexp "!!5219=107!!" then @chief_complaints := 2 - when obs regexp "!!5219=6786!!" then @chief_complaints := 3 - when obs regexp "!!5219=5954!!" then @chief_complaints := 4 - when obs regexp "!!5219=832!!" then @chief_complaints := 5 - when obs regexp "!!5219=5960!!" then @chief_complaints := 6 - when obs regexp "!!5219=10121!!" then @chief_complaints := 7 - when obs regexp "!!5219=5622!!" then @chief_complaints := 8 - else @chief_complaints := null - end as chief_complaints, - case - when obs regexp "!!8777=1072!!" then @complain_duration := 1 - when obs regexp "!!8777=1073!!" then @complain_duration := 2 - when obs regexp "!!8777=1074!!" then @complain_duration := 3 - when obs regexp "!!8777=8787!!" then @complain_duration := 4 - else @complain_duration = null - end as complain_duration, - case - when obs regexp "!!1892=" then @number_days = GetValues(obs,1892) - else @number_days = null - end as number_days, - case - when obs regexp "!!1893=" then @number_weeks = GetValues(obs,1893) - else @number_weeks = null - end as number_weeks, - case - when obs regexp "!!1894=" then @number_months = GetValues(obs,1894) - else @number_months = null - end as number_months, - case - when obs regexp "!!7953=" then @number_years = GetValues(obs,7953) - else @number_years = null - end as number_years, - case - when obs regexp "!!6584=1115!!" then @ecog_performance := 1 - when obs regexp "!!6584=6585!!" then @ecog_performance := 2 - when obs regexp "!!6584=6586!!" then @ecog_performance := 3 - when obs regexp "!!6584=6587!!" then @ecog_performance := 4 - when obs regexp "!!6584=6588!!" then @ecog_performance := 5 - else @ecog_performance = null - end as ecog_performance, - case - when obs regexp "!!1119=1115!!" then @general_exam := 1 - when obs regexp "!!1119=5201!!" then @general_exam := 2 - when obs regexp "!!1119=5245!!" then @general_exam := 3 - when obs regexp "!!1119=215!!" then @general_exam := 4 - when obs regexp "!!1119=589!!" then @general_exam := 5 - else @general_exam = null - end as general_exam, - case - when obs regexp "!!1122=1118!!" then @heent := 1 - when obs regexp "!!1122=1115!!" then @heent := 2 - when obs regexp "!!1122=5192!!" then @heent := 3 - when obs regexp "!!1122=516!!" then @heent := 4 - when obs regexp "!!1122=513!!" then @heent := 5 - when obs regexp "!!1122=5170!!" then @heent := 6 - when obs regexp "!!1122=5334!!" then @heent := 7 - when obs regexp "!!1122=6672!!" then @heent := 8 - else @heent = null - end as heent, - case - when obs regexp "!!1123=1118!!" then @chest := 1 - when obs regexp "!!1123=1115!!" then @chest := 2 - when obs regexp "!!1123=5138!!" then @chest := 3 - when obs regexp "!!1123=5115!!" then @chest := 4 - when obs regexp "!!1123=5116!!" then @chest := 5 - when obs regexp "!!1123=5181!!" then @chest := 6 - when obs regexp "!!1123=5127!!" then @chest := 7 - else @chest = null - end as chest, - case - when obs regexp "!!1124=1118!!" then @heart := 1 - when obs regexp "!!1124=1115!!" then @heart := 2 - when obs regexp "!!1124=1117!!" then @heart := 3 - when obs regexp "!!1124=550!!" then @heart := 4 - when obs regexp "!!1124=5176!!" then @heart := 5 - when obs regexp "!!1124=5162!!" then @heart := 6 - when obs regexp "!!1124=5164!!" then @heart := 7 - else @heart = null - end as heart, - case - when obs regexp "!!1125=1118!!" then @abdomen_exam := 1 - when obs regexp "!!1125=1115!!" then @abdomen_exam := 2 - when obs regexp "!!1125=5105!!" then @abdomen_exam := 3 - when obs regexp "!!1125=5008!!" then @abdomen_exam := 4 - when obs regexp "!!1125=5009!!" then @abdomen_exam := 5 - when obs regexp "!!1125=581!!" then @abdomen_exam := 6 - when obs regexp "!!1125=5103!!" then @abdomen_exam := 7 - else @abdomen_exam = null - end as abdomen_exam, - case - when obs regexp "!!1126=1118!!" then @urogenital := 1 - when obs regexp "!!1126=1115!!" then @urogenital := 2 - when obs regexp "!!1126=1116!!" then @urogenital := 3 - when obs regexp "!!1126=2186!!" then @urogenital := 4 - when obs regexp "!!1126=864!!" then @urogenital := 5 - when obs regexp "!!1126=6334!!" then @urogenital := 6 - when obs regexp "!!1126=1447!!" then @urogenital := 7 - when obs regexp "!!1126=5993!!" then @urogenital := 8 - when obs regexp "!!1126=8998!!" then @urogenital := 9 - when obs regexp "!!1126=1489!!" then @urogenital := 10 - when obs regexp "!!1126=8417!!" then @urogenital := 11 - when obs regexp "!!1126=8261!!" then @urogenital := 12 - else @urogenital = null - end as urogenital, - case - when obs regexp "!!1127=1118!!" then @extremities := 1 - when obs regexp "!!1127=1115!!" then @extremities := 2 - when obs regexp "!!1127=590!!" then @extremities := 3 - when obs regexp "!!1127=7293!!" then @extremities := 4 - when obs regexp "!!1127=134!!" then @extremities := 5 - else @extremities = null - end as extremities, - case - when obs regexp "!!8420=1118!!" then @testicular_exam := 1 - when obs regexp "!!8420=1115!!" then @testicular_exam := 2 - when obs regexp "!!8420=1116!!" then @testicular_exam := 3 - else @testicular_exam = null - end as testicular_exam, - case - when obs regexp "!!1121=1118!!" then @nodal_survey := 1 - when obs regexp "!!1121=1115!!" then @nodal_survey := 2 - when obs regexp "!!1121=1116!!" then @nodal_survey := 3 - when obs regexp "!!1121=8261!!" then @nodal_survey := 4 - else @nodal_survey = null - end as nodal_survey, - case - when obs regexp "!!1128=1118!!" then @musculo_skeletal := 1 - when obs regexp "!!1128=1115!!" then @musculo_skeletal := 2 - when obs regexp "!!1128=1116!!" then @musculo_skeletal := 3 - else @musculo_skeletal = null - end as musculo_skeletal, - case - when obs regexp "!!1129=1118!!" then @neurologic := 1 - when obs regexp "!!1129=1115!!" then @neurologic := 2 - when obs regexp "!!1129=599!!" then @neurologic := 3 - when obs regexp "!!1129=497!!" then @neurologic := 4 - when obs regexp "!!1129=5108!!" then @neurologic := 5 - else @neurologic = null - end as neurologic, - case - when obs regexp "!!5704=" then @previous_tb_treatment = GetValues(obs,5704) - else @previous_tb_treatment = null - end as previous_tb_treatment, - case - when obs regexp "!!6245=142!!" then @ever_diagnosed := 1 - when obs regexp "!!6245=5!!" then @ever_diagnosed := 2 - when obs regexp "!!6245=6237!!" then @ever_diagnosed := 3 - when obs regexp "!!6245=903!!" then @ever_diagnosed := 4 - when obs regexp "!!6245=6033!!" then @ever_diagnosed := 5 - when obs regexp "!!6245=43!!" then @ever_diagnosed := 6 - when obs regexp "!!6245=58!!" then @ever_diagnosed := 7 - else @ever_diagnosed = null - end as ever_diagnosed, - case - when obs regexp "!!9281=" then @disease_diagnosis_year = GetValues(obs,9281) - else @disease_diagnosis_year = null - end as disease_diagnosis_year, - case - when obs regexp "!!6709=1067!!" then @hiv_status := 1 - when obs regexp "!!6709=664!!" then @hiv_status := 2 - when obs regexp "!!6709=703!!" then @hiv_status := 3 - else @hiv_status = null - end as hiv_status, - case - when obs regexp "!!12=1115!!" then @chest_xray_result := 1 - when obs regexp "!!12=1116!!" then @chest_xray_result := 2 - else @chest_xray_result = null - end as chest_xray_result, - case - when obs regexp "!!7113=1115!!" then @chest_ct_scan_results := 1 - when obs regexp "!!7113=1117!!" then @chest_ct_scan_results := 2 - else @chest_ct_scan_results = null - end as chest_ct_scan_results, - case - when obs regexp "!!10123=1115!!" then @pet_scan_results := 1 - when obs regexp "!!10123=1118!!" then @pet_scan_results := 2 - else @pet_scan_results = null - end as pet_scan_results, - case - when obs regexp "!!845=1115!!" then @abdominal_ultrasound_result := 1 - when obs regexp "!!845=1119!!" then @abdominal_ultrasound_result := 2 - else @abdominal_ultrasound_result = null - end as abdominal_ultrasound_result, - case - when obs regexp "!!10124=" then @other_imaging_results = GetValues(obs,10124) - else @other_imaging_results = null - end as other_imaging_results, - - case - when obs regexp "!!1271=1107!!" then @test_ordered := 1 - when obs regexp "!!1271=1019!!" then @test_ordered := 2 - when obs regexp "!!1271=790!!" then @test_ordered := 3 - when obs regexp "!!1271=953!!" then @test_ordered := 4 - when obs regexp "!!1271=6898!!" then @test_ordered := 5 - when obs regexp "!!1271=9009!!" then @test_ordered := 6 - else @test_ordered = null - end as test_ordered, - case - when obs regexp "!!8190=" then @other_radiology = GetValues(obs,8190) - else @other_radiology = null - end as other_radiology, - case - when obs regexp "!!9538=" then @other_laboratory = GetValues(obs,9538) - else @other_laboratory = null - end as other_laboratory, - case - when obs regexp "!!10127=1107!!" then @procedure_ordered := 1 - when obs regexp "!!10127=10075!!" then @procedure_ordered := 2 - when obs regexp "!!10127=10076!!" then @procedure_ordered := 3 - when obs regexp "!!10127=10126!!" then @procedure_ordered := 4 - when obs regexp "!!10127=5622!!" then @procedure_ordered := 5 - else @procedure_ordered = null - end as procedure_ordered, - case - when obs regexp "!!10442=1107!!" then @procedure_performed := 1 - when obs regexp "!!10442=10075!!" then @procedure_performed := 2 - when obs regexp "!!10442=10076!!" then @procedure_performed := 3 - when obs regexp "!!10442=10126!!" then @procedure_performed := 4 - when obs regexp "!!10442=5622!!" then @procedure_performed := 5 - else @procedure_performed = null - end as procedure_performed, - case - when obs regexp "!!1915=" then @other_procedures = GetValues(obs,1915) - else @other_procedures = null - end as other_procedures, - case - when obs regexp "!!10443=" then @procedure_date = GetValues(obs,10443) - else @procedure_date = null - end as procedure_date, + SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; - case - when obs regexp "!!7176=10129!!" then @lung_cancer_type := 1 - when obs regexp "!!7176=10130!!" then @lung_cancer_type := 2 - when obs regexp "!!7176=5622!!" then @lung_cancer_type := 3 - else @lung_cancer_type = null - end as lung_cancer_type, - case - when obs regexp "!!10137=6563!!" then @small_cell_lung_ca_staging := 1 - when obs regexp "!!10137=6564!!" then @small_cell_lung_ca_staging := 2 - else @small_cell_lung_ca_staging = null - end as small_cell_lung_ca_staging, - case - when obs regexp "!!10132=7421!!" then @non_small_cell_ca_type := 1 - when obs regexp "!!10132=7422!!" then @non_small_cell_ca_type := 2 - when obs regexp "!!10132=10131!!" then @non_small_cell_ca_type := 3 - when obs regexp "!!10132=10209!!" then @non_small_cell_ca_type := 3 - else @non_small_cell_ca_type = null - end as non_small_cell_ca_type, - case - when obs regexp "!!9728=" then @diagnosis_date = GetValues(obs,9728) - else @diagnosis_date = null - end as diagnosis_date, - case - when obs regexp "!!9868=9852!!" then @staging := 1 - when obs regexp "!!9868=9856!!" then @staging := 2 - when obs regexp "!!9868=9860!!" then @staging := 3 - when obs regexp "!!9868=9864!!" then @staging := 4 - when obs regexp "!!9868=6563!!" then @staging := 5 - when obs regexp "!!9868=6564!!" then @staging := 6 - else @staging = null - end as staging, - case - when obs regexp "!!10441=" then @cancer_staging_date = GetValues(obs,10441) - else @cancer_staging_date = null - end as cancer_staging_date, - case - when obs regexp "!!10445=" then @metastasis_region = GetValues(obs,10445) - else @metastasis_region = null - end as metastasis_region, - case - when obs regexp "!!8723=6575!!" then @treatment_plan := 1 - when obs regexp "!!8723=8427!!" then @treatment_plan := 2 - when obs regexp "!!8723=7465!!" then @treatment_plan := 3 - when obs regexp "!!8723=5622!!" then @treatment_plan := 4 - else @treatment_plan = null - end as treatment_plan, - case - when obs regexp "!!10039=" then @other_treatment_plan = GetValues(obs,10039) - else @other_treatment_plan = null - end as other_treatment_plan, - case - when obs regexp "!!10133=" then @radiotherapy_sessions = GetValues(obs,10133) - else @radiotherapy_sessions = null - end as radiotherapy_sessions, - case - when obs regexp "!!10134=" then @surgery_date = GetValues(obs,10134) - else @surgery_date = null - end as surgery_date, - case - when obs regexp "!!9869=1256!!" then @chemotherapy_plan := 1 - when obs regexp "!!9869=1259!!" then @chemotherapy_plan := 2 - when obs regexp "!!9869=1260!!" then @chemotherapy_plan := 3 - when obs regexp "!!9869=1257!!" then @chemotherapy_plan := 4 - else @chemotherapy_plan = null - end as chemotherapy_plan, - case - when obs regexp "!!1190=" then @chemo_start_date = GetValues(obs,1190) - else @chemo_start_date = null - end as chemo_start_date, - case - when obs regexp "!!2206=9218!!" then @treatment_intent := 1 - when obs regexp "!!2206=8428!!" then @treatment_intent := 2 - when obs regexp "!!2206=8724!!" then @treatment_intent := 3 - when obs regexp "!!2206=9219!!" then @treatment_intent := 4 - else @treatment_intent = null - end as treatment_intent, - case - when obs regexp "!!9946=" then @chemo_regimen = GetValues(obs,9946) - else @chemo_regimen = null - end as chemo_regimen, - case - when obs regexp "!!6643=" then @chemo_cycle = GetValues(obs,6643) - else @chemo_cycle = null - end as chemo_cycle, - case - when obs regexp "!!6644=" then @total_planned_chemo_cycle = GetValues(obs,6644) - else @total_planned_chemo_cycle = null - end as total_planned_chemo_cycle, - case - when obs regexp "!!9918=7202!!" then @chemo_drug := 1 - when obs regexp "!!9918=8506!!" then @chemo_drug := 2 - when obs regexp "!!9918=7210!!" then @chemo_drug := 3 - when obs regexp "!!9918=8492!!" then @chemo_drug := 4 - when obs regexp "!!9918=8507!!" then @chemo_drug := 5 - else @chemo_drug = null - end as chemo_drug, - case - when obs regexp "!!1899=" then @dosage_in_milligrams = GetValues(obs,1899) - else @dosage_in_milligrams = null - end as dosage_in_milligrams, - case - when obs regexp "!!7463=7458!!" then @drug_route := 1 - when obs regexp "!!7463=10078!!" then @drug_route := 2 - when obs regexp "!!7463=10079!!" then @drug_route := 3 - when obs regexp "!!7463=7597!!" then @drug_route := 4 - when obs regexp "!!7463=7581!!" then @drug_route := 5 - when obs regexp "!!7463=7609!!" then @drug_route := 6 - when obs regexp "!!7463=7616!!" then @drug_route := 7 - when obs regexp "!!7463=7447!!" then @drug_route := 8 - else @drug_route = null - end as drug_route, - case - when obs regexp "!!1895=" then @other_drugs = GetValues(obs,1895) - else @other_drugs = null - end as other_drugs, - case - when obs regexp "!!2206=9220!!" then @purpose := 1 - when obs regexp "!!2206=8428!!" then @purpose := 2 - else @purpose = null - end as purpose, - case - when obs regexp "!!1272=1107!!" then @referral_ordered := 1 - when obs regexp "!!1272=8724!!" then @referral_ordered := 2 - when obs regexp "!!1272=5486!!" then @referral_ordered := 3 - when obs regexp "!!1272=5884!!" then @referral_ordered := 4 - when obs regexp "!!1272=6570!!" then @referral_ordered := 5 - when obs regexp "!!1272=5622!!" then @referral_ordered := 6 - else @referral_ordered = null - end as referral_ordered, - case - when obs regexp "!!5096=" then @rtc = GetValues(obs,5096) - else @rtc = null - end as rtc - - from flat_lung_cancer_treatment_0 t1 - join amrs.person p using (person_id) - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - );# limit 100; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_lung_cancer_treatment_1 drop prev_id, drop cur_id; - - drop table if exists flat_lung_cancer_treatment_2; - create temporary table flat_lung_cancer_treatment_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_lung_cancer_treatment, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_lung_cancer_treatment, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_lung_cancer_treatment, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id_lung_cancer_treatment, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_lung_cancer_treatment, - - case - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date - - from flat_lung_cancer_treatment_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_lung_cancer_treatment_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_lung_cancer_treatment_3; - create temporary table flat_lung_cancer_treatment_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_lung_cancer_treatment, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_lung_cancer_treatment, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_lung_cancer_treatment, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id_lung_cancer_treatment, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_lung_cancer_treatment, - - case - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_lung_cancer_treatment_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - - SELECT - COUNT(*) -INTO @new_encounter_rows FROM - flat_lung_cancer_treatment_3; - -SELECT @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; -SELECT @total_rows_written; - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - encounter_id, - encounter_type, - encounter_datetime, - visit_id, - location_id, - t2.uuid as location_uuid, - gender, - age, - cur_visit_type, - referred_from , - facility_name , - referral_date , - referred_by , - marital_status , - main_occupation, - patient_level_education , - smoke_cigarettes , - number_of_sticks , - cigga_smoke_duration, - duration_since_last_use, - tobacco_use, - tobacco_use_duration, - drink_alcohol , - chemical_exposure, - asbestos_exposure , - any_family_mem_with_cancer , - family_member , - chief_complaints , - complain_duration , - number_days , - number_weeks , - number_months , - number_years , - ecog_performance , - general_exam , - heent , - chest , - heart , - abdomen_exam , - urogenital , - extremities , - testicular_exam , - nodal_survey , - musculo_skeletal , - neurologic , - previous_tb_treatment , - ever_diagnosed , - disease_diagnosis_year , - hiv_status, - chest_xray_result, - chest_ct_scan_results , - pet_scan_results , - abdominal_ultrasound_result , - other_imaging_results , - test_ordered, - other_radiology , - other_laboratory , - procedure_ordered , - procedure_performed, - other_procedures, - procedure_date, - lung_cancer_type, - small_cell_lung_ca_staging , - non_small_cell_ca_type , - diagnosis_date , - staging , - cancer_staging_date, - metastasis_region, - treatment_plan , - other_treatment_plan , - radiotherapy_sessions , - surgery_date , - chemotherapy_plan , - chemo_start_date , - treatment_intent , - chemo_regimen , - chemo_cycle , - total_planned_chemo_cycle , - chemo_drug , - dosage_in_milligrams , - drug_route , - other_drugs , - purpose , - referral_ordered , - rtc, - - prev_encounter_datetime_lung_cancer_treatment, - next_encounter_datetime_lung_cancer_treatment, - prev_encounter_type_lung_cancer_treatment, - next_encounter_type_lung_cancer_treatment, - prev_clinical_datetime_lung_cancer_treatment, - next_clinical_datetime_lung_cancer_treatment, - prev_clinical_location_id_lung_cancer_treatment, - next_clinical_location_id_lung_cancer_treatment, - prev_clinical_rtc_date_lung_cancer_treatment, - next_clinical_rtc_date_lung_cancer_treatment - - from flat_lung_cancer_treatment_3 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_lung_cancer_treatment_build_queue__0 t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #select @person_ids_count := (select count(*) from flat_lung_cancer_treatment_build_queue_2); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count as remaining_in_build_queue; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - #select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); -SELECT - @person_ids_count AS 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - -#select 1; - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; + set @cycle_length = timestampdiff(second,@loop_start_time,now()); + set @total_time = @total_time + @cycle_length; + set @cycle_number = @cycle_number + 1; + + set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); + + SELECT + @person_ids_count AS 'persons remaining', + @cycle_length AS 'Cycle time (s)', + CEIL(@person_ids_count / cycle_size) AS remaining_cycles, + @remaining_time AS 'Est time remaining (min)'; + + end while; + + if (@query_type="build") then + SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; + SET @total_rows_to_write=0; + SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; - set @start_write = now(); - SELECT - CONCAT(@start_write, - ' : Writing ', - @total_rows_to_write, - ' to ', - @primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); + set @start_write = now(); + SELECT + CONCAT(@start_write, + ' : Writing ', + @total_rows_to_write, + ' to ', + @primary_table); + + SET @dyn_sql=CONCAT('replace into ', @primary_table, + '(select * from ',@write_table,');'); PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); -SELECT - CONCAT(@finish_write, - ' : Completed writing rows. Time to write to primary table: ', - @time_to_write, - ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); -SELECT - CONCAT('Average Cycle Length: ', - @ave_cycle_length, - ' second(s)'); + EXECUTE s1; + DEALLOCATE PREPARE s1; + + set @finish_write = now(); + set @time_to_write = timestampdiff(second,@start_write,@finish_write); + + SELECT + CONCAT(@finish_write, + ' : Completed writing rows. Time to write to primary table: ', + @time_to_write, + ' seconds '); + + SET @dyn_sql=CONCAT('drop table ',@write_table,';'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + end if; - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - SELECT - CONCAT(@table_version, - ' : Time to complete: ', - TIMESTAMPDIFF(MINUTE, @start, @end), - ' minutes'); + set @ave_cycle_length = ceil(@total_time/@cycle_number); + + SELECT + CONCAT('Average Cycle Length: ', + @ave_cycle_length, + ' second(s)'); + + set @end = now(); + insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); + SELECT + CONCAT(@table_version, + ' : Time to complete: ', + TIMESTAMPDIFF(MINUTE, @start, @end), + ' minutes'); END$$ DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_lung_cancer_treatment_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_lung_cancer_treatment_v1_0.sql deleted file mode 100644 index 3a966e3..0000000 --- a/etl-scripts/stored-procedures/generate_flat_lung_cancer_treatment_v1_0.sql +++ /dev/null @@ -1,1256 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_lung_cancer_treatment_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - SET @primary_table := "flat_lung_cancer_treatment"; - SET @query_type := query_type; - - SET @total_rows_written := 0; - - SET @encounter_types := "(169,170)"; - SET @clinical_encounter_types := "(-1)"; - SET @non_clinical_encounter_types := "(-1)"; - SET @other_encounter_types := "(-1)"; - - SET @start := now(); - SET @table_version := "flat_lung_cancer_treatment_v1_0"; - - set session sort_buffer_size := 512000000; - - SET @sep := " ## "; - SET @boundary := "!!"; - SET @last_date_created := (select max(max_date_created) from etl.flat_obs); - - CREATE TABLE IF NOT EXISTS flat_lung_cancer_treatment ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id INT, - encounter_id INT, - encounter_type INT, - encounter_datetime DATETIME, - visit_id INT, - location_id INT, - location_uuid VARCHAR(100), - gender CHAR(100), - age INT, - birthdate DATE, - death_date DATE, - cur_visit_type INT, - referred_from INT, - facility_name VARCHAR(100), - referral_date DATETIME, - referred_by INT, - marital_status INT, - main_occupation INT, - patient_level_education INT, - smoke_cigarettes INT, - number_of_sticks INT, - cigga_smoke_duration INT, - duration_since_last_use INT, - tobacco_use INT, - tobacco_use_duration INT, - drink_alcohol INT, - chemical_exposure INT, - asbestos_exposure INT, - any_family_mem_with_cancer INT, - family_member INT, - chief_complaints INT, - complain_duration INT, - number_days INT, - number_weeks INT, - number_months INT, - number_years INT, - ecog_performance INT, - general_exam INT, - heent INT, - chest INT, - heart INT, - abdomen_exam INT, - urogenital INT, - extremities INT, - testicular_exam INT, - nodal_survey INT, - musculo_skeletal INT, - neurologic INT, - previous_tb_treatment INT, - ever_diagnosed INT, - year_of_diagnosis INT, - date_of_diagnosis DATE, - hiv_status INT, - chest_xray_result INT, - chest_ct_scan_results INT, - pet_scan_results INT, - abdominal_ultrasound_result INT, - other_imaging_results INT, - test_ordered INT, - other_radiology VARCHAR(500), - other_laboratory VARCHAR(500), - procedure_ordered INT, - procedure_performed INT, - other_procedures VARCHAR(500), - procedure_date DATETIME, - lung_cancer_type INT, - small_cell_lung_ca_staging INT, - non_small_cell_ca_type INT, - staging INT, - cancer_staging_date DATETIME, - metastasis_region VARCHAR(500), - treatment_plan INT, - other_treatment_plan INT, - radiotherapy_sessions INT, - surgery_date DATETIME, - chemotherapy_plan INT, - chemo_start_date DATETIME, - chemo_stop_date DATETIME, - treatment_intent INT, - chemo_regimen VARCHAR(500), - chemo_cycle INT, - total_planned_chemo_cycle INT, - chemo_drug INT, - dosage_in_milligrams INT, - drug_route INT, - other_drugs INT, - purpose INT, - referral_ordered INT, - rtc DATETIME, - prev_encounter_datetime_lung_cancer_treatment DATETIME, - next_encounter_datetime_lung_cancer_treatment DATETIME, - prev_encounter_type_lung_cancer_treatment MEDIUMINT, - next_encounter_type_lung_cancer_treatment MEDIUMINT, - prev_clinical_datetime_lung_cancer_treatment DATETIME, - next_clinical_datetime_lung_cancer_treatment DATETIME, - prev_clinical_location_id_lung_cancer_treatment MEDIUMINT, - next_clinical_location_id_lung_cancer_treatment MEDIUMINT, - prev_clinical_rtc_date_lung_cancer_treatment DATETIME, - next_clinical_rtc_date_lung_cancer_treatment DATETIME, - PRIMARY KEY encounter_id (encounter_id), - INDEX person_date (person_id, encounter_datetime), - INDEX location_enc_date (location_uuid, encounter_datetime), - INDEX enc_date_location (encounter_datetime, location_uuid), - INDEX loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_lung_cancer_treatment), - INDEX encounter_type (encounter_type), - INDEX date_created (date_created) - ); - - if (@query_type="build") then - select 'BUILDING..........................................'; - - set @write_table = concat("flat_lung_cancer_treatment_temp_",queue_number); - set @queue_table = concat("flat_lung_cancer_treatment_build_queue_",queue_number); - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_lung_cancer_treatment_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('delete t1 from flat_lung_cancer_treatment_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_lung_cancer_treatment"; - set @queue_table = "flat_lung_cancer_treatment_sync_queue"; - CREATE TABLE IF NOT EXISTS flat_lung_cancer_treatment_sync_queue ( - person_id INT PRIMARY KEY - ); - - set @last_update = null; - - SELECT - MAX(date_updated) - INTO @last_update FROM - etl.flat_log - WHERE - table_name = @table_version; - - SELECT 'Finding patients in amrs.encounters...'; - - replace into flat_lung_cancer_treatment_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - - SELECT 'Finding patients in flat_obs...'; - - replace into flat_lung_cancer_treatment_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - - SELECT 'Finding patients in flat_lab_obs...'; - replace into flat_lung_cancer_treatment_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - SELECT 'Finding patients in flat_orders...'; - replace into flat_lung_cancer_treatment_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_lung_cancer_treatment_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - replace into flat_lung_cancer_treatment_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - end if; - - -- Remove test patients - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SELECT @person_ids_count AS 'num patients to update'; - - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - while @person_ids_count > 0 do - set @loop_start_time = now(); - drop temporary table if exists flat_lung_cancer_treatment_build_queue__0; - - SET @dyn_sql=CONCAT('create temporary table flat_lung_cancer_treatment_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - drop temporary table if exists flat_lung_cancer_treatment_0a; - SET @dyn_sql = CONCAT( - 'create temporary table flat_lung_cancer_treatment_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - case - when t1.encounter_type in ',@clinical_encounter_types,' then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in ',@non_clinical_encounter_types,' then 20 - when t1.encounter_type in ',@clinical_encounter_types,' then 10 - when t1.encounter_type in', @other_encounter_types, ' then 5 - else 1 - end as encounter_type_sort_index, - t2.orders - from etl.flat_obs t1 - join flat_lung_cancer_treatment_build_queue__0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_id) - where t1.encounter_type in ',@encounter_types,');'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - insert into flat_lung_cancer_treatment_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, #t1.location_id, - t1.obs, - null, #obs_datetimes - # in any visit, there many be multiple encounters. for this dataset, we want to include only clinical encounters (e.g. not lab or triage visit) - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_lung_cancer_treatment_build_queue__0 t0 using (person_id) - ); - - - drop temporary table if exists flat_lung_cancer_treatment_0; - create temporary table flat_lung_cancer_treatment_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_lung_cancer_treatment_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - set @birthdate = null; - set @death_date = null; - set @cur_visit_type = null; - set @referred_from = null; - set @facility_name = null; - set @referral_date = null; - set @referred_by = null; - set @marital_status = null; - set @main_occupation = null; - set @patient_level_education = null; - set @smoke_cigarettes = null; - set @number_of_sticks = null; - set @cigga_smoke_duration = null; - set @duration_since_last_use = null; - set @tobacco_use = null; - set @tobacco_use_duration = null; - set @drink_alcohol = null; - set @chemical_exposure = null; - set @asbestos_exposure = null; - set @any_family_mem_with_cancer = null; - set @family_member = null; - set @chief_complaints = null; - set @complain_duration = null; - set @number_days = null; - set @number_weeks = null; - set @number_months = null; - set @number_years = null; - set @ecog_performance = null; - set @general_exam = null; - set @heent = null; - set @chest = null; - set @heart = null; - set @abdomen_exam = null; - set @urogenital = null; - set @extremities = null; - set @testicular_exam = null; - set @nodal_survey = null; - set @musculo_skeletal = null; - set @neurologic = null; - set @previous_tb_treatment = null; - set @ever_diagnosed = null; - set @year_of_diagnosis = null; - set @date_of_diagnosis = null; - set @hiv_status = null; - set @chest_xray_result= null; - set @chest_ct_scan_results = null; - set @pet_scan_results = null; - set @abdominal_ultrasound_result = null; - set @other_imaging_results = null; - set @test_ordered = null; - set @other_radiology = null; - set @other_laboratory = null; - set @procedure_ordered= null; - set @procedure_performed = null; - set @other_procedures = null; - set @procedure_date = null; - set @lung_cancer_type = null; - set @small_cell_lung_ca_staging = null; - set @non_small_cell_ca_type = null; - set @staging = null; - set @cancer_staging_date= null; - set @metastasis_region = null; - set @treatment_plan = null; - set @other_treatment_plan = null; - set @radiotherapy_sessions= null; - set @surgery_date = null; - set @chemotherapy_plan = null; - set @chemo_start_date = null; - set @chemo_stop_date = null; - set @treatment_intent = null; - set @chemo_regimen = null; - set @chemo_cycle = null; - set @total_planned_chemo_cycle = null; - set @chemo_drug = null; - set @dosage_in_milligrams = null; - set @drug_route = null; - set @other_drugs = null; - set @purpose = null; - set @referral_ordered = null; - set @rtc = null; - - drop temporary table if exists flat_lung_cancer_treatment_1; - create temporary table flat_lung_cancer_treatment_1 #(index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - t1.encounter_id, - t1.encounter_type, - t1.encounter_datetime, - t1.visit_id, - t1.location_id, - t1.is_clinical_encounter, - p.gender, - p.birthdate, - p.death_date, - case - when timestampdiff(year,birthdate,curdate()) > 0 then round(timestampdiff(year,birthdate,curdate()),0) - else round(timestampdiff(month,birthdate,curdate())/12,2) - end as age, - case - when obs regexp "!!1839=7037!!" then @cur_visit_type := 1 - when obs regexp "!!1839=7875!!" then @cur_visit_type := 2 - else @cur_visit_type := null - end as cur_visit_type, - case - when obs regexp "!!6749=8161!!" then @referred_from := 1 - when obs regexp "!!6749=5487!!" then @referred_from := 2 - when obs regexp "!!6749=1275!!" then @referred_from := 3 - when obs regexp "!!6749=2242!!" then @referred_from := 4 - when obs regexp "!!6749=6572!!" then @referred_from := 5 - when obs regexp "!!6749=978!!" then @referred_from := 6 - when obs regexp "!!6749=5622!!" then @referred_from := 7 - else @referred_from := null - end as referred_from, - case - when obs regexp "!!6748=" then @facility_name := GetValues(obs,6748) - else @facility_name = null - end as facility_name, - case - when obs regexp "!!9158=" then @referral_date := GetValues(obs,9158) - else @referral_date = null - end as referral_date, - case - when obs regexp "!!10135=1496!!" then @referred_by := 1 - when obs regexp "!!10135=6280!!" then @referred_by := 2 - when obs regexp "!!10135=5507!!" then @referred_by := 3 - when obs regexp "!!10135=5622!!" then @referred_by := 4 - else @referred_by := null - end as referred_by, - case - when obs regexp "!!1054=1175!!" then @marital_status := 1 - when obs regexp "!!1054=1057!!" then @marital_status := 2 - when obs regexp "!!1054=5555!!" then @marital_status := 3 - when obs regexp "!!1054=6290!!" then @marital_status := 4 - when obs regexp "!!1054=1060!!" then @marital_status := 5 - when obs regexp "!!1054=1056!!" then @marital_status := 6 - when obs regexp "!!1054=1058!!" then @marital_status := 7 - when obs regexp "!!1054=1059!!" then @marital_status := 8 - else @marital_status := null - end as marital_status, - case - when obs regexp "!!1972=1967!!" then @main_occupation := 1 - when obs regexp "!!1972=6401!!" then @main_occupation := 2 - when obs regexp "!!1972=6408!!" then @main_occupation := 3 - when obs regexp "!!1972=6966!!" then @main_occupation := 4 - when obs regexp "!!1972=1969!!" then @main_occupation := 5 - when obs regexp "!!1972=1968!!" then @main_occupation := 6 - when obs regexp "!!1972=1966!!" then @main_occupation := 7 - when obs regexp "!!1972=8407!!" then @main_occupation := 8 - when obs regexp "!!1972=6284!!" then @main_occupation := 9 - when obs regexp "!!1972=8589!!" then @main_occupation := 10 - when obs regexp "!!1972=5619!!" then @main_occupation := 11 - when obs regexp "!!1972=5622!!" then @main_occupation := 12 - else @main_occupation = null - end as main_occupation, - case - when obs regexp "!!1605=1107!!" then @patient_level_education := 1 - when obs regexp "!!1605=6214!!" then @patient_level_education := 2 - when obs regexp "!!1605=6215!!" then @patient_level_education := 3 - when obs regexp "!!1605=1604!!" then @patient_level_education := 4 - else @patient_level_education := null - end as patient_level_education, - - case - when obs regexp "!!6473=1065!!" then @smoke_cigarettes := 1 - when obs regexp "!!6473=1066!!" then @smoke_cigarettes := 2 - when obs regexp "!!6473=1679!!" then @smoke_cigarettes := 3 - else @smoke_cigarettes := null - end as smoke_cigarettes, - case - when obs regexp "!!2069=" then @number_of_sticks := GetValues(obs,2069) - else @number_of_sticks = null - end as number_of_sticks, - case - when obs regexp "!!2070=" then @cigga_smoke_duration := GetValues(obs,2070) - else @cigga_smoke_duration = null - end as cigga_smoke_duration, - case - when obs regexp "!!2068=" then @duration_since_last_use := GetValues(obs,2068) - else @duration_since_last_use = null - end as duration_since_last_use, - case - when obs regexp "!!7973=1065!!" then @tobacco_use := 1 - when obs regexp "!!7973=1066!!" then @tobacco_use := 2 - when obs regexp "!!7973=1679!!" then @tobacco_use := 3 - else @tobacco_use = null - end as tobacco_use, - case - when obs regexp "!!8144=" then @tobacco_use_duration := GetValues(obs,8144) - else @tobacco_use_duration = null - end as tobacco_use_duration, - case - when obs regexp "!!1684=1065!!" then @drink_alcohol := 1 - when obs regexp "!!1684=1066!!" then @drink_alcohol := 2 - when obs regexp "!!1684=1679!!" then @drink_alcohol := 3 - else @drink_alcohol := null - end as drink_alcohol, - case - when obs regexp "!!10310=1107!!" then @chemical_exposure := 1 - when obs regexp "!!10310=10308!!" then @chemical_exposure := 2 - when obs regexp "!!10310=10370!!" then @chemical_exposure := 3 - when obs regexp "!!10310=10309!!" then @chemical_exposure := 4 - when obs regexp "!!10310=5622!!" then @chemical_exposure := 5 - else @chemical_exposure = null - end as chemical_exposure, - case - when obs regexp "!!10312=1065!!" then @asbestos_exposure := 1 - when obs regexp "!!10312=1066!!" then @asbestos_exposure := 2 - else @asbestos_exposure = null - end as asbestos_exposure, - - case - when obs regexp "!!6802=1065!!" then @any_family_mem_with_cancer := 1 - when obs regexp "!!6802=1067!!" then @any_family_mem_with_cancer := 2 - else @any_family_mem_with_cancer = null - end as any_family_mem_with_cancer, - case - when obs regexp "!!9635=1107!!" then @family_member := 1 - when obs regexp "!!9635=978!!" then @family_member := 2 - when obs regexp "!!9635=1692!!" then @family_member := 3 - when obs regexp "!!9635=972!!" then @family_member := 4 - when obs regexp "!!9635=1671!!" then @family_member := 5 - when obs regexp "!!9635=1393!!" then @family_member := 6 - when obs regexp "!!9635=1392!!" then @family_member := 7 - when obs regexp "!!9635=1395!!" then @family_member := 8 - when obs regexp "!!9635=1394!!" then @family_member := 9 - when obs regexp "!!9635=1673!!" then @family_member := 10 - else @family_member := null - end as family_member, - case - when obs regexp "!!5219=136!!" then @chief_complaints := 1 - when obs regexp "!!5219=107!!" then @chief_complaints := 2 - when obs regexp "!!5219=6786!!" then @chief_complaints := 3 - when obs regexp "!!5219=5954!!" then @chief_complaints := 4 - when obs regexp "!!5219=832!!" then @chief_complaints := 5 - when obs regexp "!!5219=5960!!" then @chief_complaints := 6 - when obs regexp "!!5219=10121!!" then @chief_complaints := 7 - when obs regexp "!!5219=5622!!" then @chief_complaints := 8 - else @chief_complaints := null - end as chief_complaints, - case - when obs regexp "!!8777=1072!!" then @complain_duration := 1 - when obs regexp "!!8777=1073!!" then @complain_duration := 2 - when obs regexp "!!8777=1074!!" then @complain_duration := 3 - when obs regexp "!!8777=8787!!" then @complain_duration := 4 - else @complain_duration = null - end as complain_duration, - case - when obs regexp "!!1892=" then @number_days := GetValues(obs,1892) - else @number_days = null - end as number_days, - case - when obs regexp "!!1893=" then @number_weeks := GetValues(obs,1893) - else @number_weeks = null - end as number_weeks, - case - when obs regexp "!!1894=" then @number_months := GetValues(obs,1894) - else @number_months = null - end as number_months, - case - when obs regexp "!!7953=" then @number_years := GetValues(obs,7953) - else @number_years = null - end as number_years, - case - when obs regexp "!!6584=1115!!" then @ecog_performance := 1 - when obs regexp "!!6584=6585!!" then @ecog_performance := 2 - when obs regexp "!!6584=6586!!" then @ecog_performance := 3 - when obs regexp "!!6584=6587!!" then @ecog_performance := 4 - when obs regexp "!!6584=6588!!" then @ecog_performance := 5 - else @ecog_performance = null - end as ecog_performance, - case - when obs regexp "!!1119=1115!!" then @general_exam := 1 - when obs regexp "!!1119=5201!!" then @general_exam := 2 - when obs regexp "!!1119=5245!!" then @general_exam := 3 - when obs regexp "!!1119=215!!" then @general_exam := 4 - when obs regexp "!!1119=589!!" then @general_exam := 5 - else @general_exam = null - end as general_exam, - case - when obs regexp "!!1122=1118!!" then @heent := 1 - when obs regexp "!!1122=1115!!" then @heent := 2 - when obs regexp "!!1122=5192!!" then @heent := 3 - when obs regexp "!!1122=516!!" then @heent := 4 - when obs regexp "!!1122=513!!" then @heent := 5 - when obs regexp "!!1122=5170!!" then @heent := 6 - when obs regexp "!!1122=5334!!" then @heent := 7 - when obs regexp "!!1122=6672!!" then @heent := 8 - else @heent = null - end as heent, - case - when obs regexp "!!1123=1118!!" then @chest := 1 - when obs regexp "!!1123=1115!!" then @chest := 2 - when obs regexp "!!1123=5138!!" then @chest := 3 - when obs regexp "!!1123=5115!!" then @chest := 4 - when obs regexp "!!1123=5116!!" then @chest := 5 - when obs regexp "!!1123=5181!!" then @chest := 6 - when obs regexp "!!1123=5127!!" then @chest := 7 - else @chest = null - end as chest, - case - when obs regexp "!!1124=1118!!" then @heart := 1 - when obs regexp "!!1124=1115!!" then @heart := 2 - when obs regexp "!!1124=1117!!" then @heart := 3 - when obs regexp "!!1124=550!!" then @heart := 4 - when obs regexp "!!1124=5176!!" then @heart := 5 - when obs regexp "!!1124=5162!!" then @heart := 6 - when obs regexp "!!1124=5164!!" then @heart := 7 - else @heart = null - end as heart, - case - when obs regexp "!!1125=1118!!" then @abdomen_exam := 1 - when obs regexp "!!1125=1115!!" then @abdomen_exam := 2 - when obs regexp "!!1125=5105!!" then @abdomen_exam := 3 - when obs regexp "!!1125=5008!!" then @abdomen_exam := 4 - when obs regexp "!!1125=5009!!" then @abdomen_exam := 5 - when obs regexp "!!1125=581!!" then @abdomen_exam := 6 - when obs regexp "!!1125=5103!!" then @abdomen_exam := 7 - else @abdomen_exam = null - end as abdomen_exam, - case - when obs regexp "!!1126=1118!!" then @urogenital := 1 - when obs regexp "!!1126=1115!!" then @urogenital := 2 - when obs regexp "!!1126=1116!!" then @urogenital := 3 - when obs regexp "!!1126=2186!!" then @urogenital := 4 - when obs regexp "!!1126=864!!" then @urogenital := 5 - when obs regexp "!!1126=6334!!" then @urogenital := 6 - when obs regexp "!!1126=1447!!" then @urogenital := 7 - when obs regexp "!!1126=5993!!" then @urogenital := 8 - when obs regexp "!!1126=8998!!" then @urogenital := 9 - when obs regexp "!!1126=1489!!" then @urogenital := 10 - when obs regexp "!!1126=8417!!" then @urogenital := 11 - when obs regexp "!!1126=8261!!" then @urogenital := 12 - else @urogenital = null - end as urogenital, - case - when obs regexp "!!1127=1118!!" then @extremities := 1 - when obs regexp "!!1127=1115!!" then @extremities := 2 - when obs regexp "!!1127=590!!" then @extremities := 3 - when obs regexp "!!1127=7293!!" then @extremities := 4 - when obs regexp "!!1127=134!!" then @extremities := 5 - else @extremities = null - end as extremities, - case - when obs regexp "!!8420=1118!!" then @testicular_exam := 1 - when obs regexp "!!8420=1115!!" then @testicular_exam := 2 - when obs regexp "!!8420=1116!!" then @testicular_exam := 3 - else @testicular_exam = null - end as testicular_exam, - case - when obs regexp "!!1121=1118!!" then @nodal_survey := 1 - when obs regexp "!!1121=1115!!" then @nodal_survey := 2 - when obs regexp "!!1121=1116!!" then @nodal_survey := 3 - when obs regexp "!!1121=8261!!" then @nodal_survey := 4 - else @nodal_survey = null - end as nodal_survey, - case - when obs regexp "!!1128=1118!!" then @musculo_skeletal := 1 - when obs regexp "!!1128=1115!!" then @musculo_skeletal := 2 - when obs regexp "!!1128=1116!!" then @musculo_skeletal := 3 - else @musculo_skeletal = null - end as musculo_skeletal, - case - when obs regexp "!!1129=1118!!" then @neurologic := 1 - when obs regexp "!!1129=1115!!" then @neurologic := 2 - when obs regexp "!!1129=599!!" then @neurologic := 3 - when obs regexp "!!1129=497!!" then @neurologic := 4 - when obs regexp "!!1129=5108!!" then @neurologic := 5 - else @neurologic = null - end as neurologic, - case - when obs regexp "!!5704=" then @previous_tb_treatment := GetValues(obs,5704) - else @previous_tb_treatment = null - end as previous_tb_treatment, - case - when obs regexp "!!6245=142!!" then @ever_diagnosed := 1 - when obs regexp "!!6245=5!!" then @ever_diagnosed := 2 - when obs regexp "!!6245=6237!!" then @ever_diagnosed := 3 - when obs regexp "!!6245=903!!" then @ever_diagnosed := 4 - when obs regexp "!!6245=6033!!" then @ever_diagnosed := 5 - when obs regexp "!!6245=43!!" then @ever_diagnosed := 6 - when obs regexp "!!6245=58!!" then @ever_diagnosed := 7 - else @ever_diagnosed = null - end as ever_diagnosed, - case - when obs regexp "!!9281=" then @year_of_diagnosis := GetValues(obs,9281) - else @year_of_diagnosis = null - end as year_of_diagnosis, - case - when obs regexp "!!9728=" then @date_of_diagnosis := DATE(replace(replace((substring_index(substring(obs, locate("!!9728=", obs)), @sep, 1)), "!!9728=", ""), "!!", "")) - else @date_of_diagnosis = null - end as date_of_diagnosis, - case - when obs regexp "!!6709=1067!!" then @hiv_status := 1 - when obs regexp "!!6709=664!!" then @hiv_status := 2 - when obs regexp "!!6709=703!!" then @hiv_status := 3 - else @hiv_status = null - end as hiv_status, - case - when obs regexp "!!12=1115!!" then @chest_xray_result := 1 - when obs regexp "!!12=1116!!" then @chest_xray_result := 2 - else @chest_xray_result = null - end as chest_xray_result, - case - when obs regexp "!!7113=1115!!" then @chest_ct_scan_results := 1 - when obs regexp "!!7113=1117!!" then @chest_ct_scan_results := 2 - else @chest_ct_scan_results = null - end as chest_ct_scan_results, - case - when obs regexp "!!10123=1115!!" then @pet_scan_results := 1 - when obs regexp "!!10123=1118!!" then @pet_scan_results := 2 - else @pet_scan_results = null - end as pet_scan_results, - case - when obs regexp "!!845=1115!!" then @abdominal_ultrasound_result := 1 - when obs regexp "!!845=1119!!" then @abdominal_ultrasound_result := 2 - else @abdominal_ultrasound_result = null - end as abdominal_ultrasound_result, - case - when obs regexp "!!10124=" then @other_imaging_results = GetValues(obs,10124) - else @other_imaging_results = null - end as other_imaging_results, - - case - when obs regexp "!!1271=1107!!" then @test_ordered := 1 - when obs regexp "!!1271=1019!!" then @test_ordered := 2 - when obs regexp "!!1271=790!!" then @test_ordered := 3 - when obs regexp "!!1271=953!!" then @test_ordered := 4 - when obs regexp "!!1271=6898!!" then @test_ordered := 5 - when obs regexp "!!1271=9009!!" then @test_ordered := 6 - else @test_ordered = null - end as test_ordered, - case - when obs regexp "!!8190=" then @other_radiology := GetValues(obs,8190) - else @other_radiology = null - end as other_radiology, - case - when obs regexp "!!9538=" then @other_laboratory := GetValues(obs,9538) - else @other_laboratory = null - end as other_laboratory, - case - when obs regexp "!!10127=1107!!" then @procedure_ordered := 1 - when obs regexp "!!10127=10075!!" then @procedure_ordered := 2 - when obs regexp "!!10127=10076!!" then @procedure_ordered := 3 - when obs regexp "!!10127=10126!!" then @procedure_ordered := 4 - when obs regexp "!!10127=5622!!" then @procedure_ordered := 5 - else @procedure_ordered = null - end as procedure_ordered, - case - when obs regexp "!!10442=1107!!" then @procedure_performed := 1 - when obs regexp "!!10442=10075!!" then @procedure_performed := 2 - when obs regexp "!!10442=10076!!" then @procedure_performed := 3 - when obs regexp "!!10442=10126!!" then @procedure_performed := 4 - when obs regexp "!!10442=5622!!" then @procedure_performed := 5 - else @procedure_performed = null - end as procedure_performed, - case - when obs regexp "!!1915=" then @other_procedures := GetValues(obs,1915) - else @other_procedures = null - end as other_procedures, - case - when obs regexp "!!10443=" then @procedure_date := GetValues(obs,10443) - else @procedure_date = null - end as procedure_date, - - case - when obs regexp "!!7176=10129!!" then @lung_cancer_type := 1 - when obs regexp "!!7176=10130!!" then @lung_cancer_type := 2 - when obs regexp "!!7176=5622!!" then @lung_cancer_type := 3 - else @lung_cancer_type = null - end as lung_cancer_type, - case - when obs regexp "!!10137=6563!!" then @small_cell_lung_ca_staging := 1 - when obs regexp "!!10137=6564!!" then @small_cell_lung_ca_staging := 2 - else @small_cell_lung_ca_staging = null - end as small_cell_lung_ca_staging, - case - when obs regexp "!!10132=7421!!" then @non_small_cell_ca_type := 1 - when obs regexp "!!10132=7422!!" then @non_small_cell_ca_type := 2 - when obs regexp "!!10132=10131!!" then @non_small_cell_ca_type := 3 - when obs regexp "!!10132=10209!!" then @non_small_cell_ca_type := 3 - else @non_small_cell_ca_type = null - end as non_small_cell_ca_type, - case - when obs regexp "!!9868=9852!!" then @staging := 1 - when obs regexp "!!9868=9856!!" then @staging := 2 - when obs regexp "!!9868=9860!!" then @staging := 3 - when obs regexp "!!9868=9864!!" then @staging := 4 - when obs regexp "!!9868=6563!!" then @staging := 5 - when obs regexp "!!9868=6564!!" then @staging := 6 - else @staging = null - end as staging, - case - when obs regexp "!!10441=" then @cancer_staging_date := GetValues(obs,10441) - else @cancer_staging_date = null - end as cancer_staging_date, - case - when obs regexp "!!10445=" then @metastasis_region := GetValues(obs,10445) - else @metastasis_region = null - end as metastasis_region, - case - when obs regexp "!!8723=6575!!" then @treatment_plan := 1 - when obs regexp "!!8723=8427!!" then @treatment_plan := 2 - when obs regexp "!!8723=7465!!" then @treatment_plan := 3 - when obs regexp "!!8723=5622!!" then @treatment_plan := 4 - else @treatment_plan = null - end as treatment_plan, - case - when obs regexp "!!10039=" then @other_treatment_plan := GetValues(obs,10039) - else @other_treatment_plan = null - end as other_treatment_plan, - case - when obs regexp "!!10133=" then @radiotherapy_sessions := GetValues(obs,10133) - else @radiotherapy_sessions = null - end as radiotherapy_sessions, - case - when obs regexp "!!10134=" then @surgery_date := GetValues(obs,10134) - else @surgery_date = null - end as surgery_date, - case - when obs regexp "!!9869=1256!!" then @chemotherapy_plan := 1 - when obs regexp "!!9869=1259!!" then @chemotherapy_plan := 2 - when obs regexp "!!9869=1260!!" then @chemotherapy_plan := 3 - when obs regexp "!!9869=1257!!" then @chemotherapy_plan := 4 - else @chemotherapy_plan = null - end as chemotherapy_plan, - case - when obs regexp "!!1190=" then @chemo_start_date := GetValues(obs,1190) - else @chemo_start_date = null - end as chemo_start_date, - case - when obs regexp "!!9869=1260!!" then @chemo_start_date := GetValues(obs_datetimes, 9869) - else @chemo_stop_date = null - end as chemo_stop_date, - case - when obs regexp "!!2206=9218!!" then @treatment_intent := 1 - when obs regexp "!!2206=8428!!" then @treatment_intent := 2 - when obs regexp "!!2206=8724!!" then @treatment_intent := 3 - when obs regexp "!!2206=9219!!" then @treatment_intent := 4 - else @treatment_intent = null - end as treatment_intent, - case - when obs regexp "!!9946=" then @chemo_regimen := GetValues(obs,9946) - else @chemo_regimen = null - end as chemo_regimen, - case - when obs regexp "!!6643=" then @chemo_cycle := GetValues(obs,6643) - else @chemo_cycle = null - end as chemo_cycle, - case - when obs regexp "!!6644=" then @total_planned_chemo_cycle := GetValues(obs,6644) - else @total_planned_chemo_cycle = null - end as total_planned_chemo_cycle, - case - when obs regexp "!!9918=7202!!" then @chemo_drug := 1 - when obs regexp "!!9918=8506!!" then @chemo_drug := 2 - when obs regexp "!!9918=7210!!" then @chemo_drug := 3 - when obs regexp "!!9918=8492!!" then @chemo_drug := 4 - when obs regexp "!!9918=8507!!" then @chemo_drug := 5 - else @chemo_drug = null - end as chemo_drug, - case - when obs regexp "!!1899=" then @dosage_in_milligrams := GetValues(obs,1899) - else @dosage_in_milligrams = null - end as dosage_in_milligrams, - case - when obs regexp "!!7463=7458!!" then @drug_route := 1 - when obs regexp "!!7463=10078!!" then @drug_route := 2 - when obs regexp "!!7463=10079!!" then @drug_route := 3 - when obs regexp "!!7463=7597!!" then @drug_route := 4 - when obs regexp "!!7463=7581!!" then @drug_route := 5 - when obs regexp "!!7463=7609!!" then @drug_route := 6 - when obs regexp "!!7463=7616!!" then @drug_route := 7 - when obs regexp "!!7463=7447!!" then @drug_route := 8 - else @drug_route = null - end as drug_route, - case - when obs regexp "!!1895=" then @other_drugs := GetValues(obs,1895) - else @other_drugs = null - end as other_drugs, - case - when obs regexp "!!2206=9220!!" then @purpose := 1 - when obs regexp "!!2206=8428!!" then @purpose := 2 - else @purpose = null - end as purpose, - case - when obs regexp "!!1272=1107!!" then @referral_ordered := 1 - when obs regexp "!!1272=8724!!" then @referral_ordered := 2 - when obs regexp "!!1272=5486!!" then @referral_ordered := 3 - when obs regexp "!!1272=5884!!" then @referral_ordered := 4 - when obs regexp "!!1272=6570!!" then @referral_ordered := 5 - when obs regexp "!!1272=5622!!" then @referral_ordered := 6 - else @referral_ordered = null - end as referral_ordered, - case - when obs regexp "!!5096=" then @rtc := GetValues(obs,5096) - else @rtc = null - end as rtc - - from flat_lung_cancer_treatment_0 t1 - join amrs.person p using (person_id) - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - alter table flat_lung_cancer_treatment_1 drop prev_id, drop cur_id; - - drop table if exists flat_lung_cancer_treatment_2; - create temporary table flat_lung_cancer_treatment_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_lung_cancer_treatment, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_lung_cancer_treatment, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_lung_cancer_treatment, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id_lung_cancer_treatment, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_lung_cancer_treatment, - - case - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date - - from flat_lung_cancer_treatment_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_lung_cancer_treatment_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_lung_cancer_treatment_3; - create temporary table flat_lung_cancer_treatment_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_lung_cancer_treatment, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_lung_cancer_treatment, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_lung_cancer_treatment, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id_lung_cancer_treatment, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_lung_cancer_treatment, - - case - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_lung_cancer_treatment_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - SELECT - COUNT(*) - INTO @new_encounter_rows FROM - flat_lung_cancer_treatment_3; - - SELECT @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; - SELECT @total_rows_written; - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - encounter_id, - encounter_type, - encounter_datetime, - visit_id, - location_id, - t2.uuid as location_uuid, - gender, - age, - birthdate, - death_date, - cur_visit_type, - referred_from, - facility_name, - referral_date, - referred_by, - marital_status, - main_occupation, - patient_level_education, - smoke_cigarettes, - number_of_sticks, - cigga_smoke_duration, - duration_since_last_use, - tobacco_use, - tobacco_use_duration, - drink_alcohol, - chemical_exposure, - asbestos_exposure, - any_family_mem_with_cancer, - family_member, - chief_complaints, - complain_duration, - number_days, - number_weeks, - number_months, - number_years, - ecog_performance, - general_exam, - heent, - chest, - heart, - abdomen_exam, - urogenital, - extremities, - testicular_exam, - nodal_survey, - musculo_skeletal, - neurologic, - previous_tb_treatment, - ever_diagnosed, - year_of_diagnosis, - date_of_diagnosis, - hiv_status, - chest_xray_result, - chest_ct_scan_results, - pet_scan_results, - abdominal_ultrasound_result, - other_imaging_results, - test_ordered, - other_radiology, - other_laboratory, - procedure_ordered, - procedure_performed, - other_procedures, - procedure_date, - lung_cancer_type, - small_cell_lung_ca_staging, - non_small_cell_ca_type, - staging, - cancer_staging_date, - metastasis_region, - treatment_plan, - other_treatment_plan, - radiotherapy_sessions, - surgery_date, - chemotherapy_plan, - chemo_start_date, - chemo_stop_date, - treatment_intent, - chemo_regimen, - chemo_cycle, - total_planned_chemo_cycle, - chemo_drug, - dosage_in_milligrams, - drug_route, - other_drugs, - purpose, - referral_ordered, - rtc, - prev_encounter_datetime_lung_cancer_treatment, - next_encounter_datetime_lung_cancer_treatment, - prev_encounter_type_lung_cancer_treatment, - next_encounter_type_lung_cancer_treatment, - prev_clinical_datetime_lung_cancer_treatment, - next_clinical_datetime_lung_cancer_treatment, - prev_clinical_location_id_lung_cancer_treatment, - next_clinical_location_id_lung_cancer_treatment, - prev_clinical_rtc_date_lung_cancer_treatment, - next_clinical_rtc_date_lung_cancer_treatment - - from flat_lung_cancer_treatment_3 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_lung_cancer_treatment_build_queue__0 t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - SELECT - @person_ids_count AS 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - - end while; - - if (@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - SELECT - CONCAT(@start_write, - ' : Writing ', - @total_rows_to_write, - ' to ', - @primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - - SELECT - CONCAT(@finish_write, - ' : Completed writing rows. Time to write to primary table: ', - @time_to_write, - ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - - SELECT - CONCAT('Average Cycle Length: ', - @ave_cycle_length, - ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - SELECT - CONCAT(@table_version, - ' : Time to complete: ', - TIMESTAMPDIFF(MINUTE, @start, @end), - ' minutes'); - -END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_multiple_myeloma_treatment_v1_2.sql b/etl-scripts/stored-procedures/generate_flat_multiple_myeloma_treatment.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_flat_multiple_myeloma_treatment_v1_2.sql rename to etl-scripts/stored-procedures/generate_flat_multiple_myeloma_treatment.sql index 2297bdc..881d5b8 100644 --- a/etl-scripts/stored-procedures/generate_flat_multiple_myeloma_treatment_v1_2.sql +++ b/etl-scripts/stored-procedures/generate_flat_multiple_myeloma_treatment.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_multiple_myeloma_treatment_v1_2`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_multiple_myeloma_treatment`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN -- v1.1: Add diagnosis date, serum_m_protein, treatment_plan, other_treatment_plan, remission_plan and remission_start_date columns. -- Also modified encounter_datetime column to just encounter_date (removed the timestamp as per the myeloma team's request). diff --git a/etl-scripts/stored-procedures/generate_flat_multiple_myeloma_treatment_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_multiple_myeloma_treatment_v1_0.sql deleted file mode 100644 index e62a854..0000000 --- a/etl-scripts/stored-procedures/generate_flat_multiple_myeloma_treatment_v1_0.sql +++ /dev/null @@ -1,892 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_multiple_myeloma_treatment_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_multiple_myeloma_treatment"; - set @query_type = query_type; -#set @query_type = "build"; - - set @total_rows_written = 0; - - set @encounter_types = "(89,90)"; - set @clinical_encounter_types = "(89,90)"; - set @non_clinical_encounter_types = "(-1)"; - set @other_encounter_types = "(-1)"; - - set @start = now(); - set @table_version = "flat_multiple_myeloma_treatment_v1.0"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @boundary = "!!"; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - CREATE TABLE IF NOT EXISTS flat_multiple_myeloma_treatment ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id INT, - encounter_id INT, - encounter_type INT, - encounter_datetime DATETIME, - visit_id INT, - location_id INT, - gender CHAR(100), - age INT, - encounter_purpose INT, - ecog_performance_index INT, - diagnosis_method INT, - diagnosis INT, - cancer_stage INT, - overall_cancer_stage INT, - lab_test_order INT, - other_lab_test INT, - mm_supportive_plan INT, - mm_signs_symptoms INT, - chemotherapy_plan INT, - chemo_start_date DATETIME, - chemo_cycle INT, - reason_chemo_stop INT, - chemotherapy_drug INT, - dosage_in_milligrams INT, - drug_route INT, - other_drugs INT, - other_medication INT, - purpose INT, - education_given_today INT, - referral INT, - next_app_date DATETIME, - prev_encounter_datetime_multiple_myeloma DATETIME, - next_encounter_datetime_multiple_myeloma DATETIME, - prev_encounter_type_multiple_myeloma MEDIUMINT, - next_encounter_type_multiple_myeloma MEDIUMINT, - prev_clinical_datetime_multiple_myeloma DATETIME, - next_clinical_datetime_multiple_myeloma DATETIME, - prev_clinical_location_id_multiple_myeloma MEDIUMINT, - next_clinical_location_id_multiple_myeloma MEDIUMINT, - prev_clinical_rtc_date_multiple_myeloma DATETIME, - next_clinical_rtc_date_multiple_myeloma DATETIME, - PRIMARY KEY encounter_id (encounter_id), - INDEX person_date (person_id , encounter_datetime), - INDEX location_id_rtc_date (location_id , next_app_date), - INDEX loc_id_enc_date_next_clinical (location_id , encounter_datetime , next_clinical_datetime_multiple_myeloma), - INDEX encounter_type (encounter_type), - INDEX date_created (date_created) -); - - - - if(@query_type="build") then - select 'BUILDING..........................................'; - -#set @write_table = concat("flat_breast_cancer_screening_temp_",1); -#set @queue_table = concat("flat_breast_cancer_screening_build_queue_",1); - - set @write_table = concat("flat_multiple_myeloma_treatment_temp_",queue_number); - set @queue_table = concat("flat_multiple_myeloma_treatment_build_queue_",queue_number); - - -#drop table if exists flat_breast_cancer_screening_temp_1; - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_multiple_myeloma_treatment_build_queue limit ', queue_size, ');'); -#SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_breast_cancer_screening_build_queue limit 500);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_breast_cancer_screening_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from flat_multiple_myeloma_treatment_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_multiple_myeloma_treatment"; - set @queue_table = "flat_multiple_myeloma_treatment_sync_queue"; -CREATE TABLE IF NOT EXISTS flat_multiple_myeloma_treatment_sync_queue ( - person_id INT PRIMARY KEY -); - - set @last_update = null; - -SELECT - MAX(date_updated) -INTO @last_update FROM - etl.flat_log -WHERE - table_name = @table_version; - -SELECT 'Finding patients in amrs.encounters...'; - - replace into flat_multiple_myeloma_treatment_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - -SELECT 'Finding patients in flat_obs...'; - - replace into flat_multiple_myeloma_treatment_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - -SELECT 'Finding patients in flat_lab_obs...'; - replace into flat_multiple_myeloma_treatment_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - -SELECT 'Finding patients in flat_orders...'; - - replace into flat_multiple_myeloma_treatment_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_multiple_myeloma_treatment_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_multiple_myeloma_treatment_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - # Remove test patients - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SELECT @person_ids_count AS 'num patients to update'; - - - #delete t1 from flat_breast_cancer_screening t1 join @queue_table t2 using (person_id); - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists flat_multiple_myeloma_treatment_build_queue__0; - - #create temporary table flat_breast_cancer_screening_build_queue__0 (select * from flat_breast_cancer_screening_build_queue_2 limit 5000); #TODO - change this when data_fetch_size changes - -#SET @dyn_sql=CONCAT('create temporary table flat_breast_cancer_screening_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit 100);'); - SET @dyn_sql=CONCAT('create temporary table flat_multiple_myeloma_treatment_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - drop temporary table if exists flat_multiple_myeloma_treatment_0a; - SET @dyn_sql = CONCAT( - 'create temporary table flat_multiple_myeloma_treatment_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - case - when t1.encounter_type in ',@clinical_encounter_types,' then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in ',@non_clinical_encounter_types,' then 20 - when t1.encounter_type in ',@clinical_encounter_types,' then 10 - when t1.encounter_type in', @other_encounter_types, ' then 5 - else 1 - end as encounter_type_sort_index, - t2.orders - from etl.flat_obs t1 - join flat_multiple_myeloma_treatment_build_queue__0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_id) - where t1.encounter_type in ',@encounter_types,');'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - - insert into flat_multiple_myeloma_treatment_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, #t1.location_id, - t1.obs, - null, #obs_datetimes - # in any visit, there many be multiple encounters. for this dataset, we want to include only clinical encounters (e.g. not lab or triage visit) - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_multiple_myeloma_treatment_build_queue__0 t0 using (person_id) - ); - - - drop temporary table if exists flat_multiple_myeloma_treatment_0; - create temporary table flat_multiple_myeloma_treatment_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_multiple_myeloma_treatment_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @encounter_purpose = null; - - set @ecog_performance_index = null; - - set @diagnosis_method =null; - - set @diagnosis =null; - - set @cancer_stage =null; - - set @overall_cancer_stage =null; - - set @lab_test_order =null; - - set @other_lab_test =null; - - set @mm_supportive_plan =null; - - set @mm_signs_symptoms = null; - - set @chemotherapy_plan =null; - - set @chemo_start_date =null; - - set @chemo_cycle =null; - - set @reason_chemo_stop =null; - - set @chemotherapy_drug =null; - - set @dosage_in_milligrams =null; - - set @drug_route =null; - - set @other_drugs =null; - - set @other_medication =null; - - set @purpose =null; - - set @education_given_today =null; - - set @referral =null; - - set @next_app_date = null; - - drop temporary table if exists flat_multiple_myeloma_treatment_1; - create temporary table flat_multiple_myeloma_treatment_1 #(index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - t1.encounter_id, - t1.encounter_type, - t1.encounter_datetime, - t1.visit_id, - #t4.name as location_name, - t1.location_id, - t1.is_clinical_encounter, - p.gender, - #p.death_date, - - - case - when timestampdiff(year,birthdate,curdate()) > 0 then round(timestampdiff(year,birthdate,curdate()),0) - else round(timestampdiff(month,birthdate,curdate())/12,2) - end as age, - - case - when obs regexp "!!1834=7850!!" then @encounter_purpose := 1 - when obs regexp "!!1834=10037!!" then @encounter_purpose := 2 - when obs regexp "!!1834=2345!!" then @encounter_purpose := 3 - when obs regexp "!!1834=1068!!" then @encounter_purpose := 4 - when obs regexp "!!1834=1246!!" then @encounter_purpose := 5 - else @encounter_purpose := null - end as encounter_purpose, - - - case - when obs regexp "!!6584=1115!!" then @ecog_performance_index := 1 - when obs regexp "!!6584=6585!!" then @ecog_performance_index := 2 - when obs regexp "!!6584=6586!!" then @ecog_performance_index := 3 - when obs regexp "!!6584=6587!!" then @ecog_performance_index := 4 - when obs regexp "!!6584=6588!!" then @ecog_performance_index := 5 - else @ecog_performance_index := null - end as ecog_performance_index, - - - case - when obs regexp "!!6504=8594!!" then @diagnosis_method := 1 - when obs regexp "!!6504=6902!!" then @diagnosis_method := 2 - when obs regexp "!!6504=10142!!" then @diagnosis_method := 3 - else @diagnosis_method := null - end as diagnosis_method, - - - case - when obs regexp "!!6042=" then @diagnosis := GetValues(obs,6042) - else @diagnosis := null - end as diagnosis, - - - case - when obs regexp "!!6582=1067!!" then @cancer_stage := 1 - when obs regexp "!!6582=10206!!" then @cancer_stage := 2 - when obs regexp "!!6582=6566!!" then @cancer_stage := 3 - when obs regexp "!!6582=1175!!" then @cancer_stage := 4 - else @cancer_stage := null - end as cancer_stage, - - - case - when obs regexp "!!9868=9852!!" then @overall_cancer_stage := 1 - when obs regexp "!!9868=9856!!" then @overall_cancer_stage := 2 - when obs regexp "!!9868=9860!!" then @overall_cancer_stage := 3 - else @overall_cancer_stage := null - end as overall_cancer_stage, - - - case - when obs regexp "!!6583=1107!!" then @lab_test_order := 1 - when obs regexp "!!6583=790!!" then @lab_test_order := 2 - when obs regexp "!!6583=1019!!" then @lab_test_order := 3 - when obs regexp "!!6583=953!!" then @lab_test_order := 4 - when obs regexp "!!6583=10205!!" then @lab_test_order := 5 - when obs regexp "!!6583=8596!!" then @lab_test_order := 6 - when obs regexp "!!6583=8595!!" then @lab_test_order := 7 - when obs regexp "!!6583=5622!!" then @lab_test_order := 8 - else @lab_test_order := null - end as lab_test_order, - - - case - when obs regexp "!!9538=" then @other_lab_test := GetValues(obs,9538) - else @other_lab_test := null - end as other_lab_test, - - - case - when obs regexp "!!10198=88!!" then @mm_supportive_plan := 1 - when obs regexp "!!10198=257!!" then @mm_supportive_plan := 2 - when obs regexp "!!10198=8598!!" then @mm_supportive_plan := 3 - when obs regexp "!!10198=8597!!" then @mm_supportive_plan := 4 - when obs regexp "!!10198=1195!!" then @mm_supportive_plan := 5 - when obs regexp "!!10198=8410!!" then @mm_supportive_plan := 6 - when obs regexp "!!10198=7458!!" then @mm_supportive_plan := 7 - when obs regexp "!!10198=8479!!" then @mm_supportive_plan := 8 - when obs regexp "!!10198=10140!!" then @mm_supportive_plan := 9 - when obs regexp "!!10198=7207!!" then @mm_supportive_plan := 10 - when obs regexp "!!10198=5622!!" then @mm_supportive_plan := 11 - else @mm_supportive_plan := null - end as mm_supportive_plan, - - case - when obs regexp "!!10170=10141!!" then @mm_signs_symptoms := 1 - when obs regexp "!!10170=1885!!" then @mm_signs_symptoms := 2 - when obs regexp "!!10170=3!!" then @mm_signs_symptoms := 3 - when obs regexp "!!10170=8592!!" then @mm_signs_symptoms := 4 - when obs regexp "!!10170=5978!!" then @mm_signs_symptoms := 5 - when obs regexp "!!10170=5949!!" then @mm_signs_symptoms := 6 - when obs regexp "!!10170=5622!!" then @mm_signs_symptoms := 7 - else @mm_signs_symptoms := null - end as mm_signs_symptoms, - - - case - when obs regexp "!!9869=1256!!" then @chemotherapy_plan := 1 - when obs regexp "!!9869=1259!!" then @chemotherapy_plan := 2 - when obs regexp "!!9869=1260!!" then @chemotherapy_plan := 3 - when obs regexp "!!9869=1257!!" then @chemotherapy_plan := 4 - when obs regexp "!!9869=6576!!" then @chemotherapy_plan := 5 - else @chemotherapy_plan := null - end as chemotherapy_plan, - - - case - when obs regexp "!!1190=" then @chemo_start_date := GetValues(obs,1190) - else @chemo_start_date := null - end as chemo_start_date, - - - case - when obs regexp "!!6643=[0-9]" then @chemo_cycle:=cast(GetValues(obs,6643) as unsigned) - else @chemo_cycle := null - end as chemo_cycle, - - - case - when obs regexp "!!9927=1267!!" then @reason_chemo_stop := 1 - when obs regexp "!!9927=7391!!" then @reason_chemo_stop := 2 - when obs regexp "!!9927=6629!!" then @reason_chemo_stop := 3 - when obs regexp "!!9927=6627!!" then @reason_chemo_stop := 4 - when obs regexp "!!9927=1879!!" then @reason_chemo_stop := 5 - when obs regexp "!!9927=5622!!" then @reason_chemo_stop := 6 - else @reason_chemo_stop := null - end as reason_chemo_stop, - - - case - when obs regexp "!!9918=491!!" then @chemotherapy_drug := 1 - when obs regexp "!!9918=7207!!" then @chemotherapy_drug := 2 - when obs regexp "!!9918=8486!!" then @chemotherapy_drug := 3 - when obs regexp "!!9918=7203!!" then @chemotherapy_drug := 4 - when obs regexp "!!9918=8479!!" then @chemotherapy_drug := 5 - when obs regexp "!!9918=7213!!" then @chemotherapy_drug := 6 - when obs regexp "!!9918=8480!!" then @chemotherapy_drug := 7 - when obs regexp "!!9918=5622!!" then @chemotherapy_drug := 8 - else @chemotherapy_drug := null - end as chemotherapy_drug, - - - case - when obs regexp "!!1899=[0-9]" then @dosage_in_milligrams:=cast(GetValues(obs,1899) as unsigned) - else @dosage_in_milligrams := null - end as dosage_in_milligrams, - - - case - when obs regexp "!!7463=7458!!" then @drug_route := 1 - when obs regexp "!!7463=10078!!" then @drug_route := 2 - when obs regexp "!!7463=10079!!" then @drug_route := 3 - when obs regexp "!!7463=7597!!" then @drug_route := 4 - when obs regexp "!!7463=7581!!" then @drug_route := 5 - when obs regexp "!!7463=7609!!" then @drug_route := 6 - when obs regexp "!!7463=7616!!" then @drug_route := 7 - when obs regexp "!!7463=7447!!" then @drug_route := 8 - else @drug_route := null - end as drug_route, - - - case - when obs regexp "!!1895=" then @other_drugs := GetValues(obs,1895) - else @other_drugs := null - end as other_drugs, - - - case - when obs regexp "!!1779=" then @other_medication := GetValues(obs,1779) - else @other_medication := null - end as other_medication, - - - - case - when obs regexp "!!2206=9220!!" then @purpose := 1 - when obs regexp "!!2206=8428!!" then @purpose := 2 - else @purpose := null - end as purpose, - - - - case - when obs regexp "!!6327=8728!!" then @education_given_today := 1 - when obs regexp "!!6327=8742!!" then @education_given_today := 2 - when obs regexp "!!6327=1905!!" then @education_given_today := 3 - when obs regexp "!!6327=10208!!" then @education_given_today := 4 - when obs regexp "!!6327=8730!!" then @education_given_today := 5 - when obs regexp "!!6327=8371!!" then @education_given_today := 6 - when obs regexp "!!6327=5622!!" then @education_given_today := 7 - else @education_given_today := null - end as education_given_today, - - - case - when obs regexp "!!1272=1107!!" then @referral := 1 - when obs regexp "!!1272=8724!!" then @referral := 2 - when obs regexp "!!1272=6571!!" then @referral := 3 - when obs regexp "!!1272=1286!!" then @referral := 4 - when obs regexp "!!1272=6572!!" then @referral := 5 - when obs regexp "!!1272=6573!!" then @referral := 6 - when obs regexp "!!1272=1905!!" then @referral := 7 - when obs regexp "!!1272=5622!!" then @referral := 8 - else @referral := null - end as referral, - - - case - when obs regexp "!!5096=" then @next_app_date := GetValues(obs,5096) - else @next_app_date := null - end as next_app_date - - from flat_multiple_myeloma_treatment_0 t1 - join amrs.person p using (person_id) - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - );# limit 100; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_multiple_myeloma_treatment_1 drop prev_id, drop cur_id; - - drop table if exists flat_multiple_myeloma_treatment_2; - create temporary table flat_multiple_myeloma_treatment_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_multiple_myeloma, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_multiple_myeloma, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_multiple_myeloma, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id_multiple_myeloma, - - - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_multiple_myeloma, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date - - - from flat_multiple_myeloma_treatment_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_multiple_myeloma_treatment_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_multiple_myeloma_treatment_3; - create temporary table flat_multiple_myeloma_treatment_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_multiple_myeloma, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_multiple_myeloma, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_multiple_myeloma, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id_multiple_myeloma, - - - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_multiple_myeloma, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_multiple_myeloma_treatment_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - - SELECT - COUNT(*) -INTO @new_encounter_rows FROM - flat_multiple_myeloma_treatment_3; - -SELECT @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; -SELECT @total_rows_written; - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - - encounter_id, - - encounter_type, - - encounter_datetime, - - visit_id, - - location_id, - - gender, - - age, - - encounter_purpose, - - ecog_performance_index, - - diagnosis_method, - - diagnosis, - - cancer_stage, - - overall_cancer_stage, - - lab_test_order, - - other_lab_test, - - mm_supportive_plan, - - mm_signs_symptoms, - - chemotherapy_plan, - - chemo_start_date, - - chemo_cycle, - - reason_chemo_stop, - - chemotherapy_drug, - - dosage_in_milligrams, - - drug_route, - - other_drugs, - - other_medication, - - purpose, - - education_given_today, - - referral, - - next_app_date, - - - - - - prev_encounter_datetime_multiple_myeloma, - - next_encounter_datetime_multiple_myeloma, - - prev_encounter_type_multiple_myeloma, - - next_encounter_type_multiple_myeloma, - - prev_clinical_datetime_multiple_myeloma, - - next_clinical_datetime_multiple_myeloma, - - prev_clinical_location_id_multiple_myeloma, - - next_clinical_location_id_multiple_myeloma, - - prev_clinical_rtc_date_multiple_myeloma, - - next_clinical_rtc_date_multiple_myeloma - - from flat_multiple_myeloma_treatment_3 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_multiple_myeloma_treatment_build_queue__0 t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #select @person_ids_count := (select count(*) from flat_breast_cancer_screening_build_queue_2); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count as remaining_in_build_queue; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - #select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); -SELECT - @person_ids_count AS 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - -#select 1; - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - SELECT - CONCAT(@start_write, - ' : Writing ', - @total_rows_to_write, - ' to ', - @primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); -SELECT - CONCAT(@finish_write, - ' : Completed writing rows. Time to write to primary table: ', - @time_to_write, - ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); -SELECT - CONCAT('Average Cycle Length: ', - @ave_cycle_length, - ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - SELECT - CONCAT(@table_version, - ' : Time to complete: ', - TIMESTAMPDIFF(MINUTE, @start, @end), - ' minutes'); - -END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_multiple_myeloma_treatment_v1_1.sql b/etl-scripts/stored-procedures/generate_flat_multiple_myeloma_treatment_v1_1.sql deleted file mode 100644 index f69ddb9..0000000 --- a/etl-scripts/stored-procedures/generate_flat_multiple_myeloma_treatment_v1_1.sql +++ /dev/null @@ -1,751 +0,0 @@ -CREATE PROCEDURE `generate_flat_multiple_myeloma_treatment_v1_1`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - -- v1.1: Add diagnosis date, serum_m_protein, treatment_plan, other_treatment_plan, remission_plan and remission_start_date columns. - -- Also modified encounter_datetime column to just encounter_date (removed the timestamp as per the myeloma team's request). - SET @primary_table := "flat_multiple_myeloma_treatment"; - SET @query_type := query_type; - - SET @total_rows_written := 0; - SET @encounter_types := "(89,90)"; - SET @clinical_encounter_types := "(89,90)"; - SET @non_clinical_encounter_types := "(-1)"; - SET @other_encounter_types := "(-1)"; - - SET @start := NOW(); - SET @table_version := "flat_multiple_myeloma_treatment_v1.1"; - - SET session sort_buffer_size = 512000000; - - SET @sep := " ## "; - SET @boundary := "!!"; - SET @last_date_created := (SELECT max(max_date_created) FROM etl.flat_obs); - - CREATE TABLE IF NOT EXISTS flat_multiple_myeloma_treatment ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id INT, - encounter_id INT, - encounter_type INT, - encounter_date DATE, - visit_id INT, - location_id INT, - gender CHAR(100), - age INT, - encounter_purpose INT, - ecog_performance_index INT, - diagnosis_method INT, - diagnosis INT, - diagnosis_date DATE, - cancer_stage INT, - overall_cancer_stage INT, - lab_test_order INT, - other_lab_test INT, - serum_m_protein INT, - treatment_plan INT, - other_treatment_plan VARCHAR(255), - remission_plan INT, - remission_start_date DATE, - mm_supportive_plan INT, - mm_signs_symptoms INT, - chemotherapy_plan INT, - chemo_start_date DATETIME, - chemo_cycle INT, - reason_chemo_stop INT, - chemotherapy_drug INT, - dosage_in_milligrams INT, - drug_route INT, - other_drugs INT, - other_medication INT, - purpose INT, - education_given_today INT, - referral INT, - next_app_date DATETIME, - prev_encounter_datetime_multiple_myeloma DATETIME, - next_encounter_datetime_multiple_myeloma DATETIME, - prev_encounter_type_multiple_myeloma MEDIUMINT, - next_encounter_type_multiple_myeloma MEDIUMINT, - prev_clinical_datetime_multiple_myeloma DATETIME, - next_clinical_datetime_multiple_myeloma DATETIME, - prev_clinical_location_id_multiple_myeloma MEDIUMINT, - next_clinical_location_id_multiple_myeloma MEDIUMINT, - prev_clinical_rtc_date_multiple_myeloma DATETIME, - next_clinical_rtc_date_multiple_myeloma DATETIME, - PRIMARY KEY encounter_id (encounter_id), - INDEX person_date (person_id, encounter_date), - INDEX location_id_rtc_date (location_id, next_app_date), - INDEX loc_id_enc_date_next_clinical (location_id, encounter_date, next_clinical_datetime_multiple_myeloma), - INDEX encounter_type (encounter_type), - INDEX date_created (date_created) - ); - - IF (@query_type = "build") THEN - SELECT 'BUILDING..........................................'; - - SET @write_table := CONCAT("flat_multiple_myeloma_treatment_temp_",queue_number); - SET @queue_table := CONCAT("flat_multiple_myeloma_treatment_build_queue_",queue_number); - - SET @dyn_sql := CONCAT('CREATE TABLE IF NOT EXISTS ', @write_table, ' LIKE ', @primary_table); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql := CONCAT('CREATE TABLE IF NOT EXISTS ', @queue_table, ' (SELECT * FROM flat_multiple_myeloma_treatment_build_queue limit ', queue_size, ');'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql := CONCAT('DELETE t1 FROM flat_multiple_myeloma_treatment_build_queue t1 join ', @queue_table, ' t2 using (person_id);'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - END IF; - - IF (@query_type = "sync") THEN - SELECT 'SYNCING..........................................'; - - SET @write_table := "flat_multiple_myeloma_treatment"; - SET @queue_table := "flat_multiple_myeloma_treatment_sync_queue"; - - CREATE TABLE IF NOT EXISTS flat_multiple_myeloma_treatment_sync_queue (person_id INT PRIMARY KEY); - - SET @last_update := null; - - SELECT MAX(date_updated) INTO @last_update FROM etl.flat_log WHERE table_name = @table_version; - - SELECT 'Finding patients in amrs.encounters...'; - REPLACE INTO flat_multiple_myeloma_treatment_sync_queue - (SELECT distinct patient_id - FROM amrs.encounter - where date_changed > @last_update - ); - - SELECT 'Finding patients in flat_obs...'; - REPLACE INTO flat_multiple_myeloma_treatment_sync_queue - (SELECT distinct person_id - FROM etl.flat_obs - where max_date_created > @last_update - ); - - SELECT 'Finding patients in flat_lab_obs...'; - REPLACE INTO flat_multiple_myeloma_treatment_sync_queue - (SELECT distinct person_id - FROM etl.flat_lab_obs - where max_date_created > @last_update - ); - - SELECT 'Finding patients in flat_orders...'; - REPLACE INTO flat_multiple_myeloma_treatment_sync_queue - (SELECT distinct person_id - FROM etl.flat_orders - where max_date_created > @last_update - ); - - REPLACE INTO flat_multiple_myeloma_treatment_sync_queue - (SELECT person_id - FROM amrs.person - where date_voided > @last_update - ); - - REPLACE INTO flat_multiple_myeloma_treatment_sync_queue - (SELECT person_id - FROM amrs.person - where date_changed > @last_update - ); - END IF; - - -- Remove test patients - SET @dyn_sql := CONCAT('DELETE t1 FROM ', @queue_table,' t1 - JOIN amrs.person_attribute t2 USING (person_id) - WHERE t2.person_attribute_type_id = 28 AND value = "true" AND voided = 0'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count := 0; - SET @dyn_sql := CONCAT('SELECT COUNT(*) INTO @person_ids_count FROM ', @queue_table); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SELECT @person_ids_count AS 'num patients to update'; - - SET @dyn_sql := CONCAT('DELETE t1 FROM ', @primary_table, ' t1 JOIN ', @queue_table,' t2 USING (person_id);'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_time := 0; - SET @cycle_number := 0; - - WHILE @person_ids_count > 0 DO - SET @loop_start_time := NOW(); - - -- Create temporary table with a set of person ids - DROP TEMPORARY TABLE IF EXISTS flat_multiple_myeloma_treatment_build_queue__0; - - SET @dyn_sql := CONCAT('CREATE TEMPORARY TABLE flat_multiple_myeloma_treatment_build_queue__0 (person_id INT PRIMARY KEY) (SELECT * FROM ', @queue_table,' limit ', cycle_size,');'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - DROP TEMPORARY TABLE IF EXISTS flat_multiple_myeloma_treatment_0a; - SET @dyn_sql := CONCAT( - 'CREATE TEMPORARY TABLE flat_multiple_myeloma_treatment_0a - (SELECT - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - CASE - WHEN t1.encounter_type in ', @clinical_encounter_types, ' THEN 1 - ELSE null - END AS is_clinical_encounter, - CASE - WHEN t1.encounter_type in ', @non_clinical_encounter_types, ' THEN 20 - WHEN t1.encounter_type in ', @clinical_encounter_types, ' THEN 10 - WHEN t1.encounter_type in', @other_encounter_types, ' THEN 5 - ELSE 1 - END AS encounter_type_sort_index, - t2.orders - FROM etl.flat_obs t1 - JOIN - flat_multiple_myeloma_treatment_build_queue__0 t0 USING (person_id) - LEFT JOIN - etl.flat_orders t2 using (encounter_id) - WHERE - t1.encounter_type in ', @encounter_types ,');' - ); - - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - INSERT INTO flat_multiple_myeloma_treatment_0a - (SELECT - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - t1.obs, - null, - -- in any visit, there many be multiple encounters. for this dataset, we want to include only clinical encounters (e.g. not lab or triage visit) - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - FROM - etl.flat_lab_obs t1 - JOIN - flat_multiple_myeloma_treatment_build_queue__0 t0 USING (person_id) - ); - - DROP TEMPORARY TABLE IF EXISTS flat_multiple_myeloma_treatment_0; - CREATE TEMPORARY TABLE flat_multiple_myeloma_treatment_0 (INDEX encounter_id (encounter_id), INDEX person_enc (person_id, encounter_datetime)) - (SELECT - * - FROM - flat_multiple_myeloma_treatment_0a - ORDER BY - person_id, DATE(encounter_datetime), encounter_type_sort_index - ); - - SET @encounter_purpose := null; - SET @ecog_performance_index := null; - SET @diagnosis_method := null; - SET @diagnosis := null; - SET @diagnosis_date := null; - SET @cancer_stage := null; - SET @overall_cancer_stage := null; - SET @lab_test_order := null; - SET @other_lab_test := null; - SET @serum_m_protein := null; - SET @treatment_plan := null; - SET @other_treatment_plan := null; - SET @remission_plan := null; - SET @remission_start_date := null; - SET @mm_supportive_plan := null; - SET @mm_signs_symptoms := null; - SET @chemotherapy_plan := null; - SET @chemo_start_date := null; - SET @chemo_cycle := null; - SET @reason_chemo_stop := null; - SET @chemotherapy_drug := null; - SET @dosage_in_milligrams := null; - SET @drug_route := null; - SET @other_drugs := null; - SET @other_medication := null; - SET @purpose := null; - SET @education_given_today := null; - SET @referral := null; - SET @next_app_date := null; - - DROP TEMPORARY TABLE IF EXISTS flat_multiple_myeloma_treatment_1; - - CREATE TEMPORARY TABLE flat_multiple_myeloma_treatment_1 - (SELECT - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - t1.encounter_id, - t1.encounter_type, - t1.encounter_datetime, - t1.visit_id, - -- t4.name as location_name, - t1.location_id, - t1.is_clinical_encounter, - p.gender, - -- p.death_date, - CASE - WHEN TIMESTAMPDIFF(YEAR, p.birthdate, curDATE()) > 0 THEN round(TIMESTAMPDIFF(YEAR, p.birthdate, curDATE()), 0) - ELSE ROUND(TIMESTAMPDIFF(MONTH, p.birthdate, curDATE()) / 12, 2) - END AS age, - CASE - WHEN obs REGEXP "!!1834=7850!!" THEN @encounter_purpose := 1 - WHEN obs REGEXP "!!1834=10037!!" THEN @encounter_purpose := 2 - WHEN obs REGEXP "!!1834=2345!!" THEN @encounter_purpose := 3 - WHEN obs REGEXP "!!1834=1068!!" THEN @encounter_purpose := 4 - WHEN obs REGEXP "!!1834=1246!!" THEN @encounter_purpose := 5 - ELSE @encounter_purpose := null - END AS encounter_purpose, - CASE - WHEN obs REGEXP "!!6584=1115!!" THEN @ecog_performance_index := 1 - WHEN obs REGEXP "!!6584=6585!!" THEN @ecog_performance_index := 2 - WHEN obs REGEXP "!!6584=6586!!" THEN @ecog_performance_index := 3 - WHEN obs REGEXP "!!6584=6587!!" THEN @ecog_performance_index := 4 - WHEN obs REGEXP "!!6584=6588!!" THEN @ecog_performance_index := 5 - ELSE @ecog_performance_index := null - END AS ecog_performance_index, - CASE - WHEN obs REGEXP "!!6504=8594!!" THEN @diagnosis_method := 1 - WHEN obs REGEXP "!!6504=6902!!" THEN @diagnosis_method := 2 - WHEN obs REGEXP "!!6504=10142!!" THEN @diagnosis_method := 3 - ELSE @diagnosis_method := null - END AS diagnosis_method, - CASE - WHEN obs REGEXP "!!6042=" THEN @diagnosis := GetValues(obs, 6042) - ELSE @diagnosis := null - END AS diagnosis, - CASE - WHEN obs REGEXP "!!9728=" THEN @diagnosis_date := GetValues(obs, 9728) - END AS diagnosis_date, - CASE - WHEN obs REGEXP "!!6582=1067!!" THEN @cancer_stage := 1 - WHEN obs REGEXP "!!6582=10206!!" THEN @cancer_stage := 2 - WHEN obs REGEXP "!!6582=6566!!" THEN @cancer_stage := 3 - WHEN obs REGEXP "!!6582=1175!!" THEN @cancer_stage := 4 - ELSE @cancer_stage := null - END AS cancer_stage, - CASE - WHEN obs REGEXP "!!9868=9852!!" THEN @overall_cancer_stage := 1 - WHEN obs REGEXP "!!9868=9856!!" THEN @overall_cancer_stage := 2 - WHEN obs REGEXP "!!9868=9860!!" THEN @overall_cancer_stage := 3 - ELSE @overall_cancer_stage := null - END AS overall_cancer_stage, - CASE - WHEN obs REGEXP "!!6583=1107!!" THEN @lab_test_order := 1 - WHEN obs REGEXP "!!6583=790!!" THEN @lab_test_order := 2 - WHEN obs REGEXP "!!6583=1019!!" THEN @lab_test_order := 3 - WHEN obs REGEXP "!!6583=953!!" THEN @lab_test_order := 4 - WHEN obs REGEXP "!!6583=10205!!" THEN @lab_test_order := 5 - WHEN obs REGEXP "!!6583=8596!!" THEN @lab_test_order := 6 - WHEN obs REGEXP "!!6583=8595!!" THEN @lab_test_order := 7 - WHEN obs REGEXP "!!6583=5622!!" THEN @lab_test_order := 8 - ELSE @lab_test_order := null - END AS lab_test_order, - CASE - WHEN obs REGEXP "!!9538=" THEN @other_lab_test := GetValues(obs, 9538) - ELSE @other_lab_test := null - END AS other_lab_test, - CASE - WHEN obs REGEXP "!!8731=" THEN @serum_m_protein := GetValues(obs, 8731) - END AS serum_m_protein, - CASE - WHEN obs REGEXP "!!8723=10586" THEN @treatment_plan := 1 - WHEN obs REGEXP "!!8723=6576" THEN @treatment_plan := 2 - WHEN obs REGEXP "!!8723=1107" THEN @treatment_plan := 3 - WHEN obs REGEXP "!!8723=5622" THEN @treatment_plan := 4 - END AS treatment_plan, - CASE - WHEN obs REGEXP "!!10039=" THEN @other_treatment_plan := GetValues(obs, 10039) - END AS other_treatment_plan, - CASE - WHEN obs REGEXP "!!10584=1260" THEN @remission_plan := 1 - WHEN obs REGEXP "!!10584=10581" THEN @remission_plan := 2 - WHEN obs REGEXP "!!10584=10582" THEN @remission_plan := 3 - WHEN obs REGEXP "!!10584=10583" THEN @remission_plan := 4 - END AS remission_plan, - CASE - WHEN obs REGEXP "!!10585" THEN @remission_start_date := GetValues(obs, 10585) - END AS remission_start_date, - CASE - WHEN obs REGEXP "!!10198=88!!" THEN @mm_supportive_plan := 1 - WHEN obs REGEXP "!!10198=257!!" THEN @mm_supportive_plan := 2 - WHEN obs REGEXP "!!10198=8598!!" THEN @mm_supportive_plan := 3 - WHEN obs REGEXP "!!10198=8597!!" THEN @mm_supportive_plan := 4 - WHEN obs REGEXP "!!10198=1195!!" THEN @mm_supportive_plan := 5 - WHEN obs REGEXP "!!10198=8410!!" THEN @mm_supportive_plan := 6 - WHEN obs REGEXP "!!10198=7458!!" THEN @mm_supportive_plan := 7 - WHEN obs REGEXP "!!10198=8479!!" THEN @mm_supportive_plan := 8 - WHEN obs REGEXP "!!10198=10140!!" THEN @mm_supportive_plan := 9 - WHEN obs REGEXP "!!10198=7207!!" THEN @mm_supportive_plan := 10 - WHEN obs REGEXP "!!10198=5622!!" THEN @mm_supportive_plan := 11 - ELSE @mm_supportive_plan := null - END AS mm_supportive_plan, - CASE - WHEN obs REGEXP "!!10170=10141!!" THEN @mm_signs_symptoms := 1 - WHEN obs REGEXP "!!10170=1885!!" THEN @mm_signs_symptoms := 2 - WHEN obs REGEXP "!!10170=3!!" THEN @mm_signs_symptoms := 3 - WHEN obs REGEXP "!!10170=8592!!" THEN @mm_signs_symptoms := 4 - WHEN obs REGEXP "!!10170=5978!!" THEN @mm_signs_symptoms := 5 - WHEN obs REGEXP "!!10170=5949!!" THEN @mm_signs_symptoms := 6 - WHEN obs REGEXP "!!10170=5622!!" THEN @mm_signs_symptoms := 7 - ELSE @mm_signs_symptoms := null - END AS mm_signs_symptoms, - CASE - WHEN obs REGEXP "!!9869=1256!!" THEN @chemotherapy_plan := 1 - WHEN obs REGEXP "!!9869=1259!!" THEN @chemotherapy_plan := 2 - WHEN obs REGEXP "!!9869=1260!!" THEN @chemotherapy_plan := 3 - WHEN obs REGEXP "!!9869=1257!!" THEN @chemotherapy_plan := 4 - WHEN obs REGEXP "!!9869=6576!!" THEN @chemotherapy_plan := 5 - ELSE @chemotherapy_plan := null - END AS chemotherapy_plan, - CASE - WHEN obs REGEXP "!!1190=" THEN @chemo_start_date := GetValues(obs,1190) - ELSE @chemo_start_date := null - END AS chemo_start_date, - CASE - WHEN obs REGEXP "!!6643=[0-9]" THEN @chemo_cycle := CAST(GetValues(obs,6643) AS unsigned) - ELSE @chemo_cycle := null - END AS chemo_cycle, - CASE - WHEN obs REGEXP "!!9927=1267!!" THEN @reason_chemo_stop := 1 - WHEN obs REGEXP "!!9927=7391!!" THEN @reason_chemo_stop := 2 - WHEN obs REGEXP "!!9927=6629!!" THEN @reason_chemo_stop := 3 - WHEN obs REGEXP "!!9927=6627!!" THEN @reason_chemo_stop := 4 - WHEN obs REGEXP "!!9927=1879!!" THEN @reason_chemo_stop := 5 - WHEN obs REGEXP "!!9927=5622!!" THEN @reason_chemo_stop := 6 - ELSE @reason_chemo_stop := null - END AS reason_chemo_stop, - CASE - WHEN obs REGEXP "!!9918=491!!" THEN @chemotherapy_drug := 1 - WHEN obs REGEXP "!!9918=7207!!" THEN @chemotherapy_drug := 2 - WHEN obs REGEXP "!!9918=8486!!" THEN @chemotherapy_drug := 3 - WHEN obs REGEXP "!!9918=7203!!" THEN @chemotherapy_drug := 4 - WHEN obs REGEXP "!!9918=8479!!" THEN @chemotherapy_drug := 5 - WHEN obs REGEXP "!!9918=7213!!" THEN @chemotherapy_drug := 6 - WHEN obs REGEXP "!!9918=8480!!" THEN @chemotherapy_drug := 7 - WHEN obs REGEXP "!!9918=5622!!" THEN @chemotherapy_drug := 8 - ELSE @chemotherapy_drug := null - END AS chemotherapy_drug, - CASE - WHEN obs REGEXP "!!1899=[0-9]" THEN @dosage_in_milligrams := CAST(GetValues(obs, 1899) AS unsigned) - ELSE @dosage_in_milligrams := null - END AS dosage_in_milligrams, - CASE - WHEN obs REGEXP "!!7463=7458!!" THEN @drug_route := 1 - WHEN obs REGEXP "!!7463=10078!!" THEN @drug_route := 2 - WHEN obs REGEXP "!!7463=10079!!" THEN @drug_route := 3 - WHEN obs REGEXP "!!7463=7597!!" THEN @drug_route := 4 - WHEN obs REGEXP "!!7463=7581!!" THEN @drug_route := 5 - WHEN obs REGEXP "!!7463=7609!!" THEN @drug_route := 6 - WHEN obs REGEXP "!!7463=7616!!" THEN @drug_route := 7 - WHEN obs REGEXP "!!7463=7447!!" THEN @drug_route := 8 - ELSE @drug_route := null - END AS drug_route, - CASE - WHEN obs REGEXP "!!1895=" THEN @other_drugs := GetValues(obs, 1895) - ELSE @other_drugs := null - END AS other_drugs, - CASE - WHEN obs REGEXP "!!1779=" THEN @other_medication := GetValues(obs, 1779) - ELSE @other_medication := null - END AS other_medication, - CASE - WHEN obs REGEXP "!!2206=9220!!" THEN @purpose := 1 - WHEN obs REGEXP "!!2206=8428!!" THEN @purpose := 2 - ELSE @purpose := null - END AS purpose, - CASE - WHEN obs REGEXP "!!6327=8728!!" THEN @education_given_today := 1 - WHEN obs REGEXP "!!6327=8742!!" THEN @education_given_today := 2 - WHEN obs REGEXP "!!6327=1905!!" THEN @education_given_today := 3 - WHEN obs REGEXP "!!6327=10208!!" THEN @education_given_today := 4 - WHEN obs REGEXP "!!6327=8730!!" THEN @education_given_today := 5 - WHEN obs REGEXP "!!6327=8371!!" THEN @education_given_today := 6 - WHEN obs REGEXP "!!6327=5622!!" THEN @education_given_today := 7 - ELSE @education_given_today := null - END AS education_given_today, - CASE - WHEN obs REGEXP "!!1272=1107!!" THEN @referral := 1 - WHEN obs REGEXP "!!1272=8724!!" THEN @referral := 2 - WHEN obs REGEXP "!!1272=6571!!" THEN @referral := 3 - WHEN obs REGEXP "!!1272=1286!!" THEN @referral := 4 - WHEN obs REGEXP "!!1272=6572!!" THEN @referral := 5 - WHEN obs REGEXP "!!1272=6573!!" THEN @referral := 6 - WHEN obs REGEXP "!!1272=1905!!" THEN @referral := 7 - WHEN obs REGEXP "!!1272=5622!!" THEN @referral := 8 - ELSE @referral := null - END AS referral, - CASE - WHEN obs REGEXP "!!5096=" THEN @next_app_date := GetValues(obs, 5096) - ELSE @next_app_date := null - END AS next_app_date - FROM - flat_multiple_myeloma_treatment_0 t1 - JOIN - amrs.person p using (person_id) - ORDER BY - person_id, DATE(encounter_datetime) DESC, encounter_type_sort_index DESC - ); - - SET @prev_id := null; - SET @cur_id := null; - SET @prev_encounter_datetime := null; - SET @cur_encounter_datetime := null; - - SET @prev_clinical_datetime := null; - SET @cur_clinical_datetime := null; - - SET @next_encounter_type := null; - SET @cur_encounter_type := null; - - SET @prev_clinical_location_id := null; - SET @cur_clinical_location_id := null; - - ALTER TABLE flat_multiple_myeloma_treatment_1 DROP prev_id, DROP cur_id; - - DROP TABLE IF EXISTS flat_multiple_myeloma_treatment_2; - CREATE TEMPORARY TABLE flat_multiple_myeloma_treatment_2 - (SELECT - *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - CASE - WHEN @prev_id = @cur_id THEN @prev_encounter_datetime := @cur_encounter_datetime - ELSE @prev_encounter_datetime := null - END AS next_encounter_datetime_multiple_myeloma, - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - CASE - when @prev_id = @cur_id THEN @next_encounter_type := @cur_encounter_type - ELSE @next_encounter_type := null - END AS next_encounter_type_multiple_myeloma, - @cur_encounter_type := encounter_type as cur_encounter_type, - CASE - when @prev_id = @cur_id THEN @prev_clinical_datetime := @cur_clinical_datetime - ELSE @prev_clinical_datetime := null - END AS next_clinical_datetime_multiple_myeloma, - CASE - when @prev_id = @cur_id THEN @prev_clinical_location_id := @cur_clinical_location_id - ELSE @prev_clinical_location_id := null - END AS next_clinical_location_id_multiple_myeloma, - CASE - when @prev_id = @cur_id THEN @prev_clinical_rtc_date := @cur_clinical_rtc_date - ELSE @prev_clinical_rtc_date := null - END AS next_clinical_rtc_date_multiple_myeloma, - CASE - when is_clinical_encounter THEN @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id THEN @cur_clinical_rtc_date - ELSE @cur_clinical_rtc_date:= null - END AS cur_clinical_rtc_date - FROM - flat_multiple_myeloma_treatment_1 - ORDER BY - person_id, DATE(encounter_datetime) DESC, encounter_type_sort_index DESC - ); - - ALTER TABLE flat_multiple_myeloma_treatment_2 DROP prev_id, DROP cur_id, DROP cur_encounter_type, DROP cur_encounter_datetime, DROP cur_clinical_rtc_date; - - SET @prev_id := null; - SET @cur_id := null; - SET @prev_encounter_type := null; - SET @cur_encounter_type := null; - SET @prev_encounter_datetime := null; - SET @cur_encounter_datetime := null; - SET @prev_clinical_datetime := null; - SET @cur_clinical_datetime := null; - SET @prev_clinical_location_id := null; - SET @cur_clinical_location_id := null; - - DROP TEMPORARY TABLE IF EXISTS flat_multiple_myeloma_treatment_3; - CREATE TEMPORARY TABLE flat_multiple_myeloma_treatment_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime DESC)) - (SELECT - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - CASE - when @prev_id = @cur_id THEN @prev_encounter_type := @cur_encounter_type - ELSE @prev_encounter_type := null - END AS prev_encounter_type_multiple_myeloma, - @cur_encounter_type := encounter_type as cur_encounter_type, - CASE - when @prev_id = @cur_id THEN @prev_encounter_datetime := @cur_encounter_datetime - ELSE @prev_encounter_datetime := null - END AS prev_encounter_datetime_multiple_myeloma, - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - CASE - when @prev_id = @cur_id THEN @prev_clinical_datetime := @cur_clinical_datetime - ELSE @prev_clinical_datetime := null - END AS prev_clinical_datetime_multiple_myeloma, - CASE - when @prev_id = @cur_id THEN @prev_clinical_location_id := @cur_clinical_location_id - ELSE @prev_clinical_location_id := null - END AS prev_clinical_location_id_multiple_myeloma, - CASE - when @prev_id = @cur_id THEN @prev_clinical_rtc_date := @cur_clinical_rtc_date - ELSE @prev_clinical_rtc_date := null - END AS prev_clinical_rtc_date_multiple_myeloma, - CASE - when is_clinical_encounter THEN @cur_clinical_rtc_date := next_app_date - when @prev_id = @cur_id THEN @cur_clinical_rtc_date - ELSE @cur_clinical_rtc_date:= null - END AS cur_clinic_rtc_date - FROM - flat_multiple_myeloma_treatment_2 t1 - ORDER BY - person_id, DATE(encounter_datetime), encounter_type_sort_index - ); - - SELECT - COUNT(*) - INTO - @new_encounter_rows - FROM - flat_multiple_myeloma_treatment_3; - - SELECT @new_encounter_rows; - SET @total_rows_written := @total_rows_written + @new_encounter_rows; - SELECT @total_rows_written; - - SET @dyn_sql := CONCAT('REPLACE INTO ', @write_table, - '(SELECT - null, - person_id, - encounter_id, - encounter_type, - DATE(t1.encounter_datetime) AS encounter_date, - visit_id, - location_id, - gender, - age, - encounter_purpose, - ecog_performance_index, - diagnosis_method, - diagnosis, - diagnosis_date, - cancer_stage, - overall_cancer_stage, - lab_test_order, - other_lab_test, - serum_m_protein, - treatment_plan, - other_treatment_plan, - remission_plan, - remission_start_date, - mm_supportive_plan, - mm_signs_symptoms, - chemotherapy_plan, - chemo_start_date, - chemo_cycle, - reason_chemo_stop, - chemotherapy_drug, - dosage_in_milligrams, - drug_route, - other_drugs, - other_medication, - purpose, - education_given_today, - referral, - next_app_date, - prev_encounter_datetime_multiple_myeloma, - next_encounter_datetime_multiple_myeloma, - prev_encounter_type_multiple_myeloma, - next_encounter_type_multiple_myeloma, - prev_clinical_datetime_multiple_myeloma, - next_clinical_datetime_multiple_myeloma, - prev_clinical_location_id_multiple_myeloma, - next_clinical_location_id_multiple_myeloma, - prev_clinical_rtc_date_multiple_myeloma, - next_clinical_rtc_date_multiple_myeloma - FROM - flat_multiple_myeloma_treatment_3 t1 - JOIN - amrs.location t2 USING (location_id))' - ); - - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql := CONCAT('DELETE t1 FROM ', @queue_table, ' t1 JOIN flat_multiple_myeloma_treatment_build_queue__0 t2 USING (person_id);'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - -- SELECT @person_ids_count := (SELECT count(*) FROM flat_breast_cancer_screening_build_queue_2); - SET @dyn_sql := CONCAT('SELECT COUNT(*) INTO @person_ids_count FROM ', @queue_table, ';'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - -- SELECT @person_ids_count as remaining_in_build_queue; - - SET @cycle_length := TIMESTAMPDIFF(SECOND, @loop_start_time, NOW()); - -- SELECT CONCAT('Cycle time: ',@cycle_length,' seconds'); - SET @total_time := @total_time + @cycle_length; - SET @cycle_number := @cycle_number + 1; - -- SELECT ceil(@person_ids_count / cycle_size) as remaining_cycles; - SET @remaining_time := ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - SELECT - @person_ids_count AS 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - - END WHILE; - - IF (@query_type = "build") THEN - SET @dyn_sql := CONCAT('DROP TABLE ', @queue_table, ';'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write := 0; - SET @dyn_sql := CONCAT("SELECT COUNT(*) INTO @total_rows_to_write FROM ", @write_table); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @start_write := NOW(); - SELECT CONCAT(@start_write, ' : Writing ', @total_rows_to_write, ' to ', @primary_table); - - SET @dyn_sql := CONCAT('REPLACE INTO ', @primary_table, '(SELECT * FROM ', @write_table, ');'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @finish_write := NOW(); - SET @time_to_write := TIMESTAMPDIFF(SECOND, @start_write, @finish_write); - - SELECT CONCAT(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql := CONCAT('drop table ', @write_table, ';'); - PREPARE s1 FROM @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - END IF; - - SET @ave_cycle_length := ceil(@total_time / @cycle_number); - SELECT CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - SET @end := NOW(); - INSERT INTO etl.flat_log VALUES (@start, @last_date_created, @table_version, TIMESTAMPDIFF(SECOND, @start, @end)); - SELECT CONCAT(@table_version, ': Time to complete: ', TIMESTAMPDIFF(MINUTE, @start, @end), ' minutes'); -END \ No newline at end of file diff --git a/etl-scripts/stored-procedures/generate_flat_obs_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_obs.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_flat_obs_v1_0.sql rename to etl-scripts/stored-procedures/generate_flat_obs.sql index 2f95aa1..6971432 100644 --- a/etl-scripts/stored-procedures/generate_flat_obs_v1_0.sql +++ b/etl-scripts/stored-procedures/generate_flat_obs.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_obs_v_1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_obs`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN set @primary_table := "flat_obs"; set @query_type = query_type; diff --git a/etl-scripts/stored-procedures/generate_flat_obs_v2_0.sql b/etl-scripts/stored-procedures/generate_flat_obs_v2_0.sql deleted file mode 100644 index 54b80f9..0000000 --- a/etl-scripts/stored-procedures/generate_flat_obs_v2_0.sql +++ /dev/null @@ -1,265 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_obs_v2_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - - set session group_concat_max_len=100000; - set @start = now(); - set @primary_table := "flat_obs_2"; - set @table_version = "flat_obs_v2.0"; - set @query_type = query_type; - set @total_rows_written = 0; - set @queue_number = queue_number; - set @queue_size = queue_size; - set @cycle_size = cycle_size; - -#set @queue_number = 1; -#set @queue_size = 10000; -#set @cycle_size = 1000; - - select max(date_created) into @last_date_created_enc from amrs.encounter; - select max(date_created) into @last_date_created_obs from amrs.obs; - set @last_date_created = if(@last_date_created_enc > @last_date_created_obs,@last_date_created_enc,@last_date_created_obs); - - - set @boundary = "!!"; - - SET @dyn_sql = CONCAT('create table if not exists ', @primary_table, - '(date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - location_id int, - obs text, - obs_datetimes text, - max_date_created datetime, - index encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index person_enc_id (person_id,encounter_id), - index max_date_created (max_date_created), - index date_created (date_created), - primary key (encounter_id) - );'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - select CONCAT('BUILDING ',@primary_table,'..........................................'); - set @write_table = concat(@primary_table,"_temp_",@queue_number); - set @queue_table = concat("flat_obs_build_queue_",@queue_number); - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(encounter_id int primary key) (select * from flat_obs_build_queue limit ', @queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_obs_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from flat_obs_build_queue t1 join ',@queue_table, ' t2 using (encounter_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - - end if; - - - if (@query_type="sync") then - select 'SYNCING FLAT_OBS..........................................'; - set @write_table = @primary_table; - set @queue_table = "flat_obs_sync_queue"; - create table if not exists flat_obs_sync_queue (encounter_id int primary key); - - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; -set @last_update = "2018-01-26"; - - # find all encounters that have date_created, date_voided, or date_changed after @last_update - SET @dyn_sql=CONCAT('replace into ',@queue_table, - '(select encounter_id - from amrs.encounter t1 - where - CASE - when t1.date_created >= @last_update and t1.voided=0 then 1 - when t1.date_created <= @last_update and t1.date_voided > @last_update then 1 - when t1.date_created <= @last_update and t1.date_changed > @last_update then 1 - else 0 - end - )'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #find all encounters which have a voided obs since last update - SET @dyn_sql=CONCAT('replace into ',@queue_table, - '(select encounter_id - from amrs.obs t1 - where t1.date_created <= @last_update - AND t1.date_voided > @last_updated - AND t1.encounter_id > 0 - )'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - # delete all rows in primary table in the queue - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table,' t1 join ',@queue_table, ' t2 using (encounter_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - while @queue_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists temp_queue_table; - - SET @dyn_sql = CONCAT('create temporary table temp_queue_table like ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql= CONCAT('replace into temp_queue_table - (select * from ',@queue_table,' limit ', @cycle_size,')'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - select count(*) as '# rows to write' from temp_queue_table; - - SET @dyn_sql = CONCAT('replace into ',@write_table, - '(select - null, - o.person_id, - e.visit_id, - o.encounter_id, - e.encounter_datetime, - e.encounter_type, - e.location_id, - group_concat( - case - when value_coded is not null then concat(@boundary,o.concept_id,\'=\',value_coded,@boundary) - when value_numeric is not null then concat(@boundary,o.concept_id,\'=\',value_numeric,@boundary) - when value_datetime is not null then concat(@boundary,o.concept_id,\'=\',date(value_datetime),@boundary) - when value_text is not null then concat(@boundary,o.concept_id,\'=\',value_text,@boundary) - when value_drug is not null then concat(@boundary,o.concept_id,\'=\',value_drug,@boundary) - when value_modifier is not null then concat(@boundary,o.concept_id,\'=\',value_modifier,@boundary) - end - order by o.concept_id,value_coded - separator \' ## \' - ) as obs, - - group_concat( - case - when value_coded is not null or value_numeric is not null or value_datetime is not null or value_boolean is not null or value_text is not null or value_drug is not null or value_modifier is not null - then concat(@boundary,o.concept_id,\'=\',date(o.obs_datetime),@boundary) - end - order by o.concept_id,value_coded - separator \' ## \' - ) as obs_datetimes, - max(o.date_created) as max_date_created - - from temp_queue_table - join amrs.obs o using (encounter_id) - join amrs.encounter e using (encounter_id) - where - o.encounter_id > 0 and o.voided=0 - group by encounter_id - );'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join temp_queue_table t2 using (encounter_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@queue_count / @cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select @queue_count as '# in queue', @cycle_length as 'Cycle Time (s)', ceil(@queue_count / @cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - SET @dyn_sql = CONCAT( - 'delete t1 - from ',@primary_table, ' t1 - join amrs.person t2 using (person_id) - where t2.voided=1;'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - select @end := now(); - insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_onc_patient_history_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_onc_patient_history.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_flat_onc_patient_history_v1_0.sql rename to etl-scripts/stored-procedures/generate_flat_onc_patient_history.sql index a0e567a..e397d75 100644 --- a/etl-scripts/stored-procedures/generate_flat_onc_patient_history_v1_0.sql +++ b/etl-scripts/stored-procedures/generate_flat_onc_patient_history.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_onc_patient_history_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_onc_patient_history`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN SET @primary_table := "flat_onc_patient_history"; SET @query_type := query_type; diff --git a/etl-scripts/stored-procedures/generate_flat_orders_v2_0.sql b/etl-scripts/stored-procedures/generate_flat_orders.sql similarity index 98% rename from etl-scripts/stored-procedures/generate_flat_orders_v2_0.sql rename to etl-scripts/stored-procedures/generate_flat_orders.sql index 70bcba8..6845708 100644 --- a/etl-scripts/stored-procedures/generate_flat_orders_v2_0.sql +++ b/etl-scripts/stored-procedures/generate_flat_orders.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_orders_v2_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_orders`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN set session group_concat_max_len=100000; diff --git a/etl-scripts/stored-procedures/generate_flat_orders_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_orders_v1_0.sql deleted file mode 100644 index 349294d..0000000 --- a/etl-scripts/stored-procedures/generate_flat_orders_v1_0.sql +++ /dev/null @@ -1,251 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_orders_v_1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_orders"; - set @query_type = query_type; - set @queue_table = ""; - set @total_rows_written = 0; - - set @start = now(); - set @table_version = "flat_orders_v1.8"; - - set session sort_buffer_size=512000000; - - - - - CREATE TABLE IF NOT EXISTS flat_orders ( - person_id INT, - encounter_id INT, - order_id INT, - encounter_datetime DATETIME, - encounter_type INT, - date_activated DATETIME, - orders TEXT, - order_datetimes TEXT, - max_date_created DATETIME, - INDEX encounter_id (encounter_id), - INDEX person_enc_id (person_id , encounter_id), - INDEX date_created (max_date_created), - PRIMARY KEY (encounter_id) -); - - SELECT CONCAT('Created flat_orders_table'); - - if(@query_type="build") then - select 'BUILDING..........................................'; - set @write_table = concat("flat_orders_test_temp_",queue_number); - set @queue_table = concat("flat_orders_test_build_queue_",queue_number); - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select person_id from flat_orders_test_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('delete t1 from flat_orders_test_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - -SELECT @person_ids_count AS 'num patients to sync/build'; - - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - drop temporary table if exists flat_orders_test_build_queue__0; - - - SET @dyn_sql=CONCAT('create temporary table IF NOT EXISTS flat_orders_test_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - CREATE temporary TABLE IF NOT EXISTS flat_person_encounters__0 ( - person_id INT, - visit_id INT, - encounter_id INT, - encounter_datetime DATETIME, - encounter_type INT, - location_id INT - ); - -SELECT CONCAT('Created flat person encounters table'); - - - SELECT CONCAT('replace into flat_person_encounters__0'); - - replace into flat_person_encounters__0( - SELECT - p.person_id, - e.visit_id, - e.encounter_id, - e.encounter_datetime, - e.encounter_type, - e.location_id - FROM - etl.flat_orders_test_build_queue__0 `p` - INNER JOIN amrs.encounter `e` on (e.patient_id = p.person_id) - ); - - - drop temporary table if exists flat_orders_test__0; - - create temporary table flat_orders_test__0 - (select - o.patient_id, - o.encounter_id, - o.order_id, - e.encounter_datetime, - e.encounter_type, - o.date_activated, - e.location_id, - group_concat(concept_id order by o.concept_id separator ' ## ') as orders, - group_concat(concat(@boundary,o.concept_id,'=',date(o.date_activated),@boundary) order by o.concept_id separator ' ## ') as order_datetimes, - max(o.date_created) as max_date_created - - from amrs.orders o - join flat_person_encounters__0 `e` using (encounter_id) - where o.encounter_id > 0 - and o.voided=0 - group by o.encounter_id - ); - - - drop temporary table if exists flat_person_encounters__0; - - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - patient_id, - encounter_id, - order_id, - encounter_datetime, - encounter_type, - date_activated, - orders, - order_datetimes, - max_date_created - from flat_orders_test__0 t1)'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_orders_test_build_queue__0 t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - - SELECT - @person_ids_count AS 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - - end while; - - - if(@query_type="build") then - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_orders_test_build_queue__0 t2 using (person_id);'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); -SELECT - CONCAT(@start_write, - ' : Writing ', - @total_rows_to_write, - ' to ', - @primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - SELECT - CONCAT(@finish_write, - ' : Completed writing rows. Time to write to primary table: ', - @time_to_write, - ' seconds '); - - - - - - end if; - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_sickle_cell_treatment_v1_0.sql b/etl-scripts/stored-procedures/generate_flat_sickle_cell_treatment.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_flat_sickle_cell_treatment_v1_0.sql rename to etl-scripts/stored-procedures/generate_flat_sickle_cell_treatment.sql index cd4123b..94771af 100644 --- a/etl-scripts/stored-procedures/generate_flat_sickle_cell_treatment_v1_0.sql +++ b/etl-scripts/stored-procedures/generate_flat_sickle_cell_treatment.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_sickle_cell_treatment_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_flat_sickle_cell_treatment`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN set @primary_table := "flat_sickle_cell_treatment"; set @query_type = query_type; diff --git a/etl-scripts/stored-procedures/generate_flat_test_obs_v2_0.sql b/etl-scripts/stored-procedures/generate_flat_test_obs_v2_0.sql deleted file mode 100644 index cdc728c..0000000 --- a/etl-scripts/stored-procedures/generate_flat_test_obs_v2_0.sql +++ /dev/null @@ -1,279 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_test_obs_v2_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - - set session group_concat_max_len=100000; - set @start = now(); - set @primary_table := "flat_test_obs"; - set @table_version = "flat_test_obs_v2.0"; - set @query_type = query_type; - set @total_rows_written = 0; - set @queue_number = queue_number; - set @queue_size = queue_size; - set @cycle_size = cycle_size; - -#set @queue_number = 1; -#set @queue_size = 10000; -#set @cycle_size = 1000; - - set @test_concept_class_id = 1; - - select max(date_created) into @last_date_created_obs from amrs.obs; - - set @boundary = "!!"; - SET @dyn_sql = CONCAT('create table if not exists ', @primary_table, - '(date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - encounter_id int, - test_datetime datetime, - encounter_type int, - location_id int, - obs text, - max_date_created datetime, - encounter_ids text, - obs_ids text, - index encounter_id (encounter_id), - index person_test_datetime (person_id, test_datetime), - index person_enc_id (person_id,encounter_id), - index max_date_created (max_date_created), - index date_created (date_created), - primary key (person_id, test_datetime) - );'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - - select CONCAT('BUILDING ',@primary_table,'..........................................') as 'Job'; - set @write_table = concat(@primary_table,'_temp_',@queue_number); - set @build_queue = concat(@primary_table,'_build_queue'); - set @queue_table = concat(@build_queue,"_",@queue_number); - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - #create table if not exists @queue_table (person_id int, primary key (person_id)); - - SET @dyn_sql=CONCAT( - 'Create table if not exists ',@queue_table,'(person_id int, obs_datetime datetime, index test_datetime (person_id, obs_datetime)) - (select * from ',@build_queue,' limit ', @queue_size, ');' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_lab_obs_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from ',@build_queue, ' t1 join ',@queue_table, ' t2 using (person_id, obs_datetime);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - - end if; - - - if (@query_type="sync") then - select CONCAT('SYNCING ',@primary_table,'..........................................'); - - set @write_table = @primary_table; - set @queue_table = concat(@primary_table,'_sync_queue'); - - create table if not exists flat_test_obs_sync_queue (person_id int, obs_datetime datetime, index test_datetime (person_id, obs_datetime)); - - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - -#set @last_update = "2018-01-26"; - - SET @dyn_sql=CONCAT('replace into ',@queue_table, - '(select person_id, obs_datetime - from amrs.obs t1 - join amrs.concept t2 using (concept_id) - where - t1.date_created >= @last_update and t1.voided=0 - AND - CASE - when class_id=@test_concept_class_id then 1 - when concept_id=1271 and value_coded=1107 then 0 - when concept_id in (1271,9239,9020) then 1 - else 0 - END - group by person_id, obs_datetime - )'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #NOTE: For indexing reasons, we have done the following in a separate query rather than include in the above. - SET @dyn_sql=CONCAT('replace into ',@queue_table, - '(select person_id, obs_datetime - from amrs.obs t1 - join amrs.concept t2 using (concept_id) - where t1.date_voided > @last_update and t1.date_created <= @last_update - AND - CASE - when class_id=@test_concept_class_id then 1 - when concept_id=1271 and value_coded=1107 then 0 - when concept_id in (1271,9239,9020) then 1 - else 0 - END - group by person_id, obs_datetime - )'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - select count(*) as 'Rows to Sync' from flat_lab_obs_sync_queue; - - end if; - - # delete all rows in primary table in the queue - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table,' t1 join ', @queue_table,' t2 on t1.person_id = t2.person_id and t1.test_datetime = t2.obs_datetime'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - - while @queue_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists temp_queue_table; - - SET @dyn_sql = CONCAT('create temporary table temp_queue_table like ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql= CONCAT('replace into temp_queue_table - (select * from ',@queue_table,' limit ', @cycle_size,')'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql = CONCAT('replace into ',@write_table, - '(select - null, - o.person_id, - min(o.obs_id) + 100000000 as encounter_id, - o.obs_datetime, - 99999 as encounter_type, - null as location_id, - group_concat( - case - when value_coded is not null then concat(@boundary,o.concept_id,\'=\',value_coded,@boundary) - when value_numeric is not null then concat(@boundary,o.concept_id,\'=\',value_numeric,@boundary) - when value_datetime is not null then concat(@boundary,o.concept_id,\'=\',date(value_datetime),@boundary) - when value_text is not null then concat(@boundary,o.concept_id,\'=\',value_text,@boundary) - when value_drug is not null then concat(@boundary,o.concept_id,\'=\',value_drug,@boundary) - when value_modifier is not null then concat(@boundary,o.concept_id,\'=\',value_modifier,@boundary) - end - order by o.concept_id,value_coded - separator \' ## \' - ) as obs, - max(o.date_created) as max_date_created, - group_concat(concat(@boundary,o.concept_id,\'=\',o.value_coded,\'=\',if(encounter_id is not null,encounter_id,\'\'),@boundary)) as encounter_ids, - group_concat(concat(@boundary,o.concept_id,\'=\',o.obs_id,@boundary)) as obs_ids - - from amrs.obs o - join ',@queue_table,' t2 using (person_id, obs_datetime) - join amrs.concept using (concept_id) - where o.voided=0 - AND - CASE - when class_id=1 then 1 - when concept_id=1271 and value_coded=1107 then 0 - when concept_id in (1271,9239,9020) then 1 - else 0 - END - - group by person_id, obs_datetime - );'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join temp_queue_table t2 using (person_id, obs_datetime);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@queue_count / @cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select @queue_count as '# in queue', @cycle_length as 'Cycle Time (s)', ceil(@queue_count / @cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - SET @dyn_sql = CONCAT( - 'delete t1 - from ',@primary_table, ' t1 - join amrs.person t2 using (person_id) - where t2.voided=1;'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - select @end := now(); - insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_flat_tests_and_imaging.sql b/etl-scripts/stored-procedures/generate_flat_tests_and_imaging.sql deleted file mode 100644 index 5c2ded0..0000000 --- a/etl-scripts/stored-procedures/generate_flat_tests_and_imaging.sql +++ /dev/null @@ -1,551 +0,0 @@ -#******************************************************************************************************** -#* CREATION OF LABS AND IMAGING FLAT TABLE **************************************************************************** -#******************************************************************************************************** - -# Need to first create this temporary table to sort the data by person,encounterdateime. -# This allows us to use the previous row's data when making calculations. -# It seems that if you don't create the temporary table first, the sort is applied -# to the final result. Any references to the previous row will not an ordered row. - -# v2.1 Notes: -# Added encounter types for GENERALNOTE (112), CLINICREVIEW (113), MOH257BLUECARD (114), HEIFOLLOWUP (115), TRANSFERFORM (116) -# Added timestamp to log - -# v2.2 Notes: -# Add ability to handle error messages. Add columns for has_errors, vl_error, cd4_error, hiv_dna_pcr_error -# Delete all rows for a patient before inserting new data - -# v2.5 Notes: -# use date(t1.test_datetime) = date(t2.encounter_datetime) joining column for flat_lab_obs table instead of encounter_id - - -use etl; -drop procedure if exists generate_flat_labs_and_imaging_v3_0; - -DELIMITER $$ - CREATE PROCEDURE generate_flat_labs_and_imaging_v3_0(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) - BEGIN - set session sort_buffer_size=512000000; - set session group_concat_max_len=100000; - set @start = now(); - set @primary_table := "flat_labs_and_imaging"; - select @table_version := 'flat_labs_and_imaging_v3.0'; - set @total_rows_written = 0; - set @query_type = query_type; - set @queue_number = queue_number; - set @queue_size = queue_size; - set @cycle_size = cycle_size; - - # 1030 = HIV DNA PCR - # 1040 = HIV Rapid test - # 856 = HIV VIRAL LOAD, QUANTITATIVE - # 5497 = CD4, BY FACS - # 730 = CD4%, BY FACS - # 21 = HEMOGLOBIN - # 653 = AST - # 790 = SERUM CREATININE - # 12 = X-RAY, CHEST, PRELIMINARY FINDINGS - # 6126 = hba1c - # 887 = rbs - # 6252 = fbs - # 1537 = ecg - # 857 = SERUM BLOOD UREA NITROGEN - # 1271 = TESTS ORDERED - # 9239 = LABORATORY TEST WITH EXCEPTION - # 9020 = LAB ERROR - set @concept_ids = '(1030, 1040, 856, 5497, 730, 21,653,790,12,6126,887,6252,1537,1271,9239,9020,857)'; - -#set @queue_number = 1; -#set @queue_size = 10000; -#set @cycle_size = 1000; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - set @boundary = "!!"; - set @sep = " ## "; - - SET @dyn_sql = CONCAT('create table if not exists ', @primary_table, - ' (date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - encounter_id int, - test_datetime datetime, - encounter_type int, - hiv_dna_pcr int, - hiv_rapid_test int, - hiv_viral_load int, - cd4_count int, - cd4_percent decimal, - hemoglobin decimal, - ast int, - creatinine decimal, - chest_xray int, - hba1c decimal, - rbs decimal, - fbs decimal, - ecg int, - urea decimal, - has_errors text, - vl_error boolean, - cd4_error boolean, - hiv_dna_pcr_error boolean, - - tests_ordered varchar(1000), - primary key encounter_id (encounter_id), - index person_date (person_id, test_datetime), - index person_uuid (uuid) - )'); - - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - select CONCAT('BUILDING ',@primary_table,'..........................................'); - set @write_table = concat(@primary_table,"_temp_",@queue_number); - set @build_queue = concat(@primary_table,'_build_queue'); - set @queue_table = concat(@build_queue,'_',@queue_number); - - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(encounter_id int primary key) (select * from ',@build_queue,' limit ', @queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_obs_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from ',@build_queue,' t1 join ',@queue_table, ' t2 using (encounter_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - - end if; - - - if (@query_type="sync") then - select CONCAT('SYNCING ',@primary_table,'..........................................'); - set @write_table = @primary_table; - set @queue_table = concat(@primary_table,'_sync_queue'); - - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; -#set @last_update = "2018-01-26"; -create table flat_labs_and_imaging_sync_queue (person_id int, obs_datetime datetime, index test_datetime (person_id, obs_datetime)); - - # find all encounters that have date_created, date_voided, or date_changed after @last_update - SET @dyn_sql=CONCAT( - 'replace into ',@queue_table, - '(select person_id, test_datetime - from flat_lab_obs - where date_created > @last_update - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - #find all voided obs since last update - SET @dyn_sql=CONCAT('replace into ',@queue_table, - '(select person_id, obs_datetime - from amrs.obs t1 - where t1.date_voided > @last_updated - and concept_id in ',@concept_ids,' - group by person_id, obs_datetime)' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - # delete all rows in primary table in the queue - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table,' t1 join ',@queue_table, ' t2 using (encounter_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - while @queue_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists temp_queue_table; - - SET @dyn_sql = CONCAT('create temporary table temp_queue_table like ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql= CONCAT('replace into temp_queue_table (select * from ',@queue_table,' limit ', @cycle_size,')'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - select count(*) as '# rows to write' from flat_vitals_0; - - SET @dyn_sql = CONCAT('replace into ',@write_table, - '( - - - )'); - - - -replace into flat_labs_and_imaging_sync_queue -(select person_id, test_datetime from flat_test_obs); - - -drop table if exists flat_labs_and_imaging_0; -set @queue_table = 'flat_labs_and_imaging_sync_queue'; - - SET @dyn_sql= CONCAT( - 'create temporary table flat_labs_and_imaging_0(primary key (encounter_id), index encounter_id (encounter_id), index person_test (person_id,test_datetime)) - (select t1.person_id, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t3.orders, - if(obs regexp "!!1030=",cast(replace(replace((substring_index(substring(obs,locate("1030=",obs)),@sep,1)),"1030=",""),"!!","") as unsigned),null) as hiv_dna_pcr, - if(obs regexp "!!1040=",cast(replace(replace((substring_index(substring(obs,locate("1040=",obs)),@sep,1)),"1040=",""),"!!","") as unsigned),null) as hiv_rapid_test, - case - when obs regexp "!!856=" then cast(replace(replace((substring_index(substring(obs,locate("856=",obs)),@sep,1)),"856=",""),"!!","") as unsigned) - end as hiv_viral_load, - if(obs regexp "!!5497=",cast(replace(replace((substring_index(substring(obs,locate("5497=",obs)),@sep,1)),"5497=",""),"!!","") as unsigned),null) as cd4_count, - if(obs regexp "!!730=",cast(replace(replace((substring_index(substring(obs,locate("730=",obs)),@sep,1)),"730=",""),"!!","") as decimal(3,1)),null) as cd4_percent, - if(obs regexp "!!21=",cast(replace(replace((substring_index(substring(obs,locate("21=",obs)),@sep,1)),"21=",""),"!!","") as decimal(4,1)),null) as hemoglobin, - if(obs regexp "!!653=",cast(replace(replace((substring_index(substring(obs,locate("653=",obs)),@sep,1)),"653=",""),"!!","") as unsigned),null) as ast, - if(obs regexp "!!790=",cast(replace(replace((substring_index(substring(obs,locate("790=",obs)),@sep,1)),"790=",""),"!!","") as decimal(4,1)),null) as creatinine, - if(obs regexp "!!12=" and not obs regexp "!!12=1107",cast(replace(replace((substring_index(substring(obs,locate("12=",obs)),@sep,1)),"12=",""),"!!","") as unsigned),null) as chest_xray, - if(obs regexp "!!6126=",cast(replace(replace((substring_index(substring(obs,locate("6126=",obs)),@sep,1)),"6126=",""),"!!","") as decimal(4,1)),null) as hba1c, - if(obs regexp "!!887=",cast(replace(replace((substring_index(substring(obs,locate("887=",obs)),@sep,1)),"887=",""),"!!","") as decimal(4,1)),null) as rbs, - if(obs regexp "!!6252=",cast(replace(replace((substring_index(substring(obs,locate("6252=",obs)),@sep,1)),"6252=",""),"!!","") as decimal(4,1)),null) as fbs, - if(obs regexp "!!1537=",cast(replace(replace((substring_index(substring(obs,locate("1537=",obs)),@sep,1)),"1537=",""),"!!","") as unsigned),null) as ecg, - if(obs regexp "!!857=",cast(replace(replace((substring_index(substring(obs,locate("857=",obs)),@sep,1)),"857=",""),"!!","") as decimal(4,1)),null) as urea, - if(obs regexp "!!9239=",obs,null) as has_errors, - if(obs regexp "!!9239=856!!",1,null) as vl_error, - if(obs regexp "!!9239=5497!!",1,null) as cd4_error, - if(obs regexp "!!9239=1030",1,null) as hiv_dna_pcr_error, - CONCAT( - case - when obs regexp "!!1271=" then - replace(replace((substring_index(substring(obs,locate("!!1271=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1271=", "") ) ) / LENGTH("!!1271=") ))),"!!1271=",""),"!!","") - else "" - end, - ifnull(orders,"") - ) as tests_ordered - - from flat_test_obs t1 ##NEED TO UPDATE - join ', @queue_table,' t2 on t1.person_id = t2.person_id and t1.test_datetime = t2.obs_datetime - left join flat_orders t3 on t1.test_datetime = t3.encounter_datetime and t1.person_id = t3.person_id - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - set @last_update = '2016-03-01'; - SET @dyn_sql= CONCAT( - 'insert into flat_labs_and_imaging_0 (person_id, encounter_id, test_datetime, encounter_type, location_id, orders,tests_ordered) - ( select t1.person_id, - t1.encounter_id, - t1.encounter_datetime as test_datetime, - t3.encounter_type, - t3.location_id, - t1.orders, - 0 as tests_ordered - from flat_orders t1 - join amrs.encounter t2 using (encounter_id) - left outer join flat_test_obs t3 ON t1.person_id = t3.person_id AND t1.encounter_datetime = t3.test_datetime - where t1.encounter_datetime >= @last_update and t3.person_id is null - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql = CONCAT( - 'insert into ',@write_table, - '(select - person_id, - t1.uuid, - encounter_id, - test_datetime, - encounter_type, - hiv_dna_pcr, - hiv_rapid_test, - hiv_viral_load, - cd4_count, - cd4_percent, - hemoglobin, - ast, - creatinine, - chest_xray, - hba1c, - rbs, - fbs, - ecg, - urea, - has_errors, - vl_error, - cd4_error, - hiv_dna_pcr_error, - tests_ordered - from flat_labs_and_imaging_0 - )' - ); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join temp_queue_table t2 using (person_id, obs_datetime);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@queue_count / @cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select @queue_count as '# in queue', @cycle_length as 'Cycle Time (s)', ceil(@queue_count / @cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - SET @dyn_sql = CONCAT( - 'delete t1 - from ',@primary_table, ' t1 - join amrs.person t2 using (person_id) - where t2.voided=1;'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - select @end := now(); - insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END $$ -DELIMITER ; - - -set session sort_buffer_size=512000000; -set session group_concat_max_len=100000; - -select @sep := " ## "; -select @unknown_encounter_type := 99999; - - -select @start := now(); -select @last_date_created := (select max(max_date_created) from flat_lab_obs); - -select @last_update := (select max(date_updated) from flat_log where table_name=@table_version); - -# then use the max_date_created from amrs.encounter. This takes about 10 seconds and is better to avoid. -select @last_update := - if(@last_update is null, - (select max(date_created) from amrs.encounter e join flat_labs_and_imaging using (encounter_id)), - @last_update); - -#otherwise set to a date before any encounters had been created (i.g. we will get all encounters) -select @last_update := if(@last_update,@last_update,'1900-01-01'); -#select @last_update := "2015-05-14"; - - -delete t1 -from flat_labs_and_imaging t1 -join new_data_person_ids_0 t2 using (person_id); - -drop table if exists flat_labs_and_imaging_0; -create temporary table flat_labs_and_imaging_0(index encounter_id (encounter_id), index person_test (person_id,test_datetime)) -(select * from -((select t1.person_id, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t2.orders - from flat_lab_obs t1 - join new_data_person_ids_0 t0 using (person_id) - left join flat_orders t2 on date(t1.test_datetime) = date(t2.encounter_datetime) and t1.person_id = t2.person_id) - UNION ALL -( select t1.person_id, - t1.encounter_id, - t1.encounter_datetime as test_datetime, - t2.encounter_type, - t2.location_id, - t2.obs, - t1.orders - from flat_orders t1 - left join flat_lab_obs t2 on date(t2.test_datetime) = date(t1.encounter_datetime) and t1.person_id = t2.person_id - where t1.encounter_datetime >= @last_update and t2.person_id is null - -) order by person_id, test_datetime) as derived ); - - -select @prev_id := null; -select @cur_id := null; -select @cur_location := null; -select @vl := null; -select @cd4_count := null; -Select @cd4_percent := null; -select @hemoglobin := null; -select @ast := null; -select @creatinine := null; -select @chest_xray := null; - -drop temporary table if exists flat_labs_and_imaging_1; -create temporary table flat_labs_and_imaging_1 (index encounter_id (encounter_id)) -(select - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as location_id, - - # 1030 = HIV DNA PCR - # 1040 = HIV Rapid test - # 856 = HIV VIRAL LOAD, QUANTITATIVE - # 5497 = CD4, BY FACS - # 730 = CD4%, BY FACS - # 21 = HEMOGLOBIN - # 653 = AST - # 790 = SERUM CREATININE - # 12 = X-RAY, CHEST, PRELIMINARY FINDINGS - # 6126 = hba1c - # 887 = rbs - # 6252 = fbs - # 1537 = ecg - # 1537 = urea - # 1271 = TESTS ORDERED - # 9239 = LABORATORY TEST WITH EXCEPTION - # 9020 = LAB ERROR - - if(obs regexp "!!1030=",cast(replace(replace((substring_index(substring(obs,locate("1030=",obs)),@sep,1)),"1030=",""),"!!","") as unsigned),null) as hiv_dna_pcr, - if(obs regexp "!!1040=",cast(replace(replace((substring_index(substring(obs,locate("1040=",obs)),@sep,1)),"1040=",""),"!!","") as unsigned),null) as hiv_rapid_test, - - case - when obs regexp "!!856=" then cast(replace(replace((substring_index(substring(obs,locate("856=",obs)),@sep,1)),"856=",""),"!!","") as unsigned) - end as hiv_viral_load, - if(obs regexp "!!5497=",cast(replace(replace((substring_index(substring(obs,locate("5497=",obs)),@sep,1)),"5497=",""),"!!","") as unsigned),null) as cd4_count, - if(obs regexp "!!730=",cast(replace(replace((substring_index(substring(obs,locate("730=",obs)),@sep,1)),"730=",""),"!!","") as decimal(3,1)),null) as cd4_percent, - if(obs regexp "!!21=",cast(replace(replace((substring_index(substring(obs,locate("21=",obs)),@sep,1)),"21=",""),"!!","") as decimal(4,1)),null) as hemoglobin, - if(obs regexp "!!653=",cast(replace(replace((substring_index(substring(obs,locate("653=",obs)),@sep,1)),"653=",""),"!!","") as unsigned),null) as ast, - if(obs regexp "!!790=",cast(replace(replace((substring_index(substring(obs,locate("790=",obs)),@sep,1)),"790=",""),"!!","") as decimal(4,1)),null) as creatinine, - if(obs regexp "!!12=" and not obs regexp "!!12=1107",cast(replace(replace((substring_index(substring(obs,locate("12=",obs)),@sep,1)),"12=",""),"!!","") as unsigned),null) as chest_xray, - if(obs regexp "!!6126=",cast(replace(replace((substring_index(substring(obs,locate("6126=",obs)),@sep,1)),"6126=",""),"!!","") as decimal(4,1)),null) as hba1c, - if(obs regexp "!!887=",cast(replace(replace((substring_index(substring(obs,locate("887=",obs)),@sep,1)),"887=",""),"!!","") as decimal(4,1)),null) as rbs, - if(obs regexp "!!6252=",cast(replace(replace((substring_index(substring(obs,locate("6252=",obs)),@sep,1)),"6252=",""),"!!","") as decimal(4,1)),null) as fbs, - if(obs regexp "!!1537=",cast(replace(replace((substring_index(substring(obs,locate("1537=",obs)),@sep,1)),"1537=",""),"!!","") as unsigned),null) as ecg, - if(obs regexp "!!857=",cast(replace(replace((substring_index(substring(obs,locate("857=",obs)),@sep,1)),"857=",""),"!!","") as decimal(4,1)),null) as urea, - if(obs regexp "!!9239=",obs,null) as has_errors, - if(obs regexp "!!9239=856!!",1,null) as vl_error, - if(obs regexp "!!9239=5497!!",1,null) as cd4_error, - if(obs regexp "!!9239=1030",1,null) as hiv_dna_pcr_error, - CASE - WHEN - (obs REGEXP '!!1271=' - AND NOT obs REGEXP '!!1271=1107') - THEN - CONCAT(REPLACE(REPLACE((SUBSTRING_INDEX(SUBSTRING(obs, LOCATE('!!1271=', obs)), - @sep, - ROUND((LENGTH(obs) - LENGTH(REPLACE(obs, '1271=', ''))) / LENGTH('!!1271=')))), - '!!1271=', - ''), - '!!', - ''), - ' ## ', - IFNULL(orders, '')) - ELSE orders - END AS tests_ordered - -from flat_labs_and_imaging_0 t1 - join amrs.person p using (person_id) -); - - - -# Remove test patients -delete t1 -from flat_labs_and_imaging t1 -join amrs.person_attribute t2 using (person_id) -where t2.person_attribute_type_id=28 and value='true' and voided=0; - -#select * from flat_labs_and_imaging; - -select @end := now(); -insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); -select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); diff --git a/etl-scripts/stored-procedures/generate_flat_vitals_v2_2.sql b/etl-scripts/stored-procedures/generate_flat_vitals.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_flat_vitals_v2_2.sql rename to etl-scripts/stored-procedures/generate_flat_vitals.sql index d59f0d3..5e333ad 100644 --- a/etl-scripts/stored-procedures/generate_flat_vitals_v2_2.sql +++ b/etl-scripts/stored-procedures/generate_flat_vitals.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_flat_vitals_v2_2`() +CREATE PROCEDURE `generate_flat_vitals`() BEGIN select @start:= now(); diff --git a/etl-scripts/stored-procedures/generate_flat_vitals_v2_1.sql b/etl-scripts/stored-procedures/generate_flat_vitals_v2_1.sql deleted file mode 100644 index da6b6f6..0000000 --- a/etl-scripts/stored-procedures/generate_flat_vitals_v2_1.sql +++ /dev/null @@ -1,276 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_flat_vitals_v2_1`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set session sort_buffer_size=512000000; - set session group_concat_max_len=100000; - set @start = now(); - set @primary_table := "flat_vitals_2"; - select @table_version := "flat_vitals_v2.1"; - set @total_rows_written = 0; - set @query_type = query_type; - set @queue_number = queue_number; - set @queue_size = queue_size; - set @cycle_size = cycle_size; - -#set @queue_number = 1; -#set @queue_size = 10000; -#set @cycle_size = 1000; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - set @boundary = "!!"; - set @sep = " ## "; - - SET @dyn_sql = CONCAT('create table if not exists ', @primary_table, - ' (date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - location_id int, - weight decimal, - height decimal, - temp decimal(4,1), - oxygen_sat int, - systolic_bp int, - diastolic_bp int, - pulse int, - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index person_uuid (uuid), - index date_created (date_created) - )'); - - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - select CONCAT('BUILDING ',@primary_table,'..........................................'); - set @write_table = concat(@primary_table,"_temp_",@queue_number); - set @queue_table = concat("flat_vitals_build_queue_",@queue_number); - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(encounter_id int primary key) (select * from flat_vitals_build_queue limit ', @queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from flat_obs_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from flat_vitals_build_queue t1 join ',@queue_table, ' t2 using (encounter_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - - end if; - - - if (@query_type="sync") then - select CONCAT('SYNCING ',@primary_table,'..........................................'); - set @write_table = @primary_table; - set @queue_table = "flat_vitals_sync_queue"; - create table if not exists flat_vitals_sync_queue (encounter_id int primary key); - - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; -#set @last_update = "2018-01-26"; - - # find all encounters that have date_created, date_voided, or date_changed after @last_update - SET @dyn_sql=CONCAT('replace into ',@queue_table, - '(select encounter_id - from amrs.encounter t1 - where - CASE - when t1.date_created >= @last_update and t1.voided=0 then 1 - when t1.date_created <= @last_update and t1.date_voided > @last_update then 1 - when t1.date_created <= @last_update and t1.date_changed > @last_update then 1 - else 0 - end - )'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - #find all encounters which have a voided obs since last update - SET @dyn_sql=CONCAT('replace into ',@queue_table, - '(select encounter_id - from amrs.obs t1 - where t1.date_created <= @last_update - AND t1.date_voided > @last_updated - AND t1.encounter_id > 0 - )'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - # delete all rows in primary table in the queue - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table,' t1 join ',@queue_table, ' t2 using (encounter_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - while @queue_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists temp_queue_table; - - SET @dyn_sql = CONCAT('create temporary table temp_queue_table like ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql= CONCAT('replace into temp_queue_table (select * from ',@queue_table,' limit ', @cycle_size,')'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - drop temporary table if exists flat_vitals_0; - create temporary table flat_vitals_0 (encounter_id int, primary key (encounter_id), index person_enc_date (person_id,encounter_datetime)) - (select - t1.person_id, - t3.uuid as person_uuid, - visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - # 5089 = WEIGHT - # 5090 = HEIGHT (CM) - # 5088 = TEMPERATURE (C) - # 5092 = BLOOD OXYGEN SATURATION - # 5085 = SYSTOLIC BLOOD PRESSURE - # 5086 = DIASTOLIC BLOOD PRESSURE - # 5087 = PULSE - - if(obs regexp "!!5089=",cast(replace(replace((substring_index(substring(obs,locate("!!5089=",obs)),@sep,1)),"!!5089=",""),"!!","") as decimal(4,1)),null) as weight, - if(obs regexp "!!5090=",cast(replace(replace((substring_index(substring(obs,locate("!!5090=",obs)),@sep,1)),"!!5090=",""),"!!","") as decimal(4,1)),null) as height, - if(obs regexp "!!5088=",cast(replace(replace((substring_index(substring(obs,locate("!!5088=",obs)),@sep,1)),"!!5088=",""),"!!","") as decimal(4,1)),null) as temp, - if(obs regexp "!!5092=",cast(replace(replace((substring_index(substring(obs,locate("!!5092=",obs)),@sep,1)),"!!5092=",""),"!!","") as unsigned),null) as oxygen_sat, - if(obs regexp "!!5085=",cast(replace(replace((substring_index(substring(obs,locate("!!5085=",obs)),@sep,1)),"!!5085=",""),"!!","") as unsigned),null) as systolic_bp, - if(obs regexp "!!5086=",cast(replace(replace((substring_index(substring(obs,locate("!!5086=",obs)),@sep,1)),"!!5086=",""),"!!","") as unsigned),null) as diastolic_bp, - if(obs regexp "!!5087=",cast(replace(replace((substring_index(substring(obs,locate("!!5087=",obs)),@sep,1)),"!!5087=",""),"!!","") as unsigned),null) as pulse - - from flat_obs t1 - join temp_queue_table t0 using (encounter_id) - join amrs.person t3 using (person_id) - where obs regexp '!!(5089|5090|5088|5092|5085|5086|5087)=' - ); - - select count(*) as '# rows to write' from flat_vitals_0; - - SET @dyn_sql = CONCAT('replace into ',@write_table, - '(select - null, - person_id, - person_uuid, - visit_id, - encounter_id, - encounter_datetime, - location_id, - weight, - height, - temp, - oxygen_sat, - systolic_bp, - diastolic_bp, - pulse - from flat_vitals_0)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join temp_queue_table t2 using (encounter_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('select count(*) into @queue_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@queue_count / @cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select @queue_count as '# in queue', @cycle_length as 'Cycle Time (s)', ceil(@queue_count / @cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - SET @dyn_sql = CONCAT( - 'delete t1 - from ',@primary_table, ' t1 - join amrs.person t2 using (person_id) - where t2.voided=1;'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - select @end := now(); - insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hei_monthly_report_dataset_v1_0.sql b/etl-scripts/stored-procedures/generate_hei_monthly_report_dataset.sql similarity index 98% rename from etl-scripts/stored-procedures/generate_hei_monthly_report_dataset_v1_0.sql rename to etl-scripts/stored-procedures/generate_hei_monthly_report_dataset.sql index a044d7c..e006fd5 100644 --- a/etl-scripts/stored-procedures/generate_hei_monthly_report_dataset_v1_0.sql +++ b/etl-scripts/stored-procedures/generate_hei_monthly_report_dataset.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_hei_monthly_report_dataset_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int, IN start_date varchar(50)) +CREATE PROCEDURE `generate_hei_monthly_report_dataset`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int, IN start_date varchar(50)) BEGIN set @start = now(); diff --git a/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset.sql b/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset.sql index fb15f71..7254a20 100644 --- a/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset.sql +++ b/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_hiv_monthly_report_dataset_v1_4`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int, IN start_date varchar(50)) +CREATE PROCEDURE `generate_hiv_monthly_report_dataset`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int, IN start_date varchar(50)) BEGIN set @start = now(); @@ -977,7 +977,7 @@ SET @dyn_sql=CONCAT('delete t1 from hiv_monthly_report_dataset_v1_2 t1 join ',@q end if; set @end = now(); - # not sure why we need last date_created, Ive replaced this with @start + # not sure why we need last date_created, I've replaced this with @start insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); diff --git a/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset_v1_0.sql b/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset_v1_0.sql deleted file mode 100644 index 5c5dc65..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset_v1_0.sql +++ /dev/null @@ -1,586 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_monthly_report_dataset_v1_0`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int, IN start_date varchar(50)) -BEGIN - - set @start = now(); - set @table_version = "hiv_monthly_report_dataset_v1.0"; -# set @last_date_created = (select max(date_created) from etl.flat_hiv_summary_v15b); - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - - #drop table if exists hiv_monthly_report_dataset; - create table if not exists hiv_monthly_report_dataset ( - date_created timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL, - elastic_id bigint, - endDate date, - encounter_id int, - person_id int, - person_uuid varchar(100), - birthdate date, - age double, - gender varchar(1), - encounter_date date, - visit_this_month boolean, - rtc_date date, - days_since_rtc_date int, - on_schedule boolean, - location_id mediumint, - location_uuid varchar(100), - clinic_county varchar(250), - clinic varchar(250), - clinic_latitude double, - clinic_longitude double, - encounter_type int, - death_date date, - enrollment_date date, - enrolled_this_month boolean, - transfer_in_this_month boolean, - transfer_in_location_id mediumint, - transfer_in_date date, - transfer_out_this_month boolean, - transfer_out_location_id mediumint, - transfer_out_date date, - prev_status varchar(250), - status varchar(250), - next_status varchar(250), - arv_first_regimen_location_id int, - arv_first_regimen_coded varchar(250), - arv_first_regimen varchar(250), - arv_first_regimen_start_date date, - days_since_starting_arvs mediumint, - started_art_this_month boolean, - art_revisit_this_month boolean, - arv_start_date date, - prev_month_arvs varchar(250), - cur_arv_meds varchar(250), - cur_arv_meds_strict varchar(250), - cur_arv_line tinyint, - cur_arv_line_strict tinyint, - cur_arv_line_reported tinyint, - on_art_this_month boolean, - eligible_for_vl boolean, - days_since_last_vl int, - vl_1 int, - vl_1_date date, - vl_in_past_year boolean, - vl_2 int, - vl_2_date date, - tb_screen boolean, - tb_screening_datetime datetime, - tb_screening_result int, - tb_screened_this_month boolean, - tb_screened_since_active boolean, - tb_screened_positive_this_month boolean, - tb_tx_start_date date, - started_tb_tx_this_month boolean, - pcp_prophylaxis_start_date date, - started_pcp_prophylaxis_this_month boolean, - on_pcp_prophylaxis_this_month boolean, - ipt_start_date date, - ipt_stop_date date, - ipt_completion_date date, - started_ipt_this_month boolean, - on_ipt_this_month boolean, - completed_ipt_past_12_months boolean, - pregnant_this_month boolean, - delivered_this_month boolean, - condoms_provided_this_month boolean, - condoms_provided_since_active boolean, - started_modern_contraception_this_month boolean, - modern_contraception_since_active boolean, - contraceptive_method int, - discordant_status int, - - primary key elastic_id (elastic_id), - index person_enc_date (person_id, encounter_date), - index person_report_date (person_id, endDate), - index endDate_location_id (endDate, location_id) - ); - - #create table if not exists hiv_monthly_report_dataset_build_queue (person_id int primary key); - #replace into hiv_monthly_report_dataset_build_queue - #(select distinct person_id from flat_hiv_summary_v15); - - if (query_type = "build") then - select "BUILDING......................."; - set @queue_table = concat("hiv_monthly_report_dataset_build_queue_",queue_number); - #set @queue_table = concat("hiv_monthly_report_dataset_build_queue_1"); - #create table if not exists @queue_table (person_id int, primary key (person_id)); - #SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from hiv_monthly_report_dataset_build_queue limit 1000);'); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(person_id int primary key) (select * from hiv_monthly_report_dataset_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from hiv_monthly_report_dataset_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from hiv_monthly_report_dataset_build_queue t1 join ',@queue_table, ' t2 using (person_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - - if (query_type = "sync") then - set @queue_table = "hiv_monthly_report_dataset_sync_queue"; - create table if not exists hiv_monthly_report_dataset_sync_queue (person_id int primary key); - - select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); - - replace into hiv_monthly_report_dataset_sync_queue - (select distinct person_id from flat_hiv_summary_v15b where date_created >= @last_update); - end if; - - - SET @num_ids := 0; - SET @dyn_sql=CONCAT('select count(*) into @num_ids from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from hiv_monthly_report_dataset t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - #create temporary table hiv_monthly_report_dataset_build_queue_0 (select * from hiv_monthly_report_dataset_build_queue_2 limit 5000); #TODO - change this when data_fetch_size changes - - drop temporary table if exists hiv_monthly_report_dataset_build_queue__0; - create temporary table hiv_monthly_report_dataset_build_queue__0 (person_id int primary key); - - SET @dyn_sql=CONCAT('insert into hiv_monthly_report_dataset_build_queue__0 (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - drop temporary table if exists hiv_monthly_report_dataset_0; - create temporary table hiv_monthly_report_dataset_0 - (select - concat(date_format(endDate,"%Y%m"),person_id) as elastic_id, - endDate, - encounter_id, - person_id, - t3.uuid as person_uuid, - date(birthdate) as birthdate, - case - when timestampdiff(year,birthdate,endDate) > 0 then round(timestampdiff(year,birthdate,endDate),0) - else round(timestampdiff(month,birthdate,endDate)/12,2) - end as age, - t3.gender, - date(encounter_datetime) as encounter_date, - - if(encounter_datetime between date_format(endDate,"%Y-%m-01") and endDate,1,0) as visit_this_month, - - date(rtc_date) as rtc_date, - timestampdiff(day,rtc_date, endDate) as days_since_rtc_date, - if(date(encounter_datetime) = date(prev_clinical_rtc_date_hiv),1,0) as on_schedule, - location_id, - t4.uuid as location_uuid, - t4.state_province as clinic_county, - # Add subcounty - t4.name as clinic, - t4.latitude as clinic_latitude, - t4.longitude as clinic_longitude, - encounter_type, - date(t2.death_date) as death_date, - - enrollment_date, - case - when enrollment_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as enrolled_this_month, - - case - when transfer_in_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as transfer_in_this_month, - transfer_in_location_id, - transfer_in_date, - - - case - when transfer_out_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as transfer_out_this_month, - - transfer_out_location_id, - transfer_out_date, - case - when t2.death_date <= endDate then @status := "dead" -# when transfer_out_date < date_format(endDate,"%Y-%m-01") then @status := "transfer_out" - when date_format(endDate, "%Y-%m-01") > transfer_out_date then @status := "transfer_out" - when timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 then @status := "active" - when timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) > 90 then @status := "ltfu" - else @status := "unknown" - end as status, - - arv_first_regimen_location_id, - - arv_first_regimen, - date(arv_first_regimen_start_date) as arv_first_regimen_start_date, - - case - when arv_first_regimen_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_art_this_month, - - case - when date_format(arv_first_regimen_start_date,"%Y-%m-01") != date_format(endDate,"%Y-%m-01") - AND cur_arv_meds is not null - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 then 1 - else 0 - end as art_revisit_this_month, - - arv_start_date, - - cur_arv_meds, - cur_arv_meds_strict, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - - # We are using liberal definition of cur_arv_meds such that - # if a patient previously on ART and not explicilty stopped, the - # patient is considered to be on ART. - case - when cur_arv_meds is not null - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 - then 1 - else 0 - end as on_art_this_month, - - - timestampdiff(day,arv_start_date,endDate) as days_since_starting_arvs, - - case - when timestampdiff(month, arv_start_date,endDate) > 6 then 1 - else 0 - end as eligible_for_vl, - - timestampdiff(day,vl_1_date,endDate) as days_since_last_vl, - - vl_1, - date(vl_1_date) as vl_1_date, - - if(timestampdiff(year,vl_1_date,endDate) < 1,1,0) as vl_in_past_year, - - - vl_2, - date(vl_2_date) as vl_2_date, - - tb_screen, - tb_screening_datetime, - tb_screening_result, - - case - when tb_screening_datetime between date_format(endDate, "%Y-%m-01") and endDate then 1 - else 0 - end as tb_screened_this_month, - - case - when tb_screening_datetime >= date(encounter_datetime) - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 then 1 - else 0 - end as tb_screened_since_active, - - case - when tb_screening_result = 6971 - AND tb_screening_datetime then 1 - else 0 - end as tb_screened_positive_this_month, - - date(tb_tx_start_date) as tb_tx_start_date,#need to know time period, i.e. screened this month or screened in past X months - case - when tb_tx_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_tb_tx_this_month, - - date(pcp_prophylaxis_start_date) as pcp_prophylaxis_start_date, - - case - when pcp_prophylaxis_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_pcp_prophylaxis_this_month, - - case - when pcp_prophylaxis_start_date is not null then 1 - else 0 - end as on_pcp_prophylaxis_this_month, - - - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - case - when ipt_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_ipt_this_month, - - case - when ipt_start_date is not null and ipt_stop_date is null then 1 - else 0 - end as on_ipt_this_month, - - - case - when timestampdiff(month,ipt_completion_date,endDate) <= 12 then 1 - else 0 - end as completed_ipt_past_12_months, - - case - when is_pregnant and edd > endDate then 1 - else 0 - end as pregnant_this_month, - - 0 as delivered_this_month, - - case - when condoms_provided_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as condoms_provided_this_month, - - case - when condoms_provided_date >= date(encounter_datetime) - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 then 1 - else 0 - end as condoms_provided_since_active, - - - case - when modern_contraceptive_method_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_modern_contraception_this_month, - - case - when modern_contraceptive_method_start_date >= date(encounter_datetime) - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 then 1 - else 0 - end as modern_contraception_since_active, - contraceptive_method, - discordant_status - - - from etl.dates t1 - join etl.flat_hiv_summary_v15b t2 - join amrs.person t3 using (person_id) - join amrs.location t4 using (location_id) - join etl.hiv_monthly_report_dataset_build_queue__0 t5 using (person_id) - - - where - t2.encounter_datetime <= t1.endDate - and (t2.next_clinical_datetime_hiv is null or t2.next_clinical_datetime_hiv > t1.endDate ) - and t2.is_clinical_encounter=1 - and t1.endDate between start_date and date_add(now(),interval 4 month) - order by person_id, endDate - ); - - - - set @prev_id = null; - set @cur_id = null; - set @cur_status = null; - set @prev_status = null; - - drop temporary table if exists hiv_monthly_report_dataset_1; - create temporary table hiv_monthly_report_dataset_1 - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - case - when @prev_id=@cur_id then @prev_status := @cur_status - else @prev_status := null - end as next_status, - @cur_status := status as cur_status - - from hiv_monthly_report_dataset_0 - order by person_id, endDate desc - ); - - alter table hiv_monthly_report_dataset_1 drop prev_id, drop cur_id, drop cur_status; - - set @prev_id = null; - set @cur_id = null; - set @cur_status = null; - set @prev_status = null; - set @cur_arv_meds = null; - drop temporary table if exists hiv_monthly_report_dataset_2; - create temporary table hiv_monthly_report_dataset_2 - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - case - when @prev_id=@cur_id then @prev_status := @cur_status - else @prev_status := null - end as prev_status, - @cur_status := status as cur_status, - - case - when @prev_id = @cur_id then @cur_arv_meds - else @cur_arv_meds := null - end as prev_month_arvs, - - @cur_arv_meds := cur_arv_meds as cur_arv_meds_2, - - days_since_starting_arvs - days_since_last_vl as days_between_arv_start_and_last_vl - - - from hiv_monthly_report_dataset_1 - order by person_id, endDate - ); - - select now(); - select count(*) as num_rows_to_be_inserted from hiv_monthly_report_dataset_2; - - #add data to table - replace into hiv_monthly_report_dataset - (select - null, #date_created will be automatically set or updated - elastic_id, - endDate, - encounter_id, - person_id, - person_uuid, - birthdate, - age, - gender, - encounter_date, - visit_this_month, - rtc_date, - days_since_rtc_date, - on_schedule, - location_id, - location_uuid, - clinic_county, - clinic, - clinic_latitude, - clinic_longitude, - encounter_type, - death_date, - enrollment_date, - enrolled_this_month, - transfer_in_this_month, - transfer_in_location_id, - transfer_in_date, - transfer_out_this_month, - transfer_out_location_id, - transfer_out_date, - prev_status, - status, - next_status, - arv_first_regimen_location_id, - arv_first_regimen_coded, - arv_first_regimen, - arv_first_regimen_start_date, - days_since_starting_arvs, - started_art_this_month, - art_revisit_this_month, - arv_start_date, - prev_month_arvs, - cur_arv_meds, - cur_arv_meds_strict, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - on_art_this_month, - eligible_for_vl, - days_since_last_vl, - vl_1, - vl_1_date, - vl_in_past_year, - vl_2, - vl_2_date, - tb_screen, - tb_screening_datetime, - tb_screening_result, - tb_screened_this_month, - tb_screened_since_active, - tb_screened_positive_this_month, - tb_tx_start_date, - started_tb_tx_this_month, - pcp_prophylaxis_start_date, - started_pcp_prophylaxis_this_month, - on_pcp_prophylaxis_this_month, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - started_ipt_this_month, - on_ipt_this_month, - completed_ipt_past_12_months, - pregnant_this_month, - delivered_this_month, - condoms_provided_this_month, - condoms_provided_since_active, - started_modern_contraception_this_month, - modern_contraception_since_active, - contraceptive_method, - discordant_status - from hiv_monthly_report_dataset_2 - ); - - - #delete from @queue_table where person_id in (select person_id from hiv_monthly_report_dataset_build_queue__0); - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join hiv_monthly_report_dataset_build_queue__0 t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count := (select count(*) from @queue_table); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count as num_remaining; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - #select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select count(*) into @num_in_hmrd from hiv_monthly_report_dataset; - - select @num_in_hmrd as num_in_hmrd, - @person_ids_count as num_remaining, - @cycle_length as 'Cycle time (s)', - ceil(@person_ids_count / cycle_size) as remaining_cycles, - @remaining_time as 'Est time remaining (min)'; - - - end while; - - if(query_type = "build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - set @end = now(); - # not sure why we need last date_created, I've replaced this with @start - insert into etl.flat_log values (@start,@start,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset_v1_1.sql b/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset_v1_1.sql deleted file mode 100644 index e987e56..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset_v1_1.sql +++ /dev/null @@ -1,586 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_monthly_report_dataset_v1_1`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int, IN start_date varchar(50)) -BEGIN - - set @start = now(); - set @table_version = "hiv_monthly_report_dataset_v1.0"; -# set @last_date_created = (select max(date_created) from etl.flat_hiv_summary_v15b); - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - - #drop table if exists hiv_monthly_report_dataset; - create table if not exists hiv_monthly_report_dataset ( - date_created timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL, - elastic_id bigint, - endDate date, - encounter_id int, - person_id int, - person_uuid varchar(100), - birthdate date, - age double, - gender varchar(1), - encounter_date date, - visit_this_month boolean, - rtc_date date, - days_since_rtc_date int, - on_schedule boolean, - location_id mediumint, - location_uuid varchar(100), - clinic_county varchar(250), - clinic varchar(250), - clinic_latitude double, - clinic_longitude double, - encounter_type int, - death_date date, - enrollment_date date, - enrolled_this_month boolean, - transfer_in_this_month boolean, - transfer_in_location_id mediumint, - transfer_in_date date, - transfer_out_this_month boolean, - transfer_out_location_id mediumint, - transfer_out_date date, - prev_status varchar(250), - status varchar(250), - next_status varchar(250), - arv_first_regimen_location_id int, - arv_first_regimen varchar(250), - arv_first_regimen_start_date date, - days_since_starting_arvs mediumint, - started_art_this_month boolean, - art_revisit_this_month boolean, - arv_start_date date, - prev_month_arvs varchar(250), - cur_arv_meds varchar(250), - cur_arv_meds_strict varchar(250), - cur_arv_line tinyint, - cur_arv_line_strict tinyint, - cur_arv_line_reported tinyint, - on_art_this_month boolean, - eligible_for_vl boolean, - days_since_last_vl int, - vl_1 int, - vl_1_date date, - vl_in_past_year boolean, - vl_2 int, - vl_2_date date, - tb_screen boolean, - tb_screening_datetime datetime, - tb_screening_result int, - tb_screened_this_month boolean, - tb_screened_since_active boolean, - tb_screened_positive_this_month boolean, - tb_tx_start_date date, - started_tb_tx_this_month boolean, - pcp_prophylaxis_start_date date, - started_pcp_prophylaxis_this_month boolean, - on_pcp_prophylaxis_this_month boolean, - ipt_start_date date, - ipt_stop_date date, - ipt_completion_date date, - started_ipt_this_month boolean, - on_ipt_this_month boolean, - completed_ipt_past_12_months boolean, - pregnant_this_month boolean, - delivered_this_month boolean, - condoms_provided_this_month boolean, - condoms_provided_since_active boolean, - started_modern_contraception_this_month boolean, - modern_contraception_since_active boolean, - contraceptive_method int, - discordant_status int, - - primary key elastic_id (elastic_id), - index person_enc_date (person_id, encounter_date), - index person_report_date (person_id, endDate), - index endDate_location_id (endDate, location_id) - ); - - #create table if not exists hiv_monthly_report_dataset_build_queue (person_id int primary key); - #replace into hiv_monthly_report_dataset_build_queue - #(select distinct person_id from flat_hiv_summary_v15); - - if (query_type = "build") then - select "BUILDING......................."; - set @queue_table = concat("hiv_monthly_report_dataset_build_queue_",queue_number); - #set @queue_table = concat("hiv_monthly_report_dataset_build_queue_1"); - #create table if not exists @queue_table (person_id int, primary key (person_id)); - #SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from hiv_monthly_report_dataset_build_queue limit 1000);'); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(person_id int primary key) (select * from hiv_monthly_report_dataset_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from hiv_monthly_report_dataset_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from hiv_monthly_report_dataset_build_queue t1 join ',@queue_table, ' t2 using (person_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - - if (query_type = "sync") then - set @queue_table = "hiv_monthly_report_dataset_sync_queue"; - create table if not exists hiv_monthly_report_dataset_sync_queue (person_id int primary key); - - select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); - - replace into hiv_monthly_report_dataset_sync_queue - (select distinct person_id from flat_hiv_summary_v15b where date_created >= @last_update); - end if; - - - SET @num_ids := 0; - SET @dyn_sql=CONCAT('select count(*) into @num_ids from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from hiv_monthly_report_dataset t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - #create temporary table hiv_monthly_report_dataset_build_queue_0 (select * from hiv_monthly_report_dataset_build_queue_2 limit 5000); #TODO - change this when data_fetch_size changes - - drop temporary table if exists hiv_monthly_report_dataset_build_queue__0; - create temporary table hiv_monthly_report_dataset_build_queue__0 (person_id int primary key); - - SET @dyn_sql=CONCAT('insert into hiv_monthly_report_dataset_build_queue__0 (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - drop temporary table if exists hiv_monthly_report_dataset_0; - create temporary table hiv_monthly_report_dataset_0 - (select - concat(date_format(endDate,"%Y%m"),person_id) as elastic_id, - endDate, - encounter_id, - person_id, - t3.uuid as person_uuid, - date(birthdate) as birthdate, - case - when timestampdiff(year,birthdate,endDate) > 0 then round(timestampdiff(year,birthdate,endDate),0) - else round(timestampdiff(month,birthdate,endDate)/12,2) - end as age, - t3.gender, - date(encounter_datetime) as encounter_date, - - if(encounter_datetime between date_format(endDate,"%Y-%m-01") and endDate,1,0) as visit_this_month, - - date(rtc_date) as rtc_date, - timestampdiff(day,rtc_date, endDate) as days_since_rtc_date, - if(date(encounter_datetime) = date(prev_clinical_rtc_date_hiv),1,0) as on_schedule, - location_id, - t4.uuid as location_uuid, - t4.state_province as clinic_county, - # Add subcounty - t4.name as clinic, - t4.latitude as clinic_latitude, - t4.longitude as clinic_longitude, - encounter_type, - date(t2.death_date) as death_date, - - enrollment_date, - case - when enrollment_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as enrolled_this_month, - - case - when transfer_in_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as transfer_in_this_month, - transfer_in_location_id, - transfer_in_date, - - - case - when transfer_out_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as transfer_out_this_month, - - transfer_out_location_id, - transfer_out_date, - case - when date_format(endDate, "%Y-%m-01") > t2.death_date then @status := "dead" -# when transfer_out_date < date_format(endDate,"%Y-%m-01") then @status := "transfer_out" - when date_format(endDate, "%Y-%m-01") > transfer_out_date then @status := "transfer_out" - when timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 then @status := "active" - when timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) > 90 then @status := "ltfu" - else @status := "unknown" - end as status, - - case when arv_first_regimen_location_id is not null then arv_first_regimen_location_id - when arv_first_regimen_location_id is null and arv_first_regimen_start_date is not null then location_id else - null end as arv_first_regimen_location_id, - - arv_first_regimen, - date(arv_first_regimen_start_date) as arv_first_regimen_start_date, - - case - when arv_first_regimen_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_art_this_month, - - case - when date_format(arv_first_regimen_start_date,"%Y-%m-01") != date_format(endDate,"%Y-%m-01") - AND cur_arv_meds is not null - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 then 1 - else 0 - end as art_revisit_this_month, - - arv_start_date, - - cur_arv_meds, - cur_arv_meds_strict, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - - # We are using liberal definition of cur_arv_meds such that - # if a patient previously on ART and not explicilty stopped, the - # patient is considered to be on ART. - case - when cur_arv_meds is not null - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 - then 1 - else 0 - end as on_art_this_month, - - - timestampdiff(day,arv_start_date,endDate) as days_since_starting_arvs, - - case - when timestampdiff(month, arv_start_date,endDate) > 6 then 1 - else 0 - end as eligible_for_vl, - - timestampdiff(day,vl_1_date,endDate) as days_since_last_vl, - - vl_1, - date(vl_1_date) as vl_1_date, - - if(timestampdiff(year,vl_1_date,endDate) < 1,1,0) as vl_in_past_year, - - - vl_2, - date(vl_2_date) as vl_2_date, - - tb_screen, - tb_screening_datetime, - tb_screening_result, - - case - when tb_screening_datetime between date_format(endDate, "%Y-%m-01") and endDate then 1 - else 0 - end as tb_screened_this_month, - - case - when tb_screening_datetime >= date(encounter_datetime) - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 then 1 - else 0 - end as tb_screened_since_active, - - case - when tb_screening_result = 6971 - AND tb_screening_datetime then 1 - else 0 - end as tb_screened_positive_this_month, - - date(tb_tx_start_date) as tb_tx_start_date,#need to know time period, i.e. screened this month or screened in past X months - case - when tb_tx_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_tb_tx_this_month, - - date(pcp_prophylaxis_start_date) as pcp_prophylaxis_start_date, - - case - when pcp_prophylaxis_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_pcp_prophylaxis_this_month, - - case - when pcp_prophylaxis_start_date is not null then 1 - else 0 - end as on_pcp_prophylaxis_this_month, - - - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - case - when ipt_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_ipt_this_month, - - case - when ipt_start_date is not null and ipt_stop_date is null then 1 - else 0 - end as on_ipt_this_month, - - - case - when timestampdiff(month,ipt_completion_date,endDate) <= 12 then 1 - else 0 - end as completed_ipt_past_12_months, - - case - when is_pregnant and edd > endDate then 1 - else 0 - end as pregnant_this_month, - - 0 as delivered_this_month, - - case - when condoms_provided_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as condoms_provided_this_month, - - case - when condoms_provided_date >= date(encounter_datetime) - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 then 1 - else 0 - end as condoms_provided_since_active, - - - case - when modern_contraceptive_method_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_modern_contraception_this_month, - - case - when modern_contraceptive_method_start_date >= date(encounter_datetime) - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 then 1 - else 0 - end as modern_contraception_since_active, - contraceptive_method, - discordant_status - - - from etl.dates t1 - join etl.flat_hiv_summary_v15b t2 - join amrs.person t3 using (person_id) - join amrs.location t4 using (location_id) - join etl.hiv_monthly_report_dataset_build_queue__0 t5 using (person_id) - - - where - t2.encounter_datetime <= t1.endDate - and (t2.next_clinical_datetime_hiv is null or t2.next_clinical_datetime_hiv > t1.endDate ) - and t2.is_clinical_encounter=1 - and t1.endDate between start_date and date_add(now(),interval 4 month) - order by person_id, endDate - ); - - - - set @prev_id = null; - set @cur_id = null; - set @cur_status = null; - set @prev_status = null; - - drop temporary table if exists hiv_monthly_report_dataset_1; - create temporary table hiv_monthly_report_dataset_1 - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - case - when @prev_id=@cur_id then @prev_status := @cur_status - else @prev_status := null - end as next_status, - @cur_status := status as cur_status - - from hiv_monthly_report_dataset_0 - order by person_id, endDate desc - ); - - alter table hiv_monthly_report_dataset_1 drop prev_id, drop cur_id, drop cur_status; - - set @prev_id = null; - set @cur_id = null; - set @cur_status = null; - set @prev_status = null; - set @cur_arv_meds = null; - drop temporary table if exists hiv_monthly_report_dataset_2; - create temporary table hiv_monthly_report_dataset_2 - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - case - when @prev_id=@cur_id then @prev_status := @cur_status - else @prev_status := null - end as prev_status, - @cur_status := status as cur_status, - - case - when @prev_id = @cur_id then @cur_arv_meds - else @cur_arv_meds := null - end as prev_month_arvs, - - @cur_arv_meds := cur_arv_meds as cur_arv_meds_2, - - days_since_starting_arvs - days_since_last_vl as days_between_arv_start_and_last_vl - - - from hiv_monthly_report_dataset_1 - order by person_id, endDate - ); - - select now(); - select count(*) as num_rows_to_be_inserted from hiv_monthly_report_dataset_2; - - #add data to table - replace into hiv_monthly_report_dataset - (select - null, #date_created will be automatically set or updated - elastic_id, - endDate, - encounter_id, - person_id, - person_uuid, - birthdate, - age, - gender, - encounter_date, - visit_this_month, - rtc_date, - days_since_rtc_date, - on_schedule, - location_id, - location_uuid, - clinic_county, - clinic, - clinic_latitude, - clinic_longitude, - encounter_type, - death_date, - enrollment_date, - enrolled_this_month, - transfer_in_this_month, - transfer_in_location_id, - transfer_in_date, - transfer_out_this_month, - transfer_out_location_id, - transfer_out_date, - prev_status, - status, - next_status, - arv_first_regimen_location_id, - arv_first_regimen, - arv_first_regimen_start_date, - days_since_starting_arvs, - started_art_this_month, - art_revisit_this_month, - arv_start_date, - prev_month_arvs, - cur_arv_meds, - cur_arv_meds_strict, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - on_art_this_month, - eligible_for_vl, - days_since_last_vl, - vl_1, - vl_1_date, - vl_in_past_year, - vl_2, - vl_2_date, - tb_screen, - tb_screening_datetime, - tb_screening_result, - tb_screened_this_month, - tb_screened_since_active, - tb_screened_positive_this_month, - tb_tx_start_date, - started_tb_tx_this_month, - pcp_prophylaxis_start_date, - started_pcp_prophylaxis_this_month, - on_pcp_prophylaxis_this_month, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - started_ipt_this_month, - on_ipt_this_month, - completed_ipt_past_12_months, - pregnant_this_month, - delivered_this_month, - condoms_provided_this_month, - condoms_provided_since_active, - started_modern_contraception_this_month, - modern_contraception_since_active, - contraceptive_method, - discordant_status - from hiv_monthly_report_dataset_2 - ); - - - #delete from @queue_table where person_id in (select person_id from hiv_monthly_report_dataset_build_queue__0); - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join hiv_monthly_report_dataset_build_queue__0 t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count := (select count(*) from @queue_table); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count as num_remaining; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - #select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select count(*) into @num_in_hmrd from hiv_monthly_report_dataset; - - select @num_in_hmrd as num_in_hmrd, - @person_ids_count as num_remaining, - @cycle_length as 'Cycle time (s)', - ceil(@person_ids_count / cycle_size) as remaining_cycles, - @remaining_time as 'Est time remaining (min)'; - - - end while; - - if(query_type = "build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - set @end = now(); - # not sure why we need last date_created, I've replaced this with @start - insert into etl.flat_log values (@start,@start,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset_v1_2.sql b/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset_v1_2.sql deleted file mode 100644 index 5029229..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset_v1_2.sql +++ /dev/null @@ -1,765 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_monthly_report_dataset_v1_2`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int, IN start_date varchar(50)) -BEGIN - - set @start = now(); - set @table_version = "hiv_monthly_report_dataset_v1.2"; - set @last_date_created = (select max(date_created) from etl.flat_hiv_summary_v15b); - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - - - #drop table if exists hiv_monthly_report_dataset_v1_2; -create table if not exists hiv_monthly_report_dataset_v1_2 ( -# create table if not exists hiv_monthly_report_dataset ( - date_created timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL, - elastic_id bigint, - endDate date, - encounter_id int, - person_id int, - person_uuid varchar(100), - birthdate date, - age double, - gender varchar(1), - encounter_date date, - visit_this_month boolean, - rtc_date date, - days_since_rtc_date int, - on_schedule boolean, - scheduled_this_month boolean, - unscheduled_this_month boolean, - f_18_and_older_this_month boolean, - location_id mediumint, - location_uuid varchar(100), - clinic_county varchar(250), - clinic varchar(250), - clinic_latitude double, - clinic_longitude double, - encounter_type int, - death_date date, - enrollment_date date, - enrolled_this_month boolean, - transfer_in_this_month boolean, - transfer_in_location_id mediumint, - transfer_in_date date, - transfer_out_this_month boolean, - transfer_out_location_id mediumint, - transfer_out_date date, - prev_status varchar(250), - status varchar(250), - next_status varchar(250), - active_in_care_this_month boolean, - is_pre_art_this_month boolean, - arv_first_regimen_location_id int, - arv_first_regimen varchar(250), - arv_first_regimen_names varchar(250), - arv_first_regimen_start_date date, - art_cohort_num smallint, - art_cohort_year smallint, - days_since_starting_arvs mediumint, - started_art_this_month boolean, - art_revisit_this_month boolean, - arv_start_date date, - prev_month_arvs varchar(250), - cur_arv_meds varchar(250), - cur_arv_meds_names varchar(250), - cur_arv_meds_strict varchar(250), - cur_arv_line tinyint, - cur_arv_line_strict tinyint, - cur_arv_line_reported tinyint, - on_art_this_month boolean, - on_original_first_line_this_month boolean, - on_alt_first_line_this_month boolean, - on_second_line_or_higher_this_month boolean, - eligible_for_vl boolean, - days_since_last_vl int, - net_12_month_cohort_this_month boolean, - active_on_art_12_month_cohort_this_month boolean, - has_vl_12_month_cohort boolean, - vl_suppressed_12_month_cohort boolean, - has_vl_this_month boolean, #patient has had VL in past year and is currently active on ART - is_suppressed_this_month boolean, #patient has had VL in past year < 1000 and is currently active on ART - vl_1 int, - vl_1_date date, - vl_in_past_year boolean, - vl_2 int, - vl_2_date date, - tb_screen boolean, - tb_screening_datetime datetime, - tb_screening_result int, - tb_screened_this_month boolean, - presumed_tb_positive_this_month boolean, - tb_tx_start_date date, - started_tb_tx_this_month boolean, - on_tb_tx_this_month boolean, - on_tb_tx_and_started_art_this_month boolean, - pcp_prophylaxis_start_date date, - started_pcp_prophylaxis_this_month boolean, - on_pcp_prophylaxis_this_month boolean, - ipt_start_date date, - ipt_stop_date date, - ipt_completion_date date, - started_ipt_this_month boolean, - on_ipt_this_month boolean, - completed_ipt_past_12_months boolean, - pregnant_this_month boolean, - is_pregnant_and_started_art_this_month boolean, - delivered_this_month boolean, - condoms_provided_this_month boolean, - condoms_provided_since_active boolean, - started_modern_contraception_this_month boolean, - modern_contraception_since_active boolean, - on_modern_contraception_this_month boolean, - contraceptive_method int, - discordant_status int, - - primary key elastic_id (elastic_id), - index person_enc_date (person_id, encounter_date), - index person_report_date (person_id, endDate), - index endDate_location_id (endDate, location_id) - ); - - #create table if not exists hiv_monthly_report_dataset_build_queue (person_id int primary key); - #replace into hiv_monthly_report_dataset_build_queue - #(select distinct person_id from flat_hiv_summary_v15); - - if (query_type = "build") then - select "BUILDING......................."; - set @queue_table = concat("hiv_monthly_report_dataset_build_queue_",queue_number); -#set @queue_table = concat("hiv_monthly_report_dataset_build_queue_1"); - #create table if not exists @queue_table (person_id int, primary key (person_id)); - #SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from hiv_monthly_report_dataset_build_queue limit 1000);'); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(person_id int primary key) (select * from hiv_monthly_report_dataset_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from hiv_monthly_report_dataset_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from hiv_monthly_report_dataset_build_queue t1 join ',@queue_table, ' t2 using (person_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - - if (query_type = "sync") then - set @queue_table = "hiv_monthly_report_dataset_sync_queue"; - create table if not exists hiv_monthly_report_dataset_sync_queue (person_id int primary key); - - select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); - - replace into hiv_monthly_report_dataset_sync_queue - (select distinct person_id from flat_hiv_summary_v15b where date_created >= @last_update); - end if; - - - SET @num_ids := 0; - SET @dyn_sql=CONCAT('select count(*) into @num_ids from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - -SET @dyn_sql=CONCAT('delete t1 from hiv_monthly_report_dataset_v1_2 t1 join ',@queue_table,' t2 using (person_id);'); -# SET @dyn_sql=CONCAT('delete t1 from hiv_monthly_report_dataset t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - #create temporary table hiv_monthly_report_dataset_build_queue_0 (select * from hiv_monthly_report_dataset_build_queue_2 limit 5000); #TODO - change this when data_fetch_size changes - - drop temporary table if exists hiv_monthly_report_dataset_build_queue__0; - create temporary table hiv_monthly_report_dataset_build_queue__0 (person_id int primary key); - -#SET @dyn_sql=CONCAT('insert into hiv_monthly_report_dataset_build_queue__0 (select * from hiv_monthly_report_dataset_build_queue_1 limit 100);'); - SET @dyn_sql=CONCAT('insert into hiv_monthly_report_dataset_build_queue__0 (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - set @age =null; - set @status = null; - - drop temporary table if exists hiv_monthly_report_dataset_0; - create temporary table hiv_monthly_report_dataset_0 - (select - concat(date_format(endDate,"%Y%m"),person_id) as elastic_id, - endDate, - encounter_id, - person_id, - t3.uuid as person_uuid, - date(birthdate) as birthdate, - case - when timestampdiff(year,birthdate,endDate) > 0 then @age := round(timestampdiff(year,birthdate,endDate),0) - else @age :=round(timestampdiff(month,birthdate,endDate)/12,2) - end as age, - t3.gender, - date(encounter_datetime) as encounter_date, - - if(encounter_datetime between date_format(endDate,"%Y-%m-01") and endDate,1,0) as visit_this_month, - - date(rtc_date) as rtc_date, - timestampdiff(day,rtc_date, endDate) as days_since_rtc_date, - - if(date(encounter_datetime) = date(prev_clinical_rtc_date_hiv),1,0) as on_schedule, - - case - when encounter_datetime between date_format(endDate,"%Y-%m-01") and endDate - AND date(encounter_datetime) = date(prev_clinical_rtc_date_hiv) - then 1 - else 0 - end as scheduled_this_month, - - case - when encounter_datetime between date_format(endDate,"%Y-%m-01") and endDate - AND date(encounter_datetime) != date(prev_clinical_rtc_date_hiv) - then 1 - else 0 - end as unscheduled_this_month, - - case - when encounter_datetime between date_format(endDate,"%Y-%m-01") and endDate - AND gender="F" - AND @age >= 18 - then 1 - else 0 - end as f_18_and_over_this_month, - - - case - when arv_first_regimen_location_id != -1 - and arv_first_regimen_start_date between date_format(endDate,"%Y-%m-01") and endDate then arv_first_regimen_location_id - else location_id - end as location_id, - - - encounter_type, - date(t2.death_date) as death_date, - - enrollment_date, - - case - when arv_first_regimen_start_date < enrollment_date then 0 - when enrollment_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as enrolled_this_month, - - case - when transfer_in_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as transfer_in_this_month, - transfer_in_location_id, - transfer_in_date, - - - case - when transfer_out_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as transfer_out_this_month, - - transfer_out_location_id, - transfer_out_date, - case - when date_format(endDate, "%Y-%m-01") > t2.death_date then @status := "dead" -# when transfer_out_date < date_format(endDate,"%Y-%m-01") then @status := "transfer_out" - when date_format(endDate, "%Y-%m-01") > transfer_out_date then @status := "transfer_out" - when timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 then @status := "active" - when timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) > 90 then @status := "ltfu" - else @status := "unknown" - end as status, - - case - when arv_first_regimen_location_id is not null then arv_first_regimen_location_id - when arv_first_regimen_location_id is null and arv_first_regimen_start_date is not null then location_id else - null end as arv_first_regimen_location_id, - - arv_first_regimen, - get_arv_names(arv_first_regimen) as arv_first_regimen_names, - date(arv_first_regimen_start_date) as arv_first_regimen_start_date, - - case - when @status="active" and arv_first_regimen_start_date between date_format(endDate,"%Y-%m-01") and endDate then @started_art_this_month := 1 - else @started_art_this_month :=0 - end as started_art_this_month, - - case - when @status != "transfer_out" and month(arv_first_regimen_start_date) = month(endDate) then @art_cohort_num := timestampdiff(year,arv_first_regimen_start_date,endDate) - else @art_cohort_num := null - end as art_cohort_num, - - case - when @status != "transfer_out" AND @art_cohort_num = 1 - then @net_12_month_cohort_this_month := 1 - else @net_12_month_cohort_this_month := 0 - end as net_12_month_cohort_this_month, - - case - when @status = "active" AND @art_cohort_num = 1 AND cur_arv_meds is not null - then @active_on_art_12_month_cohort_this_month := 1 - else @active_on_art_12_month_cohort_this_month := 0 - end as active_on_art_12_month_cohort_this_month, - - - case - when @status != "transfer_out" and month(arv_first_regimen_start_date) = month(endDate) then year(endDate) - else null - end as art_cohort_year, - - case - when @status = "active" then 1 - else 0 - end as active_in_care_this_month, - - case - when @status="active" and arv_first_regimen is null then 1 - else 0 - end as is_pre_art_this_month, - - case - when date_format(arv_first_regimen_start_date,"%Y-%m-01") != date_format(endDate,"%Y-%m-01") - AND cur_arv_meds is not null - AND @status = "active" - THEN 1 - else 0 - end as art_revisit_this_month, - - arv_start_date, - - cur_arv_meds, - get_arv_names(cur_arv_meds) as cur_arv_meds_names, - cur_arv_meds_strict, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - - # We are using liberal definition of cur_arv_meds such that - # if a patient previously on ART and not explicilty stopped, the - # patient is considered to be on ART. - case - when cur_arv_meds is not null AND @status = "active" - then 1 - else 0 - end as on_art_this_month, - - case - when cur_arv_meds = arv_first_regimen - AND @status="active" - AND cur_arv_line_reported=1 - then 1 - else 0 - end as on_original_first_line_this_month, - - case - when cur_arv_meds != arv_first_regimen - AND @status="active" - AND cur_arv_line_reported=1 - then 1 - else 0 - end as on_alt_first_line_this_month, - - case - when @status = "active" AND cur_arv_line_reported >= 2 then 1 - else 0 - end as on_second_line_or_higher_this_month, - - - - timestampdiff(day,arv_start_date,endDate) as days_since_starting_arvs, - - case - when timestampdiff(month, arv_start_date,endDate) > 6 then 1 - else 0 - end as eligible_for_vl, - - timestampdiff(day,vl_1_date,endDate) as days_since_last_vl, - - if(timestampdiff(year,vl_1_date,endDate) < 1,1,0) as vl_in_past_year, - - case - when @status = "active" AND timestampdiff(year,vl_1_date,endDate) < 1 then @has_vl_this_month := 1 - else @has_vl_this_month := 0 - end as has_vl_this_month, - - - case - when @status = "active" AND timestampdiff(year,vl_1_date,endDate) < 1 and vl_1 < 1000 then @is_suppressed_this_month := 1 - else @is_suppressed_this_month := 0 - end as is_suppressed_this_month, - - - case - when @active_on_art_12_month_cohort_this_month AND @has_vl_this_month then 1 - else 0 - end as has_vl_12_month_cohort, - - case - when @active_on_art_12_month_cohort_this_month AND @is_suppressed_this_month then 1 - else 0 - end as vl_suppressed_12_month_cohort, - - vl_1, - date(vl_1_date) as vl_1_date, - - vl_2, - date(vl_2_date) as vl_2_date, - - tb_screen, - tb_screening_datetime, - tb_screening_result, - - case - when tb_screening_datetime >= date(encounter_datetime) - AND @status = "active" then 1 - else 0 - end as tb_screened_this_month, - - case - when tb_screening_result = 6971 - AND tb_screening_datetime between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as presumed_tb_positive_this_month, - - date(tb_tx_start_date) as tb_tx_start_date,#need to know time period, i.e. screened this month or screened in past X months - - case - when tb_tx_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_tb_tx_this_month, - - on_tb_tx as on_tb_tx_this_month, - - - case - when on_tb_tx =1 and @started_art_this_month then 1 - else 0 - end as on_tb_tx_and_started_art_this_month, - - date(pcp_prophylaxis_start_date) as pcp_prophylaxis_start_date, - - case - when pcp_prophylaxis_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_pcp_prophylaxis_this_month, - - case - when @status="active" AND pcp_prophylaxis_start_date is not null then 1 - else 0 - end as on_pcp_prophylaxis_this_month, - - - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - case - when ipt_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_ipt_this_month, - - case - when ipt_start_date is not null and ipt_stop_date is null then 1 - else 0 - end as on_ipt_this_month, - - - case - when timestampdiff(month,ipt_completion_date,endDate) <= 12 then 1 - else 0 - end as completed_ipt_past_12_months, - - case - when is_pregnant and edd > endDate then 1 - else 0 - end as pregnant_this_month, - - case - when is_pregnant and edd > endDate and @started_art_this_month then 1 - else 0 - end as is_pregnant_and_started_art_this_month, - - 0 as delivered_this_month, - - case - when condoms_provided_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as condoms_provided_this_month, - - case - when condoms_provided_date >= date(encounter_datetime) - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 then 1 - else 0 - end as condoms_provided_since_active, - - - case - when modern_contraceptive_method_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_modern_contraception_this_month, - - case - when modern_contraceptive_method_start_date <= date(encounter_datetime) - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 - then 1 - else 0 - end as modern_contraception_since_active, - - case - when modern_contraceptive_method_start_date <= date(encounter_datetime) - AND @status="active" - AND gender="F" - AND @age>=15 - then 1 - else 0 - end as on_modern_contraception_this_month, - - - contraceptive_method, - discordant_status - - - from etl.dates t1 - join etl.flat_hiv_summary_v15b t2 - join amrs.person t3 using (person_id) - join etl.hiv_monthly_report_dataset_build_queue__0 t5 using (person_id) - - - where - t2.encounter_datetime <= t1.endDate - and (t2.next_clinical_datetime_hiv is null or t2.next_clinical_datetime_hiv > t1.endDate ) - and t2.is_clinical_encounter=1 - and t1.endDate between start_date and date_add(now(),interval 4 month) - order by person_id, endDate - ); - - - - set @prev_id = null; - set @cur_id = null; - set @cur_status = null; - set @prev_status = null; - - drop temporary table if exists hiv_monthly_report_dataset_1; - create temporary table hiv_monthly_report_dataset_1 - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - case - when @prev_id=@cur_id then @prev_status := @cur_status - else @prev_status := null - end as next_status, - @cur_status := status as cur_status - - from hiv_monthly_report_dataset_0 - order by person_id, endDate desc - ); - - alter table hiv_monthly_report_dataset_1 drop prev_id, drop cur_id, drop cur_status; - - set @prev_id = null; - set @cur_id = null; - set @cur_status = null; - set @prev_status = null; - set @cur_arv_meds = null; - drop temporary table if exists hiv_monthly_report_dataset_2; - create temporary table hiv_monthly_report_dataset_2 - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - case - when @prev_id=@cur_id then @prev_status := @cur_status - else @prev_status := null - end as prev_status, - @cur_status := status as cur_status, - - case - when @prev_id = @cur_id then @cur_arv_meds - else @cur_arv_meds := null - end as prev_month_arvs, - - @cur_arv_meds := cur_arv_meds as cur_arv_meds_2, - - days_since_starting_arvs - days_since_last_vl as days_between_arv_start_and_last_vl - - - from hiv_monthly_report_dataset_1 - order by person_id, endDate - ); - - select now(); - select count(*) as num_rows_to_be_inserted from hiv_monthly_report_dataset_2; - - #add data to table -# replace into hiv_monthly_report_dataset - replace into hiv_monthly_report_dataset_v1_2 - (select - null, #date_created will be automatically set or updated - elastic_id, - endDate, - encounter_id, - person_id, - person_uuid, - birthdate, - age, - gender, - encounter_date, - visit_this_month, - rtc_date, - days_since_rtc_date, - on_schedule, - scheduled_this_month, - unscheduled_this_month, - f_18_and_over_this_month, - location_id, - t2.uuid as location_uuid, - t2.state_province as clinic_county, - t2.name as clinic, - t2.latitude as clinic_latitude, - t2.longitude as clinic_longitude, - encounter_type, - death_date, - enrollment_date, - enrolled_this_month, - transfer_in_this_month, - transfer_in_location_id, - transfer_in_date, - transfer_out_this_month, - transfer_out_location_id, - transfer_out_date, - prev_status, - status, - next_status, - active_in_care_this_month, - is_pre_art_this_month, - arv_first_regimen_location_id, - arv_first_regimen, - arv_first_regimen_names, - arv_first_regimen_start_date, - art_cohort_num, - art_cohort_year, - days_since_starting_arvs, - started_art_this_month, - art_revisit_this_month, - arv_start_date, - prev_month_arvs, - cur_arv_meds, - cur_arv_meds_names, - cur_arv_meds_strict, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - on_art_this_month, - on_original_first_line_this_month, - on_alt_first_line_this_month, - on_second_line_or_higher_this_month, - eligible_for_vl, - days_since_last_vl, - net_12_month_cohort_this_month, - active_on_art_12_month_cohort_this_month, - has_vl_12_month_cohort, - vl_suppressed_12_month_cohort, - has_vl_this_month, - is_suppressed_this_month, - vl_1, - vl_1_date, - vl_in_past_year, - vl_2, - vl_2_date, - tb_screen, - tb_screening_datetime, - tb_screening_result, - tb_screened_this_month, - presumed_tb_positive_this_month, - tb_tx_start_date, - started_tb_tx_this_month, - on_tb_tx_this_month, - on_tb_tx_and_started_art_this_month, - pcp_prophylaxis_start_date, - started_pcp_prophylaxis_this_month, - on_pcp_prophylaxis_this_month, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - started_ipt_this_month, - on_ipt_this_month, - completed_ipt_past_12_months, - pregnant_this_month, - is_pregnant_and_started_art_this_month, - delivered_this_month, - condoms_provided_this_month, - condoms_provided_since_active, - started_modern_contraception_this_month, - modern_contraception_since_active, - on_modern_contraception_this_month, - contraceptive_method, - discordant_status - from hiv_monthly_report_dataset_2 t1 - join amrs.location t2 using (location_id) - ); - - - - #delete from @queue_table where person_id in (select person_id from hiv_monthly_report_dataset_build_queue__0); - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join hiv_monthly_report_dataset_build_queue__0 t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count := (select count(*) from @queue_table); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count as num_remaining; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - #select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select count(*) into @num_in_hmrd from hiv_monthly_report_dataset_v1_2; - - select @num_in_hmrd as num_in_hmrd, - @person_ids_count as num_remaining, - @cycle_length as 'Cycle time (s)', - ceil(@person_ids_count / cycle_size) as remaining_cycles, - @remaining_time as 'Est time remaining (min)'; - - - end while; - - if(query_type = "build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - set @end = now(); - # not sure why we need last date_created, I've replaced this with @start - insert into etl.flat_log values (@start,@start,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset_v1_3.sql b/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset_v1_3.sql deleted file mode 100644 index 516ad0e..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset_v1_3.sql +++ /dev/null @@ -1,984 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_monthly_report_dataset_v1_3`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int, IN start_date varchar(50)) -BEGIN - - set @start = now(); - set @table_version = "hiv_monthly_report_dataset_v1.3"; - set @last_date_created = (select max(date_created) from etl.flat_hiv_summary_v15b); - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - - - #drop table if exists hiv_monthly_report_dataset_v1_3; -create table if not exists hiv_monthly_report_dataset_v1_2 ( -# create table if not exists hiv_monthly_report_dataset ( - date_created timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL, - elastic_id bigint, - endDate date, - encounter_id int, - person_id int, - person_uuid varchar(100), - birthdate date, - age double, - gender varchar(1), - encounter_date date, - visit_this_month tinyint, - rtc_date date, - days_since_rtc_date int, - on_schedule tinyint, - scheduled_this_month tinyint, - unscheduled_this_month tinyint, - f_18_and_older_this_month tinyint, - prev_location_id mediumint, - location_id mediumint, - location_uuid varchar(100), - next_location_id mediumint, - clinic_county varchar(250), - clinic varchar(250), - clinic_latitude double, - clinic_longitude double, - encounter_type int, - death_date date, - enrollment_date date, - enrolled_this_month tinyint, - transfer_in_this_month tinyint, - transfer_in_location_id mediumint, - transfer_in_date date, - transfer_out_this_month tinyint, - transfer_out_location_id mediumint, - transfer_out_date date, - prev_status varchar(250), - status varchar(250), - next_status varchar(250), - active_in_care_this_month tinyint, - is_pre_art_this_month tinyint, - arv_first_regimen_location_id int, - arv_first_regimen varchar(250), - arv_first_regimen_names varchar(250), - arv_first_regimen_start_date date, - art_cohort_year date, - art_cohort_month date, - art_cohort_num smallint, - art_cohort_total_months smallint, - days_since_starting_arvs mediumint, - started_art_this_month tinyint, - art_revisit_this_month tinyint, - arv_start_date date, - prev_month_arvs varchar(250), - prev_month_arvs_names varchar(250), - cur_arv_meds varchar(250), - cur_arv_meds_names varchar(250), - cur_arv_meds_strict varchar(250), - cur_arv_line tinyint, - cur_arv_line_strict tinyint, - cur_arv_line_reported tinyint, - on_art_this_month tinyint, - on_original_first_line_this_month tinyint, - on_alt_first_line_this_month tinyint, - on_second_line_or_higher_this_month tinyint, - eligible_for_vl tinyint, - days_since_last_vl int, - net_12_month_cohort_this_month tinyint, - active_on_art_12_month_cohort_this_month tinyint, - has_vl_12_month_cohort tinyint, - vl_suppressed_12_month_cohort tinyint, - has_vl_this_month tinyint, #patient has had VL in past year and is currently active on ART - is_suppressed_this_month tinyint, #patient has had VL in past year < 1000 and is currently active on ART - vl_1 int, - vl_1_date date, - vl_in_past_year tinyint, - vl_2 int, - vl_2_date date, - qualifies_for_follow_up_vl tinyint, - got_follow_up_vl tinyint, - got_follow_up_vl_this_month tinyint, - num_days_to_follow_vl tinyint, - follow_up_vl_suppressed tinyint, - follow_up_vl_suppressed_this_month tinyint, - follow_up_vl_unsuppressed tinyint, - follow_up_vl_unsuppressed_this_month tinyint, - due_for_vl_this_month tinyint, - reason_for_needing_vl_this_month varchar(250), - number_of_months_has_needed_vl smallint, - needs_follow_up_vl tinyint, - had_med_change_this_month tinyint, - had_med_change_this_month_after_2_unsuppressed_vls tinyint, - same_meds_this_month_after_2_unsuppressed_vls tinyint, - tb_screen tinyint, - tb_screening_datetime datetime, - tb_screening_result int, - tb_screened_this_visit_this_month tinyint, - tb_screened_active_this_month tinyint, - presumed_tb_positive_this_month tinyint, - tb_tx_start_date date, - started_tb_tx_this_month tinyint, - on_tb_tx_this_month tinyint, - on_tb_tx_and_started_art_this_month tinyint, - pcp_prophylaxis_start_date date, - started_pcp_prophylaxis_this_month tinyint, - on_pcp_prophylaxis_this_month tinyint, - ipt_start_date date, - ipt_stop_date date, - ipt_completion_date date, - started_ipt_this_month tinyint, - on_ipt_this_month tinyint, - completed_ipt_past_12_months tinyint, - pregnant_this_month tinyint, - is_pregnant_and_started_art_this_month tinyint, - delivered_this_month tinyint, - condoms_provided_this_month tinyint, - condoms_provided_since_active tinyint, - started_modern_contraception_this_month tinyint, - modern_contraception_since_active tinyint, - on_modern_contraception_this_month tinyint, - contraceptive_method int, - discordant_status int, - - primary key elastic_id (elastic_id), - index person_enc_date (person_id, encounter_date), - index person_report_date (person_id, endDate), - index endDate_location_id (endDate, location_id), - index date_created (date_created), - index status_change (location_id, endDate, status, prev_status) - - ); - - #create table if not exists hiv_monthly_report_dataset_build_queue (person_id int primary key); - #replace into hiv_monthly_report_dataset_build_queue - #(select distinct person_id from flat_hiv_summary_v15); - - if (query_type = "build") then - select "BUILDING......................."; - set @queue_table = concat("hiv_monthly_report_dataset_build_queue_",queue_number); -#set @queue_table = concat("hiv_monthly_report_dataset_build_queue_1"); - #create table if not exists @queue_table (person_id int, primary key (person_id)); - #SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from hiv_monthly_report_dataset_build_queue limit 1000);'); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(person_id int primary key) (select * from hiv_monthly_report_dataset_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from hiv_monthly_report_dataset_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from hiv_monthly_report_dataset_build_queue t1 join ',@queue_table, ' t2 using (person_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - - if (query_type = "sync") then - set @queue_table = "hiv_monthly_report_dataset_sync_queue"; - create table if not exists hiv_monthly_report_dataset_sync_queue (person_id int primary key); - - select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); - - replace into hiv_monthly_report_dataset_sync_queue - (select distinct person_id from flat_hiv_summary_v15b where date_created >= @last_update); - end if; - - - SET @num_ids := 0; - SET @dyn_sql=CONCAT('select count(*) into @num_ids from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - -SET @dyn_sql=CONCAT('delete t1 from hiv_monthly_report_dataset_v1_2 t1 join ',@queue_table,' t2 using (person_id);'); -# SET @dyn_sql=CONCAT('delete t1 from hiv_monthly_report_dataset t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - #create temporary table hiv_monthly_report_dataset_build_queue_0 (select * from hiv_monthly_report_dataset_build_queue_2 limit 5000); #TODO - change this when data_fetch_size changes - - drop temporary table if exists hiv_monthly_report_dataset_build_queue__0; - create temporary table hiv_monthly_report_dataset_build_queue__0 (person_id int primary key); - -#SET @dyn_sql=CONCAT('insert into hiv_monthly_report_dataset_build_queue__0 (select * from hiv_monthly_report_dataset_build_queue_1 limit 100);'); - SET @dyn_sql=CONCAT('insert into hiv_monthly_report_dataset_build_queue__0 (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - set @age =null; - set @status = null; - - drop temporary table if exists hiv_monthly_report_dataset_0; - create temporary table hiv_monthly_report_dataset_0 - (select - concat(date_format(endDate,"%Y%m"),person_id) as elastic_id, - endDate, - encounter_id, - person_id, - t3.uuid as person_uuid, - date(birthdate) as birthdate, - case - when timestampdiff(year,birthdate,endDate) > 0 then @age := round(timestampdiff(year,birthdate,endDate),0) - else @age :=round(timestampdiff(month,birthdate,endDate)/12,2) - end as age, - t3.gender, - date(encounter_datetime) as encounter_date, - - if(encounter_datetime between date_format(endDate,"%Y-%m-01") and endDate,1,0) as visit_this_month, - - date(rtc_date) as rtc_date, - timestampdiff(day,rtc_date, endDate) as days_since_rtc_date, - - if(date(encounter_datetime) = date(prev_clinical_rtc_date_hiv),1,0) as on_schedule, - - case - when encounter_datetime between date_format(endDate,"%Y-%m-01") and endDate - AND date(encounter_datetime) = date(prev_clinical_rtc_date_hiv) - then 1 - else 0 - end as scheduled_this_month, - - case - when encounter_datetime between date_format(endDate,"%Y-%m-01") and endDate - AND date(encounter_datetime) != date(prev_clinical_rtc_date_hiv) - then 1 - else 0 - end as unscheduled_this_month, - - case - when encounter_datetime between date_format(endDate,"%Y-%m-01") and endDate - AND gender="F" - AND @age >= 18 - then 1 - else 0 - end as f_18_and_over_this_month, - - - case - when arv_first_regimen_location_id != 9999 - and arv_first_regimen_start_date between date_format(endDate,"%Y-%m-01") and endDate then arv_first_regimen_location_id - else location_id - end as location_id, - - - encounter_type, - date(t2.death_date) as death_date, - - enrollment_date, - - case - when arv_first_regimen_start_date < enrollment_date then 0 - when enrollment_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as enrolled_this_month, - - case - when transfer_in_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as transfer_in_this_month, - transfer_in_location_id, - transfer_in_date, - - - case - when transfer_out_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as transfer_out_this_month, - - transfer_out_location_id, - transfer_out_date, - case - when date_format(endDate, "%Y-%m-01") > t2.death_date then @status := "dead" -# when transfer_out_date < date_format(endDate,"%Y-%m-01") then @status := "transfer_out" - when date_format(endDate, "%Y-%m-01") > transfer_out_date then @status := "transfer_out" - when timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 then @status := "active" - when timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) > 90 then @status := "ltfu" - else @status := "unknown" - end as status, - - case - when arv_first_regimen_location_id is not null then arv_first_regimen_location_id - when arv_first_regimen_location_id is null and arv_first_regimen_start_date is not null then location_id else - null end as arv_first_regimen_location_id, - - arv_first_regimen, - get_arv_names(arv_first_regimen) as arv_first_regimen_names, - date(arv_first_regimen_start_date) as arv_first_regimen_start_date, - - date_format(arv_first_regimen_start_date, "%Y-01-01") as art_cohort_year, - - date_format(arv_first_regimen_start_date,"%Y-%m-01") as art_cohort_month, - - timestampdiff(month,arv_first_regimen_start_date,endDate) as art_cohort_total_months, - - case - when @status != "transfer_out" and month(arv_first_regimen_start_date) = month(endDate) then @art_cohort_num := timestampdiff(year,arv_first_regimen_start_date,endDate) - else @art_cohort_num := null - end as art_cohort_num, - - case - when @status="active" and arv_first_regimen_start_date between date_format(endDate,"%Y-%m-01") and endDate then @started_art_this_month := 1 - else @started_art_this_month :=0 - end as started_art_this_month, - - - case - when @status != "transfer_out" AND @art_cohort_num = 1 - then @net_12_month_cohort_this_month := 1 - else @net_12_month_cohort_this_month := 0 - end as net_12_month_cohort_this_month, - - case - when @status = "active" AND @art_cohort_num = 1 AND cur_arv_meds is not null - then @active_on_art_12_month_cohort_this_month := 1 - else @active_on_art_12_month_cohort_this_month := 0 - end as active_on_art_12_month_cohort_this_month, - - - case - when @status = "active" then 1 - else 0 - end as active_in_care_this_month, - - case - when @status="active" and arv_first_regimen is null then 1 - else 0 - end as is_pre_art_this_month, - - case - when date_format(arv_first_regimen_start_date,"%Y-%m-01") != date_format(endDate,"%Y-%m-01") - AND cur_arv_meds is not null - AND @status = "active" - THEN 1 - else 0 - end as art_revisit_this_month, - - arv_start_date, - - cur_arv_meds, - get_arv_names(cur_arv_meds) as cur_arv_meds_names, - cur_arv_meds_strict, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - - # We are using liberal definition of cur_arv_meds such that - # if a patient previously on ART and not explicilty stopped, the - # patient is considered to be on ART. - case - when cur_arv_meds is not null AND @status = "active" - then 1 - else 0 - end as on_art_this_month, - - case - when cur_arv_meds = arv_first_regimen - AND @status="active" - AND cur_arv_line_reported=1 - then 1 - else 0 - end as on_original_first_line_this_month, - - case - when cur_arv_meds != arv_first_regimen - AND @status="active" - AND cur_arv_line_reported=1 - then 1 - else 0 - end as on_alt_first_line_this_month, - - case - when @status = "active" AND cur_arv_line_reported >= 2 then 1 - else 0 - end as on_second_line_or_higher_this_month, - - - - timestampdiff(day,arv_start_date,endDate) as days_since_starting_arvs, - - case - when timestampdiff(month, arv_start_date,endDate) > 6 then 1 - else 0 - end as eligible_for_vl, - - - timestampdiff(day,vl_1_date,endDate) as days_since_last_vl, - - if(timestampdiff(year,vl_1_date,endDate) < 1,1,0) as vl_in_past_year, - - case - when @status = "active" AND timestampdiff(year,vl_1_date,endDate) < 1 then @has_vl_this_month := 1 - else @has_vl_this_month := 0 - end as has_vl_this_month, - - - case - when @status = "active" AND timestampdiff(year,vl_1_date,endDate) < 1 and vl_1 < 1000 then @is_suppressed_this_month := 1 - else @is_suppressed_this_month := 0 - end as is_suppressed_this_month, - - - case - when @active_on_art_12_month_cohort_this_month AND @has_vl_this_month then 1 - else 0 - end as has_vl_12_month_cohort, - - case - when @active_on_art_12_month_cohort_this_month AND @is_suppressed_this_month then 1 - else 0 - end as vl_suppressed_12_month_cohort, - - vl_1, - date(vl_1_date) as vl_1_date, - - vl_2, - date(vl_2_date) as vl_2_date, - - case - when vl_1>=1000 and timestampdiff(month,vl_1_date,endDate) between 3 and 12 then @qualifies_for_follow_up_vl := 1 - when vl_2>=1000 and timestampdiff(month,vl_2_date,endDate) between 3 and 12 then @qualifies_for_follow_up_vl := 1 - else @qualifies_for_follow_up_vl := 0 - end as qualifies_for_follow_up_vl, - - case - when @qualifies_for_follow_up_vl AND timestampdiff(month,vl_2_date,vl_1_date) < 12 - then @got_follow_up_vl := 1 - else @got_follow_up_vl := 0 - end as got_follow_up_vl, - - case - when @got_follow_up_vl and vl_1_date between date_format(endDate,"%Y-%m-01") and endDate then @got_follow_up_vl_this_month := 1 - else @got_follow_up_vl_this_month := 0 - end as got_follow_up_vl_this_month, - - case - when @got_follow_up_vl then timestampdiff(day,vl_2_date, vl_1_date) - else null - end as num_days_to_follow_vl, - - - case - when @got_follow_up_vl AND vl_1 < 1000 then @follow_up_vl_suppressed := 1 - else @follow_up_vl_suppressed := 0 - end as follow_up_vl_suppressed, - - case - when @got_follow_up_vl_this_month AND vl_1 < 1000 then @follow_up_vl_suppressed_this_month := 1 - else @follow_up_vl_suppressed_this_month := 0 - end as follow_up_vl_suppressed_this_month, - - - case - when @got_follow_up_vl AND vl_1 >= 1000 then @follow_up_vl_unsuppressed := 1 - else @follow_up_vl_unsuppressed := 0 - end as follow_up_vl_unsuppressed, - - case - when @got_follow_up_vl_this_month AND vl_1 >= 1000 then @follow_up_vl_unsuppressed_this_month := 1 - else @follow_up_vl_unsuppressed_this_month := 0 - end as follow_up_vl_unsuppressed_this_month, - - - case - when - vl_1 > 1000 - AND vl_1_date > arv_start_date - AND timestampdiff(month,vl_1_date,endDate) > 3 - AND timestampdiff(year,vl_1_date,endDate) <= 1 - then @needs_follow_up_vl :=1 - else @needs_follow_up_vl := 0 - end as needs_follow_up_vl, - - - - case - when - vl_1 > 1000 - AND vl_1_date > arv_start_date - AND timestampdiff(month,vl_1_date,endDate) between 3 and 11 - THEN 1 - when timestampdiff(month,arv_start_date,endDate) between 6 and 11 AND vl_1 is null THEN 1 - when - timestampdiff(month,arv_start_date,endDate) between 12 and 18 - AND vl_2 is null then 1 - when - timestampdiff(month,arv_start_date,endDate) >= 12 - AND ifnull(timestampdiff(month,vl_1_date,endDate) >= 12,1) - then 1 - end as due_for_vl_this_month, - - case - when vl_1 > 1000 - AND vl_1_date > arv_start_date - AND timestampdiff(month,vl_1_date,endDate) between 3 and 11 - THEN "Previous VL Unsuppressed" - when timestampdiff(month,arv_start_date,endDate) between 6 and 11 AND vl_1 is null then "On ART for 6 months" - when - timestampdiff(month,arv_start_date,endDate) between 12 and 18 - AND vl_2 is null - THEN "On ART for 12 months" - when - timestampdiff(month,arv_start_date,endDate) >= 12 - AND ifnull(timestampdiff(month,vl_1_date,endDate) >= 12,1) - then "Annual VL" - - end as reason_for_needing_vl_this_month, - - - - case - when vl_1 > 1000 - AND vl_1_date > arv_start_date - AND timestampdiff(month,vl_1_date,endDate) between 3 and 11 - then timestampdiff(month,vl_1_date,endDate) - 3 - - when timestampdiff(month,arv_start_date,endDate) between 6 and 11 AND vl_1 is null - then timestampdiff(month,arv_start_date,endDate) - 6 - - when - timestampdiff(month,arv_start_date,endDate) between 12 and 18 - AND vl_2 is null - then timestampdiff(month,arv_start_date,endDate) - 12 - - when - timestampdiff(month,arv_start_date,endDate) >= 12 - AND ifnull(timestampdiff(month,vl_1_date,endDate) >= 12,1) - then timestampdiff(month,ifnull(vl_1_date,arv_start_date),endDate) - 12 - end as number_of_months_has_needed_vl, - - - tb_screen, - tb_screening_datetime, - tb_screening_result, - - case - when tb_screening_datetime between date_format(endDate, "%Y-%m-01") and endDate - AND @status = "active" then 1 - else 0 - end as tb_screened_this_visit_this_month, - - case - when tb_screening_datetime >= date(encounter_datetime) - AND @status = "active" then 1 - else 0 - end as tb_screened_active_this_month, - - case - when tb_screening_result = 6971 - AND tb_screening_datetime between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as presumed_tb_positive_this_month, - - date(tb_tx_start_date) as tb_tx_start_date,#need to know time period, i.e. screened this month or screened in past X months - - case - when tb_tx_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_tb_tx_this_month, - - on_tb_tx as on_tb_tx_this_month, - - - case - when on_tb_tx =1 and @started_art_this_month then 1 - else 0 - end as on_tb_tx_and_started_art_this_month, - - date(pcp_prophylaxis_start_date) as pcp_prophylaxis_start_date, - - case - when pcp_prophylaxis_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_pcp_prophylaxis_this_month, - - case - when @status="active" AND pcp_prophylaxis_start_date is not null then 1 - else 0 - end as on_pcp_prophylaxis_this_month, - - - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - case - when ipt_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_ipt_this_month, - - case - when ipt_start_date is not null and ipt_stop_date is null then 1 - else 0 - end as on_ipt_this_month, - - - case - when timestampdiff(month,ipt_completion_date,endDate) <= 12 then 1 - else 0 - end as completed_ipt_past_12_months, - - case - when is_pregnant and edd > endDate then 1 - else 0 - end as pregnant_this_month, - - case - when is_pregnant and edd > endDate and @started_art_this_month then 1 - else 0 - end as is_pregnant_and_started_art_this_month, - - 0 as delivered_this_month, - - case - when condoms_provided_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as condoms_provided_this_month, - - case - when condoms_provided_date >= date(encounter_datetime) - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 then 1 - else 0 - end as condoms_provided_since_active, - - - case - when modern_contraceptive_method_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_modern_contraception_this_month, - - case - when modern_contraceptive_method_start_date <= date(encounter_datetime) - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 30 day)),endDate) <= 90 - then 1 - else 0 - end as modern_contraception_since_active, - - case - when modern_contraceptive_method_start_date <= date(encounter_datetime) - AND @status="active" - AND gender="F" - AND @age>=15 - then 1 - else 0 - end as on_modern_contraception_this_month, - - - contraceptive_method, - discordant_status - - - from etl.dates t1 - join etl.flat_hiv_summary_v15b t2 - join amrs.person t3 using (person_id) - join etl.hiv_monthly_report_dataset_build_queue__0 t5 using (person_id) - - - where - #t2.encounter_datetime <= t1.endDate - t2.encounter_datetime < date_add(endDate, interval 1 day) - and (t2.next_clinical_datetime_hiv is null or t2.next_clinical_datetime_hiv >= date_add(t1.endDate, interval 1 day) ) - and t2.is_clinical_encounter=1 - and t1.endDate between start_date and date_add(now(),interval 1 month) - order by person_id, endDate - ); - - - - set @prev_id = null; - set @cur_id = null; - set @cur_status = null; - set @prev_status = null; - set @prev_location_id = null; - set @cur_location_id = null; - - drop temporary table if exists hiv_monthly_report_dataset_1; - create temporary table hiv_monthly_report_dataset_1 - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_location_id := @cur_location_id - else @prev_location_id := null - end as next_location_id, - - @cur_location_id := location_id as cur_location_id, - - case - when @prev_id=@cur_id then @prev_status := @cur_status - else @prev_status := null - end as next_status, - @cur_status := status as cur_status - - from hiv_monthly_report_dataset_0 - order by person_id, endDate desc - ); - - alter table hiv_monthly_report_dataset_1 drop prev_id, drop cur_id, drop cur_status, drop cur_location_id; - - set @prev_id = null; - set @cur_id = null; - set @cur_status = null; - set @prev_status = null; - set @cur_arv_meds = null; - set @prev_arv_meds = null; - set @cur_location_id = null; - set @prev_location_id = null; - drop temporary table if exists hiv_monthly_report_dataset_2; - create temporary table hiv_monthly_report_dataset_2 - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_location_id := @cur_location_id - else @prev_location_id := null - end as prev_location_id, - - @cur_location_id := location_id as cur_location_id, - - case - when @prev_id=@cur_id then @prev_status := @cur_status - else @prev_status := null - end as prev_status, - @cur_status := status as cur_status, - - case - when @prev_id = @cur_id then @prev_month_arvs := @cur_arv_meds - else @prev_month_arvs := null - end as prev_month_arvs, - - @cur_arv_meds := cur_arv_meds as cur_arv_meds_2, - - case - when @prev_id = @cur_id then @prev_month_arvs_names := @cur_arv_meds_names - else @prev_month_arvs_names := null - end as prev_month_arvs_names, - - @cur_arv_meds_names := cur_arv_meds_names as cur_arv_meds_names_2, - - - days_since_starting_arvs - days_since_last_vl as days_between_arv_start_and_last_vl, - - case - when @prev_id != @cur_id then 0 - when cur_arv_meds != @prev_month_arvs then 1 - when (@prev_month_arvs is not null and cur_arv_meds is null) then 1 - else 0 - end as had_med_change_this_month, - - case - when @prev_id != @cur_id then 0 - when @follow_up_vl_unsuppressed AND cur_arv_meds != @prev_month_arvs then 1 - when @follow_up_vl_unsuppressed AND (@prev_month_arvs is not null and cur_arv_meds is null) then 1 - else 0 - end as had_med_change_this_month_after_2_unsuppressed_vls, - - - case - when @prev_id != @cur_id then 0 - when @follow_up_vl_unsuppressed AND cur_arv_meds = @prev_month_arvs then 1 - else 0 - end as same_meds_this_month_after_2_unsuppressed_vls - - - - from hiv_monthly_report_dataset_1 - order by person_id, endDate - ); - - select now(); - select count(*) as num_rows_to_be_inserted from hiv_monthly_report_dataset_2; - - #add data to table -# replace into hiv_monthly_report_dataset - replace into hiv_monthly_report_dataset_v1_2 - (select - null, #date_created will be automatically set or updated - elastic_id, - endDate, - encounter_id, - person_id, - person_uuid, - birthdate, - age, - gender, - encounter_date, - visit_this_month, - rtc_date, - days_since_rtc_date, - on_schedule, - scheduled_this_month, - unscheduled_this_month, - f_18_and_over_this_month, - prev_location_id, - location_id, - t2.uuid as location_uuid, - next_location_id, - t2.state_province as clinic_county, - t2.name as clinic, - t2.latitude as clinic_latitude, - t2.longitude as clinic_longitude, - encounter_type, - death_date, - enrollment_date, - enrolled_this_month, - transfer_in_this_month, - transfer_in_location_id, - transfer_in_date, - transfer_out_this_month, - transfer_out_location_id, - transfer_out_date, - prev_status, - status, - next_status, - active_in_care_this_month, - is_pre_art_this_month, - arv_first_regimen_location_id, - arv_first_regimen, - arv_first_regimen_names, - arv_first_regimen_start_date, - art_cohort_year, - art_cohort_month, - art_cohort_num, - art_cohort_total_months, - days_since_starting_arvs, - started_art_this_month, - art_revisit_this_month, - arv_start_date, - prev_month_arvs, - prev_month_arvs_names, - cur_arv_meds, - cur_arv_meds_names, - cur_arv_meds_strict, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - on_art_this_month, - on_original_first_line_this_month, - on_alt_first_line_this_month, - on_second_line_or_higher_this_month, - eligible_for_vl, - days_since_last_vl, - net_12_month_cohort_this_month, - active_on_art_12_month_cohort_this_month, - has_vl_12_month_cohort, - vl_suppressed_12_month_cohort, - has_vl_this_month, - is_suppressed_this_month, - vl_1, - vl_1_date, - vl_in_past_year, - vl_2, - vl_2_date, - qualifies_for_follow_up_vl, - got_follow_up_vl, - got_follow_up_vl_this_month, - num_days_to_follow_vl, - follow_up_vl_suppressed, - follow_up_vl_suppressed_this_month, - follow_up_vl_unsuppressed, - follow_up_vl_unsuppressed_this_month, - due_for_vl_this_month, - reason_for_needing_vl_this_month, - number_of_months_has_needed_vl, - needs_follow_up_vl, - had_med_change_this_month, - had_med_change_this_month_after_2_unsuppressed_vls, - same_meds_this_month_after_2_unsuppressed_vls, - tb_screen, - tb_screening_datetime, - tb_screening_result, - tb_screened_this_visit_this_month, - tb_screened_active_this_month, - presumed_tb_positive_this_month, - tb_tx_start_date, - started_tb_tx_this_month, - on_tb_tx_this_month, - on_tb_tx_and_started_art_this_month, - pcp_prophylaxis_start_date, - started_pcp_prophylaxis_this_month, - on_pcp_prophylaxis_this_month, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - started_ipt_this_month, - on_ipt_this_month, - completed_ipt_past_12_months, - pregnant_this_month, - is_pregnant_and_started_art_this_month, - delivered_this_month, - condoms_provided_this_month, - condoms_provided_since_active, - started_modern_contraception_this_month, - modern_contraception_since_active, - on_modern_contraception_this_month, - contraceptive_method, - discordant_status - from hiv_monthly_report_dataset_2 t1 - join amrs.location t2 using (location_id) - ); - - - - #delete from @queue_table where person_id in (select person_id from hiv_monthly_report_dataset_build_queue__0); - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join hiv_monthly_report_dataset_build_queue__0 t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count := (select count(*) from @queue_table); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count as num_remaining; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - #select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - -# select count(*) into @num_in_hmrd from hiv_monthly_report_dataset_v1_2; - - select @num_in_hmrd as num_in_hmrd, - @person_ids_count as num_remaining, - @cycle_length as 'Cycle time (s)', - ceil(@person_ids_count / cycle_size) as remaining_cycles, - @remaining_time as 'Est time remaining (min)'; - - - end while; - - if(query_type = "build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - set @end = now(); - # not sure why we need last date_created, I've replaced this with @start - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset_v1_4.sql b/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset_v1_4.sql deleted file mode 100644 index 97d5f8e..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_monthly_report_dataset_v1_4.sql +++ /dev/null @@ -1,985 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_monthly_report_dataset_v1_4`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int, IN start_date varchar(50)) -BEGIN - - set @start = now(); - set @table_version = "hiv_monthly_report_dataset_v1.4"; - set @last_date_created = (select max(date_created) from etl.flat_hiv_summary_v15b); - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - - - #drop table if exists hiv_monthly_report_dataset_v1_3; -create table if not exists hiv_monthly_report_dataset_v1_2 ( -# create table if not exists hiv_monthly_report_dataset ( - date_created timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL, - elastic_id bigint, - endDate date, - encounter_id int, - person_id int, - person_uuid varchar(100), - birthdate date, - age double, - gender varchar(1), - encounter_date date, - visit_this_month tinyint, - rtc_date date, - days_since_rtc_date int, - on_schedule tinyint, - scheduled_this_month tinyint, - unscheduled_this_month tinyint, - f_18_and_older_this_month tinyint, - prev_location_id mediumint, - location_id mediumint, - location_uuid varchar(100), - next_location_id mediumint, - clinic_county varchar(250), - clinic varchar(250), - clinic_latitude double, - clinic_longitude double, - encounter_type int, - death_date date, - enrollment_date date, - enrolled_this_month tinyint, - transfer_in_this_month tinyint, - transfer_in_location_id mediumint, - transfer_in_date date, - transfer_out_this_month tinyint, - transfer_out_location_id mediumint, - transfer_out_date date, - prev_status varchar(250), - status varchar(250), - next_status varchar(250), - active_in_care_this_month tinyint, - is_pre_art_this_month tinyint, - arv_first_regimen_location_id int, - arv_first_regimen varchar(250), - arv_first_regimen_names varchar(250), - arv_first_regimen_start_date date, - art_cohort_year date, - art_cohort_month date, - art_cohort_num smallint, - art_cohort_total_months smallint, - days_since_starting_arvs mediumint, - started_art_this_month tinyint, - art_revisit_this_month tinyint, - arv_start_date date, - prev_month_arvs varchar(250), - prev_month_arvs_names varchar(250), - cur_arv_meds varchar(250), - cur_arv_meds_names varchar(250), - cur_arv_meds_strict varchar(250), - cur_arv_line tinyint, - cur_arv_line_strict tinyint, - cur_arv_line_reported tinyint, - on_art_this_month tinyint, - on_original_first_line_this_month tinyint, - on_alt_first_line_this_month tinyint, - on_second_line_or_higher_this_month tinyint, - eligible_for_vl tinyint, - days_since_last_vl int, - net_12_month_cohort_this_month tinyint, - active_on_art_12_month_cohort_this_month tinyint, - has_vl_12_month_cohort tinyint, - vl_suppressed_12_month_cohort tinyint, - has_vl_this_month tinyint, #patient has had VL in past year and is currently active on ART - is_suppressed_this_month tinyint, #patient has had VL in past year < 1000 and is currently active on ART - vl_1 int, - vl_1_date date, - vl_in_past_year tinyint, - vl_2 int, - vl_2_date date, - qualifies_for_follow_up_vl tinyint, - got_follow_up_vl tinyint, - got_follow_up_vl_this_month tinyint, - num_days_to_follow_vl tinyint, - follow_up_vl_suppressed tinyint, - follow_up_vl_suppressed_this_month tinyint, - follow_up_vl_unsuppressed tinyint, - follow_up_vl_unsuppressed_this_month tinyint, - due_for_vl_this_month tinyint, - reason_for_needing_vl_this_month varchar(250), - number_of_months_has_needed_vl smallint, - needs_follow_up_vl tinyint, - had_med_change_this_month tinyint, - had_med_change_this_month_after_2_unsuppressed_vls tinyint, - same_meds_this_month_after_2_unsuppressed_vls tinyint, - tb_screen tinyint, - tb_screening_datetime datetime, - tb_screening_result int, - tb_screened_this_visit_this_month tinyint, - tb_screened_active_this_month tinyint, - presumed_tb_positive_this_month tinyint, - tb_tx_start_date date, - started_tb_tx_this_month tinyint, - on_tb_tx_this_month tinyint, - on_tb_tx_and_started_art_this_month tinyint, - pcp_prophylaxis_start_date date, - started_pcp_prophylaxis_this_month tinyint, - on_pcp_prophylaxis_this_month tinyint, - ipt_start_date date, - ipt_stop_date date, - ipt_completion_date date, - started_ipt_this_month tinyint, - on_ipt_this_month tinyint, - completed_ipt_past_12_months tinyint, - pregnant_this_month tinyint, - is_pregnant_and_started_art_this_month tinyint, - delivered_this_month tinyint, - condoms_provided_this_month tinyint, - condoms_provided_since_active tinyint, - started_modern_contraception_this_month tinyint, - modern_contraception_since_active tinyint, - on_modern_contraception_this_month tinyint, - contraceptive_method int, - discordant_status int, - - primary key elastic_id (elastic_id), - index person_enc_date (person_id, encounter_date), - index person_report_date (person_id, endDate), - index endDate_location_id (endDate, location_id), - index date_created (date_created), - index status_change (location_id, endDate, status, prev_status) - - ); - - #create table if not exists hiv_monthly_report_dataset_build_queue (person_id int primary key); - #replace into hiv_monthly_report_dataset_build_queue - #(select distinct person_id from flat_hiv_summary_v15); - - if (query_type = "build") then - select "BUILDING......................."; - set @queue_table = concat("hiv_monthly_report_dataset_build_queue_",queue_number); -#set @queue_table = concat("hiv_monthly_report_dataset_build_queue_1"); - #create table if not exists @queue_table (person_id int, primary key (person_id)); - #SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from hiv_monthly_report_dataset_build_queue limit 1000);'); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(person_id int primary key) (select * from hiv_monthly_report_dataset_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #delete t1 from hiv_monthly_report_dataset_build_queue t1 join @queue_table t2 using (person_id) - SET @dyn_sql=CONCAT('delete t1 from hiv_monthly_report_dataset_build_queue t1 join ',@queue_table, ' t2 using (person_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - - if (query_type = "sync") then - set @queue_table = "hiv_monthly_report_dataset_sync_queue"; - create table if not exists hiv_monthly_report_dataset_sync_queue (person_id int primary key); - - select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); - - replace into hiv_monthly_report_dataset_sync_queue - (select distinct person_id from flat_hiv_summary_v15b where date_created >= @last_update); - end if; - - - SET @num_ids := 0; - SET @dyn_sql=CONCAT('select count(*) into @num_ids from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - -SET @dyn_sql=CONCAT('delete t1 from hiv_monthly_report_dataset_v1_2 t1 join ',@queue_table,' t2 using (person_id);'); -# SET @dyn_sql=CONCAT('delete t1 from hiv_monthly_report_dataset t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - #create temporary table hiv_monthly_report_dataset_build_queue_0 (select * from hiv_monthly_report_dataset_build_queue_2 limit 5000); #TODO - change this when data_fetch_size changes - - drop temporary table if exists hiv_monthly_report_dataset_build_queue__0; - create temporary table hiv_monthly_report_dataset_build_queue__0 (person_id int primary key); - -#SET @dyn_sql=CONCAT('insert into hiv_monthly_report_dataset_build_queue__0 (select * from hiv_monthly_report_dataset_build_queue_1 limit 100);'); - SET @dyn_sql=CONCAT('insert into hiv_monthly_report_dataset_build_queue__0 (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - set @age =null; - set @status = null; - - drop temporary table if exists hiv_monthly_report_dataset_0; - create temporary table hiv_monthly_report_dataset_0 - (select - concat(date_format(endDate,"%Y%m"),person_id) as elastic_id, - endDate, - encounter_id, - person_id, - t3.uuid as person_uuid, - date(birthdate) as birthdate, - case - when timestampdiff(year,birthdate,endDate) > 0 then @age := round(timestampdiff(year,birthdate,endDate),0) - else @age :=round(timestampdiff(month,birthdate,endDate)/12,2) - end as age, - t3.gender, - date(encounter_datetime) as encounter_date, - - if(encounter_datetime between date_format(endDate,"%Y-%m-01") and endDate,1,0) as visit_this_month, - - date(rtc_date) as rtc_date, - timestampdiff(day,rtc_date, endDate) as days_since_rtc_date, - - if(date(encounter_datetime) = date(prev_clinical_rtc_date_hiv),1,0) as on_schedule, - - case - when encounter_datetime between date_format(endDate,"%Y-%m-01") and endDate - AND date(encounter_datetime) = date(prev_clinical_rtc_date_hiv) - then 1 - else 0 - end as scheduled_this_month, - - case - when encounter_datetime between date_format(endDate,"%Y-%m-01") and endDate - AND date(encounter_datetime) != date(prev_clinical_rtc_date_hiv) - then 1 - else 0 - end as unscheduled_this_month, - - case - when encounter_datetime between date_format(endDate,"%Y-%m-01") and endDate - AND gender="F" - AND @age >= 18 - then 1 - else 0 - end as f_18_and_over_this_month, - - - case - when arv_first_regimen_location_id != 9999 - and arv_first_regimen_start_date between date_format(endDate,"%Y-%m-01") and endDate then arv_first_regimen_location_id - else location_id - end as location_id, - - - encounter_type, - date(t2.death_date) as death_date, - - enrollment_date, - - case - when arv_first_regimen_start_date < enrollment_date then 0 - when enrollment_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as enrolled_this_month, - - case - when transfer_in_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as transfer_in_this_month, - transfer_in_location_id, - transfer_in_date, - - - case - when transfer_out_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as transfer_out_this_month, - - transfer_out_location_id, - transfer_out_date, - case - when date_format(endDate, "%Y-%m-01") > t2.death_date then @status := "dead" -# when transfer_out_date < date_format(endDate,"%Y-%m-01") then @status := "transfer_out" - when date_format(endDate, "%Y-%m-01") > transfer_out_date then @status := "transfer_out" - when timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 28 day)),endDate) <= 28 then @status := "active" - when timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 28 day)),endDate) between 29 and 90 then @status := "defaulter" - when timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 28 day)),endDate) > 90 then @status := "ltfu" - else @status := "unknown" - end as status, - - case - when arv_first_regimen_location_id is not null then arv_first_regimen_location_id - when arv_first_regimen_location_id is null and arv_first_regimen_start_date is not null then location_id else - null end as arv_first_regimen_location_id, - - arv_first_regimen, - get_arv_names(arv_first_regimen) as arv_first_regimen_names, - date(arv_first_regimen_start_date) as arv_first_regimen_start_date, - - date_format(arv_first_regimen_start_date, "%Y-01-01") as art_cohort_year, - - date_format(arv_first_regimen_start_date,"%Y-%m-01") as art_cohort_month, - - timestampdiff(month,arv_first_regimen_start_date,endDate) as art_cohort_total_months, - - case - when @status != "transfer_out" and month(arv_first_regimen_start_date) = month(endDate) then @art_cohort_num := timestampdiff(year,arv_first_regimen_start_date,endDate) - else @art_cohort_num := null - end as art_cohort_num, - - case - when @status="active" and arv_first_regimen_start_date between date_format(endDate,"%Y-%m-01") and endDate then @started_art_this_month := 1 - else @started_art_this_month :=0 - end as started_art_this_month, - - - case - when @status != "transfer_out" AND @art_cohort_num = 1 - then @net_12_month_cohort_this_month := 1 - else @net_12_month_cohort_this_month := 0 - end as net_12_month_cohort_this_month, - - case - when @status = "active" AND @art_cohort_num = 1 AND cur_arv_meds is not null - then @active_on_art_12_month_cohort_this_month := 1 - else @active_on_art_12_month_cohort_this_month := 0 - end as active_on_art_12_month_cohort_this_month, - - - case - when @status = "active" then 1 - else 0 - end as active_in_care_this_month, - - case - when @status="active" and arv_first_regimen is null then 1 - else 0 - end as is_pre_art_this_month, - - case - when date_format(arv_first_regimen_start_date,"%Y-%m-01") != date_format(endDate,"%Y-%m-01") - AND cur_arv_meds is not null - AND @status = "active" - THEN 1 - else 0 - end as art_revisit_this_month, - - arv_start_date, - - cur_arv_meds, - get_arv_names(cur_arv_meds) as cur_arv_meds_names, - cur_arv_meds_strict, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - - # We are using liberal definition of cur_arv_meds such that - # if a patient previously on ART and not explicilty stopped, the - # patient is considered to be on ART. - case - when cur_arv_meds is not null AND @status = "active" - then 1 - else 0 - end as on_art_this_month, - - case - when cur_arv_meds = arv_first_regimen - AND @status="active" - AND cur_arv_line_reported=1 - then 1 - else 0 - end as on_original_first_line_this_month, - - case - when cur_arv_meds != arv_first_regimen - AND @status="active" - AND cur_arv_line_reported=1 - then 1 - else 0 - end as on_alt_first_line_this_month, - - case - when @status = "active" AND cur_arv_line_reported >= 2 then 1 - else 0 - end as on_second_line_or_higher_this_month, - - - - timestampdiff(day,arv_start_date,endDate) as days_since_starting_arvs, - - case - when timestampdiff(month, arv_start_date,endDate) > 6 then 1 - else 0 - end as eligible_for_vl, - - - timestampdiff(day,vl_1_date,endDate) as days_since_last_vl, - - if(timestampdiff(year,vl_1_date,endDate) < 1,1,0) as vl_in_past_year, - - case - when @status = "active" AND timestampdiff(year,vl_1_date,endDate) < 1 then @has_vl_this_month := 1 - else @has_vl_this_month := 0 - end as has_vl_this_month, - - - case - when @status = "active" AND timestampdiff(year,vl_1_date,endDate) < 1 and vl_1 < 1000 then @is_suppressed_this_month := 1 - else @is_suppressed_this_month := 0 - end as is_suppressed_this_month, - - - case - when @active_on_art_12_month_cohort_this_month AND @has_vl_this_month then 1 - else 0 - end as has_vl_12_month_cohort, - - case - when @active_on_art_12_month_cohort_this_month AND @is_suppressed_this_month then 1 - else 0 - end as vl_suppressed_12_month_cohort, - - vl_1, - date(vl_1_date) as vl_1_date, - - vl_2, - date(vl_2_date) as vl_2_date, - - case - when vl_1>=1000 and timestampdiff(month,vl_1_date,endDate) between 3 and 12 then @qualifies_for_follow_up_vl := 1 - when vl_2>=1000 and timestampdiff(month,vl_2_date,endDate) between 3 and 12 then @qualifies_for_follow_up_vl := 1 - else @qualifies_for_follow_up_vl := 0 - end as qualifies_for_follow_up_vl, - - case - when @qualifies_for_follow_up_vl AND timestampdiff(month,vl_2_date,vl_1_date) < 12 - then @got_follow_up_vl := 1 - else @got_follow_up_vl := 0 - end as got_follow_up_vl, - - case - when @got_follow_up_vl and vl_1_date between date_format(endDate,"%Y-%m-01") and endDate then @got_follow_up_vl_this_month := 1 - else @got_follow_up_vl_this_month := 0 - end as got_follow_up_vl_this_month, - - case - when @got_follow_up_vl then timestampdiff(day,vl_2_date, vl_1_date) - else null - end as num_days_to_follow_vl, - - - case - when @got_follow_up_vl AND vl_1 < 1000 then @follow_up_vl_suppressed := 1 - else @follow_up_vl_suppressed := 0 - end as follow_up_vl_suppressed, - - case - when @got_follow_up_vl_this_month AND vl_1 < 1000 then @follow_up_vl_suppressed_this_month := 1 - else @follow_up_vl_suppressed_this_month := 0 - end as follow_up_vl_suppressed_this_month, - - - case - when @got_follow_up_vl AND vl_1 >= 1000 then @follow_up_vl_unsuppressed := 1 - else @follow_up_vl_unsuppressed := 0 - end as follow_up_vl_unsuppressed, - - case - when @got_follow_up_vl_this_month AND vl_1 >= 1000 then @follow_up_vl_unsuppressed_this_month := 1 - else @follow_up_vl_unsuppressed_this_month := 0 - end as follow_up_vl_unsuppressed_this_month, - - - case - when - vl_1 > 1000 - AND vl_1_date > arv_start_date - AND timestampdiff(month,vl_1_date,endDate) > 3 - AND timestampdiff(year,vl_1_date,endDate) <= 1 - then @needs_follow_up_vl :=1 - else @needs_follow_up_vl := 0 - end as needs_follow_up_vl, - - - - case - when - vl_1 > 1000 - AND vl_1_date > arv_start_date - AND timestampdiff(month,vl_1_date,endDate) between 3 and 11 - THEN 1 - when timestampdiff(month,arv_start_date,endDate) between 6 and 11 AND vl_1 is null THEN 1 - when - timestampdiff(month,arv_start_date,endDate) between 12 and 18 - AND vl_2 is null then 1 - when - timestampdiff(month,arv_start_date,endDate) >= 12 - AND ifnull(timestampdiff(month,vl_1_date,endDate) >= 12,1) - then 1 - end as due_for_vl_this_month, - - case - when vl_1 > 1000 - AND vl_1_date > arv_start_date - AND timestampdiff(month,vl_1_date,endDate) between 3 and 11 - THEN "Previous VL Unsuppressed" - when timestampdiff(month,arv_start_date,endDate) between 6 and 11 AND vl_1 is null then "On ART for 6 months" - when - timestampdiff(month,arv_start_date,endDate) between 12 and 18 - AND vl_2 is null - THEN "On ART for 12 months" - when - timestampdiff(month,arv_start_date,endDate) >= 12 - AND ifnull(timestampdiff(month,vl_1_date,endDate) >= 12,1) - then "Annual VL" - - end as reason_for_needing_vl_this_month, - - - - case - when vl_1 > 1000 - AND vl_1_date > arv_start_date - AND timestampdiff(month,vl_1_date,endDate) between 3 and 11 - then timestampdiff(month,vl_1_date,endDate) - 3 - - when timestampdiff(month,arv_start_date,endDate) between 6 and 11 AND vl_1 is null - then timestampdiff(month,arv_start_date,endDate) - 6 - - when - timestampdiff(month,arv_start_date,endDate) between 12 and 18 - AND vl_2 is null - then timestampdiff(month,arv_start_date,endDate) - 12 - - when - timestampdiff(month,arv_start_date,endDate) >= 12 - AND ifnull(timestampdiff(month,vl_1_date,endDate) >= 12,1) - then timestampdiff(month,ifnull(vl_1_date,arv_start_date),endDate) - 12 - end as number_of_months_has_needed_vl, - - @tb_tx_start_date := date(tb_tx_start_date) as tb_tx_start_date,#need to know time period, i.e. screened this month or screened in past X months - - case - when tb_tx_start_date between date_format(endDate,"%Y-%m-01") and endDate then @started_tb_tx_this_month := 1 - else @started_tb_tx_this_month := 0 - end as started_tb_tx_this_month, - - @on_tb_tx_this_month := on_tb_tx as on_tb_tx_this_month, - - case - when on_tb_tx =1 and @started_art_this_month then 1 - else 0 - end as on_tb_tx_and_started_art_this_month, - - tb_screen, - tb_screening_datetime, - tb_screening_result, - - case - when tb_screening_datetime between date_format(endDate, "%Y-%m-01") and endDate - AND @status = "active" then 1 - else 0 - end as tb_screened_this_visit_this_month, - - case - when tb_screening_datetime >= date(encounter_datetime) - AND @status = "active" then 1 - when tb_screening_datetime between DATE_SUB(endDate, INTERVAL 6 MONTH) and endDate - AND @status = "active" AND @started_tb_tx_this_month != 1 then 1 - else 0 - end as tb_screened_active_this_month, - - case - when tb_screening_result = 6971 - AND tb_screening_datetime between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as presumed_tb_positive_this_month, - - date(pcp_prophylaxis_start_date) as pcp_prophylaxis_start_date, - - case - when pcp_prophylaxis_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_pcp_prophylaxis_this_month, - - case - when @status="active" AND pcp_prophylaxis_start_date is not null then 1 - else 0 - end as on_pcp_prophylaxis_this_month, - - - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - case - when ipt_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_ipt_this_month, - - case - when ipt_start_date is not null and ipt_stop_date is null then 1 - else 0 - end as on_ipt_this_month, - - - case - when timestampdiff(month,ipt_completion_date,endDate) <= 12 then 1 - else 0 - end as completed_ipt_past_12_months, - - case - when is_pregnant and edd > endDate then 1 - else 0 - end as pregnant_this_month, - - case - when is_pregnant and edd > endDate and @started_art_this_month then 1 - else 0 - end as is_pregnant_and_started_art_this_month, - - 0 as delivered_this_month, - - case - when condoms_provided_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as condoms_provided_this_month, - - case - when condoms_provided_date >= date(encounter_datetime) - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 28 day)),endDate) <= 28 then 1 - else 0 - end as condoms_provided_since_active, - - - case - when modern_contraceptive_method_start_date between date_format(endDate,"%Y-%m-01") and endDate then 1 - else 0 - end as started_modern_contraception_this_month, - - case - when modern_contraceptive_method_start_date <= date(encounter_datetime) - AND timestampdiff(day,if(rtc_date,rtc_date,date_add(encounter_datetime, interval 28 day)),endDate) <= 28 - then 1 - else 0 - end as modern_contraception_since_active, - - case - when modern_contraceptive_method_start_date <= date(encounter_datetime) - AND @status="active" - AND gender="F" - AND @age>=15 - then 1 - else 0 - end as on_modern_contraception_this_month, - - - contraceptive_method, - discordant_status - - - from etl.dates t1 - join etl.flat_hiv_summary_v15b t2 - join amrs.person t3 using (person_id) - join etl.hiv_monthly_report_dataset_build_queue__0 t5 using (person_id) - - - where - #t2.encounter_datetime <= t1.endDate - t2.encounter_datetime < date_add(endDate, interval 1 day) - and (t2.next_clinical_datetime_hiv is null or t2.next_clinical_datetime_hiv >= date_add(t1.endDate, interval 1 day) ) - and t2.is_clinical_encounter=1 - and t1.endDate between start_date and date_add(now(),interval 2 year) - order by person_id, endDate - ); - - - - set @prev_id = null; - set @cur_id = null; - set @cur_status = null; - set @prev_status = null; - set @prev_location_id = null; - set @cur_location_id = null; - - drop temporary table if exists hiv_monthly_report_dataset_1; - create temporary table hiv_monthly_report_dataset_1 - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_location_id := @cur_location_id - else @prev_location_id := null - end as next_location_id, - - @cur_location_id := location_id as cur_location_id, - - case - when @prev_id=@cur_id then @prev_status := @cur_status - else @prev_status := null - end as next_status, - @cur_status := status as cur_status - - from hiv_monthly_report_dataset_0 - order by person_id, endDate desc - ); - - alter table hiv_monthly_report_dataset_1 drop prev_id, drop cur_id, drop cur_status, drop cur_location_id; - - set @prev_id = null; - set @cur_id = null; - set @cur_status = null; - set @prev_status = null; - set @cur_arv_meds = null; - set @prev_arv_meds = null; - set @cur_location_id = null; - set @prev_location_id = null; - drop temporary table if exists hiv_monthly_report_dataset_2; - create temporary table hiv_monthly_report_dataset_2 - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_location_id := @cur_location_id - else @prev_location_id := null - end as prev_location_id, - - @cur_location_id := location_id as cur_location_id, - - case - when @prev_id=@cur_id then @prev_status := @cur_status - else @prev_status := null - end as prev_status, - @cur_status := status as cur_status, - - case - when @prev_id = @cur_id then @prev_month_arvs := @cur_arv_meds - else @prev_month_arvs := null - end as prev_month_arvs, - - @cur_arv_meds := cur_arv_meds as cur_arv_meds_2, - - case - when @prev_id = @cur_id then @prev_month_arvs_names := @cur_arv_meds_names - else @prev_month_arvs_names := null - end as prev_month_arvs_names, - - @cur_arv_meds_names := cur_arv_meds_names as cur_arv_meds_names_2, - - - days_since_starting_arvs - days_since_last_vl as days_between_arv_start_and_last_vl, - - case - when @prev_id != @cur_id then 0 - when cur_arv_meds != @prev_month_arvs then 1 - when (@prev_month_arvs is not null and cur_arv_meds is null) then 1 - else 0 - end as had_med_change_this_month, - - case - when @prev_id != @cur_id then 0 - when @follow_up_vl_unsuppressed AND cur_arv_meds != @prev_month_arvs then 1 - when @follow_up_vl_unsuppressed AND (@prev_month_arvs is not null and cur_arv_meds is null) then 1 - else 0 - end as had_med_change_this_month_after_2_unsuppressed_vls, - - - case - when @prev_id != @cur_id then 0 - when @follow_up_vl_unsuppressed AND cur_arv_meds = @prev_month_arvs then 1 - else 0 - end as same_meds_this_month_after_2_unsuppressed_vls - - - - from hiv_monthly_report_dataset_1 - order by person_id, endDate - ); - - select now(); - select count(*) as num_rows_to_be_inserted from hiv_monthly_report_dataset_2; - - #add data to table -# replace into hiv_monthly_report_dataset - replace into hiv_monthly_report_dataset_v1_2 - (select - null, #date_created will be automatically set or updated - elastic_id, - endDate, - encounter_id, - person_id, - person_uuid, - birthdate, - age, - gender, - encounter_date, - visit_this_month, - rtc_date, - days_since_rtc_date, - on_schedule, - scheduled_this_month, - unscheduled_this_month, - f_18_and_over_this_month, - prev_location_id, - location_id, - t2.uuid as location_uuid, - next_location_id, - t2.state_province as clinic_county, - t2.name as clinic, - t2.latitude as clinic_latitude, - t2.longitude as clinic_longitude, - encounter_type, - death_date, - enrollment_date, - enrolled_this_month, - transfer_in_this_month, - transfer_in_location_id, - transfer_in_date, - transfer_out_this_month, - transfer_out_location_id, - transfer_out_date, - prev_status, - status, - next_status, - active_in_care_this_month, - is_pre_art_this_month, - arv_first_regimen_location_id, - arv_first_regimen, - arv_first_regimen_names, - arv_first_regimen_start_date, - art_cohort_year, - art_cohort_month, - art_cohort_num, - art_cohort_total_months, - days_since_starting_arvs, - started_art_this_month, - art_revisit_this_month, - arv_start_date, - prev_month_arvs, - prev_month_arvs_names, - cur_arv_meds, - cur_arv_meds_names, - cur_arv_meds_strict, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - on_art_this_month, - on_original_first_line_this_month, - on_alt_first_line_this_month, - on_second_line_or_higher_this_month, - eligible_for_vl, - days_since_last_vl, - net_12_month_cohort_this_month, - active_on_art_12_month_cohort_this_month, - has_vl_12_month_cohort, - vl_suppressed_12_month_cohort, - has_vl_this_month, - is_suppressed_this_month, - vl_1, - vl_1_date, - vl_in_past_year, - vl_2, - vl_2_date, - qualifies_for_follow_up_vl, - got_follow_up_vl, - got_follow_up_vl_this_month, - num_days_to_follow_vl, - follow_up_vl_suppressed, - follow_up_vl_suppressed_this_month, - follow_up_vl_unsuppressed, - follow_up_vl_unsuppressed_this_month, - due_for_vl_this_month, - reason_for_needing_vl_this_month, - number_of_months_has_needed_vl, - needs_follow_up_vl, - had_med_change_this_month, - had_med_change_this_month_after_2_unsuppressed_vls, - same_meds_this_month_after_2_unsuppressed_vls, - tb_screen, - tb_screening_datetime, - tb_screening_result, - tb_screened_this_visit_this_month, - tb_screened_active_this_month, - presumed_tb_positive_this_month, - tb_tx_start_date, - started_tb_tx_this_month, - on_tb_tx_this_month, - on_tb_tx_and_started_art_this_month, - pcp_prophylaxis_start_date, - started_pcp_prophylaxis_this_month, - on_pcp_prophylaxis_this_month, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - started_ipt_this_month, - on_ipt_this_month, - completed_ipt_past_12_months, - pregnant_this_month, - is_pregnant_and_started_art_this_month, - delivered_this_month, - condoms_provided_this_month, - condoms_provided_since_active, - started_modern_contraception_this_month, - modern_contraception_since_active, - on_modern_contraception_this_month, - contraceptive_method, - discordant_status - from hiv_monthly_report_dataset_2 t1 - join amrs.location t2 using (location_id) - ); - - - - #delete from @queue_table where person_id in (select person_id from hiv_monthly_report_dataset_build_queue__0); - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join hiv_monthly_report_dataset_build_queue__0 t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count := (select count(*) from @queue_table); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count as num_remaining; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - #select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - -# select count(*) into @num_in_hmrd from hiv_monthly_report_dataset_v1_2; - - select @num_in_hmrd as num_in_hmrd, - @person_ids_count as num_remaining, - @cycle_length as 'Cycle time (s)', - ceil(@person_ids_count / cycle_size) as remaining_cycles, - @remaining_time as 'Est time remaining (min)'; - - - end while; - - if(query_type = "build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - set @end = now(); - # not sure why we need last date_created, I've replaced this with @start - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_11.sql b/etl-scripts/stored-procedures/generate_hiv_summary_11.sql deleted file mode 100644 index ee52ab4..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_11.sql +++ /dev/null @@ -1,1233 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_11`() -BEGIN - - select @start := now(); - select @start := now(); - select @table_version := "flat_hiv_summary_11_v2.11"; - - set session sort_buffer_size=512000000; - - select @sep := " ## "; - select @lab_encounter_type := 99999; - select @death_encounter_type := 31; - select @last_date_created := (select max(max_date_created) from etl.flat_obs); - - - - create table if not exists flat_hiv_summary_11 ( - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - is_clinical_encounter int, - location_id int, - location_uuid varchar(100), - visit_num int, - enrollment_date datetime, - hiv_start_date datetime, - death_date datetime, - scheduled_visit int, - transfer_out int, - transfer_in int, - - patient_care_status int, - out_of_care int, - prev_rtc_date datetime, - rtc_date datetime, - - arv_start_location int, - arv_first_regimen_start_date datetime, - arv_start_date datetime, - prev_arv_start_date datetime, - prev_arv_end_date datetime, - - arv_first_regimen varchar(500), - prev_arv_meds varchar(500), - cur_arv_meds varchar(500), - prev_arv_line int, - cur_arv_line int, - - - prev_arv_adherence varchar(200), - cur_arv_adherence varchar(200), - hiv_status_disclosed int, - - first_evidence_patient_pregnant datetime, - edd datetime, - screened_for_tb boolean, - tb_screening_result boolean, - tb_prophylaxis_start_date datetime, - tb_prophylaxis_end_date datetime, - tb_tx_start_date datetime, - tb_tx_end_date datetime, - pcp_prophylaxis_start_date datetime, - cd4_resulted double, - cd4_resulted_date datetime, - cd4_1 double, - cd4_1_date datetime, - cd4_2 double, - cd4_2_date datetime, - cd4_percent_1 double, - cd4_percent_1_date datetime, - cd4_percent_2 double, - cd4_percent_2_date datetime, - vl_resulted int, - vl_resulted_date datetime, - vl_1 int, - vl_1_date datetime, - vl_2 int, - vl_2_date datetime, - vl_order_date datetime, - cd4_order_date datetime, - - - hiv_dna_pcr_order_date datetime, - hiv_dna_pcr_resulted int, - hiv_dna_pcr_resulted_date datetime, - hiv_dna_pcr_1 int, - hiv_dna_pcr_1_date datetime, - hiv_dna_pcr_2 int, - hiv_dna_pcr_2_date datetime, - - - hiv_rapid_test_resulted int, - hiv_rapid_test_resulted_date datetime, - - condoms_provided int, - using_modern_contraceptive_method int, - - cur_who_stage int, - prev_encounter_datetime_hiv datetime, - next_encounter_datetime_hiv datetime, - prev_encounter_type_hiv mediumint, - next_encounter_type_hiv mediumint, - prev_clinical_datetime_hiv datetime, - next_clinical_datetime_hiv datetime, - - prev_clinical_rtc_date_hiv datetime, - next_clinical_rtc_date_hiv datetime, - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index location_rtc (location_uuid,rtc_date), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index encounter_type (encounter_type) - ); - - select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); - - - select @last_update := - if(@last_update is null, - (select max(date_created) from amrs.encounter e join etl.flat_hiv_summary_11 using (encounter_id)), - @last_update); - - - select @last_update := if(@last_update,@last_update,'1900-01-01'); - - - - drop table if exists new_data_person_ids; - create temporary table new_data_person_ids(person_id int, primary key (person_id)); - replace into new_data_person_ids - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - limit 10 - ); - - replace into new_data_person_ids - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - - limit 10 - ); - - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - - limit 10 - ); - - replace into new_data_person_ids - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - limit 10 - ); - - replace into new_data_person_ids - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - limit 10 - ); - - select @person_ids_count := (select count(*) from new_data_person_ids); - - delete t1 from flat_hiv_summary_11 t1 join new_data_person_ids t2 using (person_id); - - while @person_ids_count > 0 do - - - drop table if exists new_data_person_ids_0; - - create temporary table new_data_person_ids_0 (select * from new_data_person_ids limit 10000); - - delete from new_data_person_ids where person_id in (select person_id from new_data_person_ids_0); - - select @person_ids_count := (select count(*) from new_data_person_ids); - - drop table if exists flat_hiv_summary_11_0a; - create temporary table flat_hiv_summary_11_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - 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,115,120) then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (110,@lab_encounter_type,111,5,6,7,8,9,13,16,21,22,23,43) then 1 - else 10 - end as encounter_type_sort_index, - t2.orders - from etl.flat_obs t1 - join new_data_person_ids_0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_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,115,116,117,120) - ); - - insert into flat_hiv_summary_11_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join new_data_person_ids_0 t0 using (person_id) - ); - - drop table if exists flat_hiv_summary_11_0; - create temporary table flat_hiv_summary_11_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_11_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - select @prev_id := null; - select @cur_id := null; - select @enrollment_date := null; - select @hiv_start_date := null; - select @cur_location := null; - select @cur_rtc_date := null; - select @prev_rtc_date := null; - select @hiv_start_date := null; - select @prev_arv_start_date := null; - select @arv_start_date := null; - select @prev_arv_end_date := null; - select @arv_start_location := null; - select @art_first_regimen_start_date := null; - select @arv_first_regimen := null; - select @prev_arv_line := null; - select @cur_arv_line := null; - select @prev_arv_adherence := null; - select @cur_arv_adherence := null; - select @hiv_status_disclosed := null; - select @first_evidence_pt_pregnant := null; - select @edd := null; - select @prev_arv_meds := null; - select @cur_arv_meds := null; - select @tb_prophylaxis_start_date := null; - select @tb_prophylaxis_end_date := null; - select @tb_treatment_start_date := null; - select @tb_treatment_end_date := null; - select @pcp_prophylaxis_start_date := null; - select @screened_for_tb := null; - select @tb_screening_result := null; - select @death_date := null; - select @vl_1:=null; - select @vl_2:=null; - select @vl_1_date:=null; - select @vl_2_date:=null; - select @vl_resulted:=null; - select @vl_resulted_date:=null; - - select @cd4_resulted:=null; - select @cd4_resulted_date:=null; - select @cd4_1:=null; - select @cd4_1_date:=null; - select @cd4_2:=null; - select @cd4_2_date:=null; - select @cd4_percent_1:=null; - select @cd4_percent_1_date:=null; - select @cd4_percent_2:=null; - select @cd4_percent_2_date:=null; - select @vl_order_date := null; - select @cd4_order_date := null; - - select @hiv_dna_pcr_order_date := null; - select @hiv_dna_pcr_1:=null; - select @hiv_dna_pcr_2:=null; - select @hiv_dna_pcr_1_date:=null; - select @hiv_dna_pcr_2_date:=null; - - select @hiv_rapid_test_resulted:=null; - select @hiv_rapid_test_resulted_date:= null; - - select @patient_care_status:=null; - - select @condoms_provided := null; - select @using_modern_contraceptive_method := null; - - - select @cur_who_stage := null; - - - - - - drop temporary table if exists flat_hiv_summary_11_1; - create temporary table flat_hiv_summary_11_1 (index encounter_id (encounter_id)) - (select - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.is_clinical_encounter, - - case - when @prev_id != @cur_id and t1.encounter_type in (21,@lab_encounter_type) then @enrollment_date := null - when @prev_id != @cur_id then @enrollment_date := encounter_datetime - when t1.encounter_type not in (21,@lab_encounter_type) and @enrollment_date is null then @enrollment_date := encounter_datetime - else @enrollment_date - end as enrollment_date, - - - - - - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as 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 - end as visit_num, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - - case - when obs regexp "!!7015=" then @transfer_in := replace(replace((substring_index(substring(obs,locate("!!7015=",obs)),@sep,1)),"!!7015=",""),"!!","") - else @transfer_in := null - end as transfer_in, - - - - - - case - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=1594!!" then 1 - when obs regexp "!!9082=(1287|9068|9504)!!" then 1 - else null - end as transfer_out, - - - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - - - - - case - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @death_encounter_type then @patient_care_status := 159 - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - - - - - - - case - when obs regexp "!!1946=1065!!" then @hiv_start_date := null - when t1.encounter_type=@lab_encounter_type and obs regexp "!!(1040|1030)=664!!" then @hiv_start_date:=null - when @prev_id != @cur_id or @hiv_start_date is null then - case - when obs regexp "!!(1040|1030)=664!!" then @hiv_start_date := date(encounter_datetime) - when obs regexp "!!(1088|1255)=" then @hiv_start_date := date(t1.encounter_datetime) - else @hiv_start_date := null - end - else @hiv_start_date - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_location := location_id - when @prev_id = @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location := location_id - when @prev_id != @cur_id then @arv_start_location := null - else @arv_start_location - end as arv_start_location, - - case - when @prev_id=@cur_id then @prev_arv_meds := @cur_arv_meds - else @prev_arv_meds := null - end as prev_arv_meds, - - - - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!1250=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1250=", "") ) ) / LENGTH("!!1250=") ))),"!!1250=",""),"!!","") - when obs regexp "!!1088=" then @cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!1088=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1088=", "") ) ) / LENGTH("!!1088=") ))),"!!1088=",""),"!!","") - when obs regexp "!!2154=" then @cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!2154=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!2154=", "") ) ) / LENGTH("!!2154=") ))),"!!2154=",""),"!!","") - when @prev_id=@cur_id then @cur_arv_meds - else @cur_arv_meds:= null - end as cur_arv_meds, - - - case - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id = @cur_id then @arv_first_regimen - else @arv_first_regimen := null - end as arv_first_regimen, - - case - when @arv_first_regimen_start_date is null and (obs regexp "!!1255=(1256|1259|1850)" or obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!") then @arv_first_regimen_start_date := date(t1.encounter_datetime) - when @prev_id != @cur_id then @arv_first_regimen_start_date := null - else @arv_first_regimen_start_date - end as arv_first_regimen_start_date, - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := null - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=6156!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=6156!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=6156!!" then @cur_arv_line := 3 - when @prev_id = @cur_id then @cur_arv_line - else @cur_arv_line := null - end as cur_arv_line, - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := null - end as prev_arv_start_date, - - - - - - - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - when @cur_arv_meds != @prev_arv_meds and @cur_arv_line != @prev_arv_line then @arv_start_date := date(t1.encounter_datetime) - when @prev_id != @cur_id then @arv_start_date := null - else @arv_start_date - end as arv_start_date, - - case - when @prev_arv_start_date != @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end as prev_arv_end_date, - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := null - end as prev_arv_adherence, - - - - - - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id = @cur_id then @cur_arv_adherence - else @cur_arv_adherence := null - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when obs regexp "!!6596=" then @hiv_status_disclosed := null - when @prev_id != @cur_id then @hiv_status_disclosed := null - else @hiv_status_disclosed - end as hiv_status_disclosed, - - - - - - - case - when @prev_id != @cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" then @first_evidence_pt_pregnant := encounter_datetime - else @first_evidence_pt_pregnant := null - end - when @first_evidence_pt_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") then @first_evidence_pt_pregnant := encounter_datetime - when @first_evidence_pt_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@first_evidence_pt_pregnant,encounter_datetime) > 40 or timestampdiff(week,@edd,encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @first_evidence_pt_pregnant := null - else @first_evidence_pt_pregnant - end as first_evidence_patient_pregnant, - - - - - - - - case - when @prev_id != @cur_id then - case - when @first_evidence_patient_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - when @first_evidence_pt_pregnant then @edd := date_add(@first_evidence_pt_pregnant,interval 6 month) - else @edd := null - end - when @edd is null or @edd = @first_evidence_pt_pregnant then - case - when @first_evidence_pt_pregnant then @edd := date_add(@first_evidence_pt_pregnant,interval 6 month) - when @first_evidence_patient_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - when @first_evidence_pt_pregnant then @edd := date_add(@first_evidence_pt_pregnant,interval 6 month) - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - - - - - - - - case - when obs regexp "!!6174=" then @screened_for_tb := true - when obs regexp "!!2022=1065!!" then @screened_for_tb := true - when obs regexp "!!307=" then @screened_for_tb := true - when obs regexp "!!12=" then @screened_for_tb := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @screened_for_tb := true - when orders regexp "(12|307|8064|2311|2323)" then @screened_for_tb := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @screened_for_tb := true - when obs regexp "!!5958=1077!!" then @screened_for_tb := true - when obs regexp "!!2020=1065!!" then @screened_for_tb := true - when obs regexp "!!2021=1065!!" then @screened_for_tb := true - when obs regexp "!!2028=" then @screened_for_tb := true - when obs regexp "!!1268=(1256|1850)!!" then @screened_for_tb := true - when obs regexp "!!5959=(1073|1074)!!" then @screened_for_tb := true - when obs regexp "!!5971=(1073|1074)!!" then @screened_for_tb := true - when obs regexp "!!1492=107!!" then @screened_for_tb := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @screened_for_tb := true - end as screened_for_tb, - - - - - - - - - case - - when obs regexp "!!2022=1065!!" then @tb_screening_result := true - when obs regexp "!!307=" then @tb_screening_result := true - when obs regexp "!!12=" then @tb_screening_result := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screening_result := true - when orders regexp "(12|307|8064|2311|2323)" then @tb_screening_result := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screening_result := true - when obs regexp "!!5958=1077!!" then @tb_screening_result := true - when obs regexp "!!2020=1065!!" then @tb_screening_result := true - when obs regexp "!!2021=1065!!" then @tb_screening_result := true - when obs regexp "!!2028=" then @tb_screening_result := true - when obs regexp "!!1268=(1256|1850)!!" then @tb_screening_result := true - when obs regexp "!!5959=(1073|1074)!!" then @tb_screening_result := true - when obs regexp "!!5971=(1073|1074)!!" then @tb_screening_result := true - when obs regexp "!!1492=107!!" then @tb_screening_result := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screening_result := true - when obs not regexp "!!6174=1107" then @tb_screening_result := true - else @tb_screening_result := false - end as tb_screening_result, - - case - when obs regexp "!!1265=(1256|1257|1850)!!" then @on_tb_prophylaxis := 1 - when obs regexp "!!1110=656!!" then @on_tb_prophylaxis := 1 - when @prev_id = @cur_id then @on_tb_prophylaxis - else null - end as on_tb_prophylaxis, - - - - - - case - when @cur_id != @prev_id then - case - when obs regexp "!!1265=(1256|1850)!!" then @tb_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" then @tb_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1110=656!!" then @tb_prophylaxis_start_date := encounter_datetime - else @tb_prophylaxis_start_date := null - end - when @cur_id = @prev_id then - case - when obs regexp "!!1265=(1256|1850)!!" then @tb_prophylaxis_start_date := encounter_datetime - when @tb_prophylaxis_start_date is not null then @tb_prophylaxis_start_date - when obs regexp "!!1265=(1257|981|1406|1849)!!" then @tb_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1110=656!!" then @tb_prophylaxis_start_date := encounter_datetime - end - end as tb_prophylaxis_start_date, - - - - - - case - when @cur_id != @prev_id then - case - when obs regexp "!!1265=1260!!" then @tb_prophylaxis_end_date := encounter_datetime - else @tb_prophylaxis_end_date := null - end - when @cur_id = @prev_id then - case - when obs regexp "!!1265=1260!!" then @tb_prophylaxis_end_date := encounter_datetime - when @tb_prophylaxis_end_date is not null then @tb_prophylaxis_end_date - when @tb_prophylaxis_start_date is not null and obs regexp "!!1110=1107!!" and obs regexp "!!1265=" and obs regexp "!!1265=(1107|1260)!!" then @tb_prophylaxis_end_date := encounter_datetime - end - end as tb_prophylaxis_end_date, - - - - - - case - when @cur_id != @prev_id then - case - when obs regexp "!!1113=" then @tb_treatment_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @tb_treatment_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" then @tb_treatment_start_date := encounter_datetime - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @tb_treatment_start_date := encounter_datetime - else @tb_treatment_start_date := null - end - when @cur_id = @prev_id then - case - when obs regexp "!!1113=" then @tb_treatment_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when @tb_treatment_start_date is not null then @tb_treatment_start_date - when obs regexp "!!1268=1256!!" then @tb_treatment_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" then @tb_treatment_start_date := encounter_datetime - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @tb_treatment_start_date := encounter_datetime - end - end as tb_tx_start_date, - - - - - case - when @cur_id != @prev_id then - case - when obs regexp "!!2041=" then @tb_treatment_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_treatment_end_date := encounter_datetime - else @tb_treatment_end_date := null - end - when @cur_id = @prev_id then - case - when obs regexp "!!2041=" then @tb_treatment_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_treatment_end_date := encounter_datetime - when @tb_treatment_end_date is not null then @tb_treatment_end_date - when @tb_treatment_start_date is not null and obs regexp "!!6176=1066!!" and !(obs regexp "!!1268=(1256|1257|1259|1849|981)!!") then @tb_treatment_end_date := encounter_datetime - end - end as tb_tx_end_date, - - - - - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1261=1257!!" then - case - when @prev_id!=@cur_id or @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - else @pcp_prophylaxis_start_date - end - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1193=916!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when @prev_id=@cur_id then @pcp_prophylaxis_start_date - else @pcp_prophylaxis_start_date := null - end as pcp_prophylaxis_start_date, - - - - - - - - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when @prev_id != @cur_id or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := encounter_datetime - else @death_date := null - end - else @death_date - end as death_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - else @cd4_2:=null - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - else @cd4_2_date:=null - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=null - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=null - end as cd4_1_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - else @cd4_percent_2:=null - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - else @cd4_percent_2_date:=null - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=null - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=null - end as cd4_percent_1_date, - - - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) <>date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - else @vl_2:=null - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - else @vl_2_date:=null - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=null - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date:=null - end as vl_1_date, - - - - - - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(encounter_datetime) - when orders regexp "856" then @vl_order_date := date(encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - else @vl_order_date := null - end as vl_order_date, - - - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := null - end as cd4_order_date, - - - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := null - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - else @hiv_dna_pcr_2:=null - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - else @hiv_dna_pcr_2_date:=null - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=null - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=null - end as hiv_dna_pcr_1_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - - case - when obs regexp "!!8302=8305!!" then @condoms_provided := 1 - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided := 1 - when obs regexp "!!6579=" then @condoms_provided := 1 - else null - end as condoms_provided, - - case - when obs regexp "!!374=(5275|6220|780|5279)!!" then @using_modern_conctaceptive_method := 1 - else null - end as using_modern_contraceptive_method, - - - - - - - - - - - - - case - when obs regexp "!!5356=(1204)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1223)!!" then @cur_who_stage := 4 - when @prev_id = @cur_id then @cur_who_stage - else @cur_who_stage := null - end as cur_who_stage - - from flat_hiv_summary_11_0 t1 - join amrs.person p using (person_id) - ); - - select @prev_id := null; - select @cur_id := null; - select @prev_encounter_datetime := null; - select @cur_encounter_datetime := null; - - select @prev_clinical_datetime := null; - select @cur_clinical_datetime := null; - - select @next_encounter_type := null; - select @cur_encounter_type := null; - - - alter table flat_hiv_summary_11_1 drop prev_id, drop cur_id; - - drop table if exists flat_hiv_summary_11_2; - create temporary table flat_hiv_summary_11_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date - - from flat_hiv_summary_11_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_11_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - select @prev_id := null; - select @cur_id := null; - select @prev_encounter_type := null; - select @cur_encounter_type := null; - select @prev_encounter_datetime := null; - select @cur_encounter_datetime := null; - select @prev_clinical_datetime := null; - select @cur_clinical_datetime := null; - - drop temporary table if exists flat_hiv_summary_11_3; - create temporary table flat_hiv_summary_11_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_hiv, @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_hiv, @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_hiv_summary_11_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - replace into flat_hiv_summary_11 - (select - person_id, - t1.uuid, - t1.visit_id, - encounter_id, - encounter_datetime, - encounter_type, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - visit_num, - enrollment_date, - hiv_start_date, - death_date, - scheduled_visit, - transfer_out, - transfer_in, - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date, - arv_start_location, - arv_first_regimen_start_date, - arv_start_date, - prev_arv_start_date, - prev_arv_end_date, - arv_first_regimen, - prev_arv_meds, - cur_arv_meds, - prev_arv_line, - cur_arv_line, - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - first_evidence_patient_pregnant, - edd, - screened_for_tb, - tb_screening_result, - tb_prophylaxis_start_date, - tb_prophylaxis_end_date, - tb_tx_start_date, - tb_tx_end_date, - pcp_prophylaxis_start_date, - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - vl_order_date, - cd4_order_date, - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - condoms_provided, - using_modern_contraceptive_method, - cur_who_stage, - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv - from flat_hiv_summary_11_3 t1 - join amrs.location t2 using (location_id)); - - end while; - - select @end := now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_beta.sql b/etl-scripts/stored-procedures/generate_hiv_summary_beta.sql deleted file mode 100644 index ff51b5f..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_beta.sql +++ /dev/null @@ -1,1194 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_beta`() -BEGIN - - select @start := now(); - select @start := now(); - select @table_version := "flat_hiv_summary_v2.10"; - - set session sort_buffer_size=512000000; - - select @sep := " ## "; - select @lab_encounter_type := 99999; - select @death_encounter_type := 31; - select @last_date_created := (select max(max_date_created) from etl.flat_obs); - - - - create table if not exists flat_hiv_summary_2_10 ( - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - is_clinical_encounter int, - location_id int, - location_uuid varchar(100), - visit_num int, - enrollment_date datetime, - hiv_start_date datetime, - death_date datetime, - scheduled_visit int, - transfer_out int, - transfer_in int, - - patient_care_status int, - out_of_care int, - prev_rtc_date datetime, - rtc_date datetime, - - arv_start_location int, - arv_first_regimen_start_date datetime, - arv_start_date datetime, - prev_arv_start_date datetime, - prev_arv_end_date datetime, - - arv_first_regimen varchar(500), - prev_arv_meds varchar(500), - cur_arv_meds varchar(500), - prev_arv_line int, - cur_arv_line int, - - - prev_arv_adherence varchar(200), - cur_arv_adherence varchar(200), - hiv_status_disclosed int, - - first_evidence_patient_pregnant datetime, - edd datetime, - screened_for_tb boolean, - tb_screening_result boolean, - tb_prophylaxis_start_date datetime, - tb_prophylaxis_end_date datetime, - tb_tx_start_date datetime, - pcp_prophylaxis_start_date datetime, - cd4_resulted double, - cd4_resulted_date datetime, - cd4_1 double, - cd4_1_date datetime, - cd4_2 double, - cd4_2_date datetime, - cd4_percent_1 double, - cd4_percent_1_date datetime, - cd4_percent_2 double, - cd4_percent_2_date datetime, - vl_resulted int, - vl_resulted_date datetime, - vl_1 int, - vl_1_date datetime, - vl_2 int, - vl_2_date datetime, - vl_order_date datetime, - cd4_order_date datetime, - - - hiv_dna_pcr_order_date datetime, - hiv_dna_pcr_resulted int, - hiv_dna_pcr_resulted_date datetime, - hiv_dna_pcr_1 int, - hiv_dna_pcr_1_date datetime, - hiv_dna_pcr_2 int, - hiv_dna_pcr_2_date datetime, - - - hiv_rapid_test_resulted int, - hiv_rapid_test_resulted_date datetime, - - condoms_provided int, - using_modern_contraceptive_method int, - - cur_who_stage int, - prev_encounter_datetime_hiv datetime, - next_encounter_datetime_hiv datetime, - prev_encounter_type_hiv mediumint, - next_encounter_type_hiv mediumint, - prev_clinical_datetime_hiv datetime, - next_clinical_datetime_hiv datetime, - - prev_clinical_rtc_date_hiv datetime, - next_clinical_rtc_date_hiv datetime, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index location_rtc (location_uuid,rtc_date), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index encounter_type (encounter_type) - ); - - select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); - - - select @last_update := - if(@last_update is null, - (select max(date_created) from amrs.encounter e join etl.flat_hiv_summary_2_10 using (encounter_id)), - @last_update); - - - select @last_update := if(@last_update,@last_update,'1900-01-01'); - - - - drop table if exists new_data_person_ids; - create temporary table new_data_person_ids(person_id int, primary key (person_id)); - - replace into new_data_person_ids - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - - - ); - - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - - - ); - - replace into new_data_person_ids - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into new_data_person_ids - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - select @person_ids_count := (select count(*) from new_data_person_ids); - - delete t1 from flat_hiv_summary_2_10 t1 join new_data_person_ids t2 using (person_id); - - while @person_ids_count > 0 do - - - drop table if exists new_data_person_ids_0; - - create temporary table new_data_person_ids_0 (select * from new_data_person_ids limit 10000); - - delete from new_data_person_ids where person_id in (select person_id from new_data_person_ids_0); - - select @person_ids_count := (select count(*) from new_data_person_ids); - - drop table if exists flat_hiv_summary_2_10_0a; - create temporary table flat_hiv_summary_2_10_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - 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,115,120) then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (110,@lab_encounter_type,111,5,6,7,8,9,13,16,21,22,23,43) then 1 - else 10 - end as encounter_type_sort_index, - t2.orders - from etl.flat_obs t1 - join new_data_person_ids_0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_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,115,116,117,120) - ); - - insert into flat_hiv_summary_2_10_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join new_data_person_ids_0 t0 using (person_id) - ); - - drop table if exists flat_hiv_summary_2_10_0; - create temporary table flat_hiv_summary_2_10_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_2_10_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - select @prev_id := null; - select @cur_id := null; - select @enrollment_date := null; - select @hiv_start_date := null; - select @cur_location := null; - select @cur_rtc_date := null; - select @prev_rtc_date := null; - select @hiv_start_date := null; - select @prev_arv_start_date := null; - select @arv_start_date := null; - select @prev_arv_end_date := null; - select @arv_start_location := null; - select @art_first_regimen_start_date := null; - select @arv_first_regimen := null; - select @prev_arv_line := null; - select @cur_arv_line := null; - select @prev_arv_adherence := null; - select @cur_arv_adherence := null; - select @hiv_status_disclosed := null; - select @first_evidence_pt_pregnant := null; - select @edd := null; - select @prev_arv_meds := null; - select @cur_arv_meds := null; - select @tb_prophylaxis_start_date := null; - select @tb_prophylaxis_end_date := null; - select @tb_treatment_start_date := null; - select @pcp_prophylaxis_start_date := null; - select @screened_for_tb := null; - select @tb_screening_result := null; - select @death_date := null; - select @vl_1:=null; - select @vl_2:=null; - select @vl_1_date:=null; - select @vl_2_date:=null; - select @vl_resulted:=null; - select @vl_resulted_date:=null; - - select @cd4_resulted:=null; - select @cd4_resulted_date:=null; - select @cd4_1:=null; - select @cd4_1_date:=null; - select @cd4_2:=null; - select @cd4_2_date:=null; - select @cd4_percent_1:=null; - select @cd4_percent_1_date:=null; - select @cd4_percent_2:=null; - select @cd4_percent_2_date:=null; - select @vl_order_date := null; - select @cd4_order_date := null; - - select @hiv_dna_pcr_order_date := null; - select @hiv_dna_pcr_1:=null; - select @hiv_dna_pcr_2:=null; - select @hiv_dna_pcr_1_date:=null; - select @hiv_dna_pcr_2_date:=null; - - select @hiv_rapid_test_resulted:=null; - select @hiv_rapid_test_resulted_date:= null; - - select @patient_care_status:=null; - - select @condoms_provided := null; - select @using_modern_contraceptive_method := null; - - - select @cur_who_stage := null; - - - - - - drop temporary table if exists flat_hiv_summary_2_10_1; - create temporary table flat_hiv_summary_2_10_1 (index encounter_id (encounter_id)) - (select - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.is_clinical_encounter, - - case - when @prev_id != @cur_id and t1.encounter_type in (21,@lab_encounter_type) then @enrollment_date := null - when @prev_id != @cur_id then @enrollment_date := encounter_datetime - when t1.encounter_type not in (21,@lab_encounter_type) and @enrollment_date is null then @enrollment_date := encounter_datetime - else @enrollment_date - end as enrollment_date, - - - - - - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as 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 - end as visit_num, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - - case - when obs regexp "!!7015=" then @transfer_in := replace(replace((substring_index(substring(obs,locate("!!7015=",obs)),@sep,1)),"!!7015=",""),"!!","") - else @transfer_in := null - end as transfer_in, - - - - - - case - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=1594!!" then 1 - when obs regexp "!!9082=(1287|9068|9504)!!" then 1 - else null - end as transfer_out, - - - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - - - - - case - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @death_encounter_type then @patient_care_status := 159 - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - - - - - - - case - when obs regexp "!!1946=1065!!" then @hiv_start_date := null - when t1.encounter_type=@lab_encounter_type and obs regexp "!!(1040|1030)=664!!" then @hiv_start_date:=null - when @prev_id != @cur_id or @hiv_start_date is null then - case - when obs regexp "!!(1040|1030)=664!!" then @hiv_start_date := date(encounter_datetime) - when obs regexp "!!(1088|1255)=" then @hiv_start_date := date(t1.encounter_datetime) - else @hiv_start_date := null - end - else @hiv_start_date - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_location := location_id - when @prev_id = @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location := location_id - when @prev_id != @cur_id then @arv_start_location := null - else @arv_start_location - end as arv_start_location, - - case - when @prev_id=@cur_id then @prev_arv_meds := @cur_arv_meds - else @prev_arv_meds := null - end as prev_arv_meds, - - - - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!1250=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1250=", "") ) ) / LENGTH("!!1250=") ))),"!!1250=",""),"!!","") - when obs regexp "!!1088=" then @cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!1088=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1088=", "") ) ) / LENGTH("!!1088=") ))),"!!1088=",""),"!!","") - when obs regexp "!!2154=" then @cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!2154=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!2154=", "") ) ) / LENGTH("!!2154=") ))),"!!2154=",""),"!!","") - when @prev_id=@cur_id then @cur_arv_meds - else @cur_arv_meds:= null - end as cur_arv_meds, - - - case - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id = @cur_id then @arv_first_regimen - else @arv_first_regimen := null - end as arv_first_regimen, - - case - when @arv_first_regimen_start_date is null and (obs regexp "!!1255=(1256|1259|1850)" or obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!") then @arv_first_regimen_start_date := date(t1.encounter_datetime) - when @prev_id != @cur_id then @arv_first_regimen_start_date := null - else @arv_first_regimen_start_date - end as arv_first_regimen_start_date, - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := null - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=6156!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=6156!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=6156!!" then @cur_arv_line := 3 - when @prev_id = @cur_id then @cur_arv_line - else @cur_arv_line := null - end as cur_arv_line, - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := null - end as prev_arv_start_date, - - - - - - - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - when @cur_arv_meds != @prev_arv_meds and @cur_arv_line != @prev_arv_line then @arv_start_date := date(t1.encounter_datetime) - when @prev_id != @cur_id then @arv_start_date := null - else @arv_start_date - end as arv_start_date, - - case - when @prev_arv_start_date != @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end as prev_arv_end_date, - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := null - end as prev_arv_adherence, - - - - - - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id = @cur_id then @cur_arv_adherence - else @cur_arv_adherence := null - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when obs regexp "!!6596=" then @hiv_status_disclosed := null - when @prev_id != @cur_id then @hiv_status_disclosed := null - else @hiv_status_disclosed - end as hiv_status_disclosed, - - - - - - - case - when @prev_id != @cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" then @first_evidence_pt_pregnant := encounter_datetime - else @first_evidence_pt_pregnant := null - end - when @first_evidence_pt_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") then @first_evidence_pt_pregnant := encounter_datetime - when @first_evidence_pt_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@first_evidence_pt_pregnant,encounter_datetime) > 40 or timestampdiff(week,@edd,encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @first_evidence_pt_pregnant := null - else @first_evidence_pt_pregnant - end as first_evidence_patient_pregnant, - - - - - - - - case - when @prev_id != @cur_id then - case - when @first_evidence_patient_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - when @first_evidence_pt_pregnant then @edd := date_add(@first_evidence_pt_pregnant,interval 6 month) - else @edd := null - end - when @edd is null or @edd = @first_evidence_pt_pregnant then - case - when @first_evidence_pt_pregnant then @edd := date_add(@first_evidence_pt_pregnant,interval 6 month) - when @first_evidence_patient_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - when @first_evidence_pt_pregnant then @edd := date_add(@first_evidence_pt_pregnant,interval 6 month) - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - - - - - - - - case - when obs regexp "!!6174=" then @screened_for_tb := true - when obs regexp "!!2022=1065!!" then @screened_for_tb := true - when obs regexp "!!307=" then @screened_for_tb := true - when obs regexp "!!12=" then @screened_for_tb := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @screened_for_tb := true - when orders regexp "(12|307|8064|2311|2323)" then @screened_for_tb := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @screened_for_tb := true - when obs regexp "!!5958=1077!!" then @screened_for_tb := true - when obs regexp "!!2020=1065!!" then @screened_for_tb := true - when obs regexp "!!2021=1065!!" then @screened_for_tb := true - when obs regexp "!!2028=" then @screened_for_tb := true - when obs regexp "!!1268=(1256|1850)!!" then @screened_for_tb := true - when obs regexp "!!5959=(1073|1074)!!" then @screened_for_tb := true - when obs regexp "!!5971=(1073|1074)!!" then @screened_for_tb := true - when obs regexp "!!1492=107!!" then @screened_for_tb := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @screened_for_tb := true - end as screened_for_tb, - - - - - - - - - case - - when obs regexp "!!2022=1065!!" then @tb_screening_result := true - when obs regexp "!!307=" then @tb_screening_result := true - when obs regexp "!!12=" then @tb_screening_result := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screening_result := true - when orders regexp "(12|307|8064|2311|2323)" then @tb_screening_result := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screening_result := true - when obs regexp "!!5958=1077!!" then @tb_screening_result := true - when obs regexp "!!2020=1065!!" then @tb_screening_result := true - when obs regexp "!!2021=1065!!" then @tb_screening_result := true - when obs regexp "!!2028=" then @tb_screening_result := true - when obs regexp "!!1268=(1256|1850)!!" then @tb_screening_result := true - when obs regexp "!!5959=(1073|1074)!!" then @tb_screening_result := true - when obs regexp "!!5971=(1073|1074)!!" then @tb_screening_result := true - when obs regexp "!!1492=107!!" then @tb_screening_result := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screening_result := true - when obs not regexp "!!6174=1107" then @tb_screening_result := true - else @tb_screening_result := false - end as tb_screening_result, - - case - when obs regexp "!!1265=(1256|1257|1850)!!" then @on_tb_prophylaxis := 1 - when obs regexp "!!1110=656!!" then @on_tb_prophylaxis := 1 - when @prev_id = @cur_id then @on_tb_prophylaxis - else null - end as on_tb_prophylaxis, - - - - - - case - when obs regexp "!!1265=(1107|1260)!!" then @tb_prophylaxis_start_date := null - when obs regexp "!!1265=(1256|1850)!!" then @tb_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1265=1257!!" then - case - when @prev_id!=@cur_id or @tb_prophylaxis_start_date is null then @tb_prophylaxis_start_date := encounter_datetime - else @tb_prophylaxis_start_date - end - when obs regexp "!!1110=656!!" and @tb_prophylaxis_start_date is null then @tb_prophylaxis_start_date := encounter_datetime - when @prev_id=@cur_id then @tb_prophylaxis_start_date - else @tb_prophylaxis_start_date := null - end as tb_prophylaxis_start_date, - - - - - - case - when obs regexp "!!1265=1260!!" then @tb_prophylaxis_end_date := encounter_datetime - when obs regexp "!!1265=(1256|1850|1107)!!" then @tb_prophylaxis_end_date := null - when obs regexp "!!1265=1257!!" then - case - when @prev_id!=@cur_id or @tb_prophylaxis_end_date is not null then @tb_prophylaxis_end_date := null - else @tb_prophylaxis_end_date - end - when obs regexp "!!1110=656!!" and @tb_prophylaxis_end_date is not null then @tb_prophylaxis_end_date := null - when @prev_id=@cur_id then @tb_prophylaxis_end_date - else @tb_prophylaxis_end_date := null - end as tb_prophylaxis_end_date, - - - - - - case - when obs regexp "!!1268=(1107|1260)!!" then @tb_treatment_start_date := null - when obs regexp "!!1268=1256!!" then @tb_treatment_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" then - case - when @prev_id!=@cur_id or @tb_treatment_start_date is null then @tb_treatment_start_date := encounter_datetime - else @tb_treatment_start_date - end - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" and @tb_treatment_start_date is null then @tb_treatment_start_date := encounter_datetime - when @prev_id=@cur_id then @tb_treatment_start_date - else @tb_treatment_start_date := null - end as tb_tx_start_date, - - - - - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1261=1257!!" then - case - when @prev_id!=@cur_id or @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - else @pcp_prophylaxis_start_date - end - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1193=916!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when @prev_id=@cur_id then @pcp_prophylaxis_start_date - else @pcp_prophylaxis_start_date := null - end as pcp_prophylaxis_start_date, - - - - - - - - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when @prev_id != @cur_id or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := encounter_datetime - else @death_date := null - end - else @death_date - end as death_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - else @cd4_2:=null - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - else @cd4_2_date:=null - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=null - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=null - end as cd4_1_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - else @cd4_percent_2:=null - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - else @cd4_percent_2_date:=null - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=null - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=null - end as cd4_percent_1_date, - - - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) <>date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - else @vl_2:=null - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - else @vl_2_date:=null - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=null - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date:=null - end as vl_1_date, - - - - - - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(encounter_datetime) - when orders regexp "856" then @vl_order_date := date(encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - else @vl_order_date := null - end as vl_order_date, - - - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := null - end as cd4_order_date, - - - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := null - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - else @hiv_dna_pcr_2:=null - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - else @hiv_dna_pcr_2_date:=null - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=null - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=null - end as hiv_dna_pcr_1_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - - case - when obs regexp "!!8302=8305!!" then @condoms_provided := 1 - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided := 1 - when obs regexp "!!6579=" then @condoms_provided := 1 - else null - end as condoms_provided, - - case - when obs regexp "!!374=(5275|6220|780|5279)!!" then @using_modern_conctaceptive_method := 1 - else null - end as using_modern_contraceptive_method, - - - - - - - - - - - - - case - when obs regexp "!!5356=(1204)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1223)!!" then @cur_who_stage := 4 - when @prev_id = @cur_id then @cur_who_stage - else @cur_who_stage := null - end as cur_who_stage - - from flat_hiv_summary_2_10_0 t1 - join amrs.person p using (person_id) - ); - - select @prev_id := null; - select @cur_id := null; - select @prev_encounter_datetime := null; - select @cur_encounter_datetime := null; - - select @prev_clinical_datetime := null; - select @cur_clinical_datetime := null; - - select @next_encounter_type := null; - select @cur_encounter_type := null; - - - alter table flat_hiv_summary_2_10_1 drop prev_id, drop cur_id; - - drop table if exists flat_hiv_summary_2_10_2; - create temporary table flat_hiv_summary_2_10_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date - - from flat_hiv_summary_2_10_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_2_10_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - select @prev_id := null; - select @cur_id := null; - select @prev_encounter_type := null; - select @cur_encounter_type := null; - select @prev_encounter_datetime := null; - select @cur_encounter_datetime := null; - select @prev_clinical_datetime := null; - select @cur_clinical_datetime := null; - - drop temporary table if exists flat_hiv_summary_2_10_3; - create temporary table flat_hiv_summary_2_10_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_hiv, @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_hiv, @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_hiv_summary_2_10_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - replace into flat_hiv_summary_2_10 - (select - person_id, - t1.uuid, - t1.visit_id, - encounter_id, - encounter_datetime, - encounter_type, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - visit_num, - enrollment_date, - hiv_start_date, - death_date, - scheduled_visit, - transfer_out, - transfer_in, - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date, - arv_start_location, - arv_first_regimen_start_date, - arv_start_date, - prev_arv_start_date, - prev_arv_end_date, - arv_first_regimen, - prev_arv_meds, - cur_arv_meds, - prev_arv_line, - cur_arv_line, - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - first_evidence_patient_pregnant, - edd, - screened_for_tb, - tb_screening_result, - tb_prophylaxis_start_date, - tb_prophylaxis_end_date, - tb_tx_start_date, - pcp_prophylaxis_start_date, - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - vl_order_date, - cd4_order_date, - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - condoms_provided, - using_modern_contraceptive_method, - cur_who_stage, - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv - - from flat_hiv_summary_2_10_3 t1 - join amrs.location t2 using (location_id)); - - end while; - - select @end := now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_next.sql b/etl-scripts/stored-procedures/generate_hiv_summary_next.sql deleted file mode 100644 index 8b67f63..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_next.sql +++ /dev/null @@ -1,1357 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_smry_next`() -BEGIN - select @query_type := "sync"; - select @start := now(); - select @start := now(); - select @table_version := "flat_hiv_summary_v2.14"; - - set session sort_buffer_size=512000000; - - select @sep := " ## "; - select @lab_encounter_type := 99999; - select @death_encounter_type := 31; - select @last_date_created := (select max(max_date_created) from etl.flat_obs); - - - - create table if not exists flat_hiv_smry_next ( - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - is_clinical_encounter int, - location_id int, - location_uuid varchar(100), - visit_num int, - enrollment_date datetime, - hiv_start_date datetime, - death_date datetime, - scheduled_visit int, - transfer_out int, - transfer_in int, - - patient_care_status int, - out_of_care int, - prev_rtc_date datetime, - rtc_date datetime, - - arv_start_location int, - arv_first_regimen_start_date datetime, - arv_start_date datetime, - prev_arv_start_date datetime, - prev_arv_end_date datetime, - - arv_first_regimen varchar(500), - prev_arv_meds varchar(500), - cur_arv_meds varchar(500), - prev_arv_line int, - cur_arv_line int, - - - prev_arv_adherence varchar(200), - cur_arv_adherence varchar(200), - hiv_status_disclosed int, - - first_evidence_patient_pregnant datetime, - edd datetime, - screened_for_tb boolean, - tb_screening_result boolean, - tb_prophylaxis_start_date datetime, - tb_prophylaxis_end_date datetime, - tb_tx_start_date datetime, - tb_tx_end_date datetime, - pcp_prophylaxis_start_date datetime, - cd4_resulted double, - cd4_resulted_date datetime, - cd4_1 double, - cd4_1_date datetime, - cd4_2 double, - cd4_2_date datetime, - cd4_percent_1 double, - cd4_percent_1_date datetime, - cd4_percent_2 double, - cd4_percent_2_date datetime, - vl_resulted int, - vl_resulted_date datetime, - vl_1 int, - vl_1_date datetime, - vl_2 int, - vl_2_date datetime, - vl_order_date datetime, - cd4_order_date datetime, - - - hiv_dna_pcr_order_date datetime, - hiv_dna_pcr_resulted int, - hiv_dna_pcr_resulted_date datetime, - hiv_dna_pcr_1 int, - hiv_dna_pcr_1_date datetime, - hiv_dna_pcr_2 int, - hiv_dna_pcr_2_date datetime, - - - hiv_rapid_test_resulted int, - hiv_rapid_test_resulted_date datetime, - - condoms_provided int, - using_modern_contraceptive_method int, - - cur_who_stage int, - prev_encounter_datetime_hiv datetime, - next_encounter_datetime_hiv datetime, - prev_encounter_type_hiv mediumint, - next_encounter_type_hiv mediumint, - prev_clinical_datetime_hiv datetime, - next_clinical_datetime_hiv datetime, - prev_clinical_location_id mediumint, - next_clinical_location_id mediumint, - prev_clinical_rtc_date_hiv datetime, - next_clinical_rtc_date_hiv datetime, - - - - outreach_date_bncd datetime, - outreach_death_date_bncd datetime, - outreach_patient_care_status_bncd int, - transfer_date_bncd datetime, - transfer_transfer_out_bncd int, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index location_rtc (location_uuid,rtc_date), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_hiv), - index encounter_type (encounter_type) - ); - - select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); - - - select @last_update := - if(@last_update is null, - (select max(date_created) from amrs.encounter e join etl.flat_hiv_smry_next using (encounter_id)), - @last_update); - - - select @last_update := if(@last_update,@last_update,'1900-01-01'); - - - - - create table if not exists flat_hiv_smry_next_queue(person_id int, primary key (person_id)); - - - - select @num_ids := (select count(*) from flat_hiv_smry_next_queue limit 1); - - if (@num_ids=0 or @query_type="sync") then - - replace into flat_hiv_smry_next_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - - replace into flat_hiv_smry_next_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - - - ); - - replace into flat_hiv_smry_next_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_smry_next_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - end if; - - select @person_ids_count := (select count(*) from flat_hiv_smry_next_queue); - - delete t1 from flat_hiv_smry_next t1 join flat_hiv_smry_next_queue t2 using (person_id); - - while @person_ids_count > 0 do - - - drop table if exists flat_hiv_smry_next_queue_0; - - create temporary table flat_hiv_smry_next_queue_0 (select * from flat_hiv_smry_next_queue limit 5000); - - - select @person_ids_count := (select count(*) from flat_hiv_smry_next_queue); - - drop table if exists flat_hiv_smry_next_0a; - create temporary table flat_hiv_smry_next_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - 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,129) then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,115,117,120,127,128,129) then 10 - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join flat_hiv_smry_next_queue_0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_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) - AND NOT obs regexp "!!5303=(822|664)!!" - ); - - insert into flat_hiv_smry_next_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_hiv_smry_next_queue_0 t0 using (person_id) - ); - - drop table if exists flat_hiv_smry_next_0; - create temporary table flat_hiv_smry_next_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_smry_next_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - select @prev_id := null; - select @cur_id := null; - select @enrollment_date := null; - select @hiv_start_date := null; - select @cur_location := null; - select @cur_rtc_date := null; - select @prev_rtc_date := null; - select @hiv_start_date := null; - select @prev_arv_start_date := null; - select @arv_start_date := null; - select @prev_arv_end_date := null; - select @arv_start_location := null; - select @art_first_regimen_start_date := null; - select @arv_first_regimen := null; - select @prev_arv_line := null; - select @cur_arv_line := null; - select @prev_arv_adherence := null; - select @cur_arv_adherence := null; - select @hiv_status_disclosed := null; - select @first_evidence_pt_pregnant := null; - select @edd := null; - select @prev_arv_meds := null; - select @cur_arv_meds := null; - select @tb_prophylaxis_start_date := null; - select @tb_prophylaxis_end_date := null; - select @tb_treatment_start_date := null; - select @tb_treatment_end_date := null; - select @pcp_prophylaxis_start_date := null; - select @screened_for_tb := null; - select @tb_screening_result := null; - select @death_date := null; - select @vl_1:=null; - select @vl_2:=null; - select @vl_1_date:=null; - select @vl_2_date:=null; - select @vl_resulted:=null; - select @vl_resulted_date:=null; - - select @cd4_resulted:=null; - select @cd4_resulted_date:=null; - select @cd4_1:=null; - select @cd4_1_date:=null; - select @cd4_2:=null; - select @cd4_2_date:=null; - select @cd4_percent_1:=null; - select @cd4_percent_1_date:=null; - select @cd4_percent_2:=null; - select @cd4_percent_2_date:=null; - select @vl_order_date := null; - select @cd4_order_date := null; - - select @hiv_dna_pcr_order_date := null; - select @hiv_dna_pcr_1:=null; - select @hiv_dna_pcr_2:=null; - select @hiv_dna_pcr_1_date:=null; - select @hiv_dna_pcr_2_date:=null; - - select @hiv_rapid_test_resulted:=null; - select @hiv_rapid_test_resulted_date:= null; - - select @patient_care_status:=null; - - select @condoms_provided := null; - select @using_modern_contraceptive_method := null; - - - select @cur_who_stage := null; - - - - - - drop temporary table if exists flat_hiv_smry_next_1; - create temporary table flat_hiv_smry_next_1 (index encounter_id (encounter_id)) - (select - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.is_clinical_encounter, - - case - when @prev_id != @cur_id and t1.encounter_type in (21,@lab_encounter_type) then @enrollment_date := null - when @prev_id != @cur_id then @enrollment_date := encounter_datetime - when t1.encounter_type not in (21,@lab_encounter_type) and @enrollment_date is null then @enrollment_date := encounter_datetime - else @enrollment_date - end as enrollment_date, - - - - - - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as 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 - end as visit_num, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - - case - when obs regexp "!!7015=" then @transfer_in := replace(replace((substring_index(substring(obs,locate("!!7015=",obs)),@sep,1)),"!!7015=",""),"!!","") - else @transfer_in := null - end as transfer_in, - - - - - - case - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=1594!!" then 1 - when obs regexp "!!9082=(1287|1594|9068|9504|1285)!!" then 1 - else null - end as transfer_out, - - - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504|1285)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - - - - - case - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @death_encounter_type then @patient_care_status := 159 - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - - - - - - - case - when obs regexp "!!1946=1065!!" then @hiv_start_date := null - when t1.encounter_type=@lab_encounter_type and obs regexp "!!(1040|1030)=664!!" then @hiv_start_date:=null - when @prev_id != @cur_id or @hiv_start_date is null then - case - when obs regexp "!!(1040|1030)=664!!" then @hiv_start_date := date(encounter_datetime) - when obs regexp "!!(1088|1255)=" then @hiv_start_date := date(t1.encounter_datetime) - else @hiv_start_date := null - end - else @hiv_start_date - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_location := location_id - when @prev_id = @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location := location_id - when @prev_id != @cur_id then @arv_start_location := null - else @arv_start_location - end as arv_start_location, - - case - when @prev_id=@cur_id then @prev_arv_meds := @cur_arv_meds - else @prev_arv_meds := null - end as prev_arv_meds, - - - - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!1250=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1250=", "") ) ) / LENGTH("!!1250=") ))),"!!1250=",""),"!!","") - when obs regexp "!!1088=" then @cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!1088=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1088=", "") ) ) / LENGTH("!!1088=") ))),"!!1088=",""),"!!","") - when obs regexp "!!2154=" then @cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!2154=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!2154=", "") ) ) / LENGTH("!!2154=") ))),"!!2154=",""),"!!","") - when @prev_id=@cur_id then @cur_arv_meds - else @cur_arv_meds:= null - end as cur_arv_meds, - - - case - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id = @cur_id then @arv_first_regimen - else @arv_first_regimen := null - end as arv_first_regimen, - - case - when @arv_first_regimen_start_date is null and (obs regexp "!!1255=(1256|1259|1850)" or obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!") then @arv_first_regimen_start_date := date(t1.encounter_datetime) - when @prev_id != @cur_id then @arv_first_regimen_start_date := null - else @arv_first_regimen_start_date - end as arv_first_regimen_start_date, - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := null - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=(6156|9759)!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=(6156|9759)!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=(6156|9759)!!" then @cur_arv_line := 3 - when @prev_id = @cur_id then @cur_arv_line - else @cur_arv_line := null - end as cur_arv_line, - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := null - end as prev_arv_start_date, - - - - - - - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - when @cur_arv_meds != @prev_arv_meds and @cur_arv_line != @prev_arv_line then @arv_start_date := date(t1.encounter_datetime) - when @prev_id != @cur_id then @arv_start_date := null - else @arv_start_date - end as arv_start_date, - - case - when @prev_arv_start_date != @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end as prev_arv_end_date, - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := null - end as prev_arv_adherence, - - - - - - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id = @cur_id then @cur_arv_adherence - else @cur_arv_adherence := null - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when obs regexp "!!6596=" then @hiv_status_disclosed := null - when @prev_id != @cur_id then @hiv_status_disclosed := null - else @hiv_status_disclosed - end as hiv_status_disclosed, - - - - - - - case - when @prev_id != @cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" then @first_evidence_pt_pregnant := encounter_datetime - else @first_evidence_pt_pregnant := null - end - when @first_evidence_pt_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") then @first_evidence_pt_pregnant := encounter_datetime - when @first_evidence_pt_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@first_evidence_pt_pregnant,encounter_datetime) > 40 or timestampdiff(week,@edd,encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @first_evidence_pt_pregnant := null - else @first_evidence_pt_pregnant - end as first_evidence_patient_pregnant, - - - - - - - - case - when @prev_id != @cur_id then - case - when @first_evidence_patient_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - when @first_evidence_pt_pregnant then @edd := date_add(@first_evidence_pt_pregnant,interval 6 month) - else @edd := null - end - when @edd is null or @edd = @first_evidence_pt_pregnant then - case - when @first_evidence_pt_pregnant then @edd := date_add(@first_evidence_pt_pregnant,interval 6 month) - when @first_evidence_patient_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - when @first_evidence_pt_pregnant then @edd := date_add(@first_evidence_pt_pregnant,interval 6 month) - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - - - - - - - - case - when obs regexp "!!6174=" then @screened_for_tb := true - when obs regexp "!!2022=1065!!" then @screened_for_tb := true - when obs regexp "!!307=" then @screened_for_tb := true - when obs regexp "!!12=" then @screened_for_tb := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @screened_for_tb := true - when orders regexp "(12|307|8064|2311|2323)" then @screened_for_tb := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @screened_for_tb := true - when obs regexp "!!5958=1077!!" then @screened_for_tb := true - when obs regexp "!!2020=1065!!" then @screened_for_tb := true - when obs regexp "!!2021=1065!!" then @screened_for_tb := true - when obs regexp "!!2028=" then @screened_for_tb := true - when obs regexp "!!1268=(1256|1850)!!" then @screened_for_tb := true - when obs regexp "!!5959=(1073|1074)!!" then @screened_for_tb := true - when obs regexp "!!5971=(1073|1074)!!" then @screened_for_tb := true - when obs regexp "!!1492=107!!" then @screened_for_tb := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @screened_for_tb := true - end as screened_for_tb, - - - - - - - - - case - - when obs regexp "!!2022=1065!!" then @tb_screening_result := true - when obs regexp "!!307=" then @tb_screening_result := true - when obs regexp "!!12=" then @tb_screening_result := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screening_result := true - when orders regexp "(12|307|8064|2311|2323)" then @tb_screening_result := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screening_result := true - when obs regexp "!!5958=1077!!" then @tb_screening_result := true - when obs regexp "!!2020=1065!!" then @tb_screening_result := true - when obs regexp "!!2021=1065!!" then @tb_screening_result := true - when obs regexp "!!2028=" then @tb_screening_result := true - when obs regexp "!!1268=(1256|1850)!!" then @tb_screening_result := true - when obs regexp "!!5959=(1073|1074)!!" then @tb_screening_result := true - when obs regexp "!!5971=(1073|1074)!!" then @tb_screening_result := true - when obs regexp "!!1492=107!!" then @tb_screening_result := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screening_result := true - when obs not regexp "!!6174=1107" then @tb_screening_result := true - else @tb_screening_result := false - end as tb_screening_result, - - case - when obs regexp "!!1265=(1256|1257|1850)!!" then @on_tb_prophylaxis := 1 - when obs regexp "!!1110=656!!" then @on_tb_prophylaxis := 1 - when @prev_id = @cur_id then @on_tb_prophylaxis - else null - end as on_tb_prophylaxis, - - - - - - case - when @cur_id != @prev_id then - case - when obs regexp "!!1265=(1256|1850)!!" then @tb_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" then @tb_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1110=656!!" then @tb_prophylaxis_start_date := encounter_datetime - else @tb_prophylaxis_start_date := null - end - when @cur_id = @prev_id then - case - when obs regexp "!!1265=(1256|1850)!!" then @tb_prophylaxis_start_date := encounter_datetime - when @tb_prophylaxis_start_date is not null then @tb_prophylaxis_start_date - when obs regexp "!!1265=(1257|981|1406|1849)!!" then @tb_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1110=656!!" then @tb_prophylaxis_start_date := encounter_datetime - end - end as tb_prophylaxis_start_date, - - - - - - case - when @cur_id != @prev_id then - case - when obs regexp "!!1265=1260!!" then @tb_prophylaxis_end_date := encounter_datetime - else @tb_prophylaxis_end_date := null - end - when @cur_id = @prev_id then - case - when obs regexp "!!1265=1260!!" then @tb_prophylaxis_end_date := encounter_datetime - when @tb_prophylaxis_end_date is not null then @tb_prophylaxis_end_date - when @tb_prophylaxis_start_date is not null and obs regexp "!!1110=1107!!" and obs regexp "!!1265=" and obs regexp "!!1265=(1107|1260)!!" then @tb_prophylaxis_end_date := encounter_datetime - end - end as tb_prophylaxis_end_date, - - - - - - case - when @cur_id != @prev_id then - case - when obs regexp "!!1113=" then @tb_treatment_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @tb_treatment_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" then @tb_treatment_start_date := encounter_datetime - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @tb_treatment_start_date := encounter_datetime - else @tb_treatment_start_date := null - end - when @cur_id = @prev_id then - case - when obs regexp "!!1113=" then @tb_treatment_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when @tb_treatment_start_date is not null then @tb_treatment_start_date - when obs regexp "!!1268=1256!!" then @tb_treatment_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" then @tb_treatment_start_date := encounter_datetime - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @tb_treatment_start_date := encounter_datetime - end - end as tb_tx_start_date, - - - - - case - when @cur_id != @prev_id then - case - when obs regexp "!!2041=" then @tb_treatment_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_treatment_end_date := encounter_datetime - else @tb_treatment_end_date := null - end - when @cur_id = @prev_id then - case - when obs regexp "!!2041=" then @tb_treatment_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_treatment_end_date := encounter_datetime - when @tb_treatment_end_date is not null then @tb_treatment_end_date - when @tb_treatment_start_date is not null and obs regexp "!!6176=1066!!" and !(obs regexp "!!1268=(1256|1257|1259|1849|981)!!") then @tb_treatment_end_date := encounter_datetime - end - end as tb_tx_end_date, - - - - - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1261=1257!!" then - case - when @prev_id!=@cur_id or @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - else @pcp_prophylaxis_start_date - end - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1193=916!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when @prev_id=@cur_id then @pcp_prophylaxis_start_date - else @pcp_prophylaxis_start_date := null - end as pcp_prophylaxis_start_date, - - - - - - - - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when @prev_id != @cur_id or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := encounter_datetime - else @death_date := null - end - else @death_date - end as death_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - else @cd4_2:=null - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - else @cd4_2_date:=null - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=null - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=null - end as cd4_1_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - else @cd4_percent_2:=null - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - else @cd4_percent_2_date:=null - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=null - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=null - end as cd4_percent_1_date, - - - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) <>date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - else @vl_2:=null - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - else @vl_2_date:=null - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=null - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date:=null - end as vl_1_date, - - - - - - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(encounter_datetime) - when orders regexp "856" then @vl_order_date := date(encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - else @vl_order_date := null - end as vl_order_date, - - - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := null - end as cd4_order_date, - - - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := null - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - else @hiv_dna_pcr_2:=null - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - else @hiv_dna_pcr_2_date:=null - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=null - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=null - end as hiv_dna_pcr_1_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - - case - when obs regexp "!!8302=8305!!" then @condoms_provided := 1 - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided := 1 - when obs regexp "!!6579=" then @condoms_provided := 1 - else null - end as condoms_provided, - - case - when obs regexp "!!374=(5275|6220|780|5279)!!" then @using_modern_conctaceptive_method := 1 - else null - end as using_modern_contraceptive_method, - - - - - - - - - - - - - case - when obs regexp "!!5356=(1204)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1223)!!" then @cur_who_stage := 4 - when @prev_id = @cur_id then @cur_who_stage - else @cur_who_stage := null - end as cur_who_stage - - from flat_hiv_smry_next_0 t1 - join amrs.person p using (person_id) - ); - - select @prev_id := null; - select @cur_id := null; - select @prev_encounter_datetime := null; - select @cur_encounter_datetime := null; - - select @prev_clinical_datetime := null; - select @cur_clinical_datetime := null; - - select @next_encounter_type := null; - select @cur_encounter_type := null; - - select @prev_clinical_location_id := null; - select @cur_clinical_location_id := null; - - - alter table flat_hiv_smry_next_1 drop prev_id, drop cur_id; - - drop table if exists flat_hiv_smry_next_2; - create temporary table flat_hiv_smry_next_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_date_bncd - else null - end as outreach_date_bncd, - - case - when encounter_type=21 and @outreach_date_bncd is null then @outreach_date_bncd := encounter_datetime - when is_clinical_encounter then @outreach_date_bncd := null - when @prev_id != @cur_id then @outreach_date_bncd := null - else @outreach_date_bncd - end as next_outreach_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_death_date_bncd - else null - end as outreach_death_date_bncd, - - case - when encounter_type=21 and @outreach_death_date_bncd is null then @outreach_death_date_bncd := death_date - when is_clinical_encounter then @outreach_death_date_bncd := null - when @prev_id != @cur_id then @outreach_death_date_bncd := null - else @outreach_death_date_bncd - end as next_outreach_death_date_bncd, - - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then cast(@outreach_patient_care_status_bncd as unsigned) - else null - end as outreach_patient_care_status_bncd, - - case - when encounter_type=21 and @outreach_patient_care_status_bncd is null then @outreach_patient_care_status_bncd := patient_care_status - when is_clinical_encounter then @outreach_patient_care_status_bncd := null - when @prev_id != @cur_id then @outreach_patient_care_status_bncd := null - else @outreach_patient_care_status_bncd - end as next_outreach_patient_care_status_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_date_bncd - else null - end as transfer_date_bncd, - - case - when encounter_type=116 and @transfer_date_bncd is null then @transfer_date_bncd := encounter_datetime - when is_clinical_encounter then @transfer_date_bncd := null - when @prev_id != @cur_id then @transfer_date_bncd := null - else @transfer_date_bncd - end as next_transfer_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_transfer_out_bncd - else null - end as transfer_transfer_out_bncd, - - case - when encounter_type=116 and @transfer_transfer_out_bncd is null then @transfer_transfer_out_bncd := encounter_datetime - when is_clinical_encounter then @transfer_transfer_out_bncd := null - when @prev_id != @cur_id then @transfer_transfer_out_bncd := null - else @transfer_transfer_out_bncd - end as next_transfer_transfer_out_bncd - - from flat_hiv_smry_next_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_smry_next_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - select @prev_id := null; - select @cur_id := null; - select @prev_encounter_type := null; - select @cur_encounter_type := null; - select @prev_encounter_datetime := null; - select @cur_encounter_datetime := null; - select @prev_clinical_datetime := null; - select @cur_clinical_datetime := null; - select @prev_clinical_location_id := null; - select @cur_clinical_location_id := null; - - drop temporary table if exists flat_hiv_smry_next_3; - create temporary table flat_hiv_smry_next_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_hiv, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_hiv_smry_next_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - replace into flat_hiv_smry_next - (select - person_id, - t1.uuid, - t1.visit_id, - encounter_id, - encounter_datetime, - encounter_type, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - visit_num, - enrollment_date, - hiv_start_date, - death_date, - scheduled_visit, - transfer_out, - transfer_in, - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date, - arv_start_location, - arv_first_regimen_start_date, - arv_start_date, - prev_arv_start_date, - prev_arv_end_date, - arv_first_regimen, - prev_arv_meds, - cur_arv_meds, - prev_arv_line, - cur_arv_line, - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - first_evidence_patient_pregnant, - edd, - screened_for_tb, - tb_screening_result, - tb_prophylaxis_start_date, - tb_prophylaxis_end_date, - tb_tx_start_date, - tb_tx_end_date, - pcp_prophylaxis_start_date, - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - vl_order_date, - cd4_order_date, - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - condoms_provided, - using_modern_contraceptive_method, - cur_who_stage, - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_location_id, - next_clinical_location_id, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv, - outreach_date_bncd, - outreach_death_date_bncd, - outreach_patient_care_status_bncd, - transfer_date_bncd, - transfer_transfer_out_bncd - - from flat_hiv_smry_next_3 t1 - join amrs.location t2 using (location_id)); - - delete from flat_hiv_smry_next_queue where person_id in (select person_id from flat_hiv_smry_next_queue_0); - - end while; - - - delete t1 - from flat_hiv_smry_next t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value='true' and voided=0; - - select @end := now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_v15.sql b/etl-scripts/stored-procedures/generate_hiv_summary_v15.sql deleted file mode 100644 index 93af0a1..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_v15.sql +++ /dev/null @@ -1,1469 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_v15`() -BEGIN - select @query_type := "rebuild"; - select @start := now(); - select @start := now(); - select @table_version := "flat_hiv_summary_v2.15"; - - set session sort_buffer_size=512000000; - - select @sep := " ## "; - select @lab_encounter_type := 99999; - select @death_encounter_type := 31; - select @last_date_created := (select max(max_date_created) from etl.flat_obs); - - - - create table if not exists flat_hiv_summary_v15 ( - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - is_clinical_encounter int, - - location_id int, - location_uuid varchar(100), - - visit_num int, - - enrollment_date datetime, - enrollment_location_id int, - - hiv_start_date datetime, - - death_date datetime, - scheduled_visit int, - - transfer_in tinyint, - transfer_in_location_id int, - transfer_in_date datetime, - - transfer_out tinyint, - transfer_out_location_id int, - transfer_out_date datetime, - - patient_care_status int, - out_of_care int, - prev_rtc_date datetime, - rtc_date datetime, - - arv_first_regimen varchar(500), - arv_first_regimen_location_id int, - arv_first_regimen_start_date datetime, - - prev_arv_meds varchar(500), - cur_arv_meds varchar(500), - arv_start_date datetime, - arv_start_location_id int, - prev_arv_start_date datetime, - prev_arv_end_date datetime, - - prev_arv_line int, - cur_arv_line int, - - - prev_arv_adherence varchar(200), - cur_arv_adherence varchar(200), - hiv_status_disclosed int, - - first_evidence_patient_pregnant datetime, - edd datetime, - - screened_for_tb boolean, - - ipt_start_date datetime, - ipt_stop_date datetime, - ipt_completion_date datetime, - - tb_tx_start_date datetime, - tb_tx_end_date datetime, - - pcp_prophylaxis_start_date datetime, - - condoms_provided_date datetime, - modern_contraceptive_method_start_date datetime, - cur_who_stage int, - - cd4_resulted double, - cd4_resulted_date datetime, - cd4_1 double, - cd4_1_date datetime, - cd4_2 double, - cd4_2_date datetime, - cd4_percent_1 double, - cd4_percent_1_date datetime, - cd4_percent_2 double, - cd4_percent_2_date datetime, - vl_resulted int, - vl_resulted_date datetime, - vl_1 int, - vl_1_date datetime, - vl_2 int, - vl_2_date datetime, - vl_order_date datetime, - cd4_order_date datetime, - - - hiv_dna_pcr_order_date datetime, - hiv_dna_pcr_resulted int, - hiv_dna_pcr_resulted_date datetime, - hiv_dna_pcr_1 int, - hiv_dna_pcr_1_date datetime, - hiv_dna_pcr_2 int, - hiv_dna_pcr_2_date datetime, - - - hiv_rapid_test_resulted int, - hiv_rapid_test_resulted_date datetime, - - prev_encounter_datetime_hiv datetime, - next_encounter_datetime_hiv datetime, - prev_encounter_type_hiv mediumint, - next_encounter_type_hiv mediumint, - prev_clinical_datetime_hiv datetime, - next_clinical_datetime_hiv datetime, - prev_clinical_location_id mediumint, - next_clinical_location_id mediumint, - prev_clinical_rtc_date_hiv datetime, - next_clinical_rtc_date_hiv datetime, - - - - outreach_date_bncd datetime, - outreach_death_date_bncd datetime, - outreach_patient_care_status_bncd int, - transfer_date_bncd datetime, - transfer_transfer_out_bncd datetime, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index location_rtc (location_uuid,rtc_date), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_hiv), - index encounter_type (encounter_type) - ); - - select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); - - - select @last_update := - if(@last_update is null, - (select max(date_created) from amrs.encounter e join etl.flat_hiv_summary_v15 using (encounter_id)), - @last_update); - - - select @last_update := if(@last_update,@last_update,'1900-01-01'); - - - - - create table if not exists flat_hiv_summary_build_queue(person_id int, primary key (person_id)); - - - - select @num_ids := (select count(*) from flat_hiv_summary_build_queue limit 1); - - - if (@num_ids=0 or @query_type="sync") then - - replace into flat_hiv_summary_build_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - - replace into flat_hiv_summary_build_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - - - ); - - replace into flat_hiv_summary_build_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_build_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - end if; - - select @person_ids_count := (select count(*) from flat_hiv_summary_build_queue); - - delete t1 from flat_hiv_summary_v15 t1 join flat_hiv_summary_build_queue t2 using (person_id); - - while @person_ids_count > 0 do - - - drop table if exists flat_hiv_summary_build_queue_0; - - create temporary table flat_hiv_summary_build_queue_0 (select * from flat_hiv_summary_build_queue limit 100); - - - select @person_ids_count := (select count(*) from flat_hiv_summary_build_queue); - - drop temporary table if exists flat_hiv_summary_0a; - create temporary table flat_hiv_summary_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - 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,129,138,153,154,158) then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,115,117,120,127,128,138,129,153,154,158) then 10 - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join flat_hiv_summary_build_queue_0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_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,153,154,158) - AND NOT obs regexp "!!5303=(822|664)!!" - ); - - insert into flat_hiv_summary_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_hiv_summary_build_queue_0 t0 using (person_id) - ); - - drop temporary table if exists flat_hiv_summary_0; - create temporary table flat_hiv_summary_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - select @prev_id := null; - select @cur_id := null; - select @prev_encounter_date := null; - select @cur_encounter_date := null; - select @enrollment_date := null; - select @hiv_start_date := null; - select @cur_location := null; - select @cur_rtc_date := null; - select @prev_rtc_date := null; - select @hiv_start_date := null; - select @prev_arv_start_date := null; - select @arv_start_date := null; - select @prev_arv_end_date := null; - select @arv_start_location_id := null; - select @art_first_regimen_start_date := null; - select @arv_first_regimen := null; - select @prev_arv_line := null; - select @cur_arv_line := null; - select @prev_arv_adherence := null; - select @cur_arv_adherence := null; - select @hiv_status_disclosed := null; - select @first_evidence_pt_pregnant := null; - select @edd := null; - select @prev_arv_meds := null; - select @cur_arv_meds := null; - select @ipt_start_date := null; - select @ipt_end_date := null; - select @ipt_completion_date := null; - select @tb_treatment_start_date := null; - select @tb_treatment_end_date := null; - select @pcp_prophylaxis_start_date := null; - select @screened_for_tb := null; - select @death_date := null; - - select @patient_care_status:=null; - - select @condoms_provided_date := null; - select @modern_contraceptive_method_start_date := null; - - - select @cur_who_stage := null; - - select @vl_1:=null; - select @vl_2:=null; - select @vl_1_date:=null; - select @vl_2_date:=null; - select @vl_resulted:=null; - select @vl_resulted_date:=null; - - select @cd4_resulted:=null; - select @cd4_resulted_date:=null; - select @cd4_1:=null; - select @cd4_1_date:=null; - select @cd4_2:=null; - select @cd4_2_date:=null; - select @cd4_percent_1:=null; - select @cd4_percent_1_date:=null; - select @cd4_percent_2:=null; - select @cd4_percent_2_date:=null; - select @vl_order_date := null; - select @cd4_order_date := null; - - select @hiv_dna_pcr_order_date := null; - select @hiv_dna_pcr_1:=null; - select @hiv_dna_pcr_2:=null; - select @hiv_dna_pcr_1_date:=null; - select @hiv_dna_pcr_2_date:=null; - - select @hiv_rapid_test_resulted:=null; - select @hiv_rapid_test_resulted_date:= null; - - - - - - - drop temporary table if exists flat_hiv_summary_1; - create temporary table flat_hiv_summary_1 (index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.encounter_id, - @prev_encounter_date := date(@cur_encounter_date) as prev_encounter_date, - @cur_encounter_date := date(encounter_datetime) as cur_encounter_date, - t1.encounter_datetime, - t1.encounter_type, - t1.is_clinical_encounter, - - case - when @enrollment_date is null and obs regexp "!!7013=" then @enrollment_date := replace(replace((substring_index(substring(obs,locate("!!7013=",obs)),@sep,1)),"!!7013=",""),"!!","") - when obs regexp "!!7015=" and @enrollment_date is null then @enrollmen_date := "1900-01-01" - when t1.encounter_type not in (21,@lab_encounter_type) and @enrollment_date is null then @enrollment_date := date(encounter_datetime) - when @prev_id = @cur_id then @enrollment_date - else @enrollment_date := null - end as enrollment_date, - - case - when @enrollment_location_id is null and obs regexp "!!7030=5622" then @enrollment_location_id := 9999 - when obs regexp "!!7015=" and @enrollment_location_id is null then @enrollmen_location_id := 9999 - when t1.encounter_type not in (21,@lab_encounter_type) and @enrollment_location_id is null then @enrollment_location_id := location_id - when @prev_id = @cur_id then @enrollment_location_id - else @enrollment_location_id := null - end as enrollment_location_id, - - - - - - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as 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 - end as visit_num, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - - - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504|1285)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - - - - - case - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @death_encounter_type then @patient_care_status := 159 - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - - case - when obs regexp "!!9203=" then @hiv_start_date := replace(replace((substring_index(substring(obs,locate("!!9203=",obs)),@sep,1)),"!!9203=",""),"!!","") - when obs regexp "!!7015=" then @hiv_start_date := "1900-01-01" - when @hiv_start_date is null then @hiv_start_date := date(encounter_datetime) - when @prev_id = @cur_id then @hiv_start_date - else @hiv_start_date := null - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_location_id := location_id - when @prev_id = @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location_id := location_id - when @prev_id != @cur_id then @arv_start_location_id := null - else @arv_start_location_id - end as arv_start_location_id, - - case - when @prev_id=@cur_id and @cur_arv_meds is not null then @prev_arv_meds := @cur_arv_meds - when @prev_id=@cur_id then @prev_arv_meds - else @prev_arv_meds := null - end as prev_arv_meds, - - - - - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!1250=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1250=", "") ) ) / LENGTH("!!1250=") ))),"!!1250=",""),"!!","") - when obs regexp "!!1088=" then @cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!1088=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1088=", "") ) ) / LENGTH("!!1088=") ))),"!!1088=",""),"!!","") - when obs regexp "!!2154=" then @cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!2154=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!2154=", "") ) ) / LENGTH("!!2154=") ))),"!!2154=",""),"!!","") - else @cur_arv_meds:= null - end as cur_arv_meds, - - - case - when @arv_first_regimen is null and obs regexp "!!2157=" then - @arv_first_regimen := replace(replace((substring_index(substring(obs,locate("!!2157=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!2157=", "") ) ) / LENGTH("!!2157=") ))),"!!2157=",""),"!!","") - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id = @cur_id then @arv_first_regimen - when @prev_id != @cur_id then @arv_first_regimen := null - else "-1" - end as arv_first_regimen, - - - - case - when @arv_first_regimen is null and obs regexp "!!1499=" then @arv_first_regimen_start_date := replace(replace((substring_index(substring(obs,locate("!!1499=",obs)),@sep,1)),"!!1499=",""),"!!","") - when @arv_first_regimen_start_date is null and @cur_arv_meds is not null then @arv_first_regimen_start_date := date(t1.encounter_datetime) - when @prev_id = @cur_id then @arv_first_regimen_start_date - when @prev_id != @cur_id then @arv_first_regimen_start_date := null - else @arv_first_regimen_start_date - end as arv_first_regimen_start_date, - - case - when @arv_first_regimen is null and obs regexp "!!1499=" then @arv_first_regimen_location_id := 9999 - when @arv_first_regimen_location_id is null and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @prev_id = @cur_id then @arv_first_regimen_location_id - when @prev_id != @cur_id then @arv_first_regimen_location_id := null - else "-1" - end as arv_first_regimen_location_id, - - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := null - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=(6156|9759)!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=(6156|9759)!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=(6156|9759)!!" then @cur_arv_line := 3 - when @prev_id = @cur_id then @cur_arv_line - - else @cur_arv_line := null - end as cur_arv_line, - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := null - end as prev_arv_start_date, - - - - - - - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - when @cur_arv_meds != @prev_arv_meds and @cur_arv_line != @prev_arv_line then @arv_start_date := date(t1.encounter_datetime) - when @prev_id != @cur_id then @arv_start_date := null - else @arv_start_date - end as arv_start_date, - - - case - when @prev_arv_start_date != @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end as prev_arv_end_date, - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := null - end as prev_arv_adherence, - - - - - - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id = @cur_id then @cur_arv_adherence - else @cur_arv_adherence := null - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when obs regexp "!!6596=" then @hiv_status_disclosed := null - when @prev_id != @cur_id then @hiv_status_disclosed := null - else @hiv_status_disclosed - end as hiv_status_disclosed, - - - - - - - case - when @prev_id != @cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" then @first_evidence_pt_pregnant := encounter_datetime - else @first_evidence_pt_pregnant := null - end - when @first_evidence_pt_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") then @first_evidence_pt_pregnant := encounter_datetime - when @first_evidence_pt_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@first_evidence_pt_pregnant,encounter_datetime) > 40 or timestampdiff(week,@edd,encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @first_evidence_pt_pregnant := null - else @first_evidence_pt_pregnant - end as first_evidence_patient_pregnant, - - - - - - - - case - when @prev_id != @cur_id then - case - when @first_evidence_patient_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - when @first_evidence_pt_pregnant then @edd := date_add(@first_evidence_pt_pregnant,interval 6 month) - else @edd := null - end - when @edd is null or @edd = @first_evidence_pt_pregnant then - case - when @first_evidence_pt_pregnant then @edd := date_add(@first_evidence_pt_pregnant,interval 6 month) - when @first_evidence_patient_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - when @first_evidence_pt_pregnant then @edd := date_add(@first_evidence_pt_pregnant,interval 6 month) - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - - - - - - - - case - when obs regexp "!!6174=" then @screened_for_tb := true - when obs regexp "!!2022=1065!!" then @screened_for_tb := true - when obs regexp "!!307=" then @screened_for_tb := true - when obs regexp "!!12=" then @screened_for_tb := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @screened_for_tb := true - when orders regexp "(12|307|8064|2311|2323)" then @screened_for_tb := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @screened_for_tb := true - when obs regexp "!!5958=1077!!" then @screened_for_tb := true - when obs regexp "!!2020=1065!!" then @screened_for_tb := true - when obs regexp "!!2021=1065!!" then @screened_for_tb := true - when obs regexp "!!2028=" then @screened_for_tb := true - when obs regexp "!!1268=(1256|1850)!!" then @screened_for_tb := true - when obs regexp "!!5959=(1073|1074)!!" then @screened_for_tb := true - when obs regexp "!!5971=(1073|1074)!!" then @screened_for_tb := true - when obs regexp "!!1492=107!!" then @screened_for_tb := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @screened_for_tb := true - end as screened_for_tb, - - - case - when obs regexp "!!1265=(1107|1260)!!" then @on_ipt := 0 - when obs regexp "!!1265=!!" then @on_ipt := 1 - when obs regexp "!!1110=656!!" then @on_ipt := 1 - when @prev_id = @cur_id then @on_ipt - else null - end as on_ipt, - - - - - - case - when obs regexp "!!1265=(1256|1850)!!" then @ipt_start_date := encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" and @ipt_start_date is null then @ipt_start_date := encounter_datetime - when @cur_id = @prev_id then @ipt_start_date - else @ipt_start_date := null - end as ipt_start_date, - - - - - - - case - when obs regexp "!!1266=!!" then @ipt_stop_date := encounter_datetime - when @cur_id = @prev_id then @ipt_stop_date - else @ipt_stop_date - end as ipt_stop_date, - - case - when obs regexp "!!1266=1267!!" then @ipt_completion_date := encounter_datetime - when @cur_id = @prev_id then @ipt_completion_date - else @ipt_completion_date - end as ipt_completion_date, - - - case - when obs regexp "!!1268=(1107|1260)!!" then @on_tb_treatment := 0 - when obs regexp "!!1268=" then @on_tb_treatment := 1 - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @on_tb_treatment := 1 - else 0 - end as on_tb_tx, - - - - - - - case - when obs regexp "!!1113=" then @tb_treatment_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @tb_treatment_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" and obs regexp "!!7015=" and @tb_treatment_start_date is null then @tb_treatment_start_date := null - when obs regexp "!!1268=(1257|1259|1849|981)!!" and @tb_treatment_start_date is null then @tb_treatment_start_date := encounter_datetime - - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" and @tb_treatment_start_date is null then @tb_treatment_start_date := encounter_datetime - when @cur_id = @prev_id then @tb_treatment_start_date - else @tb_treatment_start_date := null - end as tb_tx_start_date, - - - - - case - when obs regexp "!!2041=" then @tb_treatment_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_treatment_end_date := encounter_datetime - when @cur_id = @prev_id then @tb_treatment_end_date - else @tb_treatment_end_date := null - end as tb_tx_end_date, - - - - - - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1261=1257!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1193=916!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when @prev_id=@cur_id then @pcp_prophylaxis_start_date - else @pcp_prophylaxis_start_date := null - end as pcp_prophylaxis_start_date, - - - - - - - - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when @prev_id != @cur_id or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := encounter_datetime - else @death_date := null - end - else @death_date - end as death_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - else @cd4_2:=null - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - else @cd4_2_date:=null - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=null - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=null - end as cd4_1_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - else @cd4_percent_2:=null - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - else @cd4_percent_2_date:=null - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=null - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=null - end as cd4_percent_1_date, - - - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) <>date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - else @vl_2:=null - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - else @vl_2_date:=null - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=null - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date:=null - end as vl_1_date, - - - - - - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(encounter_datetime) - when orders regexp "856" then @vl_order_date := date(encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - else @vl_order_date := null - end as vl_order_date, - - - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := null - end as cd4_order_date, - - - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := null - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - else @hiv_dna_pcr_2:=null - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - else @hiv_dna_pcr_2_date:=null - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=null - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=null - end as hiv_dna_pcr_1_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - case - when obs regexp "!!8302=8305!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!6579=" then @condoms_provided_date := encounter_datetime - when @prev_id = @cur_id then @condoms_provided_date - else @condoms_provided_date := null - end as condoms_provided_date, - - - - - - - - - - case - when obs regexp "!!7240=(5275|6220|780|5279)!!" - then @modern_contraceptive_method_start_date := encounter_datetime - when obs regexp "!!374=(5275|6220|780|5279)!!" and obs regexp "!!1190=" - then @modern_contraceptive_method_start_date := replace(replace((substring_index(substring(obs,locate("!!1190=",obs)),@sep,1)),"!!1190=",""),"!!","") - when obs regexp "!!374=(5275|6220|780|5279)!!" - then @modern_contraceptive_method_start_date := encounter_datetime - when @prev_id = @cur_id then @modern_contraceptive_method_start_date - else @modern_contraceptive_method_start_date := null - end as modern_contraceptive_method_start_date, - - - - - - - - - - - - - - case - when obs regexp "!!5356=(1204)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1223)!!" then @cur_who_stage := 4 - when @prev_id = @cur_id then @cur_who_stage - else @cur_who_stage := null - end as cur_who_stage - - from flat_hiv_summary_0 t1 - join amrs.person p using (person_id) - ); - - - - select @prev_id := null; - select @cur_id := null; - select @prev_encounter_datetime := null; - select @cur_encounter_datetime := null; - - select @prev_clinical_datetime := null; - select @cur_clinical_datetime := null; - - select @next_encounter_type := null; - select @cur_encounter_type := null; - - select @prev_clinical_location_id := null; - select @cur_clinical_location_id := null; - - - alter table flat_hiv_summary_1 drop prev_id, drop cur_id; - - drop table if exists flat_hiv_summary_2; - create temporary table flat_hiv_summary_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_date_bncd - else null - end as outreach_date_bncd, - - case - when encounter_type=21 and @outreach_date_bncd is null then @outreach_date_bncd := encounter_datetime - when is_clinical_encounter then @outreach_date_bncd := null - when @prev_id != @cur_id then @outreach_date_bncd := null - else @outreach_date_bncd - end as next_outreach_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_death_date_bncd - else null - end as outreach_death_date_bncd, - - case - when encounter_type=21 and @outreach_death_date_bncd is null then @outreach_death_date_bncd := death_date - when is_clinical_encounter then @outreach_death_date_bncd := null - when @prev_id != @cur_id then @outreach_death_date_bncd := null - else @outreach_death_date_bncd - end as next_outreach_death_date_bncd, - - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then cast(@outreach_patient_care_status_bncd as unsigned) - else null - end as outreach_patient_care_status_bncd, - - case - when encounter_type=21 and @outreach_patient_care_status_bncd is null then @outreach_patient_care_status_bncd := patient_care_status - when is_clinical_encounter then @outreach_patient_care_status_bncd := null - when @prev_id != @cur_id then @outreach_patient_care_status_bncd := null - else @outreach_patient_care_status_bncd - end as next_outreach_patient_care_status_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_date_bncd - else null - end as transfer_date_bncd, - - case - when encounter_type=116 and @transfer_date_bncd is null then @transfer_date_bncd := encounter_datetime - when is_clinical_encounter then @transfer_date_bncd := null - when @prev_id != @cur_id then @transfer_date_bncd := null - else @transfer_date_bncd - end as next_transfer_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_transfer_out_bncd - else null - end as transfer_transfer_out_bncd, - - case - when encounter_type=116 and @transfer_transfer_out_bncd is null then @transfer_transfer_out_bncd := encounter_datetime - when is_clinical_encounter then @transfer_transfer_out_bncd := null - when @prev_id != @cur_id then @transfer_transfer_out_bncd := null - else @transfer_transfer_out_bncd - end as next_transfer_transfer_out_bncd - - from flat_hiv_summary_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - select @prev_id := null; - select @cur_id := null; - select @prev_encounter_type := null; - select @cur_encounter_type := null; - select @prev_encounter_datetime := null; - select @cur_encounter_datetime := null; - select @prev_clinical_datetime := null; - select @cur_clinical_datetime := null; - select @prev_clinical_location_id := null; - select @cur_clinical_location_id := null; - - drop temporary table if exists flat_hiv_summary_3; - create temporary table flat_hiv_summary_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_hiv, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_hiv_summary_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - alter table flat_hiv_summary_3 drop prev_id, drop cur_id; - - select @prev_id := null; - select @cur_id := null; - select @transfer_in := null; - select @transfer_in_date := null; - select @transfer_in_location_id := null; - select @transfer_out := null; - select @transfer_out_date := null; - select @transfer_out_location_id := null; - - drop temporary table if exists flat_hiv_summary_4; - - create temporary table flat_hiv_summary_4 ( index person_enc (person_id, encounter_datetime)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - - - case - when obs regexp "!!7015=" then @transfer_in := 1 - when prev_clinical_location_id != location_id then @transfer_in := 1 - else @transfer_in := null - end as transfer_in, - - case - when obs regexp "!!7015=" then @transfer_in_date := encounter_datetime - when prev_clinical_location_id != location_id then @transfer_in_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 then @transfer_in_location_id := prev_clinical_location_id - when @cur_id = @prev_id then @transfer_in_location_id - else @transfer_in_location_id := null - end transfer_in_location_id, - - - - - - - - - - - - case - when obs regexp "!!1285=!!" then @transfer_out := 1 - when obs regexp "!!1596=1594!!" then @transfer_out := 1 - when obs regexp "!!9082=(1287|1594|9068|9504|1285)!!" then @transfer_out := 1 - when next_clinical_location_id != location_id then @transfer_out := 1 - else @transfer_out := null - end as transfer_out, - - case - when obs regexp "!!1285=(1287|9068|2050)!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9999 - when obs regexp "!!1285=1286!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9998 - when next_clinical_location_id != location_id then @transfer_out_location_id := next_clinical_location_id - else @transfer_out_location_id := null - end transfer_out_location_id, - - - case - when @transfer_out and next_clinical_datetime_hiv is null then @transfer_out_date := cur_rtc_date - when next_clinical_location_id != location_id then @transfer_out_date := next_clinical_datetime_hiv - when transfer_transfer_out_bncd then @transfer_out_date := transfer_transfer_out_bncd - else @transfer_out_date := null - end transfer_out_date - - - - from flat_hiv_summary_3 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - replace into flat_hiv_summary_v15 - (select - person_id, - t1.uuid, - visit_id, - encounter_id, - encounter_datetime, - encounter_type, - is_clinical_encounter, - - location_id, - t2.uuid as location_uuid, - - visit_num, - - enrollment_date, - enrollment_location_id, - - hiv_start_date, - - death_date, - scheduled_visit, - - transfer_in, - transfer_in_location_id , - transfer_in_date, - - transfer_out, - transfer_out_location_id, - transfer_out_date, - - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date as rtc_date, - - arv_first_regimen , - arv_first_regimen_location_id, - arv_first_regimen_start_date, - - prev_arv_meds, - cur_arv_meds, - arv_start_date , - arv_start_location_id, - prev_arv_start_date, - prev_arv_end_date, - - prev_arv_line, - cur_arv_line, - - - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - - first_evidence_patient_pregnant, - edd, - - screened_for_tb, - - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - - tb_tx_start_date, - tb_tx_end_date, - - pcp_prophylaxis_start_date, - - condoms_provided_date, - modern_contraceptive_method_start_date, - cur_who_stage, - - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - vl_order_date, - cd4_order_date, - - - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - - - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_location_id, - next_clinical_location_id, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv, - - - outreach_date_bncd, - outreach_death_date_bncd, - outreach_patient_care_status_bncd, - transfer_date_bncd, - transfer_transfer_out_bncd - - from flat_hiv_summary_4 t1 - join amrs.location t2 using (location_id)); - - delete from flat_hiv_summary_build_queue where person_id in (select person_id from flat_hiv_summary_build_queue_0); - - end while; - - - delete t1 - from flat_hiv_summary_v15 t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value='true' and voided=0; - - select @end := now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_v15_10.sql b/etl-scripts/stored-procedures/generate_hiv_summary_v15_10.sql deleted file mode 100644 index 78e5a9d..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_v15_10.sql +++ /dev/null @@ -1,2237 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_v15_10`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN -/**/ -set @primary_table := "foo_flat_hiv_summary"; -#set @primary_table := "flat_hiv_summary_v15b"; - - set @query_type = query_type; - set @queue_table = ""; - set @total_rows_written = 0; - - set @start = now(); - set @table_version = "flat_hiv_summary_v2.18"; - set @table_version = "foo_flat_hiv_summary"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - create table if not exists flat_hiv_summary_v15b ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - is_clinical_encounter int, - location_id int, - location_uuid varchar(100), - visit_num int, - enrollment_date datetime, - enrollment_location_id int, - hiv_start_date datetime, - death_date datetime, - scheduled_visit int, - transfer_in tinyint, - transfer_in_location_id int, - transfer_in_date datetime, - transfer_out tinyint, - transfer_out_location_id int, - transfer_out_date datetime, - patient_care_status int, - out_of_care int, - prev_rtc_date datetime, - rtc_date datetime, - arv_first_regimen varchar(500), - arv_first_regimen_location_id int, - arv_first_regimen_start_date datetime, - arv_first_regimen_start_date_flex datetime, - prev_arv_meds varchar(500), - cur_arv_meds varchar(500), - cur_arv_meds_strict varchar(500), - arv_start_date datetime, - arv_start_location_id int, - prev_arv_start_date datetime, - prev_arv_end_date datetime, - prev_arv_line int, - cur_arv_line int, - cur_arv_line_strict int, - cur_arv_line_reported tinyint, - prev_arv_adherence varchar(200), - cur_arv_adherence varchar(200), - hiv_status_disclosed int, - is_pregnant boolean, - edd datetime, - tb_screen boolean, - tb_screening_result int, - tb_screening_datetime datetime, - on_ipt boolean, - ipt_start_date datetime, - ipt_stop_date datetime, - ipt_completion_date datetime, - on_tb_tx boolean, - tb_tx_start_date datetime, - tb_tx_end_date datetime, - pcp_prophylaxis_start_date datetime, - condoms_provided_date datetime, - modern_contraceptive_method_start_date datetime, - contraceptive_method int, - cur_who_stage int, - discordant_status int, - cd4_resulted double, - cd4_resulted_date datetime, - cd4_1 double, - cd4_1_date datetime, - cd4_2 double, - cd4_2_date datetime, - cd4_percent_1 double, - cd4_percent_1_date datetime, - cd4_percent_2 double, - cd4_percent_2_date datetime, - vl_resulted int, - vl_resulted_date datetime, - vl_1 int, - vl_1_date datetime, - vl_2 int, - vl_2_date datetime, - vl_order_date datetime, - cd4_order_date datetime, - hiv_dna_pcr_order_date datetime, - hiv_dna_pcr_resulted int, - hiv_dna_pcr_resulted_date datetime, - hiv_dna_pcr_1 int, - hiv_dna_pcr_1_date datetime, - hiv_dna_pcr_2 int, - hiv_dna_pcr_2_date datetime, - hiv_rapid_test_resulted int, - hiv_rapid_test_resulted_date datetime, - prev_encounter_datetime_hiv datetime, - next_encounter_datetime_hiv datetime, - prev_encounter_type_hiv mediumint, - next_encounter_type_hiv mediumint, - prev_clinical_datetime_hiv datetime, - next_clinical_datetime_hiv datetime, - prev_clinical_location_id mediumint, - next_clinical_location_id mediumint, - prev_clinical_rtc_date_hiv datetime, - next_clinical_rtc_date_hiv datetime, - outreach_date_bncd datetime, - outreach_death_date_bncd datetime, - outreach_patient_care_status_bncd int, - transfer_date_bncd datetime, - transfer_transfer_out_bncd datetime, - phone_outreach_bncd int, - home_outreach_bncd int, - outreach_missed_visit_reason int, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_hiv), - index encounter_type (encounter_type), - index date_created (date_created) - - ); - - - - - if(@query_type="build") then - select 'BUILDING..........................................'; - set @write_table = concat("flat_hiv_summary_temp_",queue_number); - set @queue_table = concat("flat_hiv_summary_test_build_queue_",queue_number); - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_hiv_summary_test_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from flat_hiv_summary_test_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - /* - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - */ - - drop temporary table if exists prev_rows; - create temporary table prev_rows like flat_hiv_summary_v15b; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_hiv_summary_v15b"; -set @write_table = "foo_flat_hiv_summary"; - -set @queue_table = "foo_flat_hiv_summary_sync_queue"; -#set @cycle_size=500; -#create table if not exists flat_hiv_summary_sync_queue (person_id int primary key); - -create table if not exists foo_flat_hiv_summary_sync_queue (person_id int primary key, min_datetime datetime, index person_date (person_id, min_datetime)); - - - set @last_update = null; - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; -#set @last_update = "2020-02-22 23:00"; - - - - - drop temporary table if exists mins; - create temporary table mins (person_id int, min_datetime datetime, index person_date (person_id, min_datetime)) - (select person_id, min(encounter_datetime) as min_datetime from etl.flat_obs where max_date_created> @last_update group by person_id); - - - insert into mins - (select person_id, min(obs_datetime) as min_obs_datetime - from amrs.obs where voided=1 and date_voided > @last_update and date_created <= @last_update group by person_id); - - insert into mins - (select patient_id, min(encounter_datetime) as min_encounter_datetime - from amrs.encounter where voided=1 and date_voided > @last_update and date_created <= @last_update group by patient_id); - - - insert into mins - (select patient_id, min(encounter_datetime) - from amrs.encounter - where date_changed > @last_update - group by patient_id - ); - - insert into mins - (select distinct person_id, min(test_datetime) - from etl.flat_lab_obs - where max_date_created > @last_update - group by person_id - ); - - insert into mins - (select person_id, min(encounter_datetime) - from etl.flat_orders - where max_date_created > @last_update - group by person_id - ); - - insert into mins - (select person_id, "1900-01-01" - from amrs.person - where date_changed > @last_update); - - - insert into mins - (select distinct person_id, "1900-01-01" from - amrs.person - where date_voided > @last_update - ); - - replace into foo_flat_hiv_summary_sync_queue - (select person_id, min(min_datetime) as min_datetime from mins group by person_id); - -/* - drop temporary table if exists prev_rows; - create temporary table prev_rows - (select * from flat_hiv_summary_v15b t1 -join foo_flat_hiv_summary_sync_queue t2 using (person_id) - where - t1.encounter_datetime < t2.min_datetime - and t1.is_clinical_encounter = 1 - and (t1.next_clinical_datetime_hiv >= t2.min_datetime or t1.next_clinical_datetime_hiv is null) - ); -*/ - - - drop temporary table if exists prev_rows; - create temporary table prev_rows - (select t1.person_id as p_id, - ifnull(t2.encounter_datetime,"1800-01-01") as min_datetime, - t2.* - from foo_flat_hiv_summary_sync_queue t1 -# left outer join flat_hiv_summary_v15b t2 -left outer join foo_flat_hiv_summary t2 - - on t1.person_id=t2.person_id - and t2.encounter_datetime < t1.min_datetime - and t2.is_clinical_encounter = 1 - and (t2.next_clinical_datetime_hiv >= t1.min_datetime or t2.next_clinical_datetime_hiv is null) - ); - - alter table prev_rows drop person_id, change column p_id person_id int; - - - drop temporary table if exists duplicates; - create temporary table duplicates - (select person_id, count(*) as n from prev_rows group by person_id having n > 1); - - delete t1 - from prev_rows t1 - join duplicates using (person_id) - where min_datetime != encounter_datetime; - - - - delete t1 - #from flat_hiv_summary_v15b t1 - #join flat_hiv_summary_sync_queue t2 using (person_id) -from foo_flat_hiv_summary t1 -join prev_rows t2 using (person_id) - where t1.encounter_datetime >= t2.min_datetime; - - - /* - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id) where t1.encounter_datetime >= t2.min_datetime ;'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - */ - - -#select encounter_datetime from etl.foo_flat_hiv_summary where person_id=1020; - end if; - - - - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - select @person_ids_count as 'num patients to sync'; - - - -/* - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; -*/ - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - - drop temporary table if exists flat_hiv_summary_build_queue__0; - -#create temporary table flat_hiv_summary_build_queue__0 (person_id int primary key) -#(select * from etl.flat_hiv_summary_sync_queue); - - SET @dyn_sql=CONCAT('create temporary table flat_hiv_summary_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - drop temporary table if exists flat_hiv_summary_0a; - create temporary table flat_hiv_summary_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - - 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,129,138,140,153,154,158,162,163) then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,115,117,120,127,128,138, 140, 153,154,158,162,163) then 10 - when t1.encounter_type in (129) then 5 - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join prev_rows t3 on t1.person_id=t3.person_id and t1.encounter_datetime >= t3.min_datetime - join flat_hiv_summary_build_queue__0 t0 on t1.person_id = t0.person_id - left outer join etl.flat_orders t2 on t1.encounter_id = t2.encounter_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) - AND NOT obs regexp "!!5303=(822|664|1067)!!" - AND NOT obs regexp "!!9082=9036!!" - ); - - - insert into flat_hiv_summary_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join prev_rows t3 on t1.person_id=t3.person_id and t1.test_datetime >= t3.min_datetime - join flat_hiv_summary_build_queue__0 t0 on t1.person_id = t0.person_id - - ); - - - - drop temporary table if exists flat_hiv_summary_0; - create temporary table flat_hiv_summary_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_date = null; - set @cur_encounter_date = null; - set @enrollment_date = null; - set @hiv_start_date = null; - set @cur_location = null; - set @cur_rtc_date = null; - set @prev_rtc_date = null; - set @hiv_start_date = null; - set @prev_arv_start_date = null; - set @arv_start_date = null; - set @prev_arv_end_date = null; - set @arv_start_location_id = null; - set @arv_first_regimen_start_date = null; - set @arv_first_regimen_start_date_flex = null; - set @arv_first_regimen = null; - set @prev_arv_line = null; - set @cur_arv_line = null; - set @prev_arv_adherence = null; - set @cur_arv_adherence = null; - set @hiv_status_disclosed = null; - set @is_pregnant = null; - set @edd = null; - set @prev_arv_meds = null; - set @cur_arv_meds = null; - set @ipt_start_date = null; - set @ipt_end_date = null; - set @ipt_completion_date = null; - - set @on_tb_tx = null; - set @tb_tx_start_date = null; - set @tb_tx_end_date = null; - set @pcp_prophylaxis_start_date = null; - set @tb_screen = null; - set @tb_screening_result = null; - set @tb_screening_datetime = null; - - set @death_date = null; - - set @patient_care_status=null; - - set @condoms_provided_date = null; - set @modern_contraceptive_method_start_date = null; - set @contraceptive_method = null; - - - set @cur_who_stage = null; - - set @vl_1=null; - set @vl_2=null; - set @vl_1_date=null; - set @vl_2_date=null; - set @vl_resulted=null; - set @vl_resulted_date=null; - - set @cd4_resulted=null; - set @cd4_resulted_date=null; - set @cd4_1=null; - set @cd4_1_date=null; - set @cd4_2=null; - set @cd4_2_date=null; - set @cd4_percent_1=null; - set @cd4_percent_1_date=null; - set @cd4_percent_2=null; - set @cd4_percent_2_date=null; - set @vl_order_date = null; - set @cd4_order_date = null; - - set @hiv_dna_pcr_order_date = null; - set @hiv_dna_pcr_1=null; - set @hiv_dna_pcr_2=null; - set @hiv_dna_pcr_1_date=null; - set @hiv_dna_pcr_2_date=null; - - set @hiv_rapid_test_resulted=null; - set @hiv_rapid_test_resulted_date= null; - - - - - - - drop temporary table if exists flat_hiv_summary_1; - create temporary table flat_hiv_summary_1 (index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.encounter_id, - @prev_encounter_date := date(@cur_encounter_date) as prev_encounter_date, - @cur_encounter_date := date(t1.encounter_datetime) as cur_encounter_date, - t1.encounter_datetime, - t1.encounter_type, - t1.is_clinical_encounter, - CASE - WHEN - (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7013=' - THEN - @enrollment_date:=REPLACE(REPLACE((SUBSTRING_INDEX(SUBSTRING(obs, LOCATE('!!7013=', obs)), - @sep, - 1)), - '!!7013=', - ''), - '!!', - '') - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:='1900-01-01' - WHEN - t1.encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - AND t2.enrollment_date is null - THEN - @enrollment_date:=DATE(t1.encounter_datetime) - WHEN @prev_id = @cur_id THEN @enrollment_date - ELSE @enrollment_date:= t2.enrollment_date - END AS enrollment_date, - - - - CASE - WHEN - (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7030=5622' - THEN - @enrollment_location_id:=9999 - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollmen_location_id:=9999 - WHEN - t1.encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - AND t2.enrollment_date is null - THEN - @enrollment_location_id:= t1.location_id - WHEN @prev_id = @cur_id THEN @enrollment_location_id - ELSE @enrollment_location_id:=t2.enrollment_location_id - END AS enrollment_location_id, - - - - - - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when t1.location_id then @cur_location := t1.location_id - when @prev_id = @cur_id then @cur_location - when t2.location_id then@cur_location := t2.location_id - else @cur_location := 16 - end as 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 - end as visit_num, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := t2.prev_rtc_date - end as prev_rtc_date, - - - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > t1.encounter_datetime,@cur_rtc_date,null) - when ifnull(t1.is_clinical_encounter=0,1) then @cur_rtc_date := t2.prev_rtc_date - else @cur_rtc_date := t2.rtc_date - end as cur_rtc_date, - - - - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504|1285)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - - - - - - - - - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when @prev_id != @cur_id or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := t1.encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := t1.encounter_datetime - else @death_date := t2.death_date - end - else @death_date - end as death_date, - - - - - - case - when @death_date <= t1.encounter_datetime then @patient_care_status := 159 - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - - case - when obs regexp "!!9203=" then @hiv_start_date := replace(replace((substring_index(substring(obs,locate("!!9203=",obs)),@sep,1)),"!!9203=",""),"!!","") - when obs regexp "!!7015=" then @hiv_start_date := "1900-01-01" - when @prev_id = @cur_id then @hiv_start_date - when t2.hiv_start_date is null then @hiv_start_date := date(t1.encounter_datetime) - else @hiv_start_date := t2.hiv_start_date - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null and @prev_id=@cur_id) then @arv_start_location_id := t1.location_id - when @prev_id = @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location_id := t1.location_id - when @prev_id = @cur_id then @arv_start_location_id - else @arv_start_location_id := t2.arv_start_location_id - end as arv_start_location_id, - - - case - when @prev_id=@cur_id and @cur_arv_meds is not null then @prev_arv_meds := @cur_arv_meds - when @prev_id=@cur_id then @prev_arv_meds - else @prev_arv_meds := t2.prev_arv_meds - end as prev_arv_meds, - - - - - #2154 : PATIENT REPORTED CURRENT ANTIRETROVIRAL TREATMENT - #2157 : PATIENT REPORTED PAST ANTIRETROVIRAL TREATMENT - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - when @prev_id = @cur_id then @cur_arv_meds - else @cur_arv_meds:= t2.cur_arv_meds - end as cur_arv_meds, - - /* - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - else null - end - */ - null as cur_arv_meds_strict, - - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := t2.prev_clinical_datetime_hiv - end as prev_clinical_datetime_hiv, - - case - when t1.is_clinical_encounter then @cur_clinical_datetime := t1.encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - when t2.is_clinical_encounter then @cur_clinical_datetime := t2.encounter_datetime - else @cur_clinical_datetime := t2.prev_clinical_datetime_hiv - end as cur_clinical_datetime, - - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := t2.prev_clinical_rtc_date_hiv - end as prev_clinical_rtc_date_hiv, - - - case - when t1.is_clinical_encounter then @cur_clinical_rtc_date := @cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - when t2.is_clinical_encounter AND t1.is_clinical_encounter then @cur_clinical_rtc_date := t2.rtc_date - else @cur_clinical_rtc_date:= t2.prev_clinical_rtc_date_hiv - end as cur_clinic_rtc_date, - - - CASE - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - AND t2.arv_first_regimen_start_date is null - THEN - @arv_first_regimen_start_date:=GetValues(obs,1499) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - AND t2.arv_first_regimen_start_date is null - THEN - @arv_first_regimen_start_date:='1900-01-01' - - WHEN - ((@arv_first_regimen_start_date IS NULL AND t2.arv_first_regimen_start_date is null) || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - AND t2.arv_first_regimen_start_date is null - THEN - @arv_first_regimen_start_date:=DATE(t1.encounter_datetime) - - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),t1.encounter_datetime) < 90) - AND t2.arv_first_regimen_start_date is null - THEN - @arv_first_regimen_start_date:= "1900-01-01" #DATE(t1.encounter_datetime) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),t1.encounter_datetime) < 90) - AND t2.arv_first_regimen_start_date is null - THEN - @arv_first_regimen_start_date:= "1900-01-01" # DATE(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - AND t2.arv_first_regimen_start_date is null - THEN - @arv_first_regimen_start_date:='1900-01-01' - WHEN @prev_id = @cur_id THEN @arv_first_regimen_start_date - else @arv_first_regimen_start_date := t2.arv_first_regimen_start_date - END AS arv_first_regimen_start_date, - - - - - /* - CASE - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_start_date_flex := GetValues(obs,1499) - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_start_date_flex :='1900-01-01' - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_start_date_flex:=DATE(t1.encounter_datetime) - - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen_start_date_flex :=date(t1.encounter_datetime) - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen_start_date_flex :=date(t1.encounter_datetime) - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_start_date_flex:='1900-01-01' - WHEN @prev_id = @cur_id THEN @arv_first_regimen_start_date_flex - WHEN @prev_id != @cur_id THEN @arv_first_regimen_start_date_flex:= t2.arv_first_regimen_start_date_flex - ELSE @arv_first_regimen_start_date_flex - END */ - null AS arv_first_regimen_start_date_flex, - - - -/* - case - when @arv_first_regimen is null and obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @arv_first_regimen := normalize_arvs(obs,'2157') - when obs regexp "!!7015=" and @arv_first_regimen is null then @arv_first_regimen := "unknown" - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id = @cur_id then @arv_first_regimen - when @prev_id != @cur_id then @arv_first_regimen := @cur_arv_meds - else "-1" - end as arv_first_regimen, -*/ - - CASE - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - AND t2.arv_first_regimen is null - THEN - @arv_first_regimen:= "unknown" - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - AND t2.arv_first_regimen is null - THEN - @arv_first_regimen:= "unknown" - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id ) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - AND t2.arv_first_regimen is null - THEN - @arv_first_regimen := @cur_arv_meds - - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),t1.encounter_datetime) < 90) - AND t2.arv_first_regimen is null - THEN - @arv_first_regimen:= "unknown" #@cur_arv_meds - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),t1.encounter_datetime) < 90) - AND t2.arv_first_regimen is null - - THEN - @arv_first_regimen:= "unknown" #@cur_arv_meds - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - AND t2.arv_first_regimen is null - THEN - @arv_first_regimen := "unknown" - - WHEN @prev_id = @cur_id THEN @arv_first_regimen - ELSE @arv_first_regimen:=t2.arv_first_regimen - END AS arv_first_regimen, - - -/* - case - when @arv_first_regimen is null and obs regexp "!!1499=" then @arv_first_regimen_location_id := 9999 - when @prev_id != @cur_id and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @arv_first_regimen_location_id is null and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @prev_id = @cur_id then @arv_first_regimen_location_id - when @prev_id != @cur_id then @arv_first_regimen_location_id := null - else "-1" - end as arv_first_regimen_location_id, -*/ - - CASE - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - AND t2.arv_first_regimen_location_id is null - THEN - @arv_first_regimen_location_id := 9999 - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - AND t2.arv_first_regimen_location_id is null - THEN - @arv_first_regimen_location_id:=9999 - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - AND t2.arv_first_regimen_location_id is null - THEN - @arv_first_regimen_location_id:=t1.location_id - - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),t1.encounter_datetime) < 90) - AND t2.arv_first_regimen_location_id is null - THEN - @arv_first_regimen_location_id:= 9999 #location_id - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),t1.encounter_datetime) < 90) - AND t2.arv_first_regimen_location_id is null - THEN - @arv_first_regimen_location_id:= 9999 #location_id - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - AND t2.arv_first_regimen_location_id is null - THEN - @arv_first_regimen_location_id:=9999 - WHEN @prev_id = @cur_id THEN @arv_first_regimen_location_id - ELSE @arv_first_regimen_location_id:=t2.arv_first_regimen_location_id - END AS arv_first_regimen_location_id, - - - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := t2.prev_arv_line - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=(6156)!!" then @cur_arv_line := 3 - when @prev_id = @cur_id then @cur_arv_line - else @cur_arv_line := t2.cur_arv_line - end as cur_arv_line, - - /* - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1250=(6156)!!" then 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1088=(6156)!!" then 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then 2 - when obs regexp "!!2154=(6156)!!" then 3 - else null - end - */ - null as cur_arv_line_strict, - - - - - - - - - - case - when obs regexp "!!6976=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6976=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6976=6695!!" then @cur_arv_line_reported := 3 - - when obs regexp "!!6744=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6744=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6744=6695!!" then @cur_arv_line_reported := 3 - when @prev_id = @cur_id then @cur_arv_line_reported - else @cur_arv_line_reported := t2.cur_arv_line_reported - end as cur_arv_line_reported, - - - - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := t2.prev_arv_start_date - end as prev_arv_start_date, - - - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null and @prev_id=@cur_id ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - - when @cur_arv_meds != @prev_arv_meds then @arv_start_date := date(t1.encounter_datetime) - when @prev_id = @cur_id then @arv_start_date - else @arv_start_date := t2.arv_start_date - end as arv_start_date, - - - # THIS INDICATOR MAY HAVE AN ERRONEOUS DEFINITION - case - when @prev_id = @cur_id then - case - when @prev_arv_start_date = @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end - else @prev_arv_end_date := t2.prev_arv_end_date - end as prev_arv_end_date, - - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := t2.prev_arv_adherence - end as prev_arv_adherence, - - - - - - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id = @cur_id then @cur_arv_adherence - else @cur_arv_adherence := t2.cur_arv_adherence - end as cur_arv_adherence, - - case - when obs regexp "!!6596=1118!!" then 0 - when obs regexp "!!6596=6594!!" then 1 - when obs regexp "!!6596=1267!!" then 2 - when obs regexp "!!6596=6595!!" then 3 - when obs regexp "!!6596=1067!!" then 4 - when obs regexp "!!6596=1175!!" then 5 - when @prev_id = @cur_id then @hiv_status_disclosed - else @hiv_status_disclosed := t2.hiv_status_disclosed - end as hiv_status_disclosed, - - - - - - case - when obs regexp "!!8351=(48|50|1066|1624|6971|9608)!!" then @is_pregnant := null - when @prev_id != @cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" or obs regexp "!!8351=(1065|1484)!!" then @is_pregnant := true - else @is_pregnant := t2.is_pregnant - end - when @is_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") or obs regexp "!!8351=(1065|1484)!!"then @is_pregnant := true - when @is_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@is_pregnant,t1.encounter_datetime) > 40 or timestampdiff(week,@edd,t1.encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @is_pregnant := null - else @is_pregnant - end as is_pregnant, - - - - - - - - - case - when @prev_id != @cur_id then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(t1.encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd := t2.edd - end - when @edd is null then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(t1.encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,t1.encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - - - case - when obs regexp "!!6174=" then @tb_screen := true - when obs regexp "!!2022=1065!!" then @tb_screen := true - when obs regexp "!!307=" then @tb_screen := true - when obs regexp "!!12=" then @tb_screen := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screen := true - when orders regexp "(12|307|8064|2311|2323)" then @tb_screen := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screen := true - when obs regexp "!!5958=1077!!" then @tb_screen := true - when obs regexp "!!2020=1065!!" then @tb_screen := true - when obs regexp "!!2021=1065!!" then @tb_screen := true - when obs regexp "!!2028=" then @tb_screen := true - when obs regexp "!!1268=(1256|1850)!!" then @tb_screen := true - when obs regexp "!!5959=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!5971=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!1492=107!!" then @tb_screen := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screen := true - end as tb_screen, - - case - when obs regexp "!!8292=" then @tb_screening_result := - replace(replace((substring_index(substring(obs,locate("!!8292=",obs)),@sep,1)),"!!8292=",""),"!!","") - when @prev_id = @cur_id then @tb_screening_result - else @tb_screening_result := t2.tb_screening_result - end as tb_screening_result, - - case - when obs regexp "!!6174=" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!2022=1065!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!307=" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!12=" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := t1.encounter_datetime - when orders regexp "(12|307|8064|2311|2323)" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!5958=1077!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!2020=1065!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!2021=1065!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!2028=" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!1268=(1256|1850)!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!5959=(1073|1074)!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!5971=(1073|1074)!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!1492=107!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screening_datetime := t1.encounter_datetime - when @cur_id = @prev_id then @tb_screening_datetime - else @tb_screening_datetime := t2.tb_screening_datetime - end as tb_screening_datetime, - - - case - when obs regexp "!!1265=(1107|1260)!!" then @on_ipt := 0 - when obs regexp "!!1265=!!" then @on_ipt := 1 - when obs regexp "!!1110=656!!" then @on_ipt := 1 - when @cur_id = @prev_id then @on_ipt - else @on_ipt := t2.on_ipt - end as on_ipt, - - - - case - when obs regexp "!!1265=(1256|1850)!!" then @ipt_start_date := t1.encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" and @ipt_start_date is null then @ipt_start_date := t1.encounter_datetime - when @cur_id = @prev_id then @ipt_start_date - else @ipt_start_date := t2.ipt_start_date - end as ipt_start_date, - - - - case - when obs regexp "!!1266=" then @ipt_stop_date := t1.encounter_datetime - when @cur_id = @prev_id then @ipt_stop_date - else @ipt_stop_date := t2.ipt_stop_date - end as ipt_stop_date, - - case - when obs regexp "!!1266=1267!!" then @ipt_completion_date := t1.encounter_datetime - when @cur_id = @prev_id then @ipt_completion_date - when @cur_id = @prev_id then @ipt_completion_date - else @ipt_completion_date := t2.ipt_completion_date - end as ipt_completion_date, - - - case - when obs regexp "!!1268=(1107|1260)!!" then @on_tb_tx := 0 - when obs regexp "!!1268=" then @on_tb_tx := 1 - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @on_tb_tx := 1 - else @on_tb_tx := 0 - end as on_tb_tx, - - - - case - when obs regexp "!!1113=" then @tb_tx_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @tb_tx_start_date := t1.encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" and obs regexp "!!7015=" and @tb_tx_start_date is null then @tb_tx_start_date := null - when obs regexp "!!1268=(1257|1259|1849|981)!!" and @tb_tx_start_date is null then @tb_tx_start_date := t1.encounter_datetime - - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" and @tb_tx_start_date is null then @tb_tx_start_date := t1.encounter_datetime - when @cur_id = @prev_id then @tb_tx_start_date - else @tb_tx_start_date := t2.tb_tx_start_date - end as tb_tx_start_date, - - - - - case - when obs regexp "!!2041=" then @tb_tx_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_tx_end_date := t1.encounter_datetime - when @cur_id = @prev_id then @tb_tx_end_date - else @tb_tx_end_date := t2.tb_tx_end_date - end as tb_tx_end_date, - - - - - - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := t1.encounter_datetime - when @prev_id = @cur_id then - case - when obs regexp "!!1261=1257!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := t1.encounter_datetime - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := t1.encounter_datetime - when obs regexp "!!1193=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := t1.encounter_datetime - else @pcp_prophylaxis_start_date - end - else @pcp_prophylaxis_start_date := t2.pcp_prophylaxis_start_date - end as pcp_prophylaxis_start_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(t1.encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - when - t1.encounter_type = @lab_encounter_type - and obs regexp "!!5497=[0-9]" - and t2.cd4_1 >= 0 - and date(t1.encounter_datetime)<>t2.cd4_1_date - then @cd4_2:= t2.cd4_1 - - else @cd4_2:=t2.cd4_2 - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - - when t1.encounter_type=@lab_encounter_type - and obs regexp "!!5497=[0-9]" - and t2.cd4_1 >= 0 - then @cd4_2_date:= t2.cd4_1_date - - else @cd4_2_date:=t2.cd4_2_date - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(t1.encounter_datetime) - when @prev_id = @cur_id and date(t1.encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(t1.encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=t2.cd4_1 - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(t1.encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=t2.cd4_1_date - end as cd4_1_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - when t1.encounter_type=@lab_encounter_type - and obs regexp "!!730=[0-9]" - and t2.cd4_percent_1 >= 0 - then @cd4_percent_2:= t2.cd4_percent_1 - - else @cd4_percent_2:=t2.cd4_percent_2 - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - - when obs regexp "!!730=[0-9]" - and t1.encounter_type = @lab_encounter_type - and t2.cd4_percent_1 >= 0 - then @cd4_percent_2_date:= t2.cd4_percent_1_date - - else @cd4_percent_2_date:=t2.cd4_percent_2_date - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=t2.cd4_percent_1 - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(t1.encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=t2.cd4_percent_1_date - end as cd4_percent_1_date, - - - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,t1.encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <> date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - when obs regexp "!!856=[0-9]" - and t2.vl_1 >= 0 - - and - if(obs_datetimes is null,t1.encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <>date(t2.vl_1_date) - then @vl_2:= t2.vl_1 - else @vl_2:=t2.vl_2 - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,t1.encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - when obs regexp "!!856=[0-9]" - and t2.vl_1 >= 0 - - and - if(obs_datetimes is null,t1.encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <>date(t2.vl_1_date) - - then @vl_2_date:= t2.vl_1_date - - else @vl_2_date:=t2.vl_2_date - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(t1.encounter_datetime) - when @prev_id = @cur_id and date(t1.encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(t1.encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=t2.vl_1 - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= t1.encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date := t2.vl_1_date - end as vl_1_date, - - - - - - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(t1.encounter_datetime) - when orders regexp "856" then @vl_order_date := date(t1.encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - when @prev_id != @cur_id then @vl_order_date := t2.vl_order_date - else @vl_order_date := null - end as vl_order_date, - - - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(t1.encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(t1.encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := t2.cd4_order_date - end as cd4_order_date, - - - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(t1.encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(t1.encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := t2.hiv_dna_pcr_order_date - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then t1.encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(t1.encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - - when t1.encounter_type = @lab_encounter_type - and obs regexp "!!1030=[0-9]" - and t2.hiv_dna_pcr_1 >= 0 - and date(t1.encounter_datetime)<>t2.hiv_dna_pcr_1_date - then @hiv_dna_pcr_2:= t2.hiv_dna_pcr_1 - - - when obs regexp "!!1030=[0-9]" - and t2.hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),t2.hiv_dna_pcr_1_date)) > 30 - then @hiv_dna_pcr_2 := t2.hiv_dna_pcr_1 - - - else @hiv_dna_pcr_2:=t2.hiv_dna_pcr_2 - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(t1.encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - - when t1.encounter_type=@lab_encounter_type - and obs regexp "!!1030=[0-9]" - and t2.hiv_dna_pcr_1 >= 0 - and date(t1.encounter_datetime)<>t2.hiv_dna_pcr_1_date - then @hiv_dna_pcr_2_date:= t2.hiv_dna_pcr_1_date - - when obs regexp "!!1030=[0-9]" - and t2.hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),t2.hiv_dna_pcr_1_date)) > 30 - then @hiv_dna_pcr_2_date:= t2.hiv_dna_pcr_1_date - - else @hiv_dna_pcr_2_date:=t2.hiv_dna_pcr_2_date - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=t2.hiv_dna_pcr_1 - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(t1.encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=t2.hiv_dna_pcr_1_date - end as hiv_dna_pcr_1_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then t1.encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - case - when obs regexp "!!8302=8305!!" then @condoms_provided_date := t1.encounter_datetime - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided_date := t1.encounter_datetime - when obs regexp "!!6579=" then @condoms_provided_date := t1.encounter_datetime - when @prev_id = @cur_id then @condoms_provided_date - else @condoms_provided_date := t2.condoms_provided_date - end as condoms_provided_date, - - - - - - - case - when obs regexp "!!7240=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(t1.encounter_datetime) - when obs regexp "!!7240=!!" - then @modern_contraceptive_method_start_date := null - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" and obs regexp "!!1190=" - then @modern_contraceptive_method_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1190=",obs)),@sep,1)),"!!1190=",""),"!!","")) - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(t1.encounter_datetime) - when obs regexp "!!374=!!" - then @modern_contraceptive_method_start_date := null - when @prev_id = @cur_id then @modern_contraceptive_method_start_date - else @modern_contraceptive_method_start_date := t2.modern_contraceptive_method_start_date - end as modern_contraceptive_method_start_date, - - - case - when obs regexp "!!7240=1107!!" then @contraceptive_method := null - when obs regexp "!!7240=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!7240=",obs)),@sep,1)),"!!7240=",""),"!!","") - when obs regexp "!!374=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!374=",obs)),@sep,1)),"!!374=",""),"!!","") - when @prev_id = @cur_id then @contraceptive_method - else @contraceptive_method := t2.contraceptive_method - end as contraceptive_method, - - - - - - - case - when obs regexp "!!5356=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!8287=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!8287=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!8287=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!8287=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!8307=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!8307=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!8307=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!8307=(1207|1223)!!" then @cur_who_stage := 4 - when @prev_id = @cur_id then @cur_who_stage - else @cur_who_stage := t2.cur_who_stage - end as cur_who_stage, - - - case - when obs regexp "!!6096=1065" then @discordant_status := "Yes" - when obs regexp "!!6096=1066" then @discordant_status := "No" - when obs regexp "!!6096=1067" then @discordant_status := "Unknown" - when obs regexp "!!6096=1175" then @discordant_status := "N/A" - when obs regexp "!!6096=6826" then @discordant_status := "Concordant Couple Positive" - when obs regexp "!!6096=6827" then @discordant_status := "Concordant Couple Negative" - when @prev_id = @cur_id then @discordant_status - else @discordant_status := t2.discordant_status - end as discordant_status - - - - - from flat_hiv_summary_0 t1 - join amrs.person p using (person_id) - left outer join prev_rows t2 using (person_id) - ); - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_hiv_summary_1 drop prev_id, drop cur_id, drop cur_clinical_datetime, drop cur_clinic_rtc_date; - - drop temporary table if exists flat_hiv_summary_1b; - create temporary table flat_hiv_summary_1b - (select * from flat_hiv_summary_1 t1 - order by person_id, date(t1.encounter_datetime) desc, encounter_type_sort_index desc - ); - - - drop table if exists flat_hiv_summary_2; - create temporary table flat_hiv_summary_2 - (select t1.*, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := t1.encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := t1.encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id, - - case - when t1.is_clinical_encounter then @cur_clinical_datetime := t1.encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when t1.is_clinical_encounter then @cur_clinical_location_id := t1.location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when t1.is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @prev_id != @cur_id then null - when t1.is_clinical_encounter then @outreach_date_bncd - else null - end as outreach_date_bncd, - - case - when t1.encounter_type=21 and @outreach_date_bncd is null then @outreach_date_bncd := t1.encounter_datetime - when t1.is_clinical_encounter then @outreach_date_bncd := null - when @prev_id = @cur_id then @outreach_date_bncd - else @outreach_date_bncd := t2.outreach_date_bncd - end as next_outreach_date_bncd, - - case - when @prev_id = @cur_id and t1.is_clinical_encounter then @outreach_death_date_bncd - when @prev_id = @cur_id then null - else null - end as outreach_death_date_bncd, - - case - when t1.encounter_type=21 and @outreach_death_date_bncd is null then @outreach_death_date_bncd := t1.death_date - when t1.is_clinical_encounter then @outreach_death_date_bncd := null - when @prev_id = @cur_id then @outreach_death_date_bncd - else @outreach_death_date_bncd := t2.outreach_death_date_bncd - end as next_outreach_death_date_bncd, - - - case - when @prev_id != @cur_id then null - when t1.is_clinical_encounter then cast(@outreach_patient_care_status_bncd as unsigned) - else null - end as outreach_patient_care_status_bncd, - - case - when t1.encounter_type=21 and @outreach_patient_care_status_bncd is null then @outreach_patient_care_status_bncd := t1.patient_care_status - when t1.is_clinical_encounter then @outreach_patient_care_status_bncd := null - when @prev_id = @cur_id then @outreach_patient_care_status_bncd - else @outreach_patient_care_status_bncd := null - end as next_outreach_patient_care_status_bncd, - - case - when @prev_id != @cur_id then null - when t1.is_clinical_encounter then @transfer_date_bncd - else null - end as transfer_date_bncd, - - case - when t1.encounter_type=116 and @transfer_date_bncd is null then @transfer_date_bncd := t1.encounter_datetime - when t1.is_clinical_encounter then @transfer_date_bncd := null - when @prev_id = @cur_id then @transfer_date_bncd - else @transfer_date_bncd := null - end as next_transfer_date_bncd, - - case - when @prev_id != @cur_id then null - when t1.is_clinical_encounter then @transfer_transfer_out_bncd - else null - end as transfer_transfer_out_bncd, - - case - when t1.encounter_type=116 and @transfer_transfer_out_bncd is null then @transfer_transfer_out_bncd := t1.encounter_datetime - when t1.is_clinical_encounter then @transfer_transfer_out_bncd := null - when @prev_id = @cur_id then @transfer_transfer_out_bncd - else @transfer_transfer_out_bncd := null - end as next_transfer_transfer_out_bncd, - case - when @prev_id != @cur_id then null - when t1.is_clinical_encounter then @phone_outreach_bncd - else null - end as phone_outreach_bncd, - case - when t1.encounter_type=21 and @phone_outreach_bncd is null then - case - when t1.obs regexp "!!9063=1065!!" AND obs regexp "!!9600=1065!!" then @phone_outreach_bncd:= 1 - when t1.obs regexp "!!1569=1555!!" AND obs regexp "!!9600=1065!!" then @phone_outreach_bncd:= 1 - when t1.obs regexp "!!1558=1555!!" AND obs regexp "!!9600=1065!!" then @phone_outreach_bncd:= 1 - when t1.obs regexp "!!9063=(1065|1560|9064|9065|9066|5622)!!" AND obs regexp "!!9600=1066!!" then @phone_outreach_bncd:= 2 - when t1.obs regexp "!!1558=1555!!" AND obs regexp "!!9600=1066!!" then @phone_outreach_bncd:= 2 - else @phone_outreach_bncd:= null - end - when t1.is_clinical_encounter then @phone_outreach_bncd := null - when @prev_id != @cur_id then @phone_outreach_bncd := null - else @phone_outreach_bncd - end as next_phone_outreach_bncd, - case - when @prev_id != @cur_id then null - when t1.is_clinical_encounter then @home_outreach_bncd - else null - end as home_outreach_bncd, - - case - when t1.encounter_type=21 and @home_outreach_bncd is null then - case - when obs regexp "!!10085=1065!!" AND obs regexp "!!1558=(7066|6116|1556|1557)!!" AND obs regexp "!!1559=1065!!" then @home_outreach_bncd:= 1 - when obs regexp "!!1558=7066!!" and obs regexp "!!1559=1065!!" then @home_outreach_bncd := 1 - when obs regexp "!!10085=1065!!" AND "!!1558=(7066|6116|1556|1557)!!" AND obs regexp "!!1559=1066!!" then @home_outreach_bncd := 2 - else @home_outreach_bncd:= null - end - when t1.is_clinical_encounter then @home_outreach_bncd := null - when @prev_id != @cur_id then @home_outreach_bncd := null - else @home_outreach_bncd - end as next_home_outreach_bncd, - - case - when @prev_id != @cur_id then null - when t1.is_clinical_encounter then @outreach_missed_visit_reason - else null - end as outreach_missed_visit_reason, - - case - when t1.encounter_type=21 and @outreach_missed_visit_reason is null then - case - when obs regexp "!!10350=1065!!" then @outreach_missed_visit_reason := replace(replace((substring_index(substring(obs,locate("!!1573=",obs)),@sep,1)),"!!1573=",""),"!!","") - when obs regexp "!!9082=" then @outreach_missed_visit_reason := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - else @outreach_missed_visit_reason:= null - end - when t1.is_clinical_encounter then @outreach_missed_visit_reason := null - when @prev_id != @cur_id then @outreach_missed_visit_reason := null - else @outreach_missed_visit_reason - end as next_outreach_missed_visit_reason - - from flat_hiv_summary_1b t1 - left outer join prev_rows t2 using (person_id) - order by person_id, date(t1.encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - drop temporary table if exists flat_hiv_summary_2b; - create temporary table flat_hiv_summary_2b - (select * from flat_hiv_summary_2 t1 order by person_id, date(t1.encounter_datetime) asc, encounter_type_sort_index); - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_hiv_summary_3; - create temporary table flat_hiv_summary_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - t1.*, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:= t2.prev_encounter_type_hiv - end as prev_encounter_type_hiv, - - @cur_encounter_type := t1.encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := t2.prev_encounter_datetime_hiv - end as prev_encounter_datetime_hiv, - - @cur_encounter_datetime := t1.encounter_datetime as cur_encounter_datetime, - -/* Moved to flat_hiv_summary_1 - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, -*/ - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - #when t2.is_clinical_encounter then @prev_clinical_location_id := t2.location_id - else @prev_clinical_location_id := t2.prev_clinical_location_id - end as prev_clinical_location_id, - - - case - when t1.is_clinical_encounter then @cur_clinical_location_id := t1.location_id - when @prev_id = @cur_id then @cur_clinical_location_id - - when t2.is_clinical_encounter then @cur_clinical_location_id := t2.location_id - else @cur_clinical_location_id := t2.prev_clinical_location_id - end as cur_clinical_location_id - -/* MOVED to flat_hiv_summary_1 - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date -*/ - from flat_hiv_summary_2b t1 - left outer join prev_rows t2 using (person_id) - order by person_id, t1.encounter_datetime asc, encounter_type_sort_index - ); - - alter table flat_hiv_summary_3 drop prev_id, drop cur_id; - - set @prev_id = null; - set @cur_id = null; - set @transfer_in = null; - set @transfer_in_date = null; - set @transfer_in_location_id = null; - set @transfer_out = null; - set @transfer_out_date = null; - set @transfer_out_location_id = null; - - drop temporary table if exists flat_hiv_summary_4; - create temporary table flat_hiv_summary_4 ( index person_enc (person_id, encounter_datetime)) - (select - t1.*, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - - - case - when obs regexp "!!7015=" then @transfer_in := 1 - when t1.prev_clinical_location_id != t1.location_id then @transfer_in := 1 - else @transfer_in := null - end as transfer_in, - - case - when obs regexp "!!7015=" then @transfer_in_date := date(t1.encounter_datetime) - when t1.prev_clinical_location_id != t1.location_id then @transfer_in_date := date(t1.encounter_datetime) - when @cur_id = @prev_id then @transfer_in_date - else @transfer_in_date := t2.transfer_in_date - end transfer_in_date, - - case - when obs regexp "!!7015=1287" then @transfer_in_location_id := 9999 - when t1.prev_clinical_location_id != t1.location_id then @transfer_in_location_id := t1.prev_clinical_location_id - when @cur_id = @prev_id then @transfer_in_location_id - else @transfer_in_location_id := t2.transfer_in_location_id - end transfer_in_location_id, - - - - - case - when obs regexp "!!1285=!!" then @transfer_out := 1 - when obs regexp "!!1596=1594!!" then @transfer_out := 1 - when obs regexp "!!9082=(1287|1594|9068|9504|1285)!!" then @transfer_out := 1 - when t1.next_clinical_location_id != t1.location_id then @transfer_out := 1 - else @transfer_out := t2.transfer_out - end as transfer_out, - - case - when obs regexp "!!1285=(1287|9068|2050)!!" and t1.next_clinical_datetime_hiv is null then @transfer_out_location_id := 9999 - when obs regexp "!!1285=1286!!" and t1.next_clinical_datetime_hiv is null then @transfer_out_location_id := 9998 - when t1.next_clinical_location_id != t1.location_id then @transfer_out_location_id := t1.next_clinical_location_id - else @transfer_out_location_id := t2.transfer_out_location_id - end transfer_out_location_id, - - - case - when @transfer_out and t1.next_clinical_datetime_hiv is null then @transfer_out_date := date(t1.cur_rtc_date) - when t1.next_clinical_location_id != t1.location_id then @transfer_out_date := date(t1.next_clinical_datetime_hiv) - when t1.transfer_transfer_out_bncd then @transfer_out_date := date(t1.transfer_transfer_out_bncd) - else @transfer_out_date := t2.transfer_out_date - end transfer_out_date - - - - from flat_hiv_summary_3 t1 - left outer join prev_rows t2 using (person_id) - order by person_id, date(t1.encounter_datetime), encounter_type_sort_index - ); - - - select count(*) into @new_encounter_rows from flat_hiv_summary_4; - - select @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; - select @total_rows_written; - - -#select encounter_id, encounter_datetime from flat_hiv_summary_4; - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - t1.uuid, - visit_id, - encounter_id, - encounter_datetime, - encounter_type, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - visit_num, - enrollment_date, - enrollment_location_id, - hiv_start_date, - death_date, - scheduled_visit, - transfer_in, - transfer_in_location_id, - transfer_in_date, - transfer_out, - transfer_out_location_id, - transfer_out_date, - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date as rtc_date, - arv_first_regimen, - arv_first_regimen_location_id, - arv_first_regimen_start_date, - arv_first_regimen_start_date_flex, - prev_arv_meds, - cur_arv_meds, - cur_arv_meds_strict, - arv_start_date, - arv_start_location_id, - prev_arv_start_date, - prev_arv_end_date, - prev_arv_line, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - is_pregnant, - edd, - tb_screen, - tb_screening_result, - tb_screening_datetime, - on_ipt, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - on_tb_tx, - tb_tx_start_date, - tb_tx_end_date, - pcp_prophylaxis_start_date, - condoms_provided_date, - modern_contraceptive_method_start_date, - contraceptive_method, - cur_who_stage, - discordant_status, - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - vl_order_date, - cd4_order_date, - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_location_id, - next_clinical_location_id, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv, - outreach_date_bncd, - outreach_death_date_bncd, - outreach_patient_care_status_bncd, - transfer_date_bncd, - transfer_transfer_out_bncd, - phone_outreach_bncd, - home_outreach_bncd, - outreach_missed_visit_reason - from flat_hiv_summary_4 t1 - left outer join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_hiv_summary_build_queue__0 t2 using (person_id);'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - - select @person_ids_count as 'persons remaining', @cycle_length as 'Cycle time (s)', ceil(@person_ids_count / cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - - select - count(*) as t, - count(if(min_datetime = "1800-01-01",1,null)) as total_rebuild - from prev_rows; - - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_v15_11.sql b/etl-scripts/stored-procedures/generate_hiv_summary_v15_11.sql deleted file mode 100644 index a8a2815..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_v15_11.sql +++ /dev/null @@ -1,2061 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_v15_11`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_hiv_summary_v15b"; - set @query_type = query_type; - set @queue_table = ""; - set @total_rows_written = 0; - - set @start = now(); - set @table_version = "flat_hiv_summary_v2.18"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - -CREATE TABLE IF NOT EXISTS flat_hiv_summary_v15b ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id INT, - uuid VARCHAR(100), - visit_id INT, - visit_type SMALLINT, - encounter_id INT, - encounter_datetime DATETIME, - encounter_type INT, - is_transit INT, - is_clinical_encounter INT, - location_id INT, - location_uuid VARCHAR(100), - visit_num INT, - enrollment_date DATETIME, - enrollment_location_id INT, - hiv_start_date DATETIME, - death_date DATETIME, - scheduled_visit INT, - transfer_in TINYINT, - transfer_in_location_id INT, - transfer_in_date DATETIME, - transfer_out TINYINT, - transfer_out_location_id INT, - transfer_out_date DATETIME, - patient_care_status INT, - out_of_care INT, - prev_rtc_date DATETIME, - rtc_date DATETIME, - arv_first_regimen VARCHAR(500), - arv_first_regimen_location_id INT, - arv_first_regimen_start_date DATETIME, - arv_first_regimen_start_date_flex DATETIME, - prev_arv_meds VARCHAR(500), - cur_arv_meds VARCHAR(500), - cur_arv_meds_strict VARCHAR(500), - cur_arv_drugs VARCHAR(500), - prev_arv_drugs VARCHAR(500), - arv_start_date DATETIME, - arv_start_location_id INT, - prev_arv_start_date DATETIME, - prev_arv_end_date DATETIME, - prev_arv_line INT, - cur_arv_line INT, - cur_arv_line_strict INT, - cur_arv_line_reported TINYINT, - prev_arv_adherence VARCHAR(200), - cur_arv_adherence VARCHAR(200), - hiv_status_disclosed INT, - is_pregnant BOOLEAN, - edd DATETIME, - tb_screen BOOLEAN, - tb_screening_result INT, - tb_screening_datetime DATETIME, - on_ipt BOOLEAN, - ipt_start_date DATETIME, - ipt_stop_date DATETIME, - ipt_completion_date DATETIME, - on_tb_tx BOOLEAN, - tb_tx_start_date DATETIME, - tb_tx_end_date DATETIME, - pcp_prophylaxis_start_date DATETIME, - condoms_provided_date DATETIME, - modern_contraceptive_method_start_date DATETIME, - contraceptive_method INT, - cur_who_stage INT, - discordant_status INT, - cd4_resulted DOUBLE, - cd4_resulted_date DATETIME, - cd4_1 DOUBLE, - cd4_1_date DATETIME, - cd4_2 DOUBLE, - cd4_2_date DATETIME, - cd4_percent_1 DOUBLE, - cd4_percent_1_date DATETIME, - cd4_percent_2 DOUBLE, - cd4_percent_2_date DATETIME, - vl_resulted INT, - vl_resulted_date DATETIME, - vl_1 INT, - vl_1_date DATETIME, - vl_2 INT, - vl_2_date DATETIME, - expected_vl_date SMALLINT, - vl_order_date DATETIME, - cd4_order_date DATETIME, - hiv_dna_pcr_order_date DATETIME, - hiv_dna_pcr_resulted INT, - hiv_dna_pcr_resulted_date DATETIME, - hiv_dna_pcr_1 INT, - hiv_dna_pcr_1_date DATETIME, - hiv_dna_pcr_2 INT, - hiv_dna_pcr_2_date DATETIME, - hiv_rapid_test_resulted INT, - hiv_rapid_test_resulted_date DATETIME, - prev_encounter_datetime_hiv DATETIME, - next_encounter_datetime_hiv DATETIME, - prev_encounter_type_hiv MEDIUMINT, - next_encounter_type_hiv MEDIUMINT, - prev_clinical_datetime_hiv DATETIME, - next_clinical_datetime_hiv DATETIME, - prev_clinical_location_id MEDIUMINT, - next_clinical_location_id MEDIUMINT, - prev_clinical_rtc_date_hiv DATETIME, - next_clinical_rtc_date_hiv DATETIME, - outreach_date_bncd DATETIME, - outreach_death_date_bncd DATETIME, - outreach_patient_care_status_bncd INT, - transfer_date_bncd DATETIME, - transfer_transfer_out_bncd DATETIME, - PRIMARY KEY encounter_id (encounter_id), - INDEX person_date (person_id , encounter_datetime), - INDEX person_uuid (uuid), - INDEX location_enc_date (location_uuid , encounter_datetime), - INDEX enc_date_location (encounter_datetime , location_uuid), - INDEX location_id_rtc_date (location_id , rtc_date), - INDEX location_uuid_rtc_date (location_uuid , rtc_date), - INDEX loc_id_enc_date_next_clinical (location_id , encounter_datetime , next_clinical_datetime_hiv), - INDEX encounter_type (encounter_type), - INDEX date_created (date_created) -); - - - - 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 @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_hiv_summary_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from flat_hiv_summary_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_hiv_summary_v15b"; - set @queue_table = "flat_hiv_summary_sync_queue"; -CREATE TABLE IF NOT EXISTS flat_hiv_summary_sync_queue ( - person_id INT PRIMARY KEY -); - - - - set @last_update = null; -SELECT - MAX(date_updated) -INTO @last_update FROM - etl.flat_log -WHERE - table_name = @table_version; - - replace into flat_hiv_summary_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - -SELECT @person_ids_count AS 'num patients to sync'; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - - drop temporary table if exists flat_hiv_summary_build_queue__0; - - - - SET @dyn_sql=CONCAT('create temporary table flat_hiv_summary_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - drop temporary table if exists flat_hiv_summary_0a; - create temporary table flat_hiv_summary_0a - (select - t1.person_id, - t1.visit_id, - v.visit_type_id as visit_type, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - case - when v.visit_type_id IS NULL then NULL - when v.visit_type_id = 24 then 1 - when v.visit_type_id IS NOT NULL AND v.visit_type_id != 24 THEN 0 - else null - end as is_transit, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - - 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) then 1 - when t1.encounter_type in (186) AND v.visit_type_id not in (24,25,80) AND v.visit_type_id is NOT NULL then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,115,117,120,127,128,138, 140, 153,154,158,162,163,186) then 10 - when t1.encounter_type in (129) then 5 - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - 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,186) - AND NOT obs regexp "!!5303=(822|664|1067)!!" - AND NOT obs regexp "!!9082=9036!!" - ); - - insert into flat_hiv_summary_0a - (select - t1.person_id, - null, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - ); - - drop temporary table if exists flat_hiv_summary_0; - create temporary table flat_hiv_summary_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @prev_id = -1; - set @cur_id = -1; - set @prev_encounter_date = null; - set @cur_encounter_date = null; - set @enrollment_date = null; - set @hiv_start_date = null; - set @cur_location = null; - set @cur_rtc_date = null; - set @prev_rtc_date = null; - set @hiv_start_date = null; - set @prev_arv_start_date = null; - set @arv_start_date = null; - set @prev_arv_end_date = null; - set @arv_start_location_id = null; - set @arv_first_regimen_start_date = null; - set @arv_first_regimen_start_date_flex = null; - set @arv_first_regimen = null; - set @prev_arv_line = null; - set @cur_arv_line = null; - set @prev_arv_adherence = null; - set @cur_arv_adherence = null; - set @hiv_status_disclosed = null; - set @is_pregnant = null; - set @edd = null; - set @prev_arv_meds = null; - set @cur_arv_meds = null; - set @cur_arv_drugs = null; - set @prev_arv_drugs = null; - set @ipt_start_date = null; - set @ipt_end_date = null; - set @ipt_completion_date = null; - - set @on_tb_tx = null; - set @tb_tx_start_date = null; - set @tb_tx_end_date = null; - set @pcp_prophylaxis_start_date = null; - set @tb_screen = null; - set @tb_screening_result = null; - set @tb_screening_datetime = null; - - set @death_date = null; - - set @patient_care_status=null; - - set @condoms_provided_date = null; - set @modern_contraceptive_method_start_date = null; - set @contraceptive_method = null; - - - set @cur_who_stage = null; - - set @vl_1=null; - set @vl_2=null; - set @vl_1_date=null; - set @vl_2_date=null; - set @vl_resulted=null; - set @vl_resulted_date=null; - - set @cd4_resulted=null; - set @cd4_resulted_date=null; - set @cd4_1=null; - set @cd4_1_date=null; - set @cd4_2=null; - set @cd4_2_date=null; - set @cd4_percent_1=null; - set @cd4_percent_1_date=null; - set @cd4_percent_2=null; - set @cd4_percent_2_date=null; - set @vl_order_date = null; - set @cd4_order_date = null; - - set @hiv_dna_pcr_order_date = null; - set @hiv_dna_pcr_1=null; - set @hiv_dna_pcr_2=null; - set @hiv_dna_pcr_1_date=null; - set @hiv_dna_pcr_2_date=null; - - set @hiv_rapid_test_resulted=null; - set @hiv_rapid_test_resulted_date= null; - - - - - - - drop temporary table if exists flat_hiv_summary_1; - create temporary table flat_hiv_summary_1 (index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.visit_type, - t1.encounter_id, - @prev_encounter_date := date(@cur_encounter_date) as prev_encounter_date, - @cur_encounter_date := date(encounter_datetime) as cur_encounter_date, - t1.encounter_datetime, - t1.encounter_type, - t1.is_transit, - t1.is_clinical_encounter, - CASE - WHEN - (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7013=' - THEN - @enrollment_date:=REPLACE(REPLACE((SUBSTRING_INDEX(SUBSTRING(obs, LOCATE('!!7013=', obs)), - @sep, - 1)), - '!!7013=', - ''), - '!!', - '') - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:='1900-01-01' - WHEN - t1.encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:=DATE(encounter_datetime) - WHEN @prev_id = @cur_id THEN @enrollment_date - ELSE @enrollment_date:=NULL - END AS enrollment_date, - - - - CASE - WHEN - (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7030=5622' - THEN - @enrollment_location_id:=9999 - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollmen_location_id:=9999 - WHEN - encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_location_id:= location_id - WHEN @prev_id = @cur_id THEN @enrollment_location_id - ELSE @enrollment_location_id:=NULL - END AS enrollment_location_id, - - - - - - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as 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 - end as visit_num, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - - - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504|1285)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - - - - - - - - - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when @prev_id != @cur_id or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := encounter_datetime - else @death_date := null - end - else @death_date - end as death_date, - - - - - - case - when @death_date <= encounter_datetime then @patient_care_status := 159 - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - - case - when obs regexp "!!9203=" then @hiv_start_date := replace(replace((substring_index(substring(obs,locate("!!9203=",obs)),@sep,1)),"!!9203=",""),"!!","") - when obs regexp "!!7015=" then @hiv_start_date := "1900-01-01" - when @hiv_start_date is null then @hiv_start_date := date(encounter_datetime) - when @prev_id = @cur_id then @hiv_start_date - else @hiv_start_date := null - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_location_id := location_id - when @prev_id = @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location_id := location_id - when @prev_id != @cur_id then @arv_start_location_id := null - else @arv_start_location_id - end as arv_start_location_id, - - - case - when @prev_id=@cur_id and @cur_arv_meds is not null then @prev_arv_meds := @cur_arv_meds - when @prev_id=@cur_id then @prev_arv_meds - else @prev_arv_meds := null - end as prev_arv_meds, - - - - - #2154 : PATIENT REPORTED CURRENT ANTIRETROVIRAL TREATMENT - #2157 : PATIENT REPORTED PAST ANTIRETROVIRAL TREATMENT - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - when @prev_id = @cur_id then @cur_arv_meds - else @cur_arv_meds:= null - end as cur_arv_meds, - - - case - when 1 then null - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - else null - end as cur_arv_meds_strict, - - case - - when @prev_id=@cur_id and @prev_arv_drugs is not null then @prev_arv_drugs := @cur_arv_drugs - when @prev_id=@cur_id then @prev_arv_drugs - else @prev_arv_drugs := null - - end as prev_arv_drugs, - - case - - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_drugs := null - when obs regexp "!!1250=" then @cur_arv_drugs := normalizeArvsDrugFormulation(obs) - - - when obs regexp "!!1088=" then @cur_arv_drugs := normalizeArvsDrugFormulation(obs) - - when obs regexp "!!2154=" then @cur_arv_drugs := normalizeArvsDrugFormulation(obs) - - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_drugs := normalizeArvsDrugFormulation(obs) - - when @prev_id = @cur_id then @cur_arv_drugs - else @cur_arv_drugs:= null - - end as cur_arv_drugs, - - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := @cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date, - - - CASE - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_start_date:=GetValues(obs,1499) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_start_date:='1900-01-01' - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_start_date:=DATE(encounter_datetime) - - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_start_date:= "1900-01-01" #DATE(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_start_date:= "1900-01-01" # DATE(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_start_date:='1900-01-01' - WHEN @prev_id = @cur_id THEN @arv_first_regimen_start_date - WHEN @prev_id != @cur_id THEN @arv_first_regimen_start_date:=NULL - ELSE @arv_first_regimen_start_date - END AS arv_first_regimen_start_date, - - - - - /* - CASE - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_start_date_flex := GetValues(obs,1499) - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_start_date_flex :='1900-01-01' - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_start_date_flex:=DATE(encounter_datetime) - - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen_start_date_flex :=date(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen_start_date_flex :=date(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_start_date_flex:='1900-01-01' - WHEN @prev_id = @cur_id THEN @arv_first_regimen_start_date_flex - WHEN @prev_id != @cur_id THEN @arv_first_regimen_start_date_flex:=NULL - ELSE @arv_first_regimen_start_date_flex - END - */ - null AS arv_first_regimen_start_date_flex, - - - -/* - case - when @arv_first_regimen is null and obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @arv_first_regimen := normalize_arvs(obs,'2157') - when obs regexp "!!7015=" and @arv_first_regimen is null then @arv_first_regimen := "unknown" - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id = @cur_id then @arv_first_regimen - when @prev_id != @cur_id then @arv_first_regimen := @cur_arv_meds - else "-1" - end as arv_first_regimen, -*/ - - CASE - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen:= "unknown" - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen:= "unknown" - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen := @cur_arv_meds - - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen:= "unknown" #@cur_arv_meds - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen:= "unknown" #@cur_arv_meds - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen := "unknown" - - WHEN @prev_id = @cur_id THEN @arv_first_regimen - WHEN @prev_id != @cur_id THEN @arv_first_regimen:=NULL - ELSE @arv_first_regimen - END AS arv_first_regimen, - - -/* - case - when @arv_first_regimen is null and obs regexp "!!1499=" then @arv_first_regimen_location_id := 9999 - when @prev_id != @cur_id and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @arv_first_regimen_location_id is null and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @prev_id = @cur_id then @arv_first_regimen_location_id - when @prev_id != @cur_id then @arv_first_regimen_location_id := null - else "-1" - end as arv_first_regimen_location_id, -*/ - - CASE - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_location_id := 9999 - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_location_id:=9999 - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_location_id:=location_id - - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_location_id:= 9999 #location_id - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_location_id:= 9999 #location_id - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_location_id:=9999 - WHEN @prev_id = @cur_id THEN @arv_first_regimen_location_id - WHEN @prev_id != @cur_id THEN @arv_first_regimen_location_id:=NULL - ELSE @arv_first_regimen_location_id - END AS arv_first_regimen_location_id, - - - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := null - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=(6156)!!" then @cur_arv_line := 3 - when @prev_id = @cur_id then @cur_arv_line - else @cur_arv_line := null - end as cur_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1250=(6156)!!" then 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1088=(6156)!!" then 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then 2 - when obs regexp "!!2154=(6156)!!" then 3 - else null - end as cur_arv_line_strict, - - - - - - - - - - case - when obs regexp "!!6976=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6976=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6976=6695!!" then @cur_arv_line_reported := 3 - - when obs regexp "!!6744=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6744=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6744=6695!!" then @cur_arv_line_reported := 3 - when @prev_id = @cur_id then @cur_arv_line_reported - else @cur_arv_line_reported := null - end as cur_arv_line_reported, - - - - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := null - end as prev_arv_start_date, - - - - - - - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - - when @cur_arv_meds != @prev_arv_meds then @arv_start_date := date(t1.encounter_datetime) - - when @prev_id != @cur_id then @arv_start_date := null - else @arv_start_date - end as arv_start_date, - - - case - when @prev_arv_start_date != @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end as prev_arv_end_date, - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := null - end as prev_arv_adherence, - - - - - - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id = @cur_id then @cur_arv_adherence - else @cur_arv_adherence := null - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when @prev_id = @cur_id then @hiv_status_disclosed - else @hiv_status_disclosed := null - end as hiv_status_disclosed, - - - - - - - - - - - - - - - - - - case - when obs regexp "!!8351=(48|50|1066|1624|6971|9608)!!" then @is_pregnant := null - when @prev_id != @cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" or obs regexp "!!8351=(1065|1484)!!" then @is_pregnant := true - else @is_pregnant := null - end - when @is_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") or obs regexp "!!8351=(1065|1484)!!"then @is_pregnant := true - when @is_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@is_pregnant,encounter_datetime) > 40 or timestampdiff(week,@edd,encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @is_pregnant := null - else @is_pregnant - end as is_pregnant, - - - - - - - - - case - when @prev_id != @cur_id then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd := null - end - when @edd is null then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - - - case - when obs regexp "!!6174=" then @tb_screen := true - when obs regexp "!!2022=1065!!" then @tb_screen := true - when obs regexp "!!307=" then @tb_screen := true - when obs regexp "!!12=" then @tb_screen := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screen := true - when orders regexp "(12|307|8064|2311|2323)" then @tb_screen := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screen := true - when obs regexp "!!5958=1077!!" then @tb_screen := true - when obs regexp "!!2020=1065!!" then @tb_screen := true - when obs regexp "!!2021=1065!!" then @tb_screen := true - when obs regexp "!!2028=" then @tb_screen := true - when obs regexp "!!1268=(1256|1850)!!" then @tb_screen := true - when obs regexp "!!5959=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!5971=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!1492=107!!" then @tb_screen := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screen := true - end as tb_screen, - - case - when obs regexp "!!8292=" then @tb_screening_result := - replace(replace((substring_index(substring(obs,locate("!!8292=",obs)),@sep,1)),"!!8292=",""),"!!","") - when @prev_id != @cur_id then @tb_screening_result := null - else @tb_screening_result - end as tb_screening_result, - - case - when obs regexp "!!6174=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2022=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!307=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!12=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime - when orders regexp "(12|307|8064|2311|2323)" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5958=1077!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2020=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2021=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2028=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1268=(1256|1850)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5959=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5971=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1492=107!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screening_datetime := encounter_datetime - when @cur_id = @prev_id then @tb_screening_datetime - else @tb_screening_datetime := null - end as tb_screening_datetime, - - - case - when obs regexp "!!1265=(1107|1260)!!" then @on_ipt := 0 - when obs regexp "!!1265=!!" then @on_ipt := 1 - when obs regexp "!!1110=656!!" then @on_ipt := 1 - when @cur_id != @prev_id then @on_ipt := null - else @on_ipt - end as on_ipt, - - - - case - when obs regexp "!!1265=(1256|1850)!!" then @ipt_start_date := encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" and @ipt_start_date is null then @ipt_start_date := encounter_datetime - when @cur_id != @prev_id then @ipt_start_date := null - else @ipt_start_date - end as ipt_start_date, - - - - case - when obs regexp "!!1266=" then @ipt_stop_date := encounter_datetime - when @cur_id = @prev_id then @ipt_stop_date - when @cur_id != @prev_id then @ipt_stop_date := null - else @ipt_stop_date - end as ipt_stop_date, - - case - when obs regexp "!!1266=1267!!" then @ipt_completion_date := encounter_datetime - when @cur_id = @prev_id then @ipt_completion_date - when @cur_id != @prev_id then @ipt_completion_date := null - else @ipt_completion_date - end as ipt_completion_date, - - - case - when obs regexp "!!1268=(1107|1260)!!" then @on_tb_tx := 0 - when obs regexp "!!1268=" then @on_tb_tx := 1 - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @on_tb_tx := 1 - else @on_tb_tx := 0 - end as on_tb_tx, - - - - case - when obs regexp "!!1113=" then @tb_tx_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @tb_tx_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" and obs regexp "!!7015=" and @tb_tx_start_date is null then @tb_tx_start_date := null - when obs regexp "!!1268=(1257|1259|1849|981)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_start_date - else @tb_tx_start_date := null - end as tb_tx_start_date, - - - - - case - when obs regexp "!!2041=" then @tb_tx_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_tx_end_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_end_date - else @tb_tx_end_date := null - end as tb_tx_end_date, - - - - - - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1261=1257!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1193=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when @prev_id=@cur_id then @pcp_prophylaxis_start_date - else @pcp_prophylaxis_start_date := null - end as pcp_prophylaxis_start_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - else @cd4_2:=null - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - else @cd4_2_date:=null - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=null - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=null - end as cd4_1_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - else @cd4_percent_2:=null - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - else @cd4_percent_2_date:=null - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=null - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=null - end as cd4_percent_1_date, - - - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <> date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - else @vl_2:=null - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - else @vl_2_date:=null - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=null - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date:=null - end as vl_1_date, - - - - - - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(encounter_datetime) - when orders regexp "856" then @vl_order_date := date(encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - else @vl_order_date := null - end as vl_order_date, - - case - - WHEN (@cur_arv_meds IS NOT NULL AND @vl_1 > 1000) AND (TIMESTAMPDIFF(DAY, @vl_1_date,date(encounter_datetime)) >= 90) THEN 1 - - WHEN (TIMESTAMPDIFF(MONTH,@arv_start_date,date(encounter_datetime)) <= 12) AND (@vl_1_date IS NULL OR TIMESTAMPDIFF(MONTH,@vl_1_date,date(encounter_datetime)) >= 6) AND (TIMESTAMPDIFF(MONTH,@arv_start_date,date(encounter_datetime)) >= 6) THEN 1 - - WHEN (TIMESTAMPDIFF(MONTH,@arv_start_date, date(encounter_datetime)) >= 12) AND (@vl_1_date IS NULL OR TIMESTAMPDIFF(MONTH, @vl_1_date, date(encounter_datetime)) >= 12) THEN 1 - - ELSE 0 - - - end as expected_vl_date, - - - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := null - end as cd4_order_date, - - - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := null - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - else @hiv_dna_pcr_2:=null - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - else @hiv_dna_pcr_2_date:=null - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=null - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=null - end as hiv_dna_pcr_1_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - case - when obs regexp "!!8302=8305!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!6579=" then @condoms_provided_date := encounter_datetime - when @prev_id = @cur_id then @condoms_provided_date - else @condoms_provided_date := null - end as condoms_provided_date, - - - - - - - case - when obs regexp "!!7240=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(encounter_datetime) - when obs regexp "!!7240=!!" - then @modern_contraceptive_method_start_date := null - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" and obs regexp "!!1190=" - then @modern_contraceptive_method_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1190=",obs)),@sep,1)),"!!1190=",""),"!!","")) - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(encounter_datetime) - when obs regexp "!!374=!!" - then @modern_contraceptive_method_start_date := null - when @prev_id = @cur_id then @modern_contraceptive_method_start_date - else @modern_contraceptive_method_start_date := null - end as modern_contraceptive_method_start_date, - - - case - when obs regexp "!!7240=1107!!" then @contraceptive_method := null - when obs regexp "!!7240=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!7240=",obs)),@sep,1)),"!!7240=",""),"!!","") - when obs regexp "!!374=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!374=",obs)),@sep,1)),"!!374=",""),"!!","") - when @prev_id = @cur_id then @contraceptive_method - else @contraceptive_method := null - end as contraceptive_method, - - - - - - - case - when obs regexp "!!5356=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!8287=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!8287=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!8287=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!8287=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!8307=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!8307=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!8307=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!8307=(1207|1223)!!" then @cur_who_stage := 4 - when @prev_id = @cur_id then @cur_who_stage - else @cur_who_stage := null - end as cur_who_stage, - - - case - when obs regexp "!!6096=1065" then @discordant_status := "Yes" - when obs regexp "!!6096=1066" then @discordant_status := "No" - when obs regexp "!!6096=1067" then @discordant_status := "Unknown" - when obs regexp "!!6096=1175" then @discordant_status := "N/A" - when obs regexp "!!6096=6826" then @discordant_status := "Concordant Couple Positive" - when obs regexp "!!6096=6827" then @discordant_status := "Concordant Couple Negative" - when @prev_id = @cur_id then @discordant_status - else @discordant_status := null - end as discordant_status - - - - - from flat_hiv_summary_0 t1 - join amrs.person p using (person_id) - ); - - - - set @prev_id = -1; - set @cur_id = -1; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_hiv_summary_1 drop prev_id, drop cur_id, drop cur_clinical_datetime, drop cur_clinic_rtc_date; - - drop table if exists flat_hiv_summary_2; - create temporary table flat_hiv_summary_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_date_bncd - else null - end as outreach_date_bncd, - - case - when encounter_type=21 and @outreach_date_bncd is null then @outreach_date_bncd := encounter_datetime - when is_clinical_encounter then @outreach_date_bncd := null - when @prev_id != @cur_id then @outreach_date_bncd := null - else @outreach_date_bncd - end as next_outreach_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_death_date_bncd - else null - end as outreach_death_date_bncd, - - case - when encounter_type=21 and @outreach_death_date_bncd is null then @outreach_death_date_bncd := death_date - when is_clinical_encounter then @outreach_death_date_bncd := null - when @prev_id != @cur_id then @outreach_death_date_bncd := null - else @outreach_death_date_bncd - end as next_outreach_death_date_bncd, - - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then cast(@outreach_patient_care_status_bncd as unsigned) - else null - end as outreach_patient_care_status_bncd, - - case - when encounter_type=21 and @outreach_patient_care_status_bncd is null then @outreach_patient_care_status_bncd := patient_care_status - when is_clinical_encounter then @outreach_patient_care_status_bncd := null - when @prev_id != @cur_id then @outreach_patient_care_status_bncd := null - else @outreach_patient_care_status_bncd - end as next_outreach_patient_care_status_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_date_bncd - else null - end as transfer_date_bncd, - - case - when encounter_type=116 and @transfer_date_bncd is null then @transfer_date_bncd := encounter_datetime - when is_clinical_encounter then @transfer_date_bncd := null - when @prev_id != @cur_id then @transfer_date_bncd := null - else @transfer_date_bncd - end as next_transfer_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_transfer_out_bncd - else null - end as transfer_transfer_out_bncd, - - case - when encounter_type=116 and @transfer_transfer_out_bncd is null then @transfer_transfer_out_bncd := encounter_datetime - when is_clinical_encounter then @transfer_transfer_out_bncd := null - when @prev_id != @cur_id then @transfer_transfer_out_bncd := null - else @transfer_transfer_out_bncd - end as next_transfer_transfer_out_bncd - - from flat_hiv_summary_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = -1; - set @cur_id = -1; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_hiv_summary_3; - create temporary table flat_hiv_summary_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_hiv, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - -/* Moved to flat_hiv_summary_1 - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, -*/ - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id, - - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id - -/* MOVED to flat_hiv_summary_1 - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date -*/ - from flat_hiv_summary_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - alter table flat_hiv_summary_3 drop prev_id, drop cur_id; - - set @prev_id = null; - set @cur_id = null; - set @transfer_in = null; - set @transfer_in_date = null; - set @transfer_in_location_id = null; - set @transfer_out = null; - set @transfer_out_date = null; - set @transfer_out_location_id = null; - - drop temporary table if exists flat_hiv_summary_4; - - create temporary table flat_hiv_summary_4 ( index person_enc (person_id, encounter_datetime)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - - - case - when obs regexp "!!7015=" then @transfer_in := 1 - when prev_clinical_location_id != location_id 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 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 then @transfer_in_location_id := prev_clinical_location_id - when @cur_id = @prev_id then @transfer_in_location_id - else @transfer_in_location_id := null - end transfer_in_location_id, - - - - - - - - - - - - case - when obs regexp "!!1285=!!" then @transfer_out := 1 - when obs regexp "!!1596=1594!!" then @transfer_out := 1 - when obs regexp "!!9082=(1287|1594|9068|9504|1285)!!" then @transfer_out := 1 - when next_clinical_location_id != location_id then @transfer_out := 1 - else @transfer_out := null - end as transfer_out, - - case - when obs regexp "!!1285=(1287|9068|2050)!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9999 - when obs regexp "!!1285=1286!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9998 - when next_clinical_location_id != location_id then @transfer_out_location_id := next_clinical_location_id - else @transfer_out_location_id := null - end transfer_out_location_id, - - - case - when @transfer_out and next_clinical_datetime_hiv is null then @transfer_out_date := date(cur_rtc_date) - when next_clinical_location_id != location_id then @transfer_out_date := date(next_clinical_datetime_hiv) - when transfer_transfer_out_bncd then @transfer_out_date := date(transfer_transfer_out_bncd) - else @transfer_out_date := null - end transfer_out_date - - - - from flat_hiv_summary_3 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - -SELECT - COUNT(*) -INTO @new_encounter_rows FROM - flat_hiv_summary_4; - -SELECT @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; -SELECT @total_rows_written; - - - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - t1.uuid, - visit_id, - visit_type, - encounter_id, - encounter_datetime, - encounter_type, - is_transit, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - visit_num, - enrollment_date, - enrollment_location_id, - hiv_start_date, - death_date, - scheduled_visit, - transfer_in, - transfer_in_location_id, - transfer_in_date, - transfer_out, - transfer_out_location_id, - transfer_out_date, - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date as rtc_date, - arv_first_regimen, - arv_first_regimen_location_id, - arv_first_regimen_start_date, - arv_first_regimen_start_date_flex, - prev_arv_meds, - cur_arv_meds, - cur_arv_meds_strict, - cur_arv_drugs, - prev_arv_drugs, - arv_start_date, - arv_start_location_id, - prev_arv_start_date, - prev_arv_end_date, - prev_arv_line, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - is_pregnant, - edd, - tb_screen, - tb_screening_result, - tb_screening_datetime, - on_ipt, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - on_tb_tx, - tb_tx_start_date, - tb_tx_end_date, - pcp_prophylaxis_start_date, - condoms_provided_date, - modern_contraceptive_method_start_date, - contraceptive_method, - cur_who_stage, - discordant_status, - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - expected_vl_date, - vl_order_date, - cd4_order_date, - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_location_id, - next_clinical_location_id, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv, - outreach_date_bncd, - outreach_death_date_bncd, - outreach_patient_care_status_bncd, - transfer_date_bncd, - transfer_transfer_out_bncd - - from flat_hiv_summary_4 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_hiv_summary_build_queue__0 t2 using (person_id);'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - -SELECT - @person_ids_count AS 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); -SELECT - CONCAT(@start_write, - ' : Writing ', - @total_rows_to_write, - ' to ', - @primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); -SELECT - CONCAT(@finish_write, - ' : Completed writing rows. Time to write to primary table: ', - @time_to_write, - ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); -SELECT - CONCAT('Average Cycle Length: ', - @ave_cycle_length, - ' 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: ', - TIMESTAMPDIFF(MINUTE, @start, @end), - ' minutes'); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_v15_12.sql b/etl-scripts/stored-procedures/generate_hiv_summary_v15_12.sql deleted file mode 100644 index f532e27..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_v15_12.sql +++ /dev/null @@ -1,2133 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_v15_12`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_hiv_summary_v15b"; - set @query_type = query_type; - set @queue_table = ""; - set @total_rows_written = 0; - - set @start = now(); - set @table_version = "flat_hiv_summary_v2.19"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - -CREATE TABLE IF NOT EXISTS flat_hiv_summary_v15b ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id INT, - uuid VARCHAR(100), - visit_id INT, - visit_type SMALLINT, - encounter_id INT, - encounter_datetime DATETIME, - encounter_type INT, - is_transit INT, - is_clinical_encounter INT, - location_id INT, - location_uuid VARCHAR(100), - visit_num INT, - mdt_session_number INT, - enrollment_date DATETIME, - enrollment_location_id INT, - hiv_start_date DATETIME, - death_date DATETIME, - scheduled_visit INT, - transfer_in TINYINT, - transfer_in_location_id INT, - transfer_in_date DATETIME, - transfer_out TINYINT, - transfer_out_location_id INT, - transfer_out_date DATETIME, - patient_care_status INT, - out_of_care INT, - prev_rtc_date DATETIME, - rtc_date DATETIME, - med_pickup_rtc_date DATETIME, - arv_first_regimen VARCHAR(500), - arv_first_regimen_location_id INT, - arv_first_regimen_start_date DATETIME, - arv_first_regimen_start_date_flex DATETIME, - prev_arv_meds VARCHAR(500), - cur_arv_meds VARCHAR(500), - cur_arv_meds_strict VARCHAR(500), - cur_arv_drugs VARCHAR(500), - prev_arv_drugs VARCHAR(500), - arv_start_date DATETIME, - arv_start_location_id INT, - prev_arv_start_date DATETIME, - prev_arv_end_date DATETIME, - prev_arv_line INT, - cur_arv_line INT, - cur_arv_line_strict INT, - cur_arv_line_reported TINYINT, - prev_arv_adherence VARCHAR(200), - cur_arv_adherence VARCHAR(200), - hiv_status_disclosed INT, - is_pregnant BOOLEAN, - edd DATETIME, - tb_screen BOOLEAN, - tb_screening_result INT, - tb_screening_datetime DATETIME, - on_ipt BOOLEAN, - ipt_start_date DATETIME, - ipt_stop_date DATETIME, - ipt_completion_date DATETIME, - on_tb_tx BOOLEAN, - tb_tx_start_date DATETIME, - tb_tx_end_date DATETIME, - pcp_prophylaxis_start_date DATETIME, - condoms_provided_date DATETIME, - modern_contraceptive_method_start_date DATETIME, - contraceptive_method INT, - cur_who_stage INT, - discordant_status INT, - cd4_resulted DOUBLE, - cd4_resulted_date DATETIME, - cd4_1 DOUBLE, - cd4_1_date DATETIME, - cd4_2 DOUBLE, - cd4_2_date DATETIME, - cd4_percent_1 DOUBLE, - cd4_percent_1_date DATETIME, - cd4_percent_2 DOUBLE, - cd4_percent_2_date DATETIME, - vl_resulted INT, - vl_resulted_date DATETIME, - vl_1 INT, - vl_1_date DATETIME, - vl_2 INT, - vl_2_date DATETIME, - expected_vl_date SMALLINT, - vl_order_date DATETIME, - cd4_order_date DATETIME, - hiv_dna_pcr_order_date DATETIME, - hiv_dna_pcr_resulted INT, - hiv_dna_pcr_resulted_date DATETIME, - hiv_dna_pcr_1 INT, - hiv_dna_pcr_1_date DATETIME, - hiv_dna_pcr_2 INT, - hiv_dna_pcr_2_date DATETIME, - hiv_rapid_test_resulted INT, - hiv_rapid_test_resulted_date DATETIME, - prev_encounter_datetime_hiv DATETIME, - next_encounter_datetime_hiv DATETIME, - prev_encounter_type_hiv MEDIUMINT, - next_encounter_type_hiv MEDIUMINT, - prev_clinical_datetime_hiv DATETIME, - next_clinical_datetime_hiv DATETIME, - prev_clinical_location_id MEDIUMINT, - next_clinical_location_id MEDIUMINT, - prev_clinical_rtc_date_hiv DATETIME, - next_clinical_rtc_date_hiv DATETIME, - outreach_date_bncd DATETIME, - outreach_death_date_bncd DATETIME, - outreach_patient_care_status_bncd INT, - transfer_date_bncd DATETIME, - transfer_transfer_out_bncd DATETIME, - phone_outreach INT, - home_outreach INT, - outreach_attempts INT, - outreach_missed_visit_reason INT, - PRIMARY KEY encounter_id (encounter_id), - INDEX person_date (person_id , encounter_datetime), - INDEX person_uuid (uuid), - INDEX location_enc_date (location_uuid , encounter_datetime), - INDEX enc_date_location (encounter_datetime , location_uuid), - INDEX location_id_rtc_date (location_id , rtc_date), - INDEX location_uuid_rtc_date (location_uuid , rtc_date), - INDEX loc_id_enc_date_next_clinical (location_id , encounter_datetime , next_clinical_datetime_hiv), - INDEX encounter_type (encounter_type), - INDEX date_created (date_created) -); - - - - 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 @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_hiv_summary_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from flat_hiv_summary_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_hiv_summary_v15b"; - set @queue_table = "flat_hiv_summary_sync_queue"; -CREATE TABLE IF NOT EXISTS flat_hiv_summary_sync_queue ( - person_id INT PRIMARY KEY -); - - - - set @last_update = null; -SELECT - MAX(date_updated) -INTO @last_update FROM - etl.flat_log -WHERE - table_name = @table_version; - - replace into flat_hiv_summary_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - -SELECT @person_ids_count AS 'num patients to sync'; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - - drop temporary table if exists flat_hiv_summary_build_queue__0; - - - - SET @dyn_sql=CONCAT('create temporary table flat_hiv_summary_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - drop temporary table if exists flat_hiv_summary_0a; - create temporary table flat_hiv_summary_0a - (select - t1.person_id, - t1.visit_id, - v.visit_type_id as visit_type, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - case - when v.visit_type_id IS NULL then NULL - when v.visit_type_id = 24 then 1 - when v.visit_type_id IS NOT NULL AND v.visit_type_id != 24 THEN 0 - else null - end as is_transit, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - - 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) 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 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,115,117,120,127,128,138, 140, 153,154,158,162,163,186,212) then 10 - when t1.encounter_type in (129) then 5 - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - 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,186,212) - AND NOT obs regexp "!!5303=(822|664|1067)!!" - AND NOT obs regexp "!!9082=9036!!" - ); - - insert into flat_hiv_summary_0a - (select - t1.person_id, - null, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - ); - - drop temporary table if exists flat_hiv_summary_0; - create temporary table flat_hiv_summary_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @prev_id = -1; - set @cur_id = -1; - set @prev_encounter_date = null; - set @cur_encounter_date = null; - set @enrollment_date = null; - set @hiv_start_date = null; - set @cur_location = null; - set @cur_rtc_date = null; - set @prev_rtc_date = null; - set @med_pickup_rtc_date = null; - set @hiv_start_date = null; - set @prev_arv_start_date = null; - set @arv_start_date = null; - set @prev_arv_end_date = null; - set @arv_start_location_id = null; - set @arv_first_regimen_start_date = null; - set @arv_first_regimen_start_date_flex = null; - set @arv_first_regimen = null; - set @prev_arv_line = null; - set @cur_arv_line = null; - set @prev_arv_adherence = null; - set @cur_arv_adherence = null; - set @hiv_status_disclosed = null; - set @is_pregnant = null; - set @edd = null; - set @prev_arv_meds = null; - set @cur_arv_meds = null; - set @cur_arv_drugs = null; - set @prev_arv_drugs = null; - set @ipt_start_date = null; - set @ipt_end_date = null; - set @ipt_completion_date = null; - - set @on_tb_tx = null; - set @tb_tx_start_date = null; - set @tb_tx_end_date = null; - set @pcp_prophylaxis_start_date = null; - set @tb_screen = null; - set @tb_screening_result = null; - set @tb_screening_datetime = null; - - set @death_date = null; - - set @patient_care_status=null; - - set @condoms_provided_date = null; - set @modern_contraceptive_method_start_date = null; - set @contraceptive_method = null; - - - set @cur_who_stage = null; - - set @vl_1=null; - set @vl_2=null; - set @vl_1_date=null; - set @vl_2_date=null; - set @vl_resulted=null; - set @vl_resulted_date=null; - - set @cd4_resulted=null; - set @cd4_resulted_date=null; - set @cd4_1=null; - set @cd4_1_date=null; - set @cd4_2=null; - set @cd4_2_date=null; - set @cd4_percent_1=null; - set @cd4_percent_1_date=null; - set @cd4_percent_2=null; - set @cd4_percent_2_date=null; - set @vl_order_date = null; - set @cd4_order_date = null; - - set @hiv_dna_pcr_order_date = null; - set @hiv_dna_pcr_1=null; - set @hiv_dna_pcr_2=null; - set @hiv_dna_pcr_1_date=null; - set @hiv_dna_pcr_2_date=null; - - set @hiv_rapid_test_resulted=null; - set @hiv_rapid_test_resulted_date= null; - - - - - - - drop temporary table if exists flat_hiv_summary_1; - create temporary table flat_hiv_summary_1 (index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.visit_type, - t1.encounter_id, - @prev_encounter_date := date(@cur_encounter_date) as prev_encounter_date, - @cur_encounter_date := date(encounter_datetime) as cur_encounter_date, - t1.encounter_datetime, - t1.encounter_type, - t1.is_transit, - t1.is_clinical_encounter, - CASE - WHEN - (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7013=' - THEN - @enrollment_date:=REPLACE(REPLACE((SUBSTRING_INDEX(SUBSTRING(obs, LOCATE('!!7013=', obs)), - @sep, - 1)), - '!!7013=', - ''), - '!!', - '') - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:='1900-01-01' - WHEN - t1.encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:=DATE(encounter_datetime) - WHEN @prev_id = @cur_id THEN @enrollment_date - ELSE @enrollment_date:=NULL - END AS enrollment_date, - - - - CASE - WHEN - (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7030=5622' - THEN - @enrollment_location_id:=9999 - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollmen_location_id:=9999 - WHEN - encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_location_id:= location_id - WHEN @prev_id = @cur_id THEN @enrollment_location_id - ELSE @enrollment_location_id:=NULL - END AS enrollment_location_id, - - - - - - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as 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 - end as visit_num, - case - when encounter_type=110 then - case - when obs regexp "!!(10532)" then @mdt_session_number:= 4 - when obs regexp "!!(10527|10528|10529|10530|10531)" then @mdt_session_number:= 3 - when obs regexp "!!(10523|10524|10525|10526)" then @mdt_session_number:= 2 - when obs regexp "!!(10518|10519|10520|10521|10522)" then @mdt_session_number:= 1 - when @prev_id = @cur_id then @mdt_session_number - else null - end - else @mdt_session_number - end as mdt_session_number, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - case - when obs regexp "!!9605=" then @med_pickup_rtc_date := replace(replace((substring_index(substring(obs,locate("!!9605=",obs)),@sep,1)),"!!9605=",""),"!!","") - else @med_pickup_rtc_date := null - end as med_pickup_rtc_date, - - - - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504|1285)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - - - - - - - - - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when @prev_id != @cur_id or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := encounter_datetime - else @death_date := null - end - else @death_date - end as death_date, - - - - - - case - when @death_date <= encounter_datetime then @patient_care_status := 159 - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - - case - when obs regexp "!!9203=" then @hiv_start_date := replace(replace((substring_index(substring(obs,locate("!!9203=",obs)),@sep,1)),"!!9203=",""),"!!","") - when obs regexp "!!7015=" then @hiv_start_date := "1900-01-01" - when @hiv_start_date is null then @hiv_start_date := date(encounter_datetime) - when @prev_id = @cur_id then @hiv_start_date - else @hiv_start_date := null - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_location_id := location_id - when @prev_id = @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location_id := location_id - when @prev_id != @cur_id then @arv_start_location_id := null - else @arv_start_location_id - end as arv_start_location_id, - - - case - when @prev_id=@cur_id and @cur_arv_meds is not null then @prev_arv_meds := @cur_arv_meds - when @prev_id=@cur_id then @prev_arv_meds - else @prev_arv_meds := null - end as prev_arv_meds, - - - - - #2154 : PATIENT REPORTED CURRENT ANTIRETROVIRAL TREATMENT - #2157 : PATIENT REPORTED PAST ANTIRETROVIRAL TREATMENT - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - when @prev_id = @cur_id then @cur_arv_meds - else @cur_arv_meds:= null - end as cur_arv_meds, - - - case - when 1 then null - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - else null - end as cur_arv_meds_strict, - - case - - when @prev_id=@cur_id and @prev_arv_drugs is not null then @prev_arv_drugs := @cur_arv_drugs - when @prev_id=@cur_id then @prev_arv_drugs - else @prev_arv_drugs := null - - end as prev_arv_drugs, - - case - - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_drugs := null - when obs regexp "!!1250=" then @cur_arv_drugs := normalizeArvsDrugFormulation(obs) - - - when obs regexp "!!1088=" then @cur_arv_drugs := normalizeArvsDrugFormulation(obs) - - when obs regexp "!!2154=" then @cur_arv_drugs := normalizeArvsDrugFormulation(obs) - - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_drugs := normalizeArvsDrugFormulation(obs) - - when @prev_id = @cur_id then @cur_arv_drugs - else @cur_arv_drugs:= null - - end as cur_arv_drugs, - - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := @cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date, - - - CASE - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_start_date:=GetValues(obs,1499) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_start_date:='1900-01-01' - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_start_date:=DATE(encounter_datetime) - - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_start_date:= "1900-01-01" #DATE(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_start_date:= "1900-01-01" # DATE(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_start_date:='1900-01-01' - WHEN @arv_first_regimen_start_date = "1900-01-01" AND obs regexp '!!1633=' AND obs regexp '!!1499=' then - @arv_first_regimen_start_date := replace(replace((substring_index(substring(obs,locate("!!1499=",obs)),@sep,1)),"!!1499=",""),"!!","") - WHEN @prev_id = @cur_id THEN @arv_first_regimen_start_date - WHEN @prev_id != @cur_id THEN @arv_first_regimen_start_date:=NULL - ELSE @arv_first_regimen_start_date - END AS arv_first_regimen_start_date, - - - - - /* - CASE - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_start_date_flex := GetValues(obs,1499) - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_start_date_flex :='1900-01-01' - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_start_date_flex:=DATE(encounter_datetime) - - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen_start_date_flex :=date(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen_start_date_flex :=date(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_start_date_flex:='1900-01-01' - WHEN @prev_id = @cur_id THEN @arv_first_regimen_start_date_flex - WHEN @prev_id != @cur_id THEN @arv_first_regimen_start_date_flex:=NULL - ELSE @arv_first_regimen_start_date_flex - END - */ - null AS arv_first_regimen_start_date_flex, - - - -/* - case - when @arv_first_regimen is null and obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @arv_first_regimen := normalize_arvs(obs,'2157') - when obs regexp "!!7015=" and @arv_first_regimen is null then @arv_first_regimen := "unknown" - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id = @cur_id then @arv_first_regimen - when @prev_id != @cur_id then @arv_first_regimen := @cur_arv_meds - else "-1" - end as arv_first_regimen, -*/ - - CASE - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen:= "unknown" - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen:= "unknown" - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen := @cur_arv_meds - - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen:= "unknown" #@cur_arv_meds - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen:= "unknown" #@cur_arv_meds - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen := "unknown" - - WHEN @arv_first_regimen = "unknown" AND obs regexp '!!1633=1065!!' AND obs regexp '!!2157=' then - @arv_first_regimen := normalize_arvs(obs,'2157') - - WHEN @prev_id = @cur_id THEN @arv_first_regimen - WHEN @prev_id != @cur_id THEN @arv_first_regimen:=NULL - ELSE @arv_first_regimen - END AS arv_first_regimen, - - -/* - case - when @arv_first_regimen is null and obs regexp "!!1499=" then @arv_first_regimen_location_id := 9999 - when @prev_id != @cur_id and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @arv_first_regimen_location_id is null and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @prev_id = @cur_id then @arv_first_regimen_location_id - when @prev_id != @cur_id then @arv_first_regimen_location_id := null - else "-1" - end as arv_first_regimen_location_id, -*/ - - CASE - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_location_id := 9999 - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_location_id:=9999 - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_location_id:=location_id - - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_location_id:= 9999 #location_id - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_location_id:= 9999 #location_id - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_location_id:=9999 - WHEN @prev_id = @cur_id THEN @arv_first_regimen_location_id - WHEN @prev_id != @cur_id THEN @arv_first_regimen_location_id:=NULL - ELSE @arv_first_regimen_location_id - END AS arv_first_regimen_location_id, - - - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := null - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=(6156)!!" then @cur_arv_line := 3 - when @prev_id = @cur_id then @cur_arv_line - else @cur_arv_line := null - end as cur_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1250=(6156)!!" then 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1088=(6156)!!" then 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then 2 - when obs regexp "!!2154=(6156)!!" then 3 - else null - end as cur_arv_line_strict, - - - - - - - - - - case - when obs regexp "!!6976=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6976=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6976=6695!!" then @cur_arv_line_reported := 3 - - when obs regexp "!!6744=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6744=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6744=6695!!" then @cur_arv_line_reported := 3 - when @prev_id = @cur_id then @cur_arv_line_reported - else @cur_arv_line_reported := null - end as cur_arv_line_reported, - - - - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := null - end as prev_arv_start_date, - - - - - - - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - - when @cur_arv_meds != @prev_arv_meds then @arv_start_date := date(t1.encounter_datetime) - - when @prev_id != @cur_id then @arv_start_date := null - else @arv_start_date - end as arv_start_date, - - - case - when @prev_arv_start_date != @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end as prev_arv_end_date, - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := null - end as prev_arv_adherence, - - - - - - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id = @cur_id then @cur_arv_adherence - else @cur_arv_adherence := null - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when @prev_id = @cur_id then @hiv_status_disclosed - else @hiv_status_disclosed := null - end as hiv_status_disclosed, - - - - - - - - - - - - - - - - - - case - when obs regexp "!!8351=(48|50|1066|1624|6971|9608)!!" then @is_pregnant := null - when @prev_id != @cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" or obs regexp "!!8351=(1065|1484)!!" then @is_pregnant := true - else @is_pregnant := null - end - when @is_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") or obs regexp "!!8351=(1065|1484)!!"then @is_pregnant := true - when @is_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@is_pregnant,encounter_datetime) > 40 or timestampdiff(week,@edd,encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @is_pregnant := null - else @is_pregnant - end as is_pregnant, - - - - - - - - - case - when @prev_id != @cur_id then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd := null - end - when @edd is null then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - - - case - when obs regexp "!!6174=" then @tb_screen := true - when obs regexp "!!2022=1065!!" then @tb_screen := true - when obs regexp "!!307=" then @tb_screen := true - when obs regexp "!!12=" then @tb_screen := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screen := true - when orders regexp "(12|307|8064|2311|2323)" then @tb_screen := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screen := true - when obs regexp "!!5958=1077!!" then @tb_screen := true - when obs regexp "!!2020=1065!!" then @tb_screen := true - when obs regexp "!!2021=1065!!" then @tb_screen := true - when obs regexp "!!2028=" then @tb_screen := true - when obs regexp "!!1268=(1256|1850)!!" then @tb_screen := true - when obs regexp "!!5959=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!5971=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!1492=107!!" then @tb_screen := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screen := true - end as tb_screen, - - case - when obs regexp "!!8292=" then @tb_screening_result := - replace(replace((substring_index(substring(obs,locate("!!8292=",obs)),@sep,1)),"!!8292=",""),"!!","") - when @prev_id != @cur_id then @tb_screening_result := null - else @tb_screening_result - end as tb_screening_result, - - case - when obs regexp "!!6174=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2022=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!307=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!12=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime - when orders regexp "(12|307|8064|2311|2323)" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5958=1077!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2020=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2021=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2028=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1268=(1256|1850)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5959=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5971=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1492=107!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screening_datetime := encounter_datetime - when @cur_id = @prev_id then @tb_screening_datetime - else @tb_screening_datetime := null - end as tb_screening_datetime, - - - case - when obs regexp "!!1265=(1107|1260)!!" then @on_ipt := 0 - when obs regexp "!!1265=!!" then @on_ipt := 1 - when obs regexp "!!1110=656!!" then @on_ipt := 1 - when @cur_id != @prev_id then @on_ipt := null - else @on_ipt - end as on_ipt, - - - - case - when obs regexp "!!1265=(1256|1850)!!" then @ipt_start_date := encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" and @ipt_start_date is null then @ipt_start_date := encounter_datetime - when obs regexp "!!10591=1065" and obs regexp "!!1190=" then @ipt_start_date := replace(replace((substring_index(substring(obs,locate("!!1190=",obs)),@sep,1)),"!!1190=",""),"!!","") - when @cur_id != @prev_id then @ipt_start_date := null - else @ipt_start_date - end as ipt_start_date, - - - - case - when obs regexp "!!1266=" then @ipt_stop_date := encounter_datetime - when obs regexp "!!10591=1065" and obs regexp "!!8603=" then @ipt_stop_date := replace(replace((substring_index(substring(obs,locate("!!8603=",obs)),@sep,1)),"!!8603=",""),"!!","") - when @cur_id = @prev_id then @ipt_stop_date - when @cur_id != @prev_id then @ipt_stop_date := null - else @ipt_stop_date - end as ipt_stop_date, - - case - when obs regexp "!!1266=1267!!" then @ipt_completion_date := encounter_datetime - when obs regexp "!!10591=1065" and obs regexp "!!8603=" then @ipt_completion_date := replace(replace((substring_index(substring(obs,locate("!!8603=",obs)),@sep,1)),"!!8603=",""),"!!","") - when @cur_id = @prev_id then @ipt_completion_date - when @cur_id != @prev_id then @ipt_completion_date := null - else @ipt_completion_date - end as ipt_completion_date, - - - case - when obs regexp "!!1268=(1107|1260)!!" then @on_tb_tx := 0 - when obs regexp "!!1268=" then @on_tb_tx := 1 - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @on_tb_tx := 1 - else @on_tb_tx := 0 - end as on_tb_tx, - - - - case - when obs regexp "!!1113=" then @tb_tx_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @tb_tx_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" and obs regexp "!!7015=" and @tb_tx_start_date is null then @tb_tx_start_date := null - when obs regexp "!!1268=(1257|1259|1849|981)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_start_date - else @tb_tx_start_date := null - end as tb_tx_start_date, - - - - - case - when obs regexp "!!2041=" then @tb_tx_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_tx_end_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_end_date - else @tb_tx_end_date := null - end as tb_tx_end_date, - - - - - - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1261=1257!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1193=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when @prev_id=@cur_id then @pcp_prophylaxis_start_date - else @pcp_prophylaxis_start_date := null - end as pcp_prophylaxis_start_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - else @cd4_2:=null - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - else @cd4_2_date:=null - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") + 0 as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") + 0 as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=null - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=null - end as cd4_1_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - else @cd4_percent_2:=null - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - else @cd4_percent_2_date:=null - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") + 1 as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=null - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=null - end as cd4_percent_1_date, - - - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <> date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - else @vl_2:=null - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - else @vl_2_date:=null - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") + 0 as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") + 0 as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=null - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date:=null - end as vl_1_date, - - - - - - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(encounter_datetime) - when orders regexp "856" then @vl_order_date := date(encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - else @vl_order_date := null - end as vl_order_date, - - case - - WHEN (@cur_arv_meds IS NOT NULL AND @vl_1 > 1000) AND (TIMESTAMPDIFF(DAY, @vl_1_date,date(encounter_datetime)) >= 90) THEN 1 - - WHEN (TIMESTAMPDIFF(MONTH,@arv_start_date,date(encounter_datetime)) <= 12) AND (@vl_1_date IS NULL OR TIMESTAMPDIFF(MONTH,@vl_1_date,date(encounter_datetime)) >= 6) AND (TIMESTAMPDIFF(MONTH,@arv_start_date,date(encounter_datetime)) >= 6) THEN 1 - - WHEN (TIMESTAMPDIFF(MONTH,@arv_start_date, date(encounter_datetime)) >= 12) AND (@vl_1_date IS NULL OR TIMESTAMPDIFF(MONTH, @vl_1_date, date(encounter_datetime)) >= 12) THEN 1 - - ELSE 0 - - - end as expected_vl_date, - - - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := null - end as cd4_order_date, - - - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := null - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - else @hiv_dna_pcr_2:=null - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - else @hiv_dna_pcr_2_date:=null - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=null - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=null - end as hiv_dna_pcr_1_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - case - when obs regexp "!!8302=8305!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!6579=" then @condoms_provided_date := encounter_datetime - when @prev_id = @cur_id then @condoms_provided_date - else @condoms_provided_date := null - end as condoms_provided_date, - - - - - - - case - when obs regexp "!!7240=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(encounter_datetime) - when obs regexp "!!7240=!!" - then @modern_contraceptive_method_start_date := null - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" and obs regexp "!!1190=" - then @modern_contraceptive_method_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1190=",obs)),@sep,1)),"!!1190=",""),"!!","")) - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(encounter_datetime) - when obs regexp "!!374=!!" - then @modern_contraceptive_method_start_date := null - when @prev_id = @cur_id then @modern_contraceptive_method_start_date - else @modern_contraceptive_method_start_date := null - end as modern_contraceptive_method_start_date, - - - case - when obs regexp "!!9738=1066!!" then @contraceptive_method := null - when obs regexp "!!7240=1107!!" then @contraceptive_method := null - when obs regexp "!!7240=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!7240=",obs)),@sep,1)),"!!7240=",""),"!!","") - when obs regexp "!!374=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!374=",obs)),@sep,1)),"!!374=",""),"!!","") - when @prev_id = @cur_id then @contraceptive_method - else @contraceptive_method := null - end as contraceptive_method, - - case - when obs regexp "!!5356=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!8287=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!8287=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!8287=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!8287=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!8307=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!8307=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!8307=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!8307=(1207|1223)!!" then @cur_who_stage := 4 - when @prev_id = @cur_id then @cur_who_stage - else @cur_who_stage := null - end as cur_who_stage, - - - case - when obs regexp "!!6096=1065" then @discordant_status := 1 - when obs regexp "!!6096=1066" then @discordant_status := 2 - when obs regexp "!!6096=1067" then @discordant_status := 3 - when obs regexp "!!6096=1175" then @discordant_status := 4 - when obs regexp "!!6096=6826" then @discordant_status := 5 - when obs regexp "!!6096=6827" then @discordant_status := 6 - when @prev_id = @cur_id then @discordant_status - else @discordant_status := null - end as discordant_status, - - - case - when encounter_type=21 then - case - when obs regexp "!!9063=1065" AND obs regexp "!!9600=1065" then @phone_outreach:= 1 - when obs regexp "!!1569=1555" AND obs regexp "!!9600=1065" then @phone_outreach:= 1 - when obs regexp "!!1558=1555" AND obs regexp "!!9600=1065" then @phone_outreach:= 1 - when obs regexp "!!1558=1555" AND obs regexp "!!1559=1065" then @phone_outreach:= 1 - when obs regexp "!!9063=(1065|1560|9064|9065|9066|5622)" AND obs regexp "!!9600=1066" then @phone_outreach:= 2 - when obs regexp "!!1558=1555" AND obs regexp "!!9600=1066" then @phone_outreach:= 2 - when obs regexp "!!1558=1555" AND obs regexp "!!1559=1066" then @phone_outreach:= 2 - else @phone_outreach:= null - end - else @phone_outreach := null - end as phone_outreach, - case - when encounter_type=21 then - case - when obs regexp "!!10085=1065" AND obs regexp "!!1558=(7066|6116|1556|1557)" AND obs regexp "!!1559=1065" then @home_outreach:= 1 - when obs regexp "!!1558=7066" and obs regexp "!!1559=1065" then @home_outreach := 1 - when obs regexp "!!1569=1567" AND obs regexp "!!1559=1065" then @home_outreach := 1 - when obs regexp "!!10085=1065" AND "!!1558=(7066|6116|1556|1557)" AND obs regexp "!!1559=1066" then @home_outreach := 2 - when obs regexp "!!1569=1567" AND obs regexp "!!1559=1066" then @home_outreach := 2 - else @home_outreach:= null - end - else @home_outreach := null - end as home_outreach, - - case when encounter_type = 21 then obs regexp - case - when obs regexp "!!1553=" then @outreach_attempts:= CAST(GetValues(obs,'1553') AS SIGNED) - when obs regexp "!!9062=" then @outreach_attempts:= CAST(GetValues(obs,'9062') AS SIGNED) - end - else @outreach_attempts := null - end as outreach_attempts, - - 0 as outreach_missed_visit_reason - - - - - from flat_hiv_summary_0 t1 - join amrs.person p using (person_id) - ); - - - - set @prev_id = -1; - set @cur_id = -1; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_hiv_summary_1 drop prev_id, drop cur_id, drop cur_clinical_datetime, drop cur_clinic_rtc_date; - - drop table if exists flat_hiv_summary_2; - create temporary table flat_hiv_summary_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_date_bncd - else null - end as outreach_date_bncd, - - case - when encounter_type=21 and @outreach_date_bncd is null then @outreach_date_bncd := encounter_datetime - when is_clinical_encounter then @outreach_date_bncd := null - when @prev_id != @cur_id then @outreach_date_bncd := null - else @outreach_date_bncd - end as next_outreach_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_death_date_bncd - else null - end as outreach_death_date_bncd, - - case - when encounter_type=21 and @outreach_death_date_bncd is null then @outreach_death_date_bncd := death_date - when is_clinical_encounter then @outreach_death_date_bncd := null - when @prev_id != @cur_id then @outreach_death_date_bncd := null - else @outreach_death_date_bncd - end as next_outreach_death_date_bncd, - - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then cast(@outreach_patient_care_status_bncd as unsigned) - else null - end as outreach_patient_care_status_bncd, - - case - when encounter_type=21 and @outreach_patient_care_status_bncd is null then @outreach_patient_care_status_bncd := patient_care_status - when is_clinical_encounter then @outreach_patient_care_status_bncd := null - when @prev_id != @cur_id then @outreach_patient_care_status_bncd := null - else @outreach_patient_care_status_bncd - end as next_outreach_patient_care_status_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_date_bncd - else null - end as transfer_date_bncd, - - case - when encounter_type=116 and @transfer_date_bncd is null then @transfer_date_bncd := encounter_datetime - when is_clinical_encounter then @transfer_date_bncd := null - when @prev_id != @cur_id then @transfer_date_bncd := null - else @transfer_date_bncd - end as next_transfer_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_transfer_out_bncd - else null - end as transfer_transfer_out_bncd, - - case - when encounter_type=116 and @transfer_transfer_out_bncd is null then @transfer_transfer_out_bncd := encounter_datetime - when is_clinical_encounter then @transfer_transfer_out_bncd := null - when @prev_id != @cur_id then @transfer_transfer_out_bncd := null - else @transfer_transfer_out_bncd - end as next_transfer_transfer_out_bncd - - from flat_hiv_summary_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = -1; - set @cur_id = -1; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_hiv_summary_3; - create temporary table flat_hiv_summary_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_hiv, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - -/* Moved to flat_hiv_summary_1 - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, -*/ - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id, - - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id - -/* MOVED to flat_hiv_summary_1 - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date -*/ - from flat_hiv_summary_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - alter table flat_hiv_summary_3 drop prev_id, drop cur_id; - - set @prev_id = null; - set @cur_id = null; - set @transfer_in = null; - set @transfer_in_date = null; - set @transfer_in_location_id = null; - set @transfer_out = null; - set @transfer_out_date = null; - set @transfer_out_location_id = null; - - drop temporary table if exists flat_hiv_summary_4; - - create temporary table flat_hiv_summary_4 ( index person_enc (person_id, encounter_datetime)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - - - case - when obs regexp "!!7015=" then @transfer_in := 1 - when prev_clinical_location_id != location_id 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 encounter_type != 186 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 encounter_type != 186 then @transfer_in_location_id := prev_clinical_location_id - when @cur_id = @prev_id then @transfer_in_location_id - else @transfer_in_location_id := null - end transfer_in_location_id, - - - - - - - - - - - - case - when obs regexp "!!1285=!!" then @transfer_out := 1 - when obs regexp "!!1596=1594!!" then @transfer_out := 1 - when obs regexp "!!9082=(1287|1594|9068|9504|1285)!!" then @transfer_out := 1 - when next_clinical_location_id != location_id and next_encounter_type_hiv != 186 then @transfer_out := 1 - else @transfer_out := null - end as transfer_out, - - case - when obs regexp "!!1285=(1287|9068|2050)!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9999 - when obs regexp "!!1285=1286!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9998 - when next_clinical_location_id != location_id and next_encounter_type_hiv != 186 then @transfer_out_location_id := next_clinical_location_id - else @transfer_out_location_id := null - end transfer_out_location_id, - - - case - when @transfer_out and next_clinical_datetime_hiv is null then @transfer_out_date := date(cur_rtc_date) - when next_clinical_location_id != location_id then @transfer_out_date := date(next_clinical_datetime_hiv) - when transfer_transfer_out_bncd then @transfer_out_date := date(transfer_transfer_out_bncd) - else @transfer_out_date := null - end transfer_out_date - - - - from flat_hiv_summary_3 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - -SELECT - COUNT(*) -INTO @new_encounter_rows FROM - flat_hiv_summary_4; - -SELECT @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; -SELECT @total_rows_written; - - - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - t1.uuid, - visit_id, - visit_type, - encounter_id, - encounter_datetime, - encounter_type, - is_transit, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - visit_num, - mdt_session_number, - enrollment_date, - enrollment_location_id, - hiv_start_date, - death_date, - scheduled_visit, - transfer_in, - transfer_in_location_id, - transfer_in_date, - transfer_out, - transfer_out_location_id, - transfer_out_date, - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date as rtc_date, - med_pickup_rtc_date, - arv_first_regimen, - arv_first_regimen_location_id, - arv_first_regimen_start_date, - arv_first_regimen_start_date_flex, - prev_arv_meds, - cur_arv_meds, - cur_arv_meds_strict, - cur_arv_drugs, - prev_arv_drugs, - arv_start_date, - arv_start_location_id, - prev_arv_start_date, - prev_arv_end_date, - prev_arv_line, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - is_pregnant, - edd, - tb_screen, - tb_screening_result, - tb_screening_datetime, - on_ipt, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - on_tb_tx, - tb_tx_start_date, - tb_tx_end_date, - pcp_prophylaxis_start_date, - condoms_provided_date, - modern_contraceptive_method_start_date, - contraceptive_method, - cur_who_stage, - discordant_status, - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - expected_vl_date, - vl_order_date, - cd4_order_date, - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_location_id, - next_clinical_location_id, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv, - outreach_date_bncd, - outreach_death_date_bncd, - outreach_patient_care_status_bncd, - transfer_date_bncd, - transfer_transfer_out_bncd, - phone_outreach, - home_outreach, - outreach_attempts, - outreach_missed_visit_reason - - from flat_hiv_summary_4 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_hiv_summary_build_queue__0 t2 using (person_id);'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - -SELECT - @person_ids_count AS 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); -SELECT - CONCAT(@start_write, - ' : Writing ', - @total_rows_to_write, - ' to ', - @primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); -SELECT - CONCAT(@finish_write, - ' : Completed writing rows. Time to write to primary table: ', - @time_to_write, - ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); -SELECT - CONCAT('Average Cycle Length: ', - @ave_cycle_length, - ' 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: ', - TIMESTAMPDIFF(MINUTE, @start, @end), - ' minutes'); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_v15_2.sql b/etl-scripts/stored-procedures/generate_hiv_summary_v15_2.sql deleted file mode 100644 index 7ea3fc7..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_v15_2.sql +++ /dev/null @@ -1,1474 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_v15_2`() -BEGIN - select @query_type := "rebuild"; - select @start := now(); - select @start := now(); - select @table_version := "flat_hiv_summary_v2.15"; - - set session sort_buffer_size=512000000; - - select @sep := " ## "; - select @lab_encounter_type := 99999; - select @death_encounter_type := 31; - select @last_date_created := (select max(max_date_created) from etl.flat_obs); - - - - - create table if not exists flat_hiv_summary_v15 ( - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - is_clinical_encounter int, - - location_id int, - location_uuid varchar(100), - - visit_num int, - - enrollment_date datetime, - enrollment_location_id int, - - hiv_start_date datetime, - - death_date datetime, - scheduled_visit int, - - transfer_in tinyint, - transfer_in_location_id int, - transfer_in_date datetime, - - transfer_out tinyint, - transfer_out_location_id int, - transfer_out_date datetime, - - patient_care_status int, - out_of_care int, - prev_rtc_date datetime, - rtc_date datetime, - - arv_first_regimen varchar(500), - arv_first_regimen_location_id int, - arv_first_regimen_start_date datetime, - - prev_arv_meds varchar(500), - cur_arv_meds varchar(500), - arv_start_date datetime, - arv_start_location_id int, - prev_arv_start_date datetime, - prev_arv_end_date datetime, - - prev_arv_line int, - cur_arv_line int, - - - prev_arv_adherence varchar(200), - cur_arv_adherence varchar(200), - hiv_status_disclosed int, - - first_evidence_patient_pregnant datetime, - edd datetime, - - screened_for_tb boolean, - - ipt_start_date datetime, - ipt_stop_date datetime, - ipt_completion_date datetime, - - tb_tx_start_date datetime, - tb_tx_end_date datetime, - - pcp_prophylaxis_start_date datetime, - - condoms_provided_date datetime, - modern_contraceptive_method_start_date datetime, - cur_who_stage int, - - cd4_resulted double, - cd4_resulted_date datetime, - cd4_1 double, - cd4_1_date datetime, - cd4_2 double, - cd4_2_date datetime, - cd4_percent_1 double, - cd4_percent_1_date datetime, - cd4_percent_2 double, - cd4_percent_2_date datetime, - vl_resulted int, - vl_resulted_date datetime, - vl_1 int, - vl_1_date datetime, - vl_2 int, - vl_2_date datetime, - vl_order_date datetime, - cd4_order_date datetime, - - - hiv_dna_pcr_order_date datetime, - hiv_dna_pcr_resulted int, - hiv_dna_pcr_resulted_date datetime, - hiv_dna_pcr_1 int, - hiv_dna_pcr_1_date datetime, - hiv_dna_pcr_2 int, - hiv_dna_pcr_2_date datetime, - - - hiv_rapid_test_resulted int, - hiv_rapid_test_resulted_date datetime, - - prev_encounter_datetime_hiv datetime, - next_encounter_datetime_hiv datetime, - prev_encounter_type_hiv mediumint, - next_encounter_type_hiv mediumint, - prev_clinical_datetime_hiv datetime, - next_clinical_datetime_hiv datetime, - prev_clinical_location_id mediumint, - next_clinical_location_id mediumint, - prev_clinical_rtc_date_hiv datetime, - next_clinical_rtc_date_hiv datetime, - - - - outreach_date_bncd datetime, - outreach_death_date_bncd datetime, - outreach_patient_care_status_bncd int, - transfer_date_bncd datetime, - transfer_transfer_out_bncd datetime, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index location_rtc (location_uuid,rtc_date), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_hiv), - index encounter_type (encounter_type) - ); - - select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); - - - select @last_update := - if(@last_update is null, - (select max(date_created) from amrs.encounter e join etl.flat_hiv_summary_v15 using (encounter_id)), - @last_update); - - - select @last_update := if(@last_update,@last_update,'1900-01-01'); - - - - - create table if not exists flat_hiv_summary_build_queue_2(person_id int, primary key (person_id)); - - - - - - select @num_ids := (select count(*) from flat_hiv_summary_build_queue_2 limit 1); - - - - - if (@num_ids=0 or @query_type="sync") then - - replace into flat_hiv_summary_build_queue_2 - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - - replace into flat_hiv_summary_build_queue_2 - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - - - ); - - replace into flat_hiv_summary_build_queue_2 - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_build_queue_2 - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - end if; - - select @person_ids_count := (select count(*) from flat_hiv_summary_build_queue_2); - - delete t1 from flat_hiv_summary_v15 t1 join flat_hiv_summary_build_queue_2 t2 using (person_id); - - while @person_ids_count > 0 do - - - drop table if exists flat_hiv_summary_build_queue_2_0; - - create temporary table flat_hiv_summary_build_queue_2_0 (select * from flat_hiv_summary_build_queue_2 limit 100); - - - select @person_ids_count := (select count(*) from flat_hiv_summary_build_queue_2); - - drop temporary table if exists flat_hiv_summary_0a; - create temporary table flat_hiv_summary_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - 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,129,138,153,154,158) then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,115,117,120,127,128,138,129,153,154,158) then 10 - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join flat_hiv_summary_build_queue_2_0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_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,153,154,158) - AND NOT obs regexp "!!5303=(822|664)!!" - ); - - insert into flat_hiv_summary_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_hiv_summary_build_queue_2_0 t0 using (person_id) - ); - - drop temporary table if exists flat_hiv_summary_0; - create temporary table flat_hiv_summary_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - select @prev_id := null; - select @cur_id := null; - select @prev_encounter_date := null; - select @cur_encounter_date := null; - select @enrollment_date := null; - select @hiv_start_date := null; - select @cur_location := null; - select @cur_rtc_date := null; - select @prev_rtc_date := null; - select @hiv_start_date := null; - select @prev_arv_start_date := null; - select @arv_start_date := null; - select @prev_arv_end_date := null; - select @arv_start_location_id := null; - select @art_first_regimen_start_date := null; - select @arv_first_regimen := null; - select @prev_arv_line := null; - select @cur_arv_line := null; - select @prev_arv_adherence := null; - select @cur_arv_adherence := null; - select @hiv_status_disclosed := null; - select @first_evidence_pt_pregnant := null; - select @edd := null; - select @prev_arv_meds := null; - select @cur_arv_meds := null; - select @ipt_start_date := null; - select @ipt_end_date := null; - select @ipt_completion_date := null; - select @tb_treatment_start_date := null; - select @tb_treatment_end_date := null; - select @pcp_prophylaxis_start_date := null; - select @screened_for_tb := null; - select @death_date := null; - - select @patient_care_status:=null; - - select @condoms_provided_date := null; - select @modern_contraceptive_method_start_date := null; - - - select @cur_who_stage := null; - - select @vl_1:=null; - select @vl_2:=null; - select @vl_1_date:=null; - select @vl_2_date:=null; - select @vl_resulted:=null; - select @vl_resulted_date:=null; - - select @cd4_resulted:=null; - select @cd4_resulted_date:=null; - select @cd4_1:=null; - select @cd4_1_date:=null; - select @cd4_2:=null; - select @cd4_2_date:=null; - select @cd4_percent_1:=null; - select @cd4_percent_1_date:=null; - select @cd4_percent_2:=null; - select @cd4_percent_2_date:=null; - select @vl_order_date := null; - select @cd4_order_date := null; - - select @hiv_dna_pcr_order_date := null; - select @hiv_dna_pcr_1:=null; - select @hiv_dna_pcr_2:=null; - select @hiv_dna_pcr_1_date:=null; - select @hiv_dna_pcr_2_date:=null; - - select @hiv_rapid_test_resulted:=null; - select @hiv_rapid_test_resulted_date:= null; - - - - - - - drop temporary table if exists flat_hiv_summary_1; - create temporary table flat_hiv_summary_1 (index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.encounter_id, - @prev_encounter_date := date(@cur_encounter_date) as prev_encounter_date, - @cur_encounter_date := date(encounter_datetime) as cur_encounter_date, - t1.encounter_datetime, - t1.encounter_type, - t1.is_clinical_encounter, - - case - when @enrollment_date is null and obs regexp "!!7013=" then @enrollment_date := replace(replace((substring_index(substring(obs,locate("!!7013=",obs)),@sep,1)),"!!7013=",""),"!!","") - when obs regexp "!!7015=" and @enrollment_date is null then @enrollmen_date := "1900-01-01" - when t1.encounter_type not in (21,@lab_encounter_type) and @enrollment_date is null then @enrollment_date := date(encounter_datetime) - when @prev_id = @cur_id then @enrollment_date - else @enrollment_date := null - end as enrollment_date, - - case - when @enrollment_location_id is null and obs regexp "!!7030=5622" then @enrollment_location_id := 9999 - when obs regexp "!!7015=" and @enrollment_location_id is null then @enrollmen_location_id := 9999 - when t1.encounter_type not in (21,@lab_encounter_type) and @enrollment_location_id is null then @enrollment_location_id := location_id - when @prev_id = @cur_id then @enrollment_location_id - else @enrollment_location_id := null - end as enrollment_location_id, - - - - - - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as 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 - end as visit_num, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - - - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504|1285)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - - - - - case - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @death_encounter_type then @patient_care_status := 159 - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - - case - when obs regexp "!!9203=" then @hiv_start_date := replace(replace((substring_index(substring(obs,locate("!!9203=",obs)),@sep,1)),"!!9203=",""),"!!","") - when obs regexp "!!7015=" then @hiv_start_date := "1900-01-01" - when @hiv_start_date is null then @hiv_start_date := date(encounter_datetime) - when @prev_id = @cur_id then @hiv_start_date - else @hiv_start_date := null - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_location_id := location_id - when @prev_id = @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location_id := location_id - when @prev_id != @cur_id then @arv_start_location_id := null - else @arv_start_location_id - end as arv_start_location_id, - - case - when @prev_id=@cur_id and @cur_arv_meds is not null then @prev_arv_meds := @cur_arv_meds - when @prev_id=@cur_id then @prev_arv_meds - else @prev_arv_meds := null - end as prev_arv_meds, - - - - - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!1250=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1250=", "") ) ) / LENGTH("!!1250=") ))),"!!1250=",""),"!!","") - when obs regexp "!!1088=" then @cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!1088=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1088=", "") ) ) / LENGTH("!!1088=") ))),"!!1088=",""),"!!","") - when obs regexp "!!2154=" then @cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!2154=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!2154=", "") ) ) / LENGTH("!!2154=") ))),"!!2154=",""),"!!","") - else @cur_arv_meds:= null - end as cur_arv_meds, - - - case - when @arv_first_regimen is null and obs regexp "!!2157=" then - @arv_first_regimen := replace(replace((substring_index(substring(obs,locate("!!2157=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!2157=", "") ) ) / LENGTH("!!2157=") ))),"!!2157=",""),"!!","") - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id = @cur_id then @arv_first_regimen - when @prev_id != @cur_id then @arv_first_regimen := null - else "-1" - end as arv_first_regimen, - - - - case - when @arv_first_regimen is null and obs regexp "!!1499=" then @arv_first_regimen_start_date := replace(replace((substring_index(substring(obs,locate("!!1499=",obs)),@sep,1)),"!!1499=",""),"!!","") - when @arv_first_regimen_start_date is null and @cur_arv_meds is not null then @arv_first_regimen_start_date := date(t1.encounter_datetime) - when @prev_id = @cur_id then @arv_first_regimen_start_date - when @prev_id != @cur_id then @arv_first_regimen_start_date := null - else @arv_first_regimen_start_date - end as arv_first_regimen_start_date, - - case - when @arv_first_regimen is null and obs regexp "!!1499=" then @arv_first_regimen_location_id := 9999 - when @arv_first_regimen_location_id is null and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @prev_id = @cur_id then @arv_first_regimen_location_id - when @prev_id != @cur_id then @arv_first_regimen_location_id := null - else "-1" - end as arv_first_regimen_location_id, - - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := null - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=(6156|9759)!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=(6156|9759)!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=(6156|9759)!!" then @cur_arv_line := 3 - when @prev_id = @cur_id then @cur_arv_line - - else @cur_arv_line := null - end as cur_arv_line, - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := null - end as prev_arv_start_date, - - - - - - - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - when @cur_arv_meds != @prev_arv_meds and @cur_arv_line != @prev_arv_line then @arv_start_date := date(t1.encounter_datetime) - when @prev_id != @cur_id then @arv_start_date := null - else @arv_start_date - end as arv_start_date, - - - case - when @prev_arv_start_date != @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end as prev_arv_end_date, - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := null - end as prev_arv_adherence, - - - - - - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id = @cur_id then @cur_arv_adherence - else @cur_arv_adherence := null - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when obs regexp "!!6596=" then @hiv_status_disclosed := null - when @prev_id != @cur_id then @hiv_status_disclosed := null - else @hiv_status_disclosed - end as hiv_status_disclosed, - - - - - - - case - when @prev_id != @cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" then @first_evidence_pt_pregnant := encounter_datetime - else @first_evidence_pt_pregnant := null - end - when @first_evidence_pt_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") then @first_evidence_pt_pregnant := encounter_datetime - when @first_evidence_pt_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@first_evidence_pt_pregnant,encounter_datetime) > 40 or timestampdiff(week,@edd,encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @first_evidence_pt_pregnant := null - else @first_evidence_pt_pregnant - end as first_evidence_patient_pregnant, - - - - - - - - case - when @prev_id != @cur_id then - case - when @first_evidence_patient_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - when @first_evidence_pt_pregnant then @edd := date_add(@first_evidence_pt_pregnant,interval 6 month) - else @edd := null - end - when @edd is null or @edd = @first_evidence_pt_pregnant then - case - when @first_evidence_pt_pregnant then @edd := date_add(@first_evidence_pt_pregnant,interval 6 month) - when @first_evidence_patient_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - when @first_evidence_pt_pregnant then @edd := date_add(@first_evidence_pt_pregnant,interval 6 month) - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - - - - - - - - case - when obs regexp "!!6174=" then @screened_for_tb := true - when obs regexp "!!2022=1065!!" then @screened_for_tb := true - when obs regexp "!!307=" then @screened_for_tb := true - when obs regexp "!!12=" then @screened_for_tb := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @screened_for_tb := true - when orders regexp "(12|307|8064|2311|2323)" then @screened_for_tb := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @screened_for_tb := true - when obs regexp "!!5958=1077!!" then @screened_for_tb := true - when obs regexp "!!2020=1065!!" then @screened_for_tb := true - when obs regexp "!!2021=1065!!" then @screened_for_tb := true - when obs regexp "!!2028=" then @screened_for_tb := true - when obs regexp "!!1268=(1256|1850)!!" then @screened_for_tb := true - when obs regexp "!!5959=(1073|1074)!!" then @screened_for_tb := true - when obs regexp "!!5971=(1073|1074)!!" then @screened_for_tb := true - when obs regexp "!!1492=107!!" then @screened_for_tb := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @screened_for_tb := true - end as screened_for_tb, - - - case - when obs regexp "!!1265=(1107|1260)!!" then @on_ipt := 0 - when obs regexp "!!1265=!!" then @on_ipt := 1 - when obs regexp "!!1110=656!!" then @on_ipt := 1 - when @prev_id = @cur_id then @on_ipt - else null - end as on_ipt, - - - - - - case - when obs regexp "!!1265=(1256|1850)!!" then @ipt_start_date := encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" and @ipt_start_date is null then @ipt_start_date := encounter_datetime - when @cur_id = @prev_id then @ipt_start_date - else @ipt_start_date := null - end as ipt_start_date, - - - - - - - case - when obs regexp "!!1266=!!" then @ipt_stop_date := encounter_datetime - when @cur_id = @prev_id then @ipt_stop_date - else @ipt_stop_date - end as ipt_stop_date, - - case - when obs regexp "!!1266=1267!!" then @ipt_completion_date := encounter_datetime - when @cur_id = @prev_id then @ipt_completion_date - else @ipt_completion_date - end as ipt_completion_date, - - - case - when obs regexp "!!1268=(1107|1260)!!" then @on_tb_treatment := 0 - when obs regexp "!!1268=" then @on_tb_treatment := 1 - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @on_tb_treatment := 1 - else 0 - end as on_tb_tx, - - - - - - - case - when obs regexp "!!1113=" then @tb_treatment_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @tb_treatment_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" and obs regexp "!!7015=" and @tb_treatment_start_date is null then @tb_treatment_start_date := null - when obs regexp "!!1268=(1257|1259|1849|981)!!" and @tb_treatment_start_date is null then @tb_treatment_start_date := encounter_datetime - - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" and @tb_treatment_start_date is null then @tb_treatment_start_date := encounter_datetime - when @cur_id = @prev_id then @tb_treatment_start_date - else @tb_treatment_start_date := null - end as tb_tx_start_date, - - - - - case - when obs regexp "!!2041=" then @tb_treatment_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_treatment_end_date := encounter_datetime - when @cur_id = @prev_id then @tb_treatment_end_date - else @tb_treatment_end_date := null - end as tb_tx_end_date, - - - - - - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1261=1257!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1193=916!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when @prev_id=@cur_id then @pcp_prophylaxis_start_date - else @pcp_prophylaxis_start_date := null - end as pcp_prophylaxis_start_date, - - - - - - - - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when @prev_id != @cur_id or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := encounter_datetime - else @death_date := null - end - else @death_date - end as death_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - else @cd4_2:=null - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - else @cd4_2_date:=null - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=null - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=null - end as cd4_1_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - else @cd4_percent_2:=null - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - else @cd4_percent_2_date:=null - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=null - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=null - end as cd4_percent_1_date, - - - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) <>date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - else @vl_2:=null - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - else @vl_2_date:=null - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=null - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date:=null - end as vl_1_date, - - - - - - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(encounter_datetime) - when orders regexp "856" then @vl_order_date := date(encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - else @vl_order_date := null - end as vl_order_date, - - - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := null - end as cd4_order_date, - - - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := null - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - else @hiv_dna_pcr_2:=null - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - else @hiv_dna_pcr_2_date:=null - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=null - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=null - end as hiv_dna_pcr_1_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - case - when obs regexp "!!8302=8305!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!6579=" then @condoms_provided_date := encounter_datetime - when @prev_id = @cur_id then @condoms_provided_date - else @condoms_provided_date := null - end as condoms_provided_date, - - - - - - - - - - case - when obs regexp "!!7240=(5275|6220|780|5279)!!" - then @modern_contraceptive_method_start_date := encounter_datetime - when obs regexp "!!374=(5275|6220|780|5279)!!" and obs regexp "!!1190=" - then @modern_contraceptive_method_start_date := replace(replace((substring_index(substring(obs,locate("!!1190=",obs)),@sep,1)),"!!1190=",""),"!!","") - when obs regexp "!!374=(5275|6220|780|5279)!!" - then @modern_contraceptive_method_start_date := encounter_datetime - when @prev_id = @cur_id then @modern_contraceptive_method_start_date - else @modern_contraceptive_method_start_date := null - end as modern_contraceptive_method_start_date, - - - - - - - - - - - - - - case - when obs regexp "!!5356=(1204)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1223)!!" then @cur_who_stage := 4 - when @prev_id = @cur_id then @cur_who_stage - else @cur_who_stage := null - end as cur_who_stage - - from flat_hiv_summary_0 t1 - join amrs.person p using (person_id) - ); - - - - select @prev_id := null; - select @cur_id := null; - select @prev_encounter_datetime := null; - select @cur_encounter_datetime := null; - - select @prev_clinical_datetime := null; - select @cur_clinical_datetime := null; - - select @next_encounter_type := null; - select @cur_encounter_type := null; - - select @prev_clinical_location_id := null; - select @cur_clinical_location_id := null; - - - alter table flat_hiv_summary_1 drop prev_id, drop cur_id; - - drop table if exists flat_hiv_summary_2; - create temporary table flat_hiv_summary_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_date_bncd - else null - end as outreach_date_bncd, - - case - when encounter_type=21 and @outreach_date_bncd is null then @outreach_date_bncd := encounter_datetime - when is_clinical_encounter then @outreach_date_bncd := null - when @prev_id != @cur_id then @outreach_date_bncd := null - else @outreach_date_bncd - end as next_outreach_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_death_date_bncd - else null - end as outreach_death_date_bncd, - - case - when encounter_type=21 and @outreach_death_date_bncd is null then @outreach_death_date_bncd := death_date - when is_clinical_encounter then @outreach_death_date_bncd := null - when @prev_id != @cur_id then @outreach_death_date_bncd := null - else @outreach_death_date_bncd - end as next_outreach_death_date_bncd, - - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then cast(@outreach_patient_care_status_bncd as unsigned) - else null - end as outreach_patient_care_status_bncd, - - case - when encounter_type=21 and @outreach_patient_care_status_bncd is null then @outreach_patient_care_status_bncd := patient_care_status - when is_clinical_encounter then @outreach_patient_care_status_bncd := null - when @prev_id != @cur_id then @outreach_patient_care_status_bncd := null - else @outreach_patient_care_status_bncd - end as next_outreach_patient_care_status_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_date_bncd - else null - end as transfer_date_bncd, - - case - when encounter_type=116 and @transfer_date_bncd is null then @transfer_date_bncd := encounter_datetime - when is_clinical_encounter then @transfer_date_bncd := null - when @prev_id != @cur_id then @transfer_date_bncd := null - else @transfer_date_bncd - end as next_transfer_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_transfer_out_bncd - else null - end as transfer_transfer_out_bncd, - - case - when encounter_type=116 and @transfer_transfer_out_bncd is null then @transfer_transfer_out_bncd := encounter_datetime - when is_clinical_encounter then @transfer_transfer_out_bncd := null - when @prev_id != @cur_id then @transfer_transfer_out_bncd := null - else @transfer_transfer_out_bncd - end as next_transfer_transfer_out_bncd - - from flat_hiv_summary_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - select @prev_id := null; - select @cur_id := null; - select @prev_encounter_type := null; - select @cur_encounter_type := null; - select @prev_encounter_datetime := null; - select @cur_encounter_datetime := null; - select @prev_clinical_datetime := null; - select @cur_clinical_datetime := null; - select @prev_clinical_location_id := null; - select @cur_clinical_location_id := null; - - drop temporary table if exists flat_hiv_summary_3; - create temporary table flat_hiv_summary_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_hiv, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_hiv_summary_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - alter table flat_hiv_summary_3 drop prev_id, drop cur_id; - - select @prev_id := null; - select @cur_id := null; - select @transfer_in := null; - select @transfer_in_date := null; - select @transfer_in_location_id := null; - select @transfer_out := null; - select @transfer_out_date := null; - select @transfer_out_location_id := null; - - drop temporary table if exists flat_hiv_summary_4; - - create temporary table flat_hiv_summary_4 ( index person_enc (person_id, encounter_datetime)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - - - case - when obs regexp "!!7015=" then @transfer_in := 1 - when prev_clinical_location_id != location_id then @transfer_in := 1 - else @transfer_in := null - end as transfer_in, - - case - when obs regexp "!!7015=" then @transfer_in_date := encounter_datetime - when prev_clinical_location_id != location_id then @transfer_in_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 then @transfer_in_location_id := prev_clinical_location_id - when @cur_id = @prev_id then @transfer_in_location_id - else @transfer_in_location_id := null - end transfer_in_location_id, - - - - - - - - - - - - case - when obs regexp "!!1285=!!" then @transfer_out := 1 - when obs regexp "!!1596=1594!!" then @transfer_out := 1 - when obs regexp "!!9082=(1287|1594|9068|9504|1285)!!" then @transfer_out := 1 - when next_clinical_location_id != location_id then @transfer_out := 1 - else @transfer_out := null - end as transfer_out, - - case - when obs regexp "!!1285=(1287|9068|2050)!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9999 - when obs regexp "!!1285=1286!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9998 - when next_clinical_location_id != location_id then @transfer_out_location_id := next_clinical_location_id - else @transfer_out_location_id := null - end transfer_out_location_id, - - - case - when @transfer_out and next_clinical_datetime_hiv is null then @transfer_out_date := cur_rtc_date - when next_clinical_location_id != location_id then @transfer_out_date := next_clinical_datetime_hiv - when transfer_transfer_out_bncd then @transfer_out_date := transfer_transfer_out_bncd - else @transfer_out_date := null - end transfer_out_date - - - - from flat_hiv_summary_3 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - replace into flat_hiv_summary_v15 - (select - person_id, - t1.uuid, - visit_id, - encounter_id, - encounter_datetime, - encounter_type, - is_clinical_encounter, - - location_id, - t2.uuid as location_uuid, - - visit_num, - - enrollment_date, - enrollment_location_id, - - hiv_start_date, - - death_date, - scheduled_visit, - - transfer_in, - transfer_in_location_id , - transfer_in_date, - - transfer_out, - transfer_out_location_id, - transfer_out_date, - - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date as rtc_date, - - arv_first_regimen , - arv_first_regimen_location_id, - arv_first_regimen_start_date, - - prev_arv_meds, - cur_arv_meds, - arv_start_date , - arv_start_location_id, - prev_arv_start_date, - prev_arv_end_date, - - prev_arv_line, - cur_arv_line, - - - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - - first_evidence_patient_pregnant, - edd, - - screened_for_tb, - - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - - tb_tx_start_date, - tb_tx_end_date, - - pcp_prophylaxis_start_date, - - condoms_provided_date, - modern_contraceptive_method_start_date, - cur_who_stage, - - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - vl_order_date, - cd4_order_date, - - - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - - - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_location_id, - next_clinical_location_id, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv, - - - outreach_date_bncd, - outreach_death_date_bncd, - outreach_patient_care_status_bncd, - transfer_date_bncd, - transfer_transfer_out_bncd - - from flat_hiv_summary_4 t1 - join amrs.location t2 using (location_id)); - - delete from flat_hiv_summary_build_queue_2 where person_id in (select person_id from flat_hiv_summary_build_queue_2_0); - - end while; - - - delete t1 - from flat_hiv_summary_v15 t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value='true' and voided=0; - - select @end := now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_v15_3.sql b/etl-scripts/stored-procedures/generate_hiv_summary_v15_3.sql deleted file mode 100644 index 4b4e309..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_v15_3.sql +++ /dev/null @@ -1,1709 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_v15_3`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_hiv_summary_v15b"; - set @query_type = query_type; - set @queue_table = ""; - set @total_rows_written = 0; - - set @start = now(); - set @table_version = "flat_hiv_summary_v2.15"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - #delete from etl.flat_log where table_name like "%15%"; - #drop table etl.flat_hiv_summary_v15b; - create table if not exists flat_hiv_summary_v15b ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - is_clinical_encounter int, - location_id int, - location_uuid varchar(100), - visit_num int, - enrollment_date datetime, - enrollment_location_id int, - hiv_start_date datetime, - death_date datetime, - scheduled_visit int, - transfer_in tinyint, - transfer_in_location_id int, - transfer_in_date datetime, - transfer_out tinyint, - transfer_out_location_id int, - transfer_out_date datetime, - patient_care_status int, - out_of_care int, - prev_rtc_date datetime, - rtc_date datetime, - arv_first_regimen varchar(500), - arv_first_regimen_location_id int, - arv_first_regimen_start_date datetime, - prev_arv_meds varchar(500), - cur_arv_meds varchar(500), - cur_arv_meds_strict varchar(500), - arv_start_date datetime, - arv_start_location_id int, - prev_arv_start_date datetime, - prev_arv_end_date datetime, - prev_arv_line int, - cur_arv_line int, - cur_arv_line_strict int, - cur_arv_line_reported tinyint, - prev_arv_adherence varchar(200), - cur_arv_adherence varchar(200), - hiv_status_disclosed int, - is_pregnant boolean, - edd datetime, - tb_screen boolean, - tb_screening_result int, - tb_screening_datetime datetime, - on_ipt boolean, - ipt_start_date datetime, - ipt_stop_date datetime, - ipt_completion_date datetime, - on_tb_tx boolean, - tb_tx_start_date datetime, - tb_tx_end_date datetime, - pcp_prophylaxis_start_date datetime, - condoms_provided_date datetime, - modern_contraceptive_method_start_date datetime, - contraceptive_method int, - cur_who_stage int, - discordant_status int, - cd4_resulted double, - cd4_resulted_date datetime, - cd4_1 double, - cd4_1_date datetime, - cd4_2 double, - cd4_2_date datetime, - cd4_percent_1 double, - cd4_percent_1_date datetime, - cd4_percent_2 double, - cd4_percent_2_date datetime, - vl_resulted int, - vl_resulted_date datetime, - vl_1 int, - vl_1_date datetime, - vl_2 int, - vl_2_date datetime, - vl_order_date datetime, - cd4_order_date datetime, - hiv_dna_pcr_order_date datetime, - hiv_dna_pcr_resulted int, - hiv_dna_pcr_resulted_date datetime, - hiv_dna_pcr_1 int, - hiv_dna_pcr_1_date datetime, - hiv_dna_pcr_2 int, - hiv_dna_pcr_2_date datetime, - hiv_rapid_test_resulted int, - hiv_rapid_test_resulted_date datetime, - prev_encounter_datetime_hiv datetime, - next_encounter_datetime_hiv datetime, - prev_encounter_type_hiv mediumint, - next_encounter_type_hiv mediumint, - prev_clinical_datetime_hiv datetime, - next_clinical_datetime_hiv datetime, - prev_clinical_location_id mediumint, - next_clinical_location_id mediumint, - prev_clinical_rtc_date_hiv datetime, - next_clinical_rtc_date_hiv datetime, - outreach_date_bncd datetime, #bncd means before next clinical datetime - outreach_death_date_bncd datetime, - outreach_patient_care_status_bncd int, - transfer_date_bncd datetime, - transfer_transfer_out_bncd datetime, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_hiv), - index encounter_type (encounter_type), - index date_created (date_created) - - ); - - - - 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 @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_hiv_summary_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #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_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_hiv_summary_v15b"; - set @queue_table = "flat_hiv_summary_sync_queue"; - create table if not exists flat_hiv_summary_sync_queue (person_id int primary key); - - - - set @last_update = null; - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - - replace into flat_hiv_summary_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - # Remove test patients - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - select @person_ids_count as 'num patients to sync'; - -# select @person_ids_count := (select count(*) from flat_hiv_summary_build_queue_2); - - #delete t1 from flat_hiv_summary_v15b t1 join @queue_table t2 using (person_id); - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists flat_hiv_summary_build_queue__0; - - - #create temporary table flat_hiv_summary_build_queue__0 (select * from flat_hiv_summary_build_queue_2 limit 5000); #TODO - change this when data_fetch_size changes - SET @dyn_sql=CONCAT('create temporary table flat_hiv_summary_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - drop temporary table if exists flat_hiv_summary_0a; - create temporary table flat_hiv_summary_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - # in any visit, there many be multiple encounters. for this dataset, we want to include only clinical encounters (e.g. not lab or triage visit) - 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,129,138,153,154,158,162,163) then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,115,117,120,127,128,138, 153,154,158,162,163) then 10 - when t1.encounter_type in (129) then 5 #MDT ahead of other clinical encounters - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_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,153,154,158, 161,162,163) - AND NOT obs regexp "!!5303=(822|664|1067)!!" - AND NOT obs regexp "!!9082=9036!!" - ); - - insert into flat_hiv_summary_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, #t1.location_id, - t1.obs, - null, #obs_datetimes - # in any visit, there many be multiple encounters. for this dataset, we want to include only clinical encounters (e.g. not lab or triage visit) - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - ); - - drop temporary table if exists flat_hiv_summary_0; - create temporary table flat_hiv_summary_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_date = null; - set @cur_encounter_date = null; - set @enrollment_date = null; - set @hiv_start_date = null; - set @cur_location = null; - set @cur_rtc_date = null; - set @prev_rtc_date = null; - set @hiv_start_date = null; - set @prev_arv_start_date = null; - set @arv_start_date = null; - set @prev_arv_end_date = null; - set @arv_start_location_id = null; - set @art_first_regimen_start_date = null; - set @arv_first_regimen = null; - set @prev_arv_line = null; - set @cur_arv_line = null; - set @prev_arv_adherence = null; - set @cur_arv_adherence = null; - set @hiv_status_disclosed = null; - set @is_pregnant = null; - set @edd = null; - set @prev_arv_meds = null; - set @cur_arv_meds = null; - set @ipt_start_date = null; - set @ipt_end_date = null; - set @ipt_completion_date = null; - - set @on_tb_tx = null; - set @tb_tx_start_date = null; - set @tb_tx_end_date = null; - set @pcp_prophylaxis_start_date = null; - set @tb_screen = null; - set @tb_screening_result = null; - set @tb_screening_datetime = null; - - set @death_date = null; - - set @patient_care_status=null; - - set @condoms_provided_date = null; - set @modern_contraceptive_method_start_date = null; - set @contraceptive_method = null; - - #Current WHO Stage - set @cur_who_stage = null; - - set @vl_1=null; - set @vl_2=null; - set @vl_1_date=null; - set @vl_2_date=null; - set @vl_resulted=null; - set @vl_resulted_date=null; - - set @cd4_resulted=null; - set @cd4_resulted_date=null; - set @cd4_1=null; - set @cd4_1_date=null; - set @cd4_2=null; - set @cd4_2_date=null; - set @cd4_percent_1=null; - set @cd4_percent_1_date=null; - set @cd4_percent_2=null; - set @cd4_percent_2_date=null; - set @vl_order_date = null; - set @cd4_order_date = null; - - set @hiv_dna_pcr_order_date = null; - set @hiv_dna_pcr_1=null; - set @hiv_dna_pcr_2=null; - set @hiv_dna_pcr_1_date=null; - set @hiv_dna_pcr_2_date=null; - - set @hiv_rapid_test_resulted=null; - set @hiv_rapid_test_resulted_date= null; - - - #TO DO - # screened for cervical ca - # exposed infant - - drop temporary table if exists flat_hiv_summary_1; - create temporary table flat_hiv_summary_1 (index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.encounter_id, - @prev_encounter_date := date(@cur_encounter_date) as prev_encounter_date, - @cur_encounter_date := date(encounter_datetime) as cur_encounter_date, - t1.encounter_datetime, - t1.encounter_type, - t1.is_clinical_encounter, - - case - when @enrollment_date is null and obs regexp "!!7013=" then @enrollment_date := replace(replace((substring_index(substring(obs,locate("!!7013=",obs)),@sep,1)),"!!7013=",""),"!!","") - when obs regexp "!!7015=" and @enrollment_date is null then @enrollmen_date := "1900-01-01" - when t1.encounter_type not in (21,@lab_encounter_type) and @enrollment_date is null then @enrollment_date := date(encounter_datetime) - when @prev_id = @cur_id then @enrollment_date - else @enrollment_date := null - end as enrollment_date, - - case - when @enrollment_location_id is null and obs regexp "!!7030=5622" then @enrollment_location_id := 9999 - when obs regexp "!!7015=" and @enrollment_location_id is null then @enrollmen_location_id := 9999 - when t1.encounter_type not in (21,@lab_encounter_type) and @enrollment_location_id is null then @enrollment_location_id := location_id - when @prev_id = @cur_id then @enrollment_location_id - else @enrollment_location_id := null - end as enrollment_location_id, - - #1839 = CURRENT VISIT TYPE - #1246 = SCHEDULED VISIT - #1838 = UNSCHEDULED VISIT LATE - #1837 = UNSCHEDULED VISIT EARLY - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as 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 - end as visit_num, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - # 5096 = return visit date - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - - # 1946 = DISCONTINUE FROM CLINIC, HIV NEGATIVE - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504|1285)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - # 9082 = PATIENT CARE STATUS - # 6101 = CONTINUE - # 1946 = DISCONTINUE FROM CLINIC, HIV NEGATIVE (this is a question) - # 9036 = HIV NEGATIVE, NO LONGER AT RISK - case - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @death_encounter_type then @patient_care_status := 159 - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - # 9203 = DATE OF HIV DIAGNOSIS - case - when obs regexp "!!9203=" then @hiv_start_date := replace(replace((substring_index(substring(obs,locate("!!9203=",obs)),@sep,1)),"!!9203=",""),"!!","") - when obs regexp "!!7015=" then @hiv_start_date := "1900-01-01" - when @hiv_start_date is null then @hiv_start_date := date(encounter_datetime) - when @prev_id = @cur_id then @hiv_start_date - else @hiv_start_date := null - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_location_id := location_id - when @prev_id = @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location_id := location_id - when @prev_id != @cur_id then @arv_start_location_id := null - else @arv_start_location_id - end as arv_start_location_id, - - - case - when @prev_id=@cur_id and @cur_arv_meds is not null then @prev_arv_meds := @cur_arv_meds - when @prev_id=@cur_id then @prev_arv_meds - else @prev_arv_meds := null - end as prev_arv_meds, - - # 1255 = ANTIRETROVIRAL PLAN - # 1250 = ANTIRETROVIRALS STARTED - # 1088 = CURRENT ANTIRETROVIRAL DRUGS USED FOR TREATMENT - # 2154 = PATIENT REPORTED CURRENT ANTIRETROVIRAL TREATMENT - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - #@cur_arv_meds := getArvConceptIds(obs,1250) - #replace(replace((substring_index(substring(obs,locate("!!1250=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1250=", "") ) ) / LENGTH("!!1250=") ))),"!!1250=",""),"!!","") - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - #@cur_arv_meds := getArvConceptIds(obs,1088) - #replace(replace((substring_index(substring(obs,locate("!!1088=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1088=", "") ) ) / LENGTH("!!1088=") ))),"!!1088=",""),"!!","") - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - #@cur_arv_meds :=getArvConceptIds(obs,2154) - #replace(replace((substring_index(substring(obs,locate("!!2154=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!2154=", "") ) ) / LENGTH("!!2154=") ))),"!!2154=",""),"!!","") - when @prev_id = @cur_id then @cur_arv_meds - else @cur_arv_meds:= null - end as cur_arv_meds, - - - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - #getArvConceptIds(obs,1250) - #replace(replace((substring_index(substring(obs,locate("!!1250=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1250=", "") ) ) / LENGTH("!!1250=") ))),"!!1250=",""),"!!","") - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - #getArvConceptIds(obs,1088) - #replace(replace((substring_index(substring(obs,locate("!!1088=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1088=", "") ) ) / LENGTH("!!1088=") ))),"!!1088=",""),"!!","") - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - #getArvConceptIds(obs,2154) - #replace(replace((substring_index(substring(obs,locate("!!2154=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!2154=", "") ) ) / LENGTH("!!2154=") ))),"!!2154=",""),"!!","") - else null - end as cur_arv_meds_strict, - - - - case - when @arv_first_regimen is null and obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @arv_first_regimen := normalize_arvs(obs,'2157') - when obs regexp "!!7015=" and @arv_first_regimen is null then @arv_first_regimen := "unknown" - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id = @cur_id then @arv_first_regimen - when @prev_id != @cur_id then @arv_first_regimen := @cur_arv_meds - else "-1" - end as arv_first_regimen, - - - # 1499 = ANTIRETROVIRAL DRUG TREATMENT START DATE - case - when @arv_first_regimen_start_date is null and obs regexp "!!1499=" then @arv_first_regimen_start_date := replace(replace((substring_index(substring(obs,locate("!!1499=",obs)),@sep,1)),"!!1499=",""),"!!","") - when obs regexp "!!7015=" and @arv_first_regimen_start_date is null then @arv_first_regimen_start_date := "1900-01-01" - when @arv_first_regimen_start_date is null and @cur_arv_meds is not null then @arv_first_regimen_start_date := date(t1.encounter_datetime) - when @prev_id = @cur_id then @arv_first_regimen_start_date - when @prev_id != @cur_id and @cur_arv_meds is not null then @arv_first_regimen_start_date := date(t1.encounter_datetime) - when @prev_id != @cur_id then @arv_first_regimen_start_date := null - else @arv_first_regimen_start_date - end as arv_first_regimen_start_date, - - # ADD strict defintion for arv_first_regimen_start_date, - # Make sure the start arv question is required - - case - when (@prev_id != @cur_id or @arv_first_regimen is null) and obs regexp "!!1499=" then @arv_first_regimen_location_id := 9999 - when (@prev_id != @cur_id or @arv_first_regimen_location_id is null) and @arv_first_regimen = "unknown" then @arv_first_regimen_location_id := 9999 - when (@prev_id != @cur_id or @arv_first_regimen_location_id is null) and @arv_first_regimen != "unknown" then @arv_first_regimen_location_id := location_id - when(@prev_id != @cur_id or @arv_first_regimen_location_id is null) and @arv_first_regimen is not null then @arv_first_regimen_location_id := location_id - when @prev_id = @cur_id then @arv_first_regimen_location_id - when @prev_id != @cur_id then @arv_first_regimen_location_id := null - else "-1" - end as arv_first_regimen_location_id, - - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := null - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=(6156)!!" then @cur_arv_line := 3 - when @prev_id = @cur_id then @cur_arv_line - else @cur_arv_line := null - end as cur_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1250=(6156)!!" then 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1088=(6156)!!" then 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then 2 - when obs regexp "!!2154=(6156)!!" then 3 - else null - end as cur_arv_line_strict, - - - # 6976 HIV ANTIRETROVIRAL DRUG PLAN TREATMENT CATEGORY - # 6744 CURRENT HIV ANTIRETROVIRAL DRUG USE TREATMENT CATEGORY - # 6695 THIRD LINE HIV ANTIRETROVIRAL DRUG TREATMENT - # 6693 FIRST LINE HIV ANTIRETROVIRAL DRUG TREATMENT - # 6694 SECOND LINE HIV ANTIRETROVIRAL DRUG TREATMENT - # UNKNOWN ANTIRETROVIRAL DRUG (5811) - # OTHER NON-CODED (5622) - case - when obs regexp "!!6976=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6976=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6976=6695!!" then @cur_arv_line_reported := 3 - - when obs regexp "!!6744=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6744=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6744=6695!!" then @cur_arv_line_reported := 3 - when @prev_id = @cur_id then @cur_arv_line_reported - else @cur_arv_line_reported := null - end as cur_arv_line_reported, - - - - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := null - end as prev_arv_start_date, - - # 1255 = ANTIRETROVIRAL PLAN - # 1250 = ANTIRETROVIRALS STARTED - # 1088 = CURRENT ANTIRETROVIRAL DRUGS USED FOR TREATMENT - # 2154 = PATIENT REPORTED CURRENT ANTIRETROVIRAL TREATMENT - # 1260 = STOP ALL MEDICATIONS - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - #when @cur_arv_meds != @prev_arv_meds and @cur_arv_line != @prev_arv_line then @arv_start_date := date(t1.encounter_datetime) - when @cur_arv_meds != @prev_arv_meds then @arv_start_date := date(t1.encounter_datetime) - - when @prev_id != @cur_id then @arv_start_date := null - else @arv_start_date - end as arv_start_date, - - - case - when @prev_arv_start_date != @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end as prev_arv_end_date, - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := null - end as prev_arv_adherence, - - # 8288 = ANTIRETROVIRAL ADHERENCE SINCE LAST VISIT - # 6343 = GOOD - # 6655 = FAIR - # 6656 = POOR - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id = @cur_id then @cur_arv_adherence - else @cur_arv_adherence := null - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when @prev_id = @cur_id then @hiv_status_disclosed - else @hiv_status_disclosed := null - end as hiv_status_disclosed, - - - # 1279 = NUMBER OF WEEKS PREGNANT - # 5596 = ESTIMATED DATE OF CONFINEMENT - # 5599 = DATE OF CONFINEMENT - # 1146 = CONCEPTION SINCE LAST VISIT - - # 8351 = PREGNANCY STATUS, CODED - # PREGNANCY, MISCARRIAGE (48) - # PREGNANCY, TERMINATION (50) - # NO (1066) - # YES (1065) - # DO NOT KNOW (1624) - # POSSIBLE (6971) - # PREGNANT (1484) - # NOT PREGNANT (9608) - - case - when obs regexp "!!8351=(48|50|1066|1624|6971|9608)!!" then @is_pregnant := null - when @prev_id != @cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" or obs regexp "!!8351=(1065|1484)!!" then @is_pregnant := true - else @is_pregnant := null - end - when @is_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") or obs regexp "!!8351=(1065|1484)!!"then @is_pregnant := true - when @is_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@is_pregnant,encounter_datetime) > 40 or timestampdiff(week,@edd,encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @is_pregnant := null - else @is_pregnant - end as is_pregnant, - - - # 1836 = LAST MENSTRUAL PERIOD DATE - # 1279 = NUMBER OF WEEKS PREGNANT - # 5596 = ESTIMATED DATE OF CONFINEMENT - # 5599 = DATE OF CONFINEMENT - # 1146 = CONCEPTION SINCE LAST VISIT - - case - when @prev_id != @cur_id then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd := null - end - when @edd is null then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - # 6174 = REVIEW OF TUBERCULOSIS SCREENING QUESTIONS - # 2022 = TUBERCULOSIS DIAGNOSED THIS VISIT - # 1268 = TUBERCULOSIS TREATMENT PLAN - # 1270 = TUBERCULOSIS TREATMENT STARTED - # 1257 = CONTINUE REGIMEN - # 1256 = START, - # 1850 = RESTART - case - when obs regexp "!!6174=" then @tb_screen := true #there is an obs for "any symptoms of tb?" - when obs regexp "!!2022=1065!!" then @tb_screen := true #1065 = yes - when obs regexp "!!307=" then @tb_screen := true #test result for tb - when obs regexp "!!12=" then @tb_screen := true #test result for tb - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screen := true #test ordered for tb - when orders regexp "(12|307|8064|2311|2323)" then @tb_screen := true #test ordered for tb - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screen := true #test results for tb - when obs regexp "!!5958=1077!!" then @tb_screen := true #means cough is bloody - when obs regexp "!!2020=1065!!" then @tb_screen := true #means a familiy member was diagnosed for tb - when obs regexp "!!2021=1065!!" then @tb_screen := true #means a familiy member was diagnosed for tb - when obs regexp "!!2028=" then @tb_screen := true #TB DIAGNOSIS MADE ON THE BASIS OF - when obs regexp "!!1268=(1256|1850)!!" then @tb_screen := true - when obs regexp "!!5959=(1073|1074)!!" then @tb_screen := true #COUGH DURATION, CODED - when obs regexp "!!5971=(1073|1074)!!" then @tb_screen := true #CHEST PAIN DURATION, CODED - when obs regexp "!!1492=107!!" then @tb_screen := true #REVIEW OF SYSTEM, EXPRESS CARE(1492)=COUGH (107) - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screen := true - end as tb_screen, - - case - when obs regexp "!!8292=" then @tb_screening_result := - replace(replace((substring_index(substring(obs,locate("!!8292=",obs)),@sep,1)),"!!8292=",""),"!!","") - when @prev_id = @cur_id then @tb_screening_result - else @tb_screening_result := null - end as tb_screening_result, - - case - when obs regexp "!!6174=" then @tb_screening_datetime := encounter_datetime #there is an obs for "any symptoms of tb?" - when obs regexp "!!2022=1065!!" then @tb_screening_datetime := encounter_datetime #1065 = yes - when obs regexp "!!307=" then @tb_screening_datetime := encounter_datetime #test result for tb - when obs regexp "!!12=" then @tb_screening_datetime := encounter_datetime #test result for tb - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime #test ordered for tb - when orders regexp "(12|307|8064|2311|2323)" then @tb_screening_datetime := encounter_datetime #test ordered for tb - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime #test results for tb - when obs regexp "!!5958=1077!!" then @tb_screening_datetime := encounter_datetime #means cough is bloody - when obs regexp "!!2020=1065!!" then @tb_screening_datetime := encounter_datetime #means a familiy member was diagnosed for tb - when obs regexp "!!2021=1065!!" then @tb_screening_datetime := encounter_datetime #means a familiy member was diagnosed for tb - when obs regexp "!!2028=" then @tb_screening_datetime := encounter_datetime #TB DIAGNOSIS MADE ON THE BASIS OF - when obs regexp "!!1268=(1256|1850)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5959=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime #COUGH DURATION, CODED - when obs regexp "!!5971=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime #CHEST PAIN DURATION, CODED - when obs regexp "!!1492=107!!" then @tb_screening_datetime := encounter_datetime #REVIEW OF SYSTEM, EXPRESS CARE(1492)=COUGH (107) - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screening_datetime := encounter_datetime - when @cur_id = @prev_id then @tb_screening_datetime - else @tb_screening_datetime := null - end as tb_screening_datetime, - - - case - when obs regexp "!!1265=(1107|1260)!!" then @on_ipt := 0 - when obs regexp "!!1265=!!" then @on_ipt := 1 - when obs regexp "!!1110=656!!" then @on_ipt := 1 - when @prev_id = @cur_id then @on_ipt - else null - end as on_ipt, - - # 1265 = TUBERCULOSIS PROPHYLAXIS PLAN - # 1268 = TUBERCULOSIS TREATMENT PLAN - # 1110 = PATIENT REPORTED CURRENT TUBERCULOSIS PROPHYLAXIS - # 656 = ISONIAZID - case - when obs regexp "!!1265=(1256|1850)!!" then @ipt_start_date := encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" and @ipt_start_date is null then @ipt_start_date := encounter_datetime - when @cur_id = @prev_id then @ipt_start_date - else @ipt_start_date := null - end as ipt_start_date, - - - # 1265 = TUBERCULOSIS PROPHYLAXIS PLAN - # 1268 = TUBERCULOSIS TREATMENT PLAN - # 1110 = PATIENT REPORTED CURRENT TUBERCULOSIS PROPHYLAXIS - # 656 = ISONIAZID - case - when obs regexp "!!1266=!!" then @ipt_stop_date := encounter_datetime - when @cur_id = @prev_id then @ipt_stop_date - else @ipt_stop_date - end as ipt_stop_date, - - case - when obs regexp "!!1266=1267!!" then @ipt_completion_date := encounter_datetime - when @cur_id = @prev_id then @ipt_completion_date - else @ipt_completion_date - end as ipt_completion_date, - - - case - when obs regexp "!!1268=(1107|1260)!!" then @on_tb_tx := 0 - when obs regexp "!!1268=" then @on_tb_tx := 1 #first statment above catches all negatives, this statement catches all codes where patient getting drugs - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @on_tb_tx := 1 - else 0 - end as on_tb_tx, - - # 1113 = TUBERCULOSIS DRUG TREATMENT START DATE - # 1111 = PATIENT REPORTED CURRENT TUBERCULOSIS TREATMENT - # 1267 = COMPLETED - # 1107 = NONE - # 1257 = CONTINUE REGIMEN - case - when obs regexp "!!1113=" then @tb_tx_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @tb_tx_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" and obs regexp "!!7015=" and @tb_tx_start_date is null then @tb_tx_start_date := null - when obs regexp "!!1268=(1257|1259|1849|981)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_start_date - else @tb_tx_start_date := null - end as tb_tx_start_date, - - # 1111 = PATIENT REPORTED CURRENT TUBERCULOSIS TREATMENT - # 1267 = COMPLETED - # 1107 = NONE - case - when obs regexp "!!2041=" then @tb_tx_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_tx_end_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_end_date - else @tb_tx_end_date := null - end as tb_tx_end_date, - - - # 1109 = PATIENT REPORTED CURRENT PCP PROPHYLAXIS - # 1261 = PCP PROPHYLAXIS PLAN - # 1193 = CURRENT MEDICATIONS - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1261=1257!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1193=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when @prev_id=@cur_id then @pcp_prophylaxis_start_date - else @pcp_prophylaxis_start_date := null - end as pcp_prophylaxis_start_date, - - # 1570 = DATE OF DEATH - # 1734 = DEATH REPORTED BY - # 1573 = CAUSE FOR DEATH - # 1733 = REASON FOR MISSED VISIT - # 9082 = PATIENT CARE STATUS - # 6206 = OUTCOME AT END OF TUBERCULOSIS TREATMENT - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when @prev_id != @cur_id or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := encounter_datetime - else @death_date := null - end - else @death_date - end as death_date, - - # 5497 = CD4, BY FACS - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - else @cd4_2:=null - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - else @cd4_2_date:=null - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=null - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=null - end as cd4_1_date, - - # 730 = CD4%, BY FACS - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - else @cd4_percent_2:=null - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - else @cd4_percent_2_date:=null - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=null - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=null - end as cd4_percent_1_date, - - - # 856 = HIV VIRAL LOAD, QUANTITATIVE - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <> date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - else @vl_2:=null - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - else @vl_2_date:=null - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=null - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date:=null - end as vl_1_date, - - - - # 1271 = TESTS ORDERED - # 856 = HIV VIRAL LOAD, QUANTITATIVE - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(encounter_datetime) - when orders regexp "856" then @vl_order_date := date(encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - else @vl_order_date := null - end as vl_order_date, - - # 657 = CD4 PANEL - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := null - end as cd4_order_date, - - # 1030 = HIV DNA PCR - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := null - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - else @hiv_dna_pcr_2:=null - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - else @hiv_dna_pcr_2_date:=null - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=null - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=null - end as hiv_dna_pcr_1_date, - - #1040|1042 hiv rapid test - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - case - when obs regexp "!!8302=8305!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!6579=" then @condoms_provided_date := encounter_datetime - when @prev_id = @cur_id then @condoms_provided_date - else @condoms_provided_date := null - end as condoms_provided_date, - - - # 374= METHOD OF FAMILY PLANNING - # 780 = ORAL CONTRACEPTION - # 5275 = INTRAUTERINE DEVICE - # 5279 = INJECTABLE CONTRACEPTIVES - # 6220 = CONTRACEPTIVE IMPLANT - # 7240 = FAMILY PLANNING METHOD CHOSEN - - case - when obs regexp "!!7240=1107!!" then @modern_contraceptive_method_start_date := null - when obs regexp "!!7240=(5275|6220|780|5279)!!" - then @modern_contraceptive_method_start_date := encounter_datetime - when obs regexp "!!7240=!!" - then @modern_contraceptive_method_start_date := null - when obs regexp "!!374=(5275|6220|780|5279)!!" and obs regexp "!!1190=" - then @modern_contraceptive_method_start_date := replace(replace((substring_index(substring(obs,locate("!!1190=",obs)),@sep,1)),"!!1190=",""),"!!","") - when obs regexp "!!374=(5275|6220|780|5279)!!" - then @modern_contraceptive_method_start_date := encounter_datetime - when @prev_id = @cur_id then @modern_contraceptive_method_start_date - else @modern_contraceptive_method_start_date := null - end as modern_contraceptive_method_start_date, - - - case - when obs regexp "!!7240=1107!!" then @contraceptive_method := null - when obs regexp "!!7240=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!7240=",obs)),@sep,1)),"!!7240=",""),"!!","") - when obs regexp "!!374=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!374=",obs)),@sep,1)),"!!374=",""),"!!","") - when @prev_id = @cur_id then @contraceptive_method - else @contraceptive_method := null - end as contraceptive_method, - - - - #Current WHO Stage 5356 - Adults - #Adult Who Stage 1 - 1204 - #Adult Who Stage 2 - 1205 - #Adult Who Stage 3 - 1206 - #Adult Who Stage 4 - 1207 - - #Current WHO Stage 1224 - Peds - #Adult Who Stage 1 - 1220 - #Adult Who Stage 2 - 1221 - #Adult Who Stage 3 - 1222 - #Adult Who Stage 4 - 1223 - case - when obs regexp "!!5356=(1204)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1223)!!" then @cur_who_stage := 4 - when @prev_id = @cur_id then @cur_who_stage - else @cur_who_stage := null - end as cur_who_stage, - - - case - when obs regexp "!!6096=1065" then @discordant_status := "Yes" - when obs regexp "!!6096=1066" then @discordant_status := "No" - when obs regexp "!!6096=1067" then @discordant_status := "Unknown" - when obs regexp "!!6096=1175" then @discordant_status := "N/A" - when obs regexp "!!6096=6826" then @discordant_status := "Concordant Couple Positive" - when obs regexp "!!6096=6827" then @discordant_status := "Concordant Couple Negative" - when @prev_id = @cur_id then @discordant_status - else @discordant_status := null - end as discordant_status - - - - - from flat_hiv_summary_0 t1 - join amrs.person p using (person_id) - ); - - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_hiv_summary_1 drop prev_id, drop cur_id; - - drop table if exists flat_hiv_summary_2; - create temporary table flat_hiv_summary_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_date_bncd - else null - end as outreach_date_bncd, - - case - when encounter_type=21 and @outreach_date_bncd is null then @outreach_date_bncd := encounter_datetime - when is_clinical_encounter then @outreach_date_bncd := null - when @prev_id != @cur_id then @outreach_date_bncd := null - else @outreach_date_bncd - end as next_outreach_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_death_date_bncd - else null - end as outreach_death_date_bncd, - - case - when encounter_type=21 and @outreach_death_date_bncd is null then @outreach_death_date_bncd := death_date - when is_clinical_encounter then @outreach_death_date_bncd := null - when @prev_id != @cur_id then @outreach_death_date_bncd := null - else @outreach_death_date_bncd - end as next_outreach_death_date_bncd, - - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then cast(@outreach_patient_care_status_bncd as unsigned) - else null - end as outreach_patient_care_status_bncd, - - case - when encounter_type=21 and @outreach_patient_care_status_bncd is null then @outreach_patient_care_status_bncd := patient_care_status - when is_clinical_encounter then @outreach_patient_care_status_bncd := null - when @prev_id != @cur_id then @outreach_patient_care_status_bncd := null - else @outreach_patient_care_status_bncd - end as next_outreach_patient_care_status_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_date_bncd - else null - end as transfer_date_bncd, - - case - when encounter_type=116 and @transfer_date_bncd is null then @transfer_date_bncd := encounter_datetime - when is_clinical_encounter then @transfer_date_bncd := null - when @prev_id != @cur_id then @transfer_date_bncd := null - else @transfer_date_bncd - end as next_transfer_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_transfer_out_bncd - else null - end as transfer_transfer_out_bncd, - - case - when encounter_type=116 and @transfer_transfer_out_bncd is null then @transfer_transfer_out_bncd := encounter_datetime - when is_clinical_encounter then @transfer_transfer_out_bncd := null - when @prev_id != @cur_id then @transfer_transfer_out_bncd := null - else @transfer_transfer_out_bncd - end as next_transfer_transfer_out_bncd - - from flat_hiv_summary_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_hiv_summary_3; - create temporary table flat_hiv_summary_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_hiv, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_hiv_summary_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - alter table flat_hiv_summary_3 drop prev_id, drop cur_id; - - set @prev_id = null; - set @cur_id = null; - set @transfer_in = null; - set @transfer_in_date = null; - set @transfer_in_location_id = null; - set @transfer_out = null; - set @transfer_out_date = null; - set @transfer_out_location_id = null; - - drop temporary table if exists flat_hiv_summary_4; - - create temporary table flat_hiv_summary_4 ( index person_enc (person_id, encounter_datetime)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - - # 7015 = TRANSFER IN CARE FROM OTHER CENTER - case - when obs regexp "!!7015=" then @transfer_in := 1 - when prev_clinical_location_id != location_id 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 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 then @transfer_in_location_id := prev_clinical_location_id - when @cur_id = @prev_id then @transfer_in_location_id - else @transfer_in_location_id := null - end transfer_in_location_id, - - - - # 1285 = TRANSFER CARE TO OTHER CENTER - # 1596 = REASON EXITED CARE - # 9082 = PATIENT CARE STATUS - # 1286 = AMPATH (1286) - # 1287 = NON-AMPATH (1287) - # 9068 = AMPATH, NON-AMRS (9068) - # 2050 = MATERNAL CHILD HEALTH PROGRAM (2050) - - case - when obs regexp "!!1285=!!" then @transfer_out := 1 - when obs regexp "!!1596=1594!!" then @transfer_out := 1 - when obs regexp "!!9082=(1287|1594|9068|9504|1285)!!" then @transfer_out := 1 - when next_clinical_location_id != location_id then @transfer_out := 1 - else @transfer_out := null - end as transfer_out, - - case - when obs regexp "!!1285=(1287|9068|2050)!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9999 - when obs regexp "!!1285=1286!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9998 - when next_clinical_location_id != location_id then @transfer_out_location_id := next_clinical_location_id - else @transfer_out_location_id := null - end transfer_out_location_id, - - - case - when @transfer_out and next_clinical_datetime_hiv is null then @transfer_out_date := date(cur_rtc_date) - when next_clinical_location_id != location_id then @transfer_out_date := date(next_clinical_datetime_hiv) - when transfer_transfer_out_bncd then @transfer_out_date := date(transfer_transfer_out_bncd) - else @transfer_out_date := null - end transfer_out_date - - - - from flat_hiv_summary_3 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - select count(*) into @new_encounter_rows from flat_hiv_summary_4; - - select @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; - select @total_rows_written; - - - #add data to table - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - t1.uuid, - visit_id, - encounter_id, - encounter_datetime, - encounter_type, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - visit_num, - enrollment_date, - enrollment_location_id, - hiv_start_date, - death_date, - scheduled_visit, - transfer_in, - transfer_in_location_id, - transfer_in_date, - transfer_out, - transfer_out_location_id, - transfer_out_date, - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date as rtc_date, - arv_first_regimen, - arv_first_regimen_location_id, - arv_first_regimen_start_date, - prev_arv_meds, - cur_arv_meds, - cur_arv_meds_strict, - arv_start_date, - arv_start_location_id, - prev_arv_start_date, - prev_arv_end_date, - prev_arv_line, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - is_pregnant, - edd, - tb_screen, - tb_screening_result, - tb_screening_datetime, - on_ipt, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - on_tb_tx, - tb_tx_start_date, - tb_tx_end_date, - pcp_prophylaxis_start_date, - condoms_provided_date, - modern_contraceptive_method_start_date, - contraceptive_method, - cur_who_stage, - discordant_status, - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - vl_order_date, - cd4_order_date, - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_location_id, - next_clinical_location_id, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv, - outreach_date_bncd, - outreach_death_date_bncd, - outreach_patient_care_status_bncd, - transfer_date_bncd, - transfer_transfer_out_bncd - - from flat_hiv_summary_4 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #delete from @queue_table where person_id in (select person_id from flat_hiv_summary_build_queue__0); - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_hiv_summary_build_queue__0 t2 using (person_id);'); -# select @dyn_sql; - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #select @person_ids_count := (select count(*) from flat_hiv_summary_build_queue_2); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count as remaining_in_build_queue; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - #select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select @person_ids_count as 'persons remaining', @cycle_length as 'Cycle time (s)', ceil(@person_ids_count / cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_v15_4.sql b/etl-scripts/stored-procedures/generate_hiv_summary_v15_4.sql deleted file mode 100644 index 8e19a88..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_v15_4.sql +++ /dev/null @@ -1,1747 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_v15_4`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_hiv_summary_v15b"; - set @query_type = query_type; - set @queue_table = ""; - set @total_rows_written = 0; - - set @start = now(); - set @table_version = "flat_hiv_summary_v2.15"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - #delete from etl.flat_log where table_name like "%15%"; - #drop table etl.flat_hiv_summary_v15b; - create table if not exists flat_hiv_summary_v15b ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - is_clinical_encounter int, - location_id int, - location_uuid varchar(100), - visit_num int, - enrollment_date datetime, - enrollment_location_id int, - hiv_start_date datetime, - death_date datetime, - scheduled_visit int, - transfer_in tinyint, - transfer_in_location_id int, - transfer_in_date datetime, - transfer_out tinyint, - transfer_out_location_id int, - transfer_out_date datetime, - patient_care_status int, - out_of_care int, - prev_rtc_date datetime, - rtc_date datetime, - arv_first_regimen varchar(500), - arv_first_regimen_location_id int, - arv_first_regimen_start_date datetime, - prev_arv_meds varchar(500), - cur_arv_meds varchar(500), - cur_arv_meds_strict varchar(500), - arv_start_date datetime, - arv_start_location_id int, - prev_arv_start_date datetime, - prev_arv_end_date datetime, - prev_arv_line int, - cur_arv_line int, - cur_arv_line_strict int, - cur_arv_line_reported tinyint, - prev_arv_adherence varchar(200), - cur_arv_adherence varchar(200), - hiv_status_disclosed int, - is_pregnant boolean, - edd datetime, - tb_screen boolean, - tb_screening_result int, - tb_screening_datetime datetime, - on_ipt boolean, - ipt_start_date datetime, - ipt_stop_date datetime, - ipt_completion_date datetime, - on_tb_tx boolean, - tb_tx_start_date datetime, - tb_tx_end_date datetime, - pcp_prophylaxis_start_date datetime, - condoms_provided_date datetime, - modern_contraceptive_method_start_date datetime, - contraceptive_method int, - cur_who_stage int, - discordant_status int, - cd4_resulted double, - cd4_resulted_date datetime, - cd4_1 double, - cd4_1_date datetime, - cd4_2 double, - cd4_2_date datetime, - cd4_percent_1 double, - cd4_percent_1_date datetime, - cd4_percent_2 double, - cd4_percent_2_date datetime, - vl_resulted int, - vl_resulted_date datetime, - vl_1 int, - vl_1_date datetime, - vl_2 int, - vl_2_date datetime, - vl_order_date datetime, - cd4_order_date datetime, - hiv_dna_pcr_order_date datetime, - hiv_dna_pcr_resulted int, - hiv_dna_pcr_resulted_date datetime, - hiv_dna_pcr_1 int, - hiv_dna_pcr_1_date datetime, - hiv_dna_pcr_2 int, - hiv_dna_pcr_2_date datetime, - hiv_rapid_test_resulted int, - hiv_rapid_test_resulted_date datetime, - prev_encounter_datetime_hiv datetime, - next_encounter_datetime_hiv datetime, - prev_encounter_type_hiv mediumint, - next_encounter_type_hiv mediumint, - prev_clinical_datetime_hiv datetime, - next_clinical_datetime_hiv datetime, - prev_clinical_location_id mediumint, - next_clinical_location_id mediumint, - prev_clinical_rtc_date_hiv datetime, - next_clinical_rtc_date_hiv datetime, - outreach_date_bncd datetime, #bncd means before next clinical datetime - outreach_death_date_bncd datetime, - outreach_patient_care_status_bncd int, - transfer_date_bncd datetime, - transfer_transfer_out_bncd datetime, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_hiv), - index encounter_type (encounter_type), - index date_created (date_created) - - ); - - - - 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 @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #create table if not exists @queue_table (person_id int, primary key (person_id)); - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_hiv_summary_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #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_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_hiv_summary_v15b"; - set @queue_table = "flat_hiv_summary_sync_queue"; - create table if not exists flat_hiv_summary_sync_queue (person_id int primary key); - - - - set @last_update = null; - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - - replace into flat_hiv_summary_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - # Remove test patients - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - select @person_ids_count as 'num patients to sync'; - -# select @person_ids_count := (select count(*) from flat_hiv_summary_build_queue_2); - - #delete t1 from flat_hiv_summary_v15b t1 join @queue_table t2 using (person_id); - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - #create temp table with a set of person ids - drop temporary table if exists flat_hiv_summary_build_queue__0; - - - #create temporary table flat_hiv_summary_build_queue__0 (select * from flat_hiv_summary_build_queue_2 limit 5000); #TODO - change this when data_fetch_size changes - SET @dyn_sql=CONCAT('create temporary table flat_hiv_summary_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - drop temporary table if exists flat_hiv_summary_0a; - create temporary table flat_hiv_summary_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - # in any visit, there many be multiple encounters. for this dataset, we want to include only clinical encounters (e.g. not lab or triage visit) - 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,129,138,153,154,158,162,163) then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,115,117,120,127,128,138, 153,154,158,162,163) then 10 - when t1.encounter_type in (129) then 5 #MDT ahead of other clinical encounters - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_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,153,154,158, 161,162,163) - AND NOT obs regexp "!!5303=(822|664|1067)!!" - AND NOT obs regexp "!!9082=9036!!" - ); - - insert into flat_hiv_summary_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, #t1.location_id, - t1.obs, - null, #obs_datetimes - # in any visit, there many be multiple encounters. for this dataset, we want to include only clinical encounters (e.g. not lab or triage visit) - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - ); - - drop temporary table if exists flat_hiv_summary_0; - create temporary table flat_hiv_summary_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_date = null; - set @cur_encounter_date = null; - set @enrollment_date = null; - set @hiv_start_date = null; - set @cur_location = null; - set @cur_rtc_date = null; - set @prev_rtc_date = null; - set @hiv_start_date = null; - set @prev_arv_start_date = null; - set @arv_start_date = null; - set @prev_arv_end_date = null; - set @arv_start_location_id = null; - set @art_first_regimen_start_date = null; - set @arv_first_regimen = null; - set @prev_arv_line = null; - set @cur_arv_line = null; - set @prev_arv_adherence = null; - set @cur_arv_adherence = null; - set @hiv_status_disclosed = null; - set @is_pregnant = null; - set @edd = null; - set @prev_arv_meds = null; - set @cur_arv_meds = null; - set @ipt_start_date = null; - set @ipt_end_date = null; - set @ipt_completion_date = null; - - set @on_tb_tx = null; - set @tb_tx_start_date = null; - set @tb_tx_end_date = null; - set @pcp_prophylaxis_start_date = null; - set @tb_screen = null; - set @tb_screening_result = null; - set @tb_screening_datetime = null; - - set @death_date = null; - - set @patient_care_status=null; - - set @condoms_provided_date = null; - set @modern_contraceptive_method_start_date = null; - set @contraceptive_method = null; - - #Current WHO Stage - set @cur_who_stage = null; - - set @vl_1=null; - set @vl_2=null; - set @vl_1_date=null; - set @vl_2_date=null; - set @vl_resulted=null; - set @vl_resulted_date=null; - - set @cd4_resulted=null; - set @cd4_resulted_date=null; - set @cd4_1=null; - set @cd4_1_date=null; - set @cd4_2=null; - set @cd4_2_date=null; - set @cd4_percent_1=null; - set @cd4_percent_1_date=null; - set @cd4_percent_2=null; - set @cd4_percent_2_date=null; - set @vl_order_date = null; - set @cd4_order_date = null; - - set @hiv_dna_pcr_order_date = null; - set @hiv_dna_pcr_1=null; - set @hiv_dna_pcr_2=null; - set @hiv_dna_pcr_1_date=null; - set @hiv_dna_pcr_2_date=null; - - set @hiv_rapid_test_resulted=null; - set @hiv_rapid_test_resulted_date= null; - - - #TO DO - # screened for cervical ca - # exposed infant - - drop temporary table if exists flat_hiv_summary_1; - create temporary table flat_hiv_summary_1 (index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.encounter_id, - @prev_encounter_date := date(@cur_encounter_date) as prev_encounter_date, - @cur_encounter_date := date(encounter_datetime) as cur_encounter_date, - t1.encounter_datetime, - t1.encounter_type, - t1.is_clinical_encounter, - CASE - WHEN - (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7013=' - THEN - @enrollment_date:=REPLACE(REPLACE((SUBSTRING_INDEX(SUBSTRING(obs, LOCATE('!!7013=', obs)), - @sep, - 1)), - '!!7013=', - ''), - '!!', - '') - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:='1900-01-01' - WHEN - t1.encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:=DATE(encounter_datetime) - WHEN @prev_id = @cur_id THEN @enrollment_date - ELSE @enrollment_date:=NULL - END AS enrollment_date, - CASE - WHEN - (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7030=5622' - THEN - @enrollment_location_id:=9999 - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollmen_location_id:=9999 - WHEN - encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_location_id:= location_id - WHEN @prev_id = @cur_id THEN @enrollment_location_id - ELSE @enrollment_location_id:=NULL - END AS enrollment_location_id, - - #1839 = CURRENT VISIT TYPE - #1246 = SCHEDULED VISIT - #1838 = UNSCHEDULED VISIT LATE - #1837 = UNSCHEDULED VISIT EARLY - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as 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 - end as visit_num, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - # 5096 = return visit date - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - - # 1946 = DISCONTINUE FROM CLINIC, HIV NEGATIVE - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504|1285)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - # 9082 = PATIENT CARE STATUS - # 6101 = CONTINUE - # 1946 = DISCONTINUE FROM CLINIC, HIV NEGATIVE (this is a question) - # 9036 = HIV NEGATIVE, NO LONGER AT RISK - case - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @death_encounter_type then @patient_care_status := 159 - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - # 9203 = DATE OF HIV DIAGNOSIS - case - when obs regexp "!!9203=" then @hiv_start_date := replace(replace((substring_index(substring(obs,locate("!!9203=",obs)),@sep,1)),"!!9203=",""),"!!","") - when obs regexp "!!7015=" then @hiv_start_date := "1900-01-01" - when @hiv_start_date is null then @hiv_start_date := date(encounter_datetime) - when @prev_id = @cur_id then @hiv_start_date - else @hiv_start_date := null - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_location_id := location_id - when @prev_id = @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location_id := location_id - when @prev_id != @cur_id then @arv_start_location_id := null - else @arv_start_location_id - end as arv_start_location_id, - - - case - when @prev_id=@cur_id and @cur_arv_meds is not null then @prev_arv_meds := @cur_arv_meds - when @prev_id=@cur_id then @prev_arv_meds - else @prev_arv_meds := null - end as prev_arv_meds, - - # 1255 = ANTIRETROVIRAL PLAN - # 1250 = ANTIRETROVIRALS STARTED - # 1088 = CURRENT ANTIRETROVIRAL DRUGS USED FOR TREATMENT - # 2154 = PATIENT REPORTED CURRENT ANTIRETROVIRAL TREATMENT - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - #@cur_arv_meds := getArvConceptIds(obs,1250) - #replace(replace((substring_index(substring(obs,locate("!!1250=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1250=", "") ) ) / LENGTH("!!1250=") ))),"!!1250=",""),"!!","") - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - #@cur_arv_meds := getArvConceptIds(obs,1088) - #replace(replace((substring_index(substring(obs,locate("!!1088=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1088=", "") ) ) / LENGTH("!!1088=") ))),"!!1088=",""),"!!","") - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - #@cur_arv_meds :=getArvConceptIds(obs,2154) - #replace(replace((substring_index(substring(obs,locate("!!2154=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!2154=", "") ) ) / LENGTH("!!2154=") ))),"!!2154=",""),"!!","") - when @prev_id = @cur_id then @cur_arv_meds - else @cur_arv_meds:= null - end as cur_arv_meds, - - - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - #getArvConceptIds(obs,1250) - #replace(replace((substring_index(substring(obs,locate("!!1250=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1250=", "") ) ) / LENGTH("!!1250=") ))),"!!1250=",""),"!!","") - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - #getArvConceptIds(obs,1088) - #replace(replace((substring_index(substring(obs,locate("!!1088=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1088=", "") ) ) / LENGTH("!!1088=") ))),"!!1088=",""),"!!","") - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - #getArvConceptIds(obs,2154) - #replace(replace((substring_index(substring(obs,locate("!!2154=",obs)),@sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!2154=", "") ) ) / LENGTH("!!2154=") ))),"!!2154=",""),"!!","") - else null - end as cur_arv_meds_strict, - - - - case - when @arv_first_regimen is null and obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @arv_first_regimen := normalize_arvs(obs,'2157') - when obs regexp "!!7015=" and @arv_first_regimen is null then @arv_first_regimen := "unknown" - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id = @cur_id then @arv_first_regimen - when @prev_id != @cur_id then @arv_first_regimen := @cur_arv_meds - else "-1" - end as arv_first_regimen, - - - # 1499 = ANTIRETROVIRAL DRUG TREATMENT START DATE - case - when @arv_first_regimen_start_date is null and obs regexp "!!1499=" then @arv_first_regimen_start_date := replace(replace((substring_index(substring(obs,locate("!!1499=",obs)),@sep,1)),"!!1499=",""),"!!","") - when obs regexp "!!7015=" and @arv_first_regimen_start_date is null then @arv_first_regimen_start_date := "1900-01-01" - when @arv_first_regimen_start_date is null and @cur_arv_meds is not null then @arv_first_regimen_start_date := date(t1.encounter_datetime) - when @prev_id = @cur_id then @arv_first_regimen_start_date - when @prev_id != @cur_id and @cur_arv_meds is not null then @arv_first_regimen_start_date := date(t1.encounter_datetime) - when @prev_id != @cur_id then @arv_first_regimen_start_date := null - else @arv_first_regimen_start_date - end as arv_first_regimen_start_date, - - # ADD strict defintion for arv_first_regimen_start_date, - # Make sure the start arv question is required - - case - when @arv_first_regimen is null and obs regexp "!!1499=" then @arv_first_regimen_location_id := 9999 - when @arv_first_regimen_location_id is null and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @prev_id = @cur_id then @arv_first_regimen_location_id - when @prev_id != @cur_id then @arv_first_regimen_location_id := null - else "-1" - end as arv_first_regimen_location_id, - - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := null - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=(6156)!!" then @cur_arv_line := 3 - when @prev_id = @cur_id then @cur_arv_line - else @cur_arv_line := null - end as cur_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1250=(6156)!!" then 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1088=(6156)!!" then 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then 2 - when obs regexp "!!2154=(6156)!!" then 3 - else null - end as cur_arv_line_strict, - - - # 6976 HIV ANTIRETROVIRAL DRUG PLAN TREATMENT CATEGORY - # 6744 CURRENT HIV ANTIRETROVIRAL DRUG USE TREATMENT CATEGORY - # 6695 THIRD LINE HIV ANTIRETROVIRAL DRUG TREATMENT - # 6693 FIRST LINE HIV ANTIRETROVIRAL DRUG TREATMENT - # 6694 SECOND LINE HIV ANTIRETROVIRAL DRUG TREATMENT - # UNKNOWN ANTIRETROVIRAL DRUG (5811) - # OTHER NON-CODED (5622) - case - when obs regexp "!!6976=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6976=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6976=6695!!" then @cur_arv_line_reported := 3 - - when obs regexp "!!6744=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6744=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6744=6695!!" then @cur_arv_line_reported := 3 - when @prev_id = @cur_id then @cur_arv_line_reported - else @cur_arv_line_reported := null - end as cur_arv_line_reported, - - - - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := null - end as prev_arv_start_date, - - # 1255 = ANTIRETROVIRAL PLAN - # 1250 = ANTIRETROVIRALS STARTED - # 1088 = CURRENT ANTIRETROVIRAL DRUGS USED FOR TREATMENT - # 2154 = PATIENT REPORTED CURRENT ANTIRETROVIRAL TREATMENT - # 1260 = STOP ALL MEDICATIONS - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - #when @cur_arv_meds != @prev_arv_meds and @cur_arv_line != @prev_arv_line then @arv_start_date := date(t1.encounter_datetime) - when @cur_arv_meds != @prev_arv_meds then @arv_start_date := date(t1.encounter_datetime) - - when @prev_id != @cur_id then @arv_start_date := null - else @arv_start_date - end as arv_start_date, - - - case - when @prev_arv_start_date != @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end as prev_arv_end_date, - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := null - end as prev_arv_adherence, - - # 8288 = ANTIRETROVIRAL ADHERENCE SINCE LAST VISIT - # 6343 = GOOD - # 6655 = FAIR - # 6656 = POOR - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id = @cur_id then @cur_arv_adherence - else @cur_arv_adherence := null - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when @prev_id = @cur_id then @hiv_status_disclosed - else @hiv_status_disclosed := null - end as hiv_status_disclosed, - - - # 1279 = NUMBER OF WEEKS PREGNANT - # 5596 = ESTIMATED DATE OF CONFINEMENT - # 5599 = DATE OF CONFINEMENT - # 1146 = CONCEPTION SINCE LAST VISIT - - # 8351 = PREGNANCY STATUS, CODED - # PREGNANCY, MISCARRIAGE (48) - # PREGNANCY, TERMINATION (50) - # NO (1066) - # YES (1065) - # DO NOT KNOW (1624) - # POSSIBLE (6971) - # PREGNANT (1484) - # NOT PREGNANT (9608) - - case - when obs regexp "!!8351=(48|50|1066|1624|6971|9608)!!" then @is_pregnant := null - when @prev_id != @cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" or obs regexp "!!8351=(1065|1484)!!" then @is_pregnant := true - else @is_pregnant := null - end - when @is_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") or obs regexp "!!8351=(1065|1484)!!"then @is_pregnant := true - when @is_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@is_pregnant,encounter_datetime) > 40 or timestampdiff(week,@edd,encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @is_pregnant := null - else @is_pregnant - end as is_pregnant, - - - # 1836 = LAST MENSTRUAL PERIOD DATE - # 1279 = NUMBER OF WEEKS PREGNANT - # 5596 = ESTIMATED DATE OF CONFINEMENT - # 5599 = DATE OF CONFINEMENT - # 1146 = CONCEPTION SINCE LAST VISIT - - case - when @prev_id != @cur_id then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd := null - end - when @edd is null then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - # 6174 = REVIEW OF TUBERCULOSIS SCREENING QUESTIONS - # 2022 = TUBERCULOSIS DIAGNOSED THIS VISIT - # 1268 = TUBERCULOSIS TREATMENT PLAN - # 1270 = TUBERCULOSIS TREATMENT STARTED - # 1257 = CONTINUE REGIMEN - # 1256 = START, - # 1850 = RESTART - case - when obs regexp "!!6174=" then @tb_screen := true #there is an obs for "any symptoms of tb?" - when obs regexp "!!2022=1065!!" then @tb_screen := true #1065 = yes - when obs regexp "!!307=" then @tb_screen := true #test result for tb - when obs regexp "!!12=" then @tb_screen := true #test result for tb - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screen := true #test ordered for tb - when orders regexp "(12|307|8064|2311|2323)" then @tb_screen := true #test ordered for tb - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screen := true #test results for tb - when obs regexp "!!5958=1077!!" then @tb_screen := true #means cough is bloody - when obs regexp "!!2020=1065!!" then @tb_screen := true #means a familiy member was diagnosed for tb - when obs regexp "!!2021=1065!!" then @tb_screen := true #means a familiy member was diagnosed for tb - when obs regexp "!!2028=" then @tb_screen := true #TB DIAGNOSIS MADE ON THE BASIS OF - when obs regexp "!!1268=(1256|1850)!!" then @tb_screen := true - when obs regexp "!!5959=(1073|1074)!!" then @tb_screen := true #COUGH DURATION, CODED - when obs regexp "!!5971=(1073|1074)!!" then @tb_screen := true #CHEST PAIN DURATION, CODED - when obs regexp "!!1492=107!!" then @tb_screen := true #REVIEW OF SYSTEM, EXPRESS CARE(1492)=COUGH (107) - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screen := true - end as tb_screen, - - case - when obs regexp "!!8292=" then @tb_screening_result := - replace(replace((substring_index(substring(obs,locate("!!8292=",obs)),@sep,1)),"!!8292=",""),"!!","") - when @prev_id = @cur_id then @tb_screening_result - else @tb_screening_result := null - end as tb_screening_result, - - case - when obs regexp "!!6174=" then @tb_screening_datetime := encounter_datetime #there is an obs for "any symptoms of tb?" - when obs regexp "!!2022=1065!!" then @tb_screening_datetime := encounter_datetime #1065 = yes - when obs regexp "!!307=" then @tb_screening_datetime := encounter_datetime #test result for tb - when obs regexp "!!12=" then @tb_screening_datetime := encounter_datetime #test result for tb - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime #test ordered for tb - when orders regexp "(12|307|8064|2311|2323)" then @tb_screening_datetime := encounter_datetime #test ordered for tb - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime #test results for tb - when obs regexp "!!5958=1077!!" then @tb_screening_datetime := encounter_datetime #means cough is bloody - when obs regexp "!!2020=1065!!" then @tb_screening_datetime := encounter_datetime #means a familiy member was diagnosed for tb - when obs regexp "!!2021=1065!!" then @tb_screening_datetime := encounter_datetime #means a familiy member was diagnosed for tb - when obs regexp "!!2028=" then @tb_screening_datetime := encounter_datetime #TB DIAGNOSIS MADE ON THE BASIS OF - when obs regexp "!!1268=(1256|1850)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5959=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime #COUGH DURATION, CODED - when obs regexp "!!5971=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime #CHEST PAIN DURATION, CODED - when obs regexp "!!1492=107!!" then @tb_screening_datetime := encounter_datetime #REVIEW OF SYSTEM, EXPRESS CARE(1492)=COUGH (107) - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screening_datetime := encounter_datetime - when @cur_id = @prev_id then @tb_screening_datetime - else @tb_screening_datetime := null - end as tb_screening_datetime, - - - case - when obs regexp "!!1265=(1107|1260)!!" then @on_ipt := 0 - when obs regexp "!!1265=!!" then @on_ipt := 1 - when obs regexp "!!1110=656!!" then @on_ipt := 1 - when @prev_id = @cur_id then @on_ipt - else null - end as on_ipt, - - # 1265 = TUBERCULOSIS PROPHYLAXIS PLAN - # 1268 = TUBERCULOSIS TREATMENT PLAN - # 1110 = PATIENT REPORTED CURRENT TUBERCULOSIS PROPHYLAXIS - # 656 = ISONIAZID - case - when obs regexp "!!1265=(1256|1850)!!" then @ipt_start_date := encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" and @ipt_start_date is null then @ipt_start_date := encounter_datetime - when @cur_id = @prev_id then @ipt_start_date - else @ipt_start_date := null - end as ipt_start_date, - - - # 1265 = TUBERCULOSIS PROPHYLAXIS PLAN - # 1268 = TUBERCULOSIS TREATMENT PLAN - # 1110 = PATIENT REPORTED CURRENT TUBERCULOSIS PROPHYLAXIS - # 656 = ISONIAZID - case - when obs regexp "!!1266=!!" then @ipt_stop_date := encounter_datetime - when @cur_id = @prev_id then @ipt_stop_date - else @ipt_stop_date - end as ipt_stop_date, - - case - when obs regexp "!!1266=1267!!" then @ipt_completion_date := encounter_datetime - when @cur_id = @prev_id then @ipt_completion_date - else @ipt_completion_date - end as ipt_completion_date, - - - case - when obs regexp "!!1268=(1107|1260)!!" then @on_tb_tx := 0 - when obs regexp "!!1268=" then @on_tb_tx := 1 #first statment above catches all negatives, this statement catches all codes where patient getting drugs - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @on_tb_tx := 1 - else 0 - end as on_tb_tx, - - # 1113 = TUBERCULOSIS DRUG TREATMENT START DATE - # 1111 = PATIENT REPORTED CURRENT TUBERCULOSIS TREATMENT - # 1267 = COMPLETED - # 1107 = NONE - # 1257 = CONTINUE REGIMEN - case - when obs regexp "!!1113=" then @tb_tx_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @tb_tx_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" and obs regexp "!!7015=" and @tb_tx_start_date is null then @tb_tx_start_date := null - when obs regexp "!!1268=(1257|1259|1849|981)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_start_date - else @tb_tx_start_date := null - end as tb_tx_start_date, - - # 1111 = PATIENT REPORTED CURRENT TUBERCULOSIS TREATMENT - # 1267 = COMPLETED - # 1107 = NONE - case - when obs regexp "!!2041=" then @tb_tx_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_tx_end_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_end_date - else @tb_tx_end_date := null - end as tb_tx_end_date, - - - # 1109 = PATIENT REPORTED CURRENT PCP PROPHYLAXIS - # 1261 = PCP PROPHYLAXIS PLAN - # 1193 = CURRENT MEDICATIONS - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1261=1257!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1193=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when @prev_id=@cur_id then @pcp_prophylaxis_start_date - else @pcp_prophylaxis_start_date := null - end as pcp_prophylaxis_start_date, - - # 1570 = DATE OF DEATH - # 1734 = DEATH REPORTED BY - # 1573 = CAUSE FOR DEATH - # 1733 = REASON FOR MISSED VISIT - # 9082 = PATIENT CARE STATUS - # 6206 = OUTCOME AT END OF TUBERCULOSIS TREATMENT - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when @prev_id != @cur_id or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := encounter_datetime - else @death_date := null - end - else @death_date - end as death_date, - - # 5497 = CD4, BY FACS - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - else @cd4_2:=null - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - else @cd4_2_date:=null - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=null - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=null - end as cd4_1_date, - - # 730 = CD4%, BY FACS - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - else @cd4_percent_2:=null - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - else @cd4_percent_2_date:=null - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=null - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=null - end as cd4_percent_1_date, - - - # 856 = HIV VIRAL LOAD, QUANTITATIVE - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <> date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - else @vl_2:=null - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - else @vl_2_date:=null - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=null - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date:=null - end as vl_1_date, - - - - # 1271 = TESTS ORDERED - # 856 = HIV VIRAL LOAD, QUANTITATIVE - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(encounter_datetime) - when orders regexp "856" then @vl_order_date := date(encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - else @vl_order_date := null - end as vl_order_date, - - # 657 = CD4 PANEL - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := null - end as cd4_order_date, - - # 1030 = HIV DNA PCR - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := null - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - else @hiv_dna_pcr_2:=null - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - else @hiv_dna_pcr_2_date:=null - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=null - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=null - end as hiv_dna_pcr_1_date, - - #1040|1042 hiv rapid test - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - case - when obs regexp "!!8302=8305!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!6579=" then @condoms_provided_date := encounter_datetime - when @prev_id = @cur_id then @condoms_provided_date - else @condoms_provided_date := null - end as condoms_provided_date, - - - # 374= METHOD OF FAMILY PLANNING - # 780 = ORAL CONTRACEPTION - # 5275 = INTRAUTERINE DEVICE - # 5279 = INJECTABLE CONTRACEPTIVES - # 6220 = CONTRACEPTIVE IMPLANT - # 7240 = FAMILY PLANNING METHOD CHOSEN - - case - when obs regexp "!!7240=1107!!" then @modern_contraceptive_method_start_date := null - when obs regexp "!!7240=(5275|6220|780|5279)!!" - then @modern_contraceptive_method_start_date := encounter_datetime - when obs regexp "!!7240=!!" - then @modern_contraceptive_method_start_date := null - when obs regexp "!!374=(5275|6220|780|5279)!!" and obs regexp "!!1190=" - then @modern_contraceptive_method_start_date := replace(replace((substring_index(substring(obs,locate("!!1190=",obs)),@sep,1)),"!!1190=",""),"!!","") - when obs regexp "!!374=(5275|6220|780|5279)!!" - then @modern_contraceptive_method_start_date := encounter_datetime - when @prev_id = @cur_id then @modern_contraceptive_method_start_date - else @modern_contraceptive_method_start_date := null - end as modern_contraceptive_method_start_date, - - - case - when obs regexp "!!7240=1107!!" then @contraceptive_method := null - when obs regexp "!!7240=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!7240=",obs)),@sep,1)),"!!7240=",""),"!!","") - when obs regexp "!!374=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!374=",obs)),@sep,1)),"!!374=",""),"!!","") - when @prev_id = @cur_id then @contraceptive_method - else @contraceptive_method := null - end as contraceptive_method, - - - - #Current WHO Stage 5356 - Adults - #Adult Who Stage 1 - 1204 - #Adult Who Stage 2 - 1205 - #Adult Who Stage 3 - 1206 - #Adult Who Stage 4 - 1207 - - #Current WHO Stage 1224 - Peds - #Adult Who Stage 1 - 1220 - #Adult Who Stage 2 - 1221 - #Adult Who Stage 3 - 1222 - #Adult Who Stage 4 - 1223 - case - when obs regexp "!!5356=(1204)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1223)!!" then @cur_who_stage := 4 - when @prev_id = @cur_id then @cur_who_stage - else @cur_who_stage := null - end as cur_who_stage, - - - case - when obs regexp "!!6096=1065" then @discordant_status := "Yes" - when obs regexp "!!6096=1066" then @discordant_status := "No" - when obs regexp "!!6096=1067" then @discordant_status := "Unknown" - when obs regexp "!!6096=1175" then @discordant_status := "N/A" - when obs regexp "!!6096=6826" then @discordant_status := "Concordant Couple Positive" - when obs regexp "!!6096=6827" then @discordant_status := "Concordant Couple Negative" - when @prev_id = @cur_id then @discordant_status - else @discordant_status := null - end as discordant_status - - - - - from flat_hiv_summary_0 t1 - join amrs.person p using (person_id) - ); - - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_hiv_summary_1 drop prev_id, drop cur_id; - - drop table if exists flat_hiv_summary_2; - create temporary table flat_hiv_summary_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_date_bncd - else null - end as outreach_date_bncd, - - case - when encounter_type=21 and @outreach_date_bncd is null then @outreach_date_bncd := encounter_datetime - when is_clinical_encounter then @outreach_date_bncd := null - when @prev_id != @cur_id then @outreach_date_bncd := null - else @outreach_date_bncd - end as next_outreach_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_death_date_bncd - else null - end as outreach_death_date_bncd, - - case - when encounter_type=21 and @outreach_death_date_bncd is null then @outreach_death_date_bncd := death_date - when is_clinical_encounter then @outreach_death_date_bncd := null - when @prev_id != @cur_id then @outreach_death_date_bncd := null - else @outreach_death_date_bncd - end as next_outreach_death_date_bncd, - - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then cast(@outreach_patient_care_status_bncd as unsigned) - else null - end as outreach_patient_care_status_bncd, - - case - when encounter_type=21 and @outreach_patient_care_status_bncd is null then @outreach_patient_care_status_bncd := patient_care_status - when is_clinical_encounter then @outreach_patient_care_status_bncd := null - when @prev_id != @cur_id then @outreach_patient_care_status_bncd := null - else @outreach_patient_care_status_bncd - end as next_outreach_patient_care_status_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_date_bncd - else null - end as transfer_date_bncd, - - case - when encounter_type=116 and @transfer_date_bncd is null then @transfer_date_bncd := encounter_datetime - when is_clinical_encounter then @transfer_date_bncd := null - when @prev_id != @cur_id then @transfer_date_bncd := null - else @transfer_date_bncd - end as next_transfer_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_transfer_out_bncd - else null - end as transfer_transfer_out_bncd, - - case - when encounter_type=116 and @transfer_transfer_out_bncd is null then @transfer_transfer_out_bncd := encounter_datetime - when is_clinical_encounter then @transfer_transfer_out_bncd := null - when @prev_id != @cur_id then @transfer_transfer_out_bncd := null - else @transfer_transfer_out_bncd - end as next_transfer_transfer_out_bncd - - from flat_hiv_summary_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_hiv_summary_3; - create temporary table flat_hiv_summary_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_hiv, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_hiv_summary_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - alter table flat_hiv_summary_3 drop prev_id, drop cur_id; - - set @prev_id = null; - set @cur_id = null; - set @transfer_in = null; - set @transfer_in_date = null; - set @transfer_in_location_id = null; - set @transfer_out = null; - set @transfer_out_date = null; - set @transfer_out_location_id = null; - - drop temporary table if exists flat_hiv_summary_4; - - create temporary table flat_hiv_summary_4 ( index person_enc (person_id, encounter_datetime)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - - # 7015 = TRANSFER IN CARE FROM OTHER CENTER - case - when obs regexp "!!7015=" then @transfer_in := 1 - when prev_clinical_location_id != location_id 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 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 then @transfer_in_location_id := prev_clinical_location_id - when @cur_id = @prev_id then @transfer_in_location_id - else @transfer_in_location_id := null - end transfer_in_location_id, - - - - # 1285 = TRANSFER CARE TO OTHER CENTER - # 1596 = REASON EXITED CARE - # 9082 = PATIENT CARE STATUS - # 1286 = AMPATH (1286) - # 1287 = NON-AMPATH (1287) - # 9068 = AMPATH, NON-AMRS (9068) - # 2050 = MATERNAL CHILD HEALTH PROGRAM (2050) - - case - when obs regexp "!!1285=!!" then @transfer_out := 1 - when obs regexp "!!1596=1594!!" then @transfer_out := 1 - when obs regexp "!!9082=(1287|1594|9068|9504|1285)!!" then @transfer_out := 1 - when next_clinical_location_id != location_id then @transfer_out := 1 - else @transfer_out := null - end as transfer_out, - - case - when obs regexp "!!1285=(1287|9068|2050)!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9999 - when obs regexp "!!1285=1286!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9998 - when next_clinical_location_id != location_id then @transfer_out_location_id := next_clinical_location_id - else @transfer_out_location_id := null - end transfer_out_location_id, - - - case - when @transfer_out and next_clinical_datetime_hiv is null then @transfer_out_date := date(cur_rtc_date) - when next_clinical_location_id != location_id then @transfer_out_date := date(next_clinical_datetime_hiv) - when transfer_transfer_out_bncd then @transfer_out_date := date(transfer_transfer_out_bncd) - else @transfer_out_date := null - end transfer_out_date - - - - from flat_hiv_summary_3 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - select count(*) into @new_encounter_rows from flat_hiv_summary_4; - - select @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; - select @total_rows_written; - - - #add data to table - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - t1.uuid, - visit_id, - encounter_id, - encounter_datetime, - encounter_type, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - visit_num, - enrollment_date, - enrollment_location_id, - hiv_start_date, - death_date, - scheduled_visit, - transfer_in, - transfer_in_location_id, - transfer_in_date, - transfer_out, - transfer_out_location_id, - transfer_out_date, - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date as rtc_date, - arv_first_regimen, - arv_first_regimen_location_id, - arv_first_regimen_start_date, - prev_arv_meds, - cur_arv_meds, - cur_arv_meds_strict, - arv_start_date, - arv_start_location_id, - prev_arv_start_date, - prev_arv_end_date, - prev_arv_line, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - is_pregnant, - edd, - tb_screen, - tb_screening_result, - tb_screening_datetime, - on_ipt, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - on_tb_tx, - tb_tx_start_date, - tb_tx_end_date, - pcp_prophylaxis_start_date, - condoms_provided_date, - modern_contraceptive_method_start_date, - contraceptive_method, - cur_who_stage, - discordant_status, - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - vl_order_date, - cd4_order_date, - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_location_id, - next_clinical_location_id, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv, - outreach_date_bncd, - outreach_death_date_bncd, - outreach_patient_care_status_bncd, - transfer_date_bncd, - transfer_transfer_out_bncd - - from flat_hiv_summary_4 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #delete from @queue_table where person_id in (select person_id from flat_hiv_summary_build_queue__0); - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_hiv_summary_build_queue__0 t2 using (person_id);'); -# select @dyn_sql; - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - #select @person_ids_count := (select count(*) from flat_hiv_summary_build_queue_2); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count as remaining_in_build_queue; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - #select concat('Cycle time: ',@cycle_length,' seconds'); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - #select ceil(@person_ids_count / cycle_size) as remaining_cycles; - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - #select concat("Estimated time remaining: ", @remaining_time,' minutes'); - - select @person_ids_count as 'persons remaining', @cycle_length as 'Cycle time (s)', ceil(@person_ids_count / cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_v15_5.sql b/etl-scripts/stored-procedures/generate_hiv_summary_v15_5.sql deleted file mode 100644 index 2751677..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_v15_5.sql +++ /dev/null @@ -1,1781 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_v15_5`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_hiv_summary_v15b"; - set @query_type = query_type; - set @queue_table = ""; - set @total_rows_written = 0; - - set @start = now(); - set @table_version = "flat_hiv_summary_v2.15"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - - create table if not exists flat_hiv_summary_v15b ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - is_clinical_encounter int, - location_id int, - location_uuid varchar(100), - visit_num int, - enrollment_date datetime, - enrollment_location_id int, - hiv_start_date datetime, - death_date datetime, - scheduled_visit int, - transfer_in tinyint, - transfer_in_location_id int, - transfer_in_date datetime, - transfer_out tinyint, - transfer_out_location_id int, - transfer_out_date datetime, - patient_care_status int, - out_of_care int, - prev_rtc_date datetime, - rtc_date datetime, - arv_first_regimen varchar(500), - arv_first_regimen_location_id int, - arv_first_regimen_start_date datetime, - prev_arv_meds varchar(500), - cur_arv_meds varchar(500), - cur_arv_meds_strict varchar(500), - arv_start_date datetime, - arv_start_location_id int, - prev_arv_start_date datetime, - prev_arv_end_date datetime, - prev_arv_line int, - cur_arv_line int, - cur_arv_line_strict int, - cur_arv_line_reported tinyint, - prev_arv_adherence varchar(200), - cur_arv_adherence varchar(200), - hiv_status_disclosed int, - is_pregnant boolean, - edd datetime, - tb_screen boolean, - tb_screening_result int, - tb_screening_datetime datetime, - on_ipt boolean, - ipt_start_date datetime, - ipt_stop_date datetime, - ipt_completion_date datetime, - on_tb_tx boolean, - tb_tx_start_date datetime, - tb_tx_end_date datetime, - pcp_prophylaxis_start_date datetime, - condoms_provided_date datetime, - modern_contraceptive_method_start_date datetime, - contraceptive_method int, - cur_who_stage int, - discordant_status int, - cd4_resulted double, - cd4_resulted_date datetime, - cd4_1 double, - cd4_1_date datetime, - cd4_2 double, - cd4_2_date datetime, - cd4_percent_1 double, - cd4_percent_1_date datetime, - cd4_percent_2 double, - cd4_percent_2_date datetime, - vl_resulted int, - vl_resulted_date datetime, - vl_1 int, - vl_1_date datetime, - vl_2 int, - vl_2_date datetime, - vl_order_date datetime, - cd4_order_date datetime, - hiv_dna_pcr_order_date datetime, - hiv_dna_pcr_resulted int, - hiv_dna_pcr_resulted_date datetime, - hiv_dna_pcr_1 int, - hiv_dna_pcr_1_date datetime, - hiv_dna_pcr_2 int, - hiv_dna_pcr_2_date datetime, - hiv_rapid_test_resulted int, - hiv_rapid_test_resulted_date datetime, - prev_encounter_datetime_hiv datetime, - next_encounter_datetime_hiv datetime, - prev_encounter_type_hiv mediumint, - next_encounter_type_hiv mediumint, - prev_clinical_datetime_hiv datetime, - next_clinical_datetime_hiv datetime, - prev_clinical_location_id mediumint, - next_clinical_location_id mediumint, - prev_clinical_rtc_date_hiv datetime, - next_clinical_rtc_date_hiv datetime, - outreach_date_bncd datetime, - outreach_death_date_bncd datetime, - outreach_patient_care_status_bncd int, - transfer_date_bncd datetime, - transfer_transfer_out_bncd datetime, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_hiv), - index encounter_type (encounter_type), - index date_created (date_created) - - ); - - - - 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 @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_hiv_summary_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from flat_hiv_summary_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_hiv_summary_v15b"; - set @queue_table = "flat_hiv_summary_sync_queue"; - create table if not exists flat_hiv_summary_sync_queue (person_id int primary key); - - - - set @last_update = null; - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - - replace into flat_hiv_summary_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - select @person_ids_count as 'num patients to sync'; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - - drop temporary table if exists flat_hiv_summary_build_queue__0; - - - - SET @dyn_sql=CONCAT('create temporary table flat_hiv_summary_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - drop temporary table if exists flat_hiv_summary_0a; - create temporary table flat_hiv_summary_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - 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,129,138,153,154,158,162,163) then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,115,117,120,127,128,138, 153,154,158,162,163) then 10 - when t1.encounter_type in (129) then 5 - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_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,153,154,158, 161,162,163) - AND NOT obs regexp "!!5303=(822|664|1067)!!" - AND NOT obs regexp "!!9082=9036!!" - ); - - insert into flat_hiv_summary_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - ); - - drop temporary table if exists flat_hiv_summary_0; - create temporary table flat_hiv_summary_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_date = null; - set @cur_encounter_date = null; - set @enrollment_date = null; - set @hiv_start_date = null; - set @cur_location = null; - set @cur_rtc_date = null; - set @prev_rtc_date = null; - set @hiv_start_date = null; - set @prev_arv_start_date = null; - set @arv_start_date = null; - set @prev_arv_end_date = null; - set @arv_start_location_id = null; - set @art_first_regimen_start_date = null; - set @arv_first_regimen = null; - set @prev_arv_line = null; - set @cur_arv_line = null; - set @prev_arv_adherence = null; - set @cur_arv_adherence = null; - set @hiv_status_disclosed = null; - set @is_pregnant = null; - set @edd = null; - set @prev_arv_meds = null; - set @cur_arv_meds = null; - set @ipt_start_date = null; - set @ipt_end_date = null; - set @ipt_completion_date = null; - - set @on_tb_tx = null; - set @tb_tx_start_date = null; - set @tb_tx_end_date = null; - set @pcp_prophylaxis_start_date = null; - set @tb_screen = null; - set @tb_screening_result = null; - set @tb_screening_datetime = null; - - set @death_date = null; - - set @patient_care_status=null; - - set @condoms_provided_date = null; - set @modern_contraceptive_method_start_date = null; - set @contraceptive_method = null; - - - set @cur_who_stage = null; - - set @vl_1=null; - set @vl_2=null; - set @vl_1_date=null; - set @vl_2_date=null; - set @vl_resulted=null; - set @vl_resulted_date=null; - - set @cd4_resulted=null; - set @cd4_resulted_date=null; - set @cd4_1=null; - set @cd4_1_date=null; - set @cd4_2=null; - set @cd4_2_date=null; - set @cd4_percent_1=null; - set @cd4_percent_1_date=null; - set @cd4_percent_2=null; - set @cd4_percent_2_date=null; - set @vl_order_date = null; - set @cd4_order_date = null; - - set @hiv_dna_pcr_order_date = null; - set @hiv_dna_pcr_1=null; - set @hiv_dna_pcr_2=null; - set @hiv_dna_pcr_1_date=null; - set @hiv_dna_pcr_2_date=null; - - set @hiv_rapid_test_resulted=null; - set @hiv_rapid_test_resulted_date= null; - - - - - - - drop temporary table if exists flat_hiv_summary_1; - create temporary table flat_hiv_summary_1 (index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.encounter_id, - @prev_encounter_date := date(@cur_encounter_date) as prev_encounter_date, - @cur_encounter_date := date(encounter_datetime) as cur_encounter_date, - t1.encounter_datetime, - t1.encounter_type, - t1.is_clinical_encounter, - CASE - WHEN - (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7013=' - THEN - @enrollment_date:=REPLACE(REPLACE((SUBSTRING_INDEX(SUBSTRING(obs, LOCATE('!!7013=', obs)), - @sep, - 1)), - '!!7013=', - ''), - '!!', - '') - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:='1900-01-01' - WHEN - t1.encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:=DATE(encounter_datetime) - WHEN @prev_id = @cur_id THEN @enrollment_date - ELSE @enrollment_date:=NULL - END AS enrollment_date, - CASE - WHEN - (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7030=5622' - THEN - @enrollment_location_id:=9999 - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollmen_location_id:=9999 - WHEN - encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_location_id:= location_id - WHEN @prev_id = @cur_id THEN @enrollment_location_id - ELSE @enrollment_location_id:=NULL - END AS enrollment_location_id, - - - - - - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as 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 - end as visit_num, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - - - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504|1285)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - - - - - - - - - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when @prev_id != @cur_id or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := encounter_datetime - else @death_date := null - end - else @death_date - end as death_date, - - - - - - case - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when @death_date <= encounter_datetime then @patient_care_status := 159 - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - - case - when obs regexp "!!9203=" then @hiv_start_date := replace(replace((substring_index(substring(obs,locate("!!9203=",obs)),@sep,1)),"!!9203=",""),"!!","") - when obs regexp "!!7015=" then @hiv_start_date := "1900-01-01" - when @hiv_start_date is null then @hiv_start_date := date(encounter_datetime) - when @prev_id = @cur_id then @hiv_start_date - else @hiv_start_date := null - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_location_id := location_id - when @prev_id = @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location_id := location_id - when @prev_id != @cur_id then @arv_start_location_id := null - else @arv_start_location_id - end as arv_start_location_id, - - - case - when @prev_id=@cur_id and @cur_arv_meds is not null then @prev_arv_meds := @cur_arv_meds - when @prev_id=@cur_id then @prev_arv_meds - else @prev_arv_meds := null - end as prev_arv_meds, - - - - - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - - - when @prev_id = @cur_id then @cur_arv_meds - else @cur_arv_meds:= null - end as cur_arv_meds, - - - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - - - else null - end as cur_arv_meds_strict, - - - - case - when @arv_first_regimen is null and obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @arv_first_regimen := normalize_arvs(obs,'2157') - when obs regexp "!!7015=" and @arv_first_regimen is null then @arv_first_regimen := "unknown" - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id = @cur_id then @arv_first_regimen - when @prev_id != @cur_id then @arv_first_regimen := @cur_arv_meds - else "-1" - end as arv_first_regimen, - - - - CASE - WHEN - (@arv_first_regimen_start_date IS NULL - || (@arv_first_regimen_start_date IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_start_date:=REPLACE(REPLACE((SUBSTRING_INDEX(SUBSTRING(obs, LOCATE('!!1499=', obs)), - @sep, - 1)), - '!!1499=', - ''), - '!!', - '') - WHEN - (@arv_first_regimen_start_date IS NULL - || (@arv_first_regimen_start_date IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7015=' - AND (obs REGEXP '!!1255=(1256)!!' - || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_start_date:=DATE(encounter_datetime) - WHEN - (@arv_first_regimen_start_date IS NULL - || (@arv_first_regimen_start_date IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7015=' - THEN - @arv_first_regimen_start_date:='1900-01-01' - WHEN - (@arv_first_regimen_start_date IS NULL - || (@arv_first_regimen_start_date IS NOT NULL - AND @prev_id != @cur_id)) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_start_date:=DATE(encounter_datetime) - WHEN @prev_id = @cur_id THEN @arv_first_regimen_start_date - WHEN @prev_id != @cur_id THEN @arv_first_regimen_start_date:=NULL - ELSE @arv_first_regimen_start_date - END AS arv_first_regimen_start_date, - - - - - case - when @arv_first_regimen is null and obs regexp "!!1499=" then @arv_first_regimen_location_id := 9999 - when @arv_first_regimen_location_id is null and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @prev_id = @cur_id then @arv_first_regimen_location_id - when @prev_id != @cur_id then @arv_first_regimen_location_id := null - else "-1" - end as arv_first_regimen_location_id, - - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := null - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=(6156)!!" then @cur_arv_line := 3 - when @prev_id = @cur_id then @cur_arv_line - else @cur_arv_line := null - end as cur_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1250=(6156)!!" then 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1088=(6156)!!" then 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then 2 - when obs regexp "!!2154=(6156)!!" then 3 - else null - end as cur_arv_line_strict, - - - - - - - - - - case - when obs regexp "!!6976=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6976=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6976=6695!!" then @cur_arv_line_reported := 3 - - when obs regexp "!!6744=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6744=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6744=6695!!" then @cur_arv_line_reported := 3 - when @prev_id = @cur_id then @cur_arv_line_reported - else @cur_arv_line_reported := null - end as cur_arv_line_reported, - - - - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := null - end as prev_arv_start_date, - - - - - - - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - - when @cur_arv_meds != @prev_arv_meds then @arv_start_date := date(t1.encounter_datetime) - - when @prev_id != @cur_id then @arv_start_date := null - else @arv_start_date - end as arv_start_date, - - - case - when @prev_arv_start_date != @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end as prev_arv_end_date, - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := null - end as prev_arv_adherence, - - - - - - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id = @cur_id then @cur_arv_adherence - else @cur_arv_adherence := null - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when @prev_id = @cur_id then @hiv_status_disclosed - else @hiv_status_disclosed := null - end as hiv_status_disclosed, - - - - - - - - - - - - - - - - - - case - when obs regexp "!!8351=(48|50|1066|1624|6971|9608)!!" then @is_pregnant := null - when @prev_id != @cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" or obs regexp "!!8351=(1065|1484)!!" then @is_pregnant := true - else @is_pregnant := null - end - when @is_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") or obs regexp "!!8351=(1065|1484)!!"then @is_pregnant := true - when @is_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@is_pregnant,encounter_datetime) > 40 or timestampdiff(week,@edd,encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @is_pregnant := null - else @is_pregnant - end as is_pregnant, - - - - - - - - - case - when @prev_id != @cur_id then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd := null - end - when @edd is null then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - - - - - - - - case - when obs regexp "!!6174=" then @tb_screen := true - when obs regexp "!!2022=1065!!" then @tb_screen := true - when obs regexp "!!307=" then @tb_screen := true - when obs regexp "!!12=" then @tb_screen := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screen := true - when orders regexp "(12|307|8064|2311|2323)" then @tb_screen := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screen := true - when obs regexp "!!5958=1077!!" then @tb_screen := true - when obs regexp "!!2020=1065!!" then @tb_screen := true - when obs regexp "!!2021=1065!!" then @tb_screen := true - when obs regexp "!!2028=" then @tb_screen := true - when obs regexp "!!1268=(1256|1850)!!" then @tb_screen := true - when obs regexp "!!5959=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!5971=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!1492=107!!" then @tb_screen := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screen := true - end as tb_screen, - - case - when obs regexp "!!8292=" then @tb_screening_result := - replace(replace((substring_index(substring(obs,locate("!!8292=",obs)),@sep,1)),"!!8292=",""),"!!","") - when @prev_id = @cur_id then @tb_screening_result - else @tb_screening_result := null - end as tb_screening_result, - - case - when obs regexp "!!6174=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2022=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!307=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!12=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime - when orders regexp "(12|307|8064|2311|2323)" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5958=1077!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2020=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2021=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2028=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1268=(1256|1850)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5959=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5971=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1492=107!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screening_datetime := encounter_datetime - when @cur_id = @prev_id then @tb_screening_datetime - else @tb_screening_datetime := null - end as tb_screening_datetime, - - - case - when obs regexp "!!1265=(1107|1260)!!" then @on_ipt := 0 - when obs regexp "!!1265=!!" then @on_ipt := 1 - when obs regexp "!!1110=656!!" then @on_ipt := 1 - when @prev_id = @cur_id then @on_ipt - else null - end as on_ipt, - - - - - - case - when obs regexp "!!1265=(1256|1850)!!" then @ipt_start_date := encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" and @ipt_start_date is null then @ipt_start_date := encounter_datetime - when @cur_id = @prev_id then @ipt_start_date - else @ipt_start_date := null - end as ipt_start_date, - - - - - - - case - when obs regexp "!!1266=!!" then @ipt_stop_date := encounter_datetime - when @cur_id = @prev_id then @ipt_stop_date - else @ipt_stop_date - end as ipt_stop_date, - - case - when obs regexp "!!1266=1267!!" then @ipt_completion_date := encounter_datetime - when @cur_id = @prev_id then @ipt_completion_date - else @ipt_completion_date - end as ipt_completion_date, - - - case - when obs regexp "!!1268=(1107|1260)!!" then @on_tb_tx := 0 - when obs regexp "!!1268=" then @on_tb_tx := 1 - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @on_tb_tx := 1 - else 0 - end as on_tb_tx, - - - - - - - case - when obs regexp "!!1113=" then @tb_tx_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @tb_tx_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" and obs regexp "!!7015=" and @tb_tx_start_date is null then @tb_tx_start_date := null - when obs regexp "!!1268=(1257|1259|1849|981)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_start_date - else @tb_tx_start_date := null - end as tb_tx_start_date, - - - - - case - when obs regexp "!!2041=" then @tb_tx_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_tx_end_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_end_date - else @tb_tx_end_date := null - end as tb_tx_end_date, - - - - - - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1261=1257!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1193=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when @prev_id=@cur_id then @pcp_prophylaxis_start_date - else @pcp_prophylaxis_start_date := null - end as pcp_prophylaxis_start_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - else @cd4_2:=null - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - else @cd4_2_date:=null - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=null - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=null - end as cd4_1_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - else @cd4_percent_2:=null - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - else @cd4_percent_2_date:=null - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=null - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=null - end as cd4_percent_1_date, - - - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <> date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - else @vl_2:=null - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - else @vl_2_date:=null - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=null - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date:=null - end as vl_1_date, - - - - - - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(encounter_datetime) - when orders regexp "856" then @vl_order_date := date(encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - else @vl_order_date := null - end as vl_order_date, - - - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := null - end as cd4_order_date, - - - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := null - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - else @hiv_dna_pcr_2:=null - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - else @hiv_dna_pcr_2_date:=null - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=null - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=null - end as hiv_dna_pcr_1_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - case - when obs regexp "!!8302=8305!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!6579=" then @condoms_provided_date := encounter_datetime - when @prev_id = @cur_id then @condoms_provided_date - else @condoms_provided_date := null - end as condoms_provided_date, - - - - - - - - - - case - when obs regexp "!!7240=1107!!" then @modern_contraceptive_method_start_date := null - when obs regexp "!!7240=(5275|6220|780|5279)!!" - then @modern_contraceptive_method_start_date := encounter_datetime - when obs regexp "!!7240=!!" - then @modern_contraceptive_method_start_date := null - when obs regexp "!!374=(5275|6220|780|5279)!!" and obs regexp "!!1190=" - then @modern_contraceptive_method_start_date := replace(replace((substring_index(substring(obs,locate("!!1190=",obs)),@sep,1)),"!!1190=",""),"!!","") - when obs regexp "!!374=(5275|6220|780|5279)!!" - then @modern_contraceptive_method_start_date := encounter_datetime - when @prev_id = @cur_id then @modern_contraceptive_method_start_date - else @modern_contraceptive_method_start_date := null - end as modern_contraceptive_method_start_date, - - - case - when obs regexp "!!7240=1107!!" then @contraceptive_method := null - when obs regexp "!!7240=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!7240=",obs)),@sep,1)),"!!7240=",""),"!!","") - when obs regexp "!!374=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!374=",obs)),@sep,1)),"!!374=",""),"!!","") - when @prev_id = @cur_id then @contraceptive_method - else @contraceptive_method := null - end as contraceptive_method, - - - - - - - - - - - - - - - case - when obs regexp "!!5356=(1204)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1223)!!" then @cur_who_stage := 4 - when @prev_id = @cur_id then @cur_who_stage - else @cur_who_stage := null - end as cur_who_stage, - - - case - when obs regexp "!!6096=1065" then @discordant_status := "Yes" - when obs regexp "!!6096=1066" then @discordant_status := "No" - when obs regexp "!!6096=1067" then @discordant_status := "Unknown" - when obs regexp "!!6096=1175" then @discordant_status := "N/A" - when obs regexp "!!6096=6826" then @discordant_status := "Concordant Couple Positive" - when obs regexp "!!6096=6827" then @discordant_status := "Concordant Couple Negative" - when @prev_id = @cur_id then @discordant_status - else @discordant_status := null - end as discordant_status - - - - - from flat_hiv_summary_0 t1 - join amrs.person p using (person_id) - ); - - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_hiv_summary_1 drop prev_id, drop cur_id; - - drop table if exists flat_hiv_summary_2; - create temporary table flat_hiv_summary_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_date_bncd - else null - end as outreach_date_bncd, - - case - when encounter_type=21 and @outreach_date_bncd is null then @outreach_date_bncd := encounter_datetime - when is_clinical_encounter then @outreach_date_bncd := null - when @prev_id != @cur_id then @outreach_date_bncd := null - else @outreach_date_bncd - end as next_outreach_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_death_date_bncd - else null - end as outreach_death_date_bncd, - - case - when encounter_type=21 and @outreach_death_date_bncd is null then @outreach_death_date_bncd := death_date - when is_clinical_encounter then @outreach_death_date_bncd := null - when @prev_id != @cur_id then @outreach_death_date_bncd := null - else @outreach_death_date_bncd - end as next_outreach_death_date_bncd, - - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then cast(@outreach_patient_care_status_bncd as unsigned) - else null - end as outreach_patient_care_status_bncd, - - case - when encounter_type=21 and @outreach_patient_care_status_bncd is null then @outreach_patient_care_status_bncd := patient_care_status - when is_clinical_encounter then @outreach_patient_care_status_bncd := null - when @prev_id != @cur_id then @outreach_patient_care_status_bncd := null - else @outreach_patient_care_status_bncd - end as next_outreach_patient_care_status_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_date_bncd - else null - end as transfer_date_bncd, - - case - when encounter_type=116 and @transfer_date_bncd is null then @transfer_date_bncd := encounter_datetime - when is_clinical_encounter then @transfer_date_bncd := null - when @prev_id != @cur_id then @transfer_date_bncd := null - else @transfer_date_bncd - end as next_transfer_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_transfer_out_bncd - else null - end as transfer_transfer_out_bncd, - - case - when encounter_type=116 and @transfer_transfer_out_bncd is null then @transfer_transfer_out_bncd := encounter_datetime - when is_clinical_encounter then @transfer_transfer_out_bncd := null - when @prev_id != @cur_id then @transfer_transfer_out_bncd := null - else @transfer_transfer_out_bncd - end as next_transfer_transfer_out_bncd - - from flat_hiv_summary_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_hiv_summary_3; - create temporary table flat_hiv_summary_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_hiv, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_hiv_summary_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - alter table flat_hiv_summary_3 drop prev_id, drop cur_id; - - set @prev_id = null; - set @cur_id = null; - set @transfer_in = null; - set @transfer_in_date = null; - set @transfer_in_location_id = null; - set @transfer_out = null; - set @transfer_out_date = null; - set @transfer_out_location_id = null; - - drop temporary table if exists flat_hiv_summary_4; - - create temporary table flat_hiv_summary_4 ( index person_enc (person_id, encounter_datetime)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - - - case - when obs regexp "!!7015=" then @transfer_in := 1 - when prev_clinical_location_id != location_id 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 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 then @transfer_in_location_id := prev_clinical_location_id - when @cur_id = @prev_id then @transfer_in_location_id - else @transfer_in_location_id := null - end transfer_in_location_id, - - - - - - - - - - - - case - when obs regexp "!!1285=!!" then @transfer_out := 1 - when obs regexp "!!1596=1594!!" then @transfer_out := 1 - when obs regexp "!!9082=(1287|1594|9068|9504|1285)!!" then @transfer_out := 1 - when next_clinical_location_id != location_id then @transfer_out := 1 - else @transfer_out := null - end as transfer_out, - - case - when obs regexp "!!1285=(1287|9068|2050)!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9999 - when obs regexp "!!1285=1286!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9998 - when next_clinical_location_id != location_id then @transfer_out_location_id := next_clinical_location_id - else @transfer_out_location_id := null - end transfer_out_location_id, - - - case - when @transfer_out and next_clinical_datetime_hiv is null then @transfer_out_date := date(cur_rtc_date) - when next_clinical_location_id != location_id then @transfer_out_date := date(next_clinical_datetime_hiv) - when transfer_transfer_out_bncd then @transfer_out_date := date(transfer_transfer_out_bncd) - else @transfer_out_date := null - end transfer_out_date - - - - from flat_hiv_summary_3 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - select count(*) into @new_encounter_rows from flat_hiv_summary_4; - - select @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; - select @total_rows_written; - - - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - t1.uuid, - visit_id, - encounter_id, - encounter_datetime, - encounter_type, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - visit_num, - enrollment_date, - enrollment_location_id, - hiv_start_date, - death_date, - scheduled_visit, - transfer_in, - transfer_in_location_id, - transfer_in_date, - transfer_out, - transfer_out_location_id, - transfer_out_date, - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date as rtc_date, - arv_first_regimen, - arv_first_regimen_location_id, - arv_first_regimen_start_date, - prev_arv_meds, - cur_arv_meds, - cur_arv_meds_strict, - arv_start_date, - arv_start_location_id, - prev_arv_start_date, - prev_arv_end_date, - prev_arv_line, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - is_pregnant, - edd, - tb_screen, - tb_screening_result, - tb_screening_datetime, - on_ipt, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - on_tb_tx, - tb_tx_start_date, - tb_tx_end_date, - pcp_prophylaxis_start_date, - condoms_provided_date, - modern_contraceptive_method_start_date, - contraceptive_method, - cur_who_stage, - discordant_status, - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - vl_order_date, - cd4_order_date, - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_location_id, - next_clinical_location_id, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv, - outreach_date_bncd, - outreach_death_date_bncd, - outreach_patient_care_status_bncd, - transfer_date_bncd, - transfer_transfer_out_bncd - - from flat_hiv_summary_4 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_hiv_summary_build_queue__0 t2 using (person_id);'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - - select @person_ids_count as 'persons remaining', @cycle_length as 'Cycle time (s)', ceil(@person_ids_count / cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_v15_6.sql b/etl-scripts/stored-procedures/generate_hiv_summary_v15_6.sql deleted file mode 100644 index 119e4c5..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_v15_6.sql +++ /dev/null @@ -1,1763 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_v15_6`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_hiv_summary_v15b"; - set @query_type = query_type; - set @queue_table = ""; - set @total_rows_written = 0; - - set @start = now(); - set @table_version = "flat_hiv_summary_v2.15"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - - create table if not exists flat_hiv_summary_v15b ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - is_clinical_encounter int, - location_id int, - location_uuid varchar(100), - visit_num int, - enrollment_date datetime, - enrollment_location_id int, - hiv_start_date datetime, - death_date datetime, - scheduled_visit int, - transfer_in tinyint, - transfer_in_location_id int, - transfer_in_date datetime, - transfer_out tinyint, - transfer_out_location_id int, - transfer_out_date datetime, - patient_care_status int, - out_of_care int, - prev_rtc_date datetime, - rtc_date datetime, - arv_first_regimen varchar(500), - arv_first_regimen_location_id int, - arv_first_regimen_start_date datetime, - prev_arv_meds varchar(500), - cur_arv_meds varchar(500), - cur_arv_meds_strict varchar(500), - arv_start_date datetime, - arv_start_location_id int, - prev_arv_start_date datetime, - prev_arv_end_date datetime, - prev_arv_line int, - cur_arv_line int, - cur_arv_line_strict int, - cur_arv_line_reported tinyint, - prev_arv_adherence varchar(200), - cur_arv_adherence varchar(200), - hiv_status_disclosed int, - is_pregnant boolean, - edd datetime, - tb_screen boolean, - tb_screening_result int, - tb_screening_datetime datetime, - on_ipt boolean, - ipt_start_date datetime, - ipt_stop_date datetime, - ipt_completion_date datetime, - on_tb_tx boolean, - tb_tx_start_date datetime, - tb_tx_end_date datetime, - pcp_prophylaxis_start_date datetime, - condoms_provided_date datetime, - modern_contraceptive_method_start_date datetime, - contraceptive_method int, - cur_who_stage int, - discordant_status int, - cd4_resulted double, - cd4_resulted_date datetime, - cd4_1 double, - cd4_1_date datetime, - cd4_2 double, - cd4_2_date datetime, - cd4_percent_1 double, - cd4_percent_1_date datetime, - cd4_percent_2 double, - cd4_percent_2_date datetime, - vl_resulted int, - vl_resulted_date datetime, - vl_1 int, - vl_1_date datetime, - vl_2 int, - vl_2_date datetime, - vl_order_date datetime, - cd4_order_date datetime, - hiv_dna_pcr_order_date datetime, - hiv_dna_pcr_resulted int, - hiv_dna_pcr_resulted_date datetime, - hiv_dna_pcr_1 int, - hiv_dna_pcr_1_date datetime, - hiv_dna_pcr_2 int, - hiv_dna_pcr_2_date datetime, - hiv_rapid_test_resulted int, - hiv_rapid_test_resulted_date datetime, - prev_encounter_datetime_hiv datetime, - next_encounter_datetime_hiv datetime, - prev_encounter_type_hiv mediumint, - next_encounter_type_hiv mediumint, - prev_clinical_datetime_hiv datetime, - next_clinical_datetime_hiv datetime, - prev_clinical_location_id mediumint, - next_clinical_location_id mediumint, - prev_clinical_rtc_date_hiv datetime, - next_clinical_rtc_date_hiv datetime, - outreach_date_bncd datetime, - outreach_death_date_bncd datetime, - outreach_patient_care_status_bncd int, - transfer_date_bncd datetime, - transfer_transfer_out_bncd datetime, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_hiv), - index encounter_type (encounter_type), - index date_created (date_created) - - ); - - - - 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 @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_hiv_summary_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from flat_hiv_summary_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_hiv_summary_v15b"; - set @queue_table = "flat_hiv_summary_sync_queue"; - create table if not exists flat_hiv_summary_sync_queue (person_id int primary key); - - - - set @last_update = null; - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - - replace into flat_hiv_summary_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - select @person_ids_count as 'num patients to sync'; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - - drop temporary table if exists flat_hiv_summary_build_queue__0; - - - - SET @dyn_sql=CONCAT('create temporary table flat_hiv_summary_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - drop temporary table if exists flat_hiv_summary_0a; - create temporary table flat_hiv_summary_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - 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,129,138,153,154,158,162,163) then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,115,117,120,127,128,138, 153,154,158,162,163) then 10 - when t1.encounter_type in (129) then 5 - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_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,153,154,158, 161,162,163) - AND NOT obs regexp "!!5303=(822|664|1067)!!" - AND NOT obs regexp "!!9082=9036!!" - ); - - insert into flat_hiv_summary_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - ); - - drop temporary table if exists flat_hiv_summary_0; - create temporary table flat_hiv_summary_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_date = null; - set @cur_encounter_date = null; - set @enrollment_date = null; - set @hiv_start_date = null; - set @cur_location = null; - set @cur_rtc_date = null; - set @prev_rtc_date = null; - set @hiv_start_date = null; - set @prev_arv_start_date = null; - set @arv_start_date = null; - set @prev_arv_end_date = null; - set @arv_start_location_id = null; - set @art_first_regimen_start_date = null; - set @arv_first_regimen = null; - set @prev_arv_line = null; - set @cur_arv_line = null; - set @prev_arv_adherence = null; - set @cur_arv_adherence = null; - set @hiv_status_disclosed = null; - set @is_pregnant = null; - set @edd = null; - set @prev_arv_meds = null; - set @cur_arv_meds = null; - set @ipt_start_date = null; - set @ipt_end_date = null; - set @ipt_completion_date = null; - - set @on_tb_tx = null; - set @tb_tx_start_date = null; - set @tb_tx_end_date = null; - set @pcp_prophylaxis_start_date = null; - set @tb_screen = null; - set @tb_screening_result = null; - set @tb_screening_datetime = null; - - set @death_date = null; - - set @patient_care_status=null; - - set @condoms_provided_date = null; - set @modern_contraceptive_method_start_date = null; - set @contraceptive_method = null; - - - set @cur_who_stage = null; - - set @vl_1=null; - set @vl_2=null; - set @vl_1_date=null; - set @vl_2_date=null; - set @vl_resulted=null; - set @vl_resulted_date=null; - - set @cd4_resulted=null; - set @cd4_resulted_date=null; - set @cd4_1=null; - set @cd4_1_date=null; - set @cd4_2=null; - set @cd4_2_date=null; - set @cd4_percent_1=null; - set @cd4_percent_1_date=null; - set @cd4_percent_2=null; - set @cd4_percent_2_date=null; - set @vl_order_date = null; - set @cd4_order_date = null; - - set @hiv_dna_pcr_order_date = null; - set @hiv_dna_pcr_1=null; - set @hiv_dna_pcr_2=null; - set @hiv_dna_pcr_1_date=null; - set @hiv_dna_pcr_2_date=null; - - set @hiv_rapid_test_resulted=null; - set @hiv_rapid_test_resulted_date= null; - - - - - - - drop temporary table if exists flat_hiv_summary_1; - create temporary table flat_hiv_summary_1 (index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.encounter_id, - @prev_encounter_date := date(@cur_encounter_date) as prev_encounter_date, - @cur_encounter_date := date(encounter_datetime) as cur_encounter_date, - t1.encounter_datetime, - t1.encounter_type, - t1.is_clinical_encounter, - CASE - WHEN - (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7013=' - THEN - @enrollment_date:=REPLACE(REPLACE((SUBSTRING_INDEX(SUBSTRING(obs, LOCATE('!!7013=', obs)), - @sep, - 1)), - '!!7013=', - ''), - '!!', - '') - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:='1900-01-01' - WHEN - t1.encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:=DATE(encounter_datetime) - WHEN @prev_id = @cur_id THEN @enrollment_date - ELSE @enrollment_date:=NULL - END AS enrollment_date, - - - - CASE - WHEN - (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7030=5622' - THEN - @enrollment_location_id:=9999 - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollmen_location_id:=9999 - WHEN - encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_location_id:= location_id - WHEN @prev_id = @cur_id THEN @enrollment_location_id - ELSE @enrollment_location_id:=NULL - END AS enrollment_location_id, - - - - - - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as 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 - end as visit_num, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - - - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504|1285)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - - - - - - - - - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when @prev_id != @cur_id or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := encounter_datetime - else @death_date := null - end - else @death_date - end as death_date, - - - - - - case - when @death_date <= encounter_datetime then @patient_care_status := 159 - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - - case - when obs regexp "!!9203=" then @hiv_start_date := replace(replace((substring_index(substring(obs,locate("!!9203=",obs)),@sep,1)),"!!9203=",""),"!!","") - when obs regexp "!!7015=" then @hiv_start_date := "1900-01-01" - when @hiv_start_date is null then @hiv_start_date := date(encounter_datetime) - when @prev_id = @cur_id then @hiv_start_date - else @hiv_start_date := null - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_location_id := location_id - when @prev_id = @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location_id := location_id - when @prev_id != @cur_id then @arv_start_location_id := null - else @arv_start_location_id - end as arv_start_location_id, - - - case - when @prev_id=@cur_id and @cur_arv_meds is not null then @prev_arv_meds := @cur_arv_meds - when @prev_id=@cur_id then @prev_arv_meds - else @prev_arv_meds := null - end as prev_arv_meds, - - - - - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - when @prev_id = @cur_id then @cur_arv_meds - else @cur_arv_meds:= null - end as cur_arv_meds, - - - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - else null - end as cur_arv_meds_strict, - - - - case - when @arv_first_regimen is null and obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @arv_first_regimen := normalize_arvs(obs,'2157') - when obs regexp "!!7015=" and @arv_first_regimen is null then @arv_first_regimen := "unknown" - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id = @cur_id then @arv_first_regimen - when @prev_id != @cur_id then @arv_first_regimen := @cur_arv_meds - else "-1" - end as arv_first_regimen, - - - - CASE - WHEN - (@arv_first_regimen_start_date IS NULL - || (@arv_first_regimen_start_date IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_start_date:=REPLACE(REPLACE((SUBSTRING_INDEX(SUBSTRING(obs, LOCATE('!!1499=', obs)), - @sep, - 1)), - '!!1499=', - ''), - '!!', - '') - WHEN - (@arv_first_regimen_start_date IS NULL - || (@arv_first_regimen_start_date IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7015=' - AND (obs REGEXP '!!1255=(1256)!!' - || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_start_date:=DATE(encounter_datetime) - WHEN - (@arv_first_regimen_start_date IS NULL - || (@arv_first_regimen_start_date IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7015=' - THEN - @arv_first_regimen_start_date:='1900-01-01' - WHEN - (@arv_first_regimen_start_date IS NULL - || (@arv_first_regimen_start_date IS NOT NULL - AND @prev_id != @cur_id)) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_start_date:=DATE(encounter_datetime) - WHEN @prev_id = @cur_id THEN @arv_first_regimen_start_date - WHEN @prev_id != @cur_id THEN @arv_first_regimen_start_date:=NULL - ELSE @arv_first_regimen_start_date - END AS arv_first_regimen_start_date, - - - - case - when @arv_first_regimen is null and obs regexp "!!1499=" then @arv_first_regimen_location_id := 9999 - when @prev_id != @cur_id and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @arv_first_regimen_location_id is null and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @prev_id = @cur_id then @arv_first_regimen_location_id - when @prev_id != @cur_id then @arv_first_regimen_location_id := null - else "-1" - end as arv_first_regimen_location_id, - - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := null - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=(6156)!!" then @cur_arv_line := 3 - when @prev_id = @cur_id then @cur_arv_line - else @cur_arv_line := null - end as cur_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1250=(6156)!!" then 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1088=(6156)!!" then 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then 2 - when obs regexp "!!2154=(6156)!!" then 3 - else null - end as cur_arv_line_strict, - - - - - - - - - - case - when obs regexp "!!6976=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6976=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6976=6695!!" then @cur_arv_line_reported := 3 - - when obs regexp "!!6744=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6744=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6744=6695!!" then @cur_arv_line_reported := 3 - when @prev_id = @cur_id then @cur_arv_line_reported - else @cur_arv_line_reported := null - end as cur_arv_line_reported, - - - - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := null - end as prev_arv_start_date, - - - - - - - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - - when @cur_arv_meds != @prev_arv_meds then @arv_start_date := date(t1.encounter_datetime) - - when @prev_id != @cur_id then @arv_start_date := null - else @arv_start_date - end as arv_start_date, - - - case - when @prev_arv_start_date != @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end as prev_arv_end_date, - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := null - end as prev_arv_adherence, - - - - - - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id = @cur_id then @cur_arv_adherence - else @cur_arv_adherence := null - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when @prev_id = @cur_id then @hiv_status_disclosed - else @hiv_status_disclosed := null - end as hiv_status_disclosed, - - - - - - - - - - - - - - - - - - case - when obs regexp "!!8351=(48|50|1066|1624|6971|9608)!!" then @is_pregnant := null - when @prev_id != @cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" or obs regexp "!!8351=(1065|1484)!!" then @is_pregnant := true - else @is_pregnant := null - end - when @is_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") or obs regexp "!!8351=(1065|1484)!!"then @is_pregnant := true - when @is_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@is_pregnant,encounter_datetime) > 40 or timestampdiff(week,@edd,encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @is_pregnant := null - else @is_pregnant - end as is_pregnant, - - - - - - - - - case - when @prev_id != @cur_id then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd := null - end - when @edd is null then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - - - case - when obs regexp "!!6174=" then @tb_screen := true - when obs regexp "!!2022=1065!!" then @tb_screen := true - when obs regexp "!!307=" then @tb_screen := true - when obs regexp "!!12=" then @tb_screen := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screen := true - when orders regexp "(12|307|8064|2311|2323)" then @tb_screen := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screen := true - when obs regexp "!!5958=1077!!" then @tb_screen := true - when obs regexp "!!2020=1065!!" then @tb_screen := true - when obs regexp "!!2021=1065!!" then @tb_screen := true - when obs regexp "!!2028=" then @tb_screen := true - when obs regexp "!!1268=(1256|1850)!!" then @tb_screen := true - when obs regexp "!!5959=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!5971=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!1492=107!!" then @tb_screen := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screen := true - end as tb_screen, - - case - when obs regexp "!!8292=" then @tb_screening_result := - replace(replace((substring_index(substring(obs,locate("!!8292=",obs)),@sep,1)),"!!8292=",""),"!!","") - when @prev_id != @cur_id then @tb_screening_result := null - else @tb_screening_result - end as tb_screening_result, - - case - when obs regexp "!!6174=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2022=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!307=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!12=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime - when orders regexp "(12|307|8064|2311|2323)" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5958=1077!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2020=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2021=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2028=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1268=(1256|1850)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5959=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5971=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1492=107!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screening_datetime := encounter_datetime - when @cur_id = @prev_id then @tb_screening_datetime - else @tb_screening_datetime := null - end as tb_screening_datetime, - - - case - when obs regexp "!!1265=(1107|1260)!!" then @on_ipt := 0 - when obs regexp "!!1265=!!" then @on_ipt := 1 - when obs regexp "!!1110=656!!" then @on_ipt := 1 - when @cur_id != @prev_id then @on_ipt := null - else @on_ipt - end as on_ipt, - - - - case - when obs regexp "!!1265=(1256|1850)!!" then @ipt_start_date := encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" and @ipt_start_date is null then @ipt_start_date := encounter_datetime - when @cur_id != @prev_id then @ipt_start_date := null - else @ipt_start_date - end as ipt_start_date, - - - - case - when obs regexp "!!1266=" then @ipt_stop_date := encounter_datetime - when @cur_id = @prev_id then @ipt_stop_date - when @cur_id != @prev_id then @ipt_stop_date := null - else @ipt_stop_date - end as ipt_stop_date, - - case - when obs regexp "!!1266=1267!!" then @ipt_completion_date := encounter_datetime - when @cur_id = @prev_id then @ipt_completion_date - when @cur_id != @prev_id then @ipt_completion_date := null - else @ipt_completion_date - end as ipt_completion_date, - - - case - when obs regexp "!!1268=(1107|1260)!!" then @on_tb_tx := 0 - when obs regexp "!!1268=" then @on_tb_tx := 1 - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @on_tb_tx := 1 - else @on_tb_tx := 0 - end as on_tb_tx, - - - - case - when obs regexp "!!1113=" then @tb_tx_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @tb_tx_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" and obs regexp "!!7015=" and @tb_tx_start_date is null then @tb_tx_start_date := null - when obs regexp "!!1268=(1257|1259|1849|981)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_start_date - else @tb_tx_start_date := null - end as tb_tx_start_date, - - - - - case - when obs regexp "!!2041=" then @tb_tx_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_tx_end_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_end_date - else @tb_tx_end_date := null - end as tb_tx_end_date, - - - - - - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1261=1257!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1193=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when @prev_id=@cur_id then @pcp_prophylaxis_start_date - else @pcp_prophylaxis_start_date := null - end as pcp_prophylaxis_start_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - else @cd4_2:=null - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - else @cd4_2_date:=null - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=null - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=null - end as cd4_1_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - else @cd4_percent_2:=null - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - else @cd4_percent_2_date:=null - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=null - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=null - end as cd4_percent_1_date, - - - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <> date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - else @vl_2:=null - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - else @vl_2_date:=null - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=null - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date:=null - end as vl_1_date, - - - - - - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(encounter_datetime) - when orders regexp "856" then @vl_order_date := date(encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - else @vl_order_date := null - end as vl_order_date, - - - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := null - end as cd4_order_date, - - - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := null - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - else @hiv_dna_pcr_2:=null - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - else @hiv_dna_pcr_2_date:=null - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=null - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=null - end as hiv_dna_pcr_1_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - case - when obs regexp "!!8302=8305!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!6579=" then @condoms_provided_date := encounter_datetime - when @prev_id = @cur_id then @condoms_provided_date - else @condoms_provided_date := null - end as condoms_provided_date, - - - - - - - case - when obs regexp "!!7240=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(encounter_datetime) - when obs regexp "!!7240=!!" - then @modern_contraceptive_method_start_date := null - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" and obs regexp "!!1190=" - then @modern_contraceptive_method_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1190=",obs)),@sep,1)),"!!1190=",""),"!!","")) - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(encounter_datetime) - when obs regexp "!!374=!!" - then @modern_contraceptive_method_start_date := null - when @prev_id = @cur_id then @modern_contraceptive_method_start_date - else @modern_contraceptive_method_start_date := null - end as modern_contraceptive_method_start_date, - - - case - when obs regexp "!!7240=1107!!" then @contraceptive_method := null - when obs regexp "!!7240=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!7240=",obs)),@sep,1)),"!!7240=",""),"!!","") - when obs regexp "!!374=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!374=",obs)),@sep,1)),"!!374=",""),"!!","") - when @prev_id = @cur_id then @contraceptive_method - else @contraceptive_method := null - end as contraceptive_method, - - - - - - - case - when obs regexp "!!5356=(1204)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1223)!!" then @cur_who_stage := 4 - when @prev_id = @cur_id then @cur_who_stage - else @cur_who_stage := null - end as cur_who_stage, - - - case - when obs regexp "!!6096=1065" then @discordant_status := "Yes" - when obs regexp "!!6096=1066" then @discordant_status := "No" - when obs regexp "!!6096=1067" then @discordant_status := "Unknown" - when obs regexp "!!6096=1175" then @discordant_status := "N/A" - when obs regexp "!!6096=6826" then @discordant_status := "Concordant Couple Positive" - when obs regexp "!!6096=6827" then @discordant_status := "Concordant Couple Negative" - when @prev_id = @cur_id then @discordant_status - else @discordant_status := null - end as discordant_status - - - - - from flat_hiv_summary_0 t1 - join amrs.person p using (person_id) - ); - - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_hiv_summary_1 drop prev_id, drop cur_id; - - drop table if exists flat_hiv_summary_2; - create temporary table flat_hiv_summary_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_date_bncd - else null - end as outreach_date_bncd, - - case - when encounter_type=21 and @outreach_date_bncd is null then @outreach_date_bncd := encounter_datetime - when is_clinical_encounter then @outreach_date_bncd := null - when @prev_id != @cur_id then @outreach_date_bncd := null - else @outreach_date_bncd - end as next_outreach_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_death_date_bncd - else null - end as outreach_death_date_bncd, - - case - when encounter_type=21 and @outreach_death_date_bncd is null then @outreach_death_date_bncd := death_date - when is_clinical_encounter then @outreach_death_date_bncd := null - when @prev_id != @cur_id then @outreach_death_date_bncd := null - else @outreach_death_date_bncd - end as next_outreach_death_date_bncd, - - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then cast(@outreach_patient_care_status_bncd as unsigned) - else null - end as outreach_patient_care_status_bncd, - - case - when encounter_type=21 and @outreach_patient_care_status_bncd is null then @outreach_patient_care_status_bncd := patient_care_status - when is_clinical_encounter then @outreach_patient_care_status_bncd := null - when @prev_id != @cur_id then @outreach_patient_care_status_bncd := null - else @outreach_patient_care_status_bncd - end as next_outreach_patient_care_status_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_date_bncd - else null - end as transfer_date_bncd, - - case - when encounter_type=116 and @transfer_date_bncd is null then @transfer_date_bncd := encounter_datetime - when is_clinical_encounter then @transfer_date_bncd := null - when @prev_id != @cur_id then @transfer_date_bncd := null - else @transfer_date_bncd - end as next_transfer_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_transfer_out_bncd - else null - end as transfer_transfer_out_bncd, - - case - when encounter_type=116 and @transfer_transfer_out_bncd is null then @transfer_transfer_out_bncd := encounter_datetime - when is_clinical_encounter then @transfer_transfer_out_bncd := null - when @prev_id != @cur_id then @transfer_transfer_out_bncd := null - else @transfer_transfer_out_bncd - end as next_transfer_transfer_out_bncd - - from flat_hiv_summary_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_hiv_summary_3; - create temporary table flat_hiv_summary_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_hiv, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_hiv_summary_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - alter table flat_hiv_summary_3 drop prev_id, drop cur_id; - - set @prev_id = null; - set @cur_id = null; - set @transfer_in = null; - set @transfer_in_date = null; - set @transfer_in_location_id = null; - set @transfer_out = null; - set @transfer_out_date = null; - set @transfer_out_location_id = null; - - drop temporary table if exists flat_hiv_summary_4; - - create temporary table flat_hiv_summary_4 ( index person_enc (person_id, encounter_datetime)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - - - case - when obs regexp "!!7015=" then @transfer_in := 1 - when prev_clinical_location_id != location_id 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 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 then @transfer_in_location_id := prev_clinical_location_id - when @cur_id = @prev_id then @transfer_in_location_id - else @transfer_in_location_id := null - end transfer_in_location_id, - - - - - - - - - - - - case - when obs regexp "!!1285=!!" then @transfer_out := 1 - when obs regexp "!!1596=1594!!" then @transfer_out := 1 - when obs regexp "!!9082=(1287|1594|9068|9504|1285)!!" then @transfer_out := 1 - when next_clinical_location_id != location_id then @transfer_out := 1 - else @transfer_out := null - end as transfer_out, - - case - when obs regexp "!!1285=(1287|9068|2050)!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9999 - when obs regexp "!!1285=1286!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9998 - when next_clinical_location_id != location_id then @transfer_out_location_id := next_clinical_location_id - else @transfer_out_location_id := null - end transfer_out_location_id, - - - case - when @transfer_out and next_clinical_datetime_hiv is null then @transfer_out_date := date(cur_rtc_date) - when next_clinical_location_id != location_id then @transfer_out_date := date(next_clinical_datetime_hiv) - when transfer_transfer_out_bncd then @transfer_out_date := date(transfer_transfer_out_bncd) - else @transfer_out_date := null - end transfer_out_date - - - - from flat_hiv_summary_3 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - select count(*) into @new_encounter_rows from flat_hiv_summary_4; - - select @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; - select @total_rows_written; - - - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - t1.uuid, - visit_id, - encounter_id, - encounter_datetime, - encounter_type, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - visit_num, - enrollment_date, - enrollment_location_id, - hiv_start_date, - death_date, - scheduled_visit, - transfer_in, - transfer_in_location_id, - transfer_in_date, - transfer_out, - transfer_out_location_id, - transfer_out_date, - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date as rtc_date, - arv_first_regimen, - arv_first_regimen_location_id, - arv_first_regimen_start_date, - prev_arv_meds, - cur_arv_meds, - cur_arv_meds_strict, - arv_start_date, - arv_start_location_id, - prev_arv_start_date, - prev_arv_end_date, - prev_arv_line, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - is_pregnant, - edd, - tb_screen, - tb_screening_result, - tb_screening_datetime, - on_ipt, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - on_tb_tx, - tb_tx_start_date, - tb_tx_end_date, - pcp_prophylaxis_start_date, - condoms_provided_date, - modern_contraceptive_method_start_date, - contraceptive_method, - cur_who_stage, - discordant_status, - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - vl_order_date, - cd4_order_date, - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_location_id, - next_clinical_location_id, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv, - outreach_date_bncd, - outreach_death_date_bncd, - outreach_patient_care_status_bncd, - transfer_date_bncd, - transfer_transfer_out_bncd - - from flat_hiv_summary_4 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_hiv_summary_build_queue__0 t2 using (person_id);'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - - select @person_ids_count as 'persons remaining', @cycle_length as 'Cycle time (s)', ceil(@person_ids_count / cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_v15_7.sql b/etl-scripts/stored-procedures/generate_hiv_summary_v15_7.sql deleted file mode 100644 index 9f472b0..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_v15_7.sql +++ /dev/null @@ -1,1965 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_v15_7`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_hiv_summary_v15b"; - set @query_type = query_type; - set @queue_table = ""; - set @total_rows_written = 0; - - set @start = now(); - set @table_version = "flat_hiv_summary_v2.17"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - - create table if not exists flat_hiv_summary_v15b ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - is_clinical_encounter int, - location_id int, - location_uuid varchar(100), - visit_num int, - enrollment_date datetime, - enrollment_location_id int, - hiv_start_date datetime, - death_date datetime, - scheduled_visit int, - transfer_in tinyint, - transfer_in_location_id int, - transfer_in_date datetime, - transfer_out tinyint, - transfer_out_location_id int, - transfer_out_date datetime, - patient_care_status int, - out_of_care int, - prev_rtc_date datetime, - rtc_date datetime, - arv_first_regimen varchar(500), - arv_first_regimen_location_id int, - arv_first_regimen_start_date datetime, - arv_first_regimen_start_date_flex datetime, - prev_arv_meds varchar(500), - cur_arv_meds varchar(500), - cur_arv_meds_strict varchar(500), - arv_start_date datetime, - arv_start_location_id int, - prev_arv_start_date datetime, - prev_arv_end_date datetime, - prev_arv_line int, - cur_arv_line int, - cur_arv_line_strict int, - cur_arv_line_reported tinyint, - prev_arv_adherence varchar(200), - cur_arv_adherence varchar(200), - hiv_status_disclosed int, - is_pregnant boolean, - edd datetime, - tb_screen boolean, - tb_screening_result int, - tb_screening_datetime datetime, - on_ipt boolean, - ipt_start_date datetime, - ipt_stop_date datetime, - ipt_completion_date datetime, - on_tb_tx boolean, - tb_tx_start_date datetime, - tb_tx_end_date datetime, - pcp_prophylaxis_start_date datetime, - condoms_provided_date datetime, - modern_contraceptive_method_start_date datetime, - contraceptive_method int, - cur_who_stage int, - discordant_status int, - cd4_resulted double, - cd4_resulted_date datetime, - cd4_1 double, - cd4_1_date datetime, - cd4_2 double, - cd4_2_date datetime, - cd4_percent_1 double, - cd4_percent_1_date datetime, - cd4_percent_2 double, - cd4_percent_2_date datetime, - vl_resulted int, - vl_resulted_date datetime, - vl_1 int, - vl_1_date datetime, - vl_2 int, - vl_2_date datetime, - vl_order_date datetime, - cd4_order_date datetime, - hiv_dna_pcr_order_date datetime, - hiv_dna_pcr_resulted int, - hiv_dna_pcr_resulted_date datetime, - hiv_dna_pcr_1 int, - hiv_dna_pcr_1_date datetime, - hiv_dna_pcr_2 int, - hiv_dna_pcr_2_date datetime, - hiv_rapid_test_resulted int, - hiv_rapid_test_resulted_date datetime, - prev_encounter_datetime_hiv datetime, - next_encounter_datetime_hiv datetime, - prev_encounter_type_hiv mediumint, - next_encounter_type_hiv mediumint, - prev_clinical_datetime_hiv datetime, - next_clinical_datetime_hiv datetime, - prev_clinical_location_id mediumint, - next_clinical_location_id mediumint, - prev_clinical_rtc_date_hiv datetime, - next_clinical_rtc_date_hiv datetime, - outreach_date_bncd datetime, - outreach_death_date_bncd datetime, - outreach_patient_care_status_bncd int, - transfer_date_bncd datetime, - transfer_transfer_out_bncd datetime, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_hiv), - index encounter_type (encounter_type), - index date_created (date_created) - - ); - - - - 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 @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_hiv_summary_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from flat_hiv_summary_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_hiv_summary_v15b"; - set @queue_table = "flat_hiv_summary_sync_queue"; - create table if not exists flat_hiv_summary_sync_queue (person_id int primary key); - - - - set @last_update = null; - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - - replace into flat_hiv_summary_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - select @person_ids_count as 'num patients to sync'; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - - drop temporary table if exists flat_hiv_summary_build_queue__0; - - - - SET @dyn_sql=CONCAT('create temporary table flat_hiv_summary_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - drop temporary table if exists flat_hiv_summary_0a; - create temporary table flat_hiv_summary_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - - 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,129,138,140,153,154,158,162,163) then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,115,117,120,127,128,138, 140, 153,154,158,162,163) then 10 - when t1.encounter_type in (129) then 5 - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_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) - AND NOT obs regexp "!!5303=(822|664|1067)!!" - AND NOT obs regexp "!!9082=9036!!" - ); - - insert into flat_hiv_summary_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - ); - - drop temporary table if exists flat_hiv_summary_0; - create temporary table flat_hiv_summary_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_date = null; - set @cur_encounter_date = null; - set @enrollment_date = null; - set @hiv_start_date = null; - set @cur_location = null; - set @cur_rtc_date = null; - set @prev_rtc_date = null; - set @hiv_start_date = null; - set @prev_arv_start_date = null; - set @arv_start_date = null; - set @prev_arv_end_date = null; - set @arv_start_location_id = null; - set @arv_first_regimen_start_date = null; - set @arv_first_regimen_start_date_flex = null; - set @arv_first_regimen = null; - set @prev_arv_line = null; - set @cur_arv_line = null; - set @prev_arv_adherence = null; - set @cur_arv_adherence = null; - set @hiv_status_disclosed = null; - set @is_pregnant = null; - set @edd = null; - set @prev_arv_meds = null; - set @cur_arv_meds = null; - set @ipt_start_date = null; - set @ipt_end_date = null; - set @ipt_completion_date = null; - - set @on_tb_tx = null; - set @tb_tx_start_date = null; - set @tb_tx_end_date = null; - set @pcp_prophylaxis_start_date = null; - set @tb_screen = null; - set @tb_screening_result = null; - set @tb_screening_datetime = null; - - set @death_date = null; - - set @patient_care_status=null; - - set @condoms_provided_date = null; - set @modern_contraceptive_method_start_date = null; - set @contraceptive_method = null; - - - set @cur_who_stage = null; - - set @vl_1=null; - set @vl_2=null; - set @vl_1_date=null; - set @vl_2_date=null; - set @vl_resulted=null; - set @vl_resulted_date=null; - - set @cd4_resulted=null; - set @cd4_resulted_date=null; - set @cd4_1=null; - set @cd4_1_date=null; - set @cd4_2=null; - set @cd4_2_date=null; - set @cd4_percent_1=null; - set @cd4_percent_1_date=null; - set @cd4_percent_2=null; - set @cd4_percent_2_date=null; - set @vl_order_date = null; - set @cd4_order_date = null; - - set @hiv_dna_pcr_order_date = null; - set @hiv_dna_pcr_1=null; - set @hiv_dna_pcr_2=null; - set @hiv_dna_pcr_1_date=null; - set @hiv_dna_pcr_2_date=null; - - set @hiv_rapid_test_resulted=null; - set @hiv_rapid_test_resulted_date= null; - - - - - - - drop temporary table if exists flat_hiv_summary_1; - create temporary table flat_hiv_summary_1 (index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.encounter_id, - @prev_encounter_date := date(@cur_encounter_date) as prev_encounter_date, - @cur_encounter_date := date(encounter_datetime) as cur_encounter_date, - t1.encounter_datetime, - t1.encounter_type, - t1.is_clinical_encounter, - CASE - WHEN - (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7013=' - THEN - @enrollment_date:=REPLACE(REPLACE((SUBSTRING_INDEX(SUBSTRING(obs, LOCATE('!!7013=', obs)), - @sep, - 1)), - '!!7013=', - ''), - '!!', - '') - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:='1900-01-01' - WHEN - t1.encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:=DATE(encounter_datetime) - WHEN @prev_id = @cur_id THEN @enrollment_date - ELSE @enrollment_date:=NULL - END AS enrollment_date, - - - - CASE - WHEN - (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7030=5622' - THEN - @enrollment_location_id:=9999 - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollmen_location_id:=9999 - WHEN - encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_location_id:= location_id - WHEN @prev_id = @cur_id THEN @enrollment_location_id - ELSE @enrollment_location_id:=NULL - END AS enrollment_location_id, - - - - - - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as 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 - end as visit_num, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - - - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504|1285)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - - - - - - - - - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when @prev_id != @cur_id or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := encounter_datetime - else @death_date := null - end - else @death_date - end as death_date, - - - - - - case - when @death_date <= encounter_datetime then @patient_care_status := 159 - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - - case - when obs regexp "!!9203=" then @hiv_start_date := replace(replace((substring_index(substring(obs,locate("!!9203=",obs)),@sep,1)),"!!9203=",""),"!!","") - when obs regexp "!!7015=" then @hiv_start_date := "1900-01-01" - when @hiv_start_date is null then @hiv_start_date := date(encounter_datetime) - when @prev_id = @cur_id then @hiv_start_date - else @hiv_start_date := null - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_location_id := location_id - when @prev_id = @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location_id := location_id - when @prev_id != @cur_id then @arv_start_location_id := null - else @arv_start_location_id - end as arv_start_location_id, - - - case - when @prev_id=@cur_id and @cur_arv_meds is not null then @prev_arv_meds := @cur_arv_meds - when @prev_id=@cur_id then @prev_arv_meds - else @prev_arv_meds := null - end as prev_arv_meds, - - - - - #2154 : PATIENT REPORTED CURRENT ANTIRETROVIRAL TREATMENT - #2157 : PATIENT REPORTED PAST ANTIRETROVIRAL TREATMENT - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - when @prev_id = @cur_id then @cur_arv_meds - else @cur_arv_meds:= null - end as cur_arv_meds, - - - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - else null - end as cur_arv_meds_strict, - - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := @cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date, - - - CASE - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_start_date:=GetValues(obs,1499) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_start_date:='1900-01-01' - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_start_date:=DATE(encounter_datetime) - - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_start_date:= "1900-01-01" #DATE(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_start_date:= "1900-01-01" # DATE(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_start_date:='1900-01-01' - WHEN @prev_id = @cur_id THEN @arv_first_regimen_start_date - WHEN @prev_id != @cur_id THEN @arv_first_regimen_start_date:=NULL - ELSE @arv_first_regimen_start_date - END AS arv_first_regimen_start_date, - - - - - - CASE - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_start_date_flex := GetValues(obs,1499) - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_start_date_flex :='1900-01-01' - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_start_date_flex:=DATE(encounter_datetime) - - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen_start_date_flex :=date(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen_start_date_flex :=date(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_start_date_flex:='1900-01-01' - WHEN @prev_id = @cur_id THEN @arv_first_regimen_start_date_flex - WHEN @prev_id != @cur_id THEN @arv_first_regimen_start_date_flex:=NULL - ELSE @arv_first_regimen_start_date_flex - END AS arv_first_regimen_start_date_flex, - - - -/* - case - when @arv_first_regimen is null and obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @arv_first_regimen := normalize_arvs(obs,'2157') - when obs regexp "!!7015=" and @arv_first_regimen is null then @arv_first_regimen := "unknown" - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id = @cur_id then @arv_first_regimen - when @prev_id != @cur_id then @arv_first_regimen := @cur_arv_meds - else "-1" - end as arv_first_regimen, -*/ - - CASE - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen:= "unknown" - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen:= "unknown" - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen := @cur_arv_meds - - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen:= "unknown" #@cur_arv_meds - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen:= "unknown" #@cur_arv_meds - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen := "unknown" - - WHEN @prev_id = @cur_id THEN @arv_first_regimen - WHEN @prev_id != @cur_id THEN @arv_first_regimen:=NULL - ELSE @arv_first_regimen - END AS arv_first_regimen, - - -/* - case - when @arv_first_regimen is null and obs regexp "!!1499=" then @arv_first_regimen_location_id := 9999 - when @prev_id != @cur_id and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @arv_first_regimen_location_id is null and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @prev_id = @cur_id then @arv_first_regimen_location_id - when @prev_id != @cur_id then @arv_first_regimen_location_id := null - else "-1" - end as arv_first_regimen_location_id, -*/ - - CASE - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_location_id := 9999 - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_location_id:=9999 - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_location_id:=location_id - - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_location_id:= 9999 #location_id - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_location_id:= 9999 #location_id - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_location_id:=9999 - WHEN @prev_id = @cur_id THEN @arv_first_regimen_location_id - WHEN @prev_id != @cur_id THEN @arv_first_regimen_location_id:=NULL - ELSE @arv_first_regimen_location_id - END AS arv_first_regimen_location_id, - - - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := null - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=(6156)!!" then @cur_arv_line := 3 - when @prev_id = @cur_id then @cur_arv_line - else @cur_arv_line := null - end as cur_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1250=(6156)!!" then 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1088=(6156)!!" then 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then 2 - when obs regexp "!!2154=(6156)!!" then 3 - else null - end as cur_arv_line_strict, - - - - - - - - - - case - when obs regexp "!!6976=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6976=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6976=6695!!" then @cur_arv_line_reported := 3 - - when obs regexp "!!6744=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6744=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6744=6695!!" then @cur_arv_line_reported := 3 - when @prev_id = @cur_id then @cur_arv_line_reported - else @cur_arv_line_reported := null - end as cur_arv_line_reported, - - - - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := null - end as prev_arv_start_date, - - - - - - - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - - when @cur_arv_meds != @prev_arv_meds then @arv_start_date := date(t1.encounter_datetime) - - when @prev_id != @cur_id then @arv_start_date := null - else @arv_start_date - end as arv_start_date, - - - case - when @prev_arv_start_date != @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end as prev_arv_end_date, - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := null - end as prev_arv_adherence, - - - - - - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id = @cur_id then @cur_arv_adherence - else @cur_arv_adherence := null - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when @prev_id = @cur_id then @hiv_status_disclosed - else @hiv_status_disclosed := null - end as hiv_status_disclosed, - - - - - - - - - - - - - - - - - - case - when obs regexp "!!8351=(48|50|1066|1624|6971|9608)!!" then @is_pregnant := null - when @prev_id != @cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" or obs regexp "!!8351=(1065|1484)!!" then @is_pregnant := true - else @is_pregnant := null - end - when @is_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") or obs regexp "!!8351=(1065|1484)!!"then @is_pregnant := true - when @is_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@is_pregnant,encounter_datetime) > 40 or timestampdiff(week,@edd,encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @is_pregnant := null - else @is_pregnant - end as is_pregnant, - - - - - - - - - case - when @prev_id != @cur_id then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd := null - end - when @edd is null then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - - - case - when obs regexp "!!6174=" then @tb_screen := true - when obs regexp "!!2022=1065!!" then @tb_screen := true - when obs regexp "!!307=" then @tb_screen := true - when obs regexp "!!12=" then @tb_screen := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screen := true - when orders regexp "(12|307|8064|2311|2323)" then @tb_screen := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screen := true - when obs regexp "!!5958=1077!!" then @tb_screen := true - when obs regexp "!!2020=1065!!" then @tb_screen := true - when obs regexp "!!2021=1065!!" then @tb_screen := true - when obs regexp "!!2028=" then @tb_screen := true - when obs regexp "!!1268=(1256|1850)!!" then @tb_screen := true - when obs regexp "!!5959=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!5971=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!1492=107!!" then @tb_screen := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screen := true - end as tb_screen, - - case - when obs regexp "!!8292=" then @tb_screening_result := - replace(replace((substring_index(substring(obs,locate("!!8292=",obs)),@sep,1)),"!!8292=",""),"!!","") - when @prev_id != @cur_id then @tb_screening_result := null - else @tb_screening_result - end as tb_screening_result, - - case - when obs regexp "!!6174=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2022=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!307=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!12=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime - when orders regexp "(12|307|8064|2311|2323)" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5958=1077!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2020=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2021=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2028=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1268=(1256|1850)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5959=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5971=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1492=107!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screening_datetime := encounter_datetime - when @cur_id = @prev_id then @tb_screening_datetime - else @tb_screening_datetime := null - end as tb_screening_datetime, - - - case - when obs regexp "!!1265=(1107|1260)!!" then @on_ipt := 0 - when obs regexp "!!1265=!!" then @on_ipt := 1 - when obs regexp "!!1110=656!!" then @on_ipt := 1 - when @cur_id != @prev_id then @on_ipt := null - else @on_ipt - end as on_ipt, - - - - case - when obs regexp "!!1265=(1256|1850)!!" then @ipt_start_date := encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" and @ipt_start_date is null then @ipt_start_date := encounter_datetime - when @cur_id != @prev_id then @ipt_start_date := null - else @ipt_start_date - end as ipt_start_date, - - - - case - when obs regexp "!!1266=" then @ipt_stop_date := encounter_datetime - when @cur_id = @prev_id then @ipt_stop_date - when @cur_id != @prev_id then @ipt_stop_date := null - else @ipt_stop_date - end as ipt_stop_date, - - case - when obs regexp "!!1266=1267!!" then @ipt_completion_date := encounter_datetime - when @cur_id = @prev_id then @ipt_completion_date - when @cur_id != @prev_id then @ipt_completion_date := null - else @ipt_completion_date - end as ipt_completion_date, - - - case - when obs regexp "!!1268=(1107|1260)!!" then @on_tb_tx := 0 - when obs regexp "!!1268=" then @on_tb_tx := 1 - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @on_tb_tx := 1 - else @on_tb_tx := 0 - end as on_tb_tx, - - - - case - when obs regexp "!!1113=" then @tb_tx_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @tb_tx_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" and obs regexp "!!7015=" and @tb_tx_start_date is null then @tb_tx_start_date := null - when obs regexp "!!1268=(1257|1259|1849|981)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_start_date - else @tb_tx_start_date := null - end as tb_tx_start_date, - - - - - case - when obs regexp "!!2041=" then @tb_tx_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_tx_end_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_end_date - else @tb_tx_end_date := null - end as tb_tx_end_date, - - - - - - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1261=1257!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1193=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when @prev_id=@cur_id then @pcp_prophylaxis_start_date - else @pcp_prophylaxis_start_date := null - end as pcp_prophylaxis_start_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - else @cd4_2:=null - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - else @cd4_2_date:=null - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=null - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=null - end as cd4_1_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - else @cd4_percent_2:=null - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - else @cd4_percent_2_date:=null - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=null - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=null - end as cd4_percent_1_date, - - - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <> date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - else @vl_2:=null - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - else @vl_2_date:=null - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(encounter_datetime) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=null - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date:=null - end as vl_1_date, - - - - - - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(encounter_datetime) - when orders regexp "856" then @vl_order_date := date(encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - else @vl_order_date := null - end as vl_order_date, - - - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := null - end as cd4_order_date, - - - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := null - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - else @hiv_dna_pcr_2:=null - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - else @hiv_dna_pcr_2_date:=null - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=null - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=null - end as hiv_dna_pcr_1_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - case - when obs regexp "!!8302=8305!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!6579=" then @condoms_provided_date := encounter_datetime - when @prev_id = @cur_id then @condoms_provided_date - else @condoms_provided_date := null - end as condoms_provided_date, - - - - - - - case - when obs regexp "!!7240=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(encounter_datetime) - when obs regexp "!!7240=!!" - then @modern_contraceptive_method_start_date := null - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" and obs regexp "!!1190=" - then @modern_contraceptive_method_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1190=",obs)),@sep,1)),"!!1190=",""),"!!","")) - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(encounter_datetime) - when obs regexp "!!374=!!" - then @modern_contraceptive_method_start_date := null - when @prev_id = @cur_id then @modern_contraceptive_method_start_date - else @modern_contraceptive_method_start_date := null - end as modern_contraceptive_method_start_date, - - - case - when obs regexp "!!7240=1107!!" then @contraceptive_method := null - when obs regexp "!!7240=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!7240=",obs)),@sep,1)),"!!7240=",""),"!!","") - when obs regexp "!!374=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!374=",obs)),@sep,1)),"!!374=",""),"!!","") - when @prev_id = @cur_id then @contraceptive_method - else @contraceptive_method := null - end as contraceptive_method, - - - - - - - case - when obs regexp "!!5356=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!8287=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!8287=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!8287=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!8287=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!8307=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!8307=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!8307=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!8307=(1207|1223)!!" then @cur_who_stage := 4 - when @prev_id = @cur_id then @cur_who_stage - else @cur_who_stage := null - end as cur_who_stage, - - - case - when obs regexp "!!6096=1065" then @discordant_status := "Yes" - when obs regexp "!!6096=1066" then @discordant_status := "No" - when obs regexp "!!6096=1067" then @discordant_status := "Unknown" - when obs regexp "!!6096=1175" then @discordant_status := "N/A" - when obs regexp "!!6096=6826" then @discordant_status := "Concordant Couple Positive" - when obs regexp "!!6096=6827" then @discordant_status := "Concordant Couple Negative" - when @prev_id = @cur_id then @discordant_status - else @discordant_status := null - end as discordant_status - - - - - from flat_hiv_summary_0 t1 - join amrs.person p using (person_id) - ); - - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_hiv_summary_1 drop prev_id, drop cur_id, drop cur_clinical_datetime, drop cur_clinic_rtc_date; - - drop table if exists flat_hiv_summary_2; - create temporary table flat_hiv_summary_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_date_bncd - else null - end as outreach_date_bncd, - - case - when encounter_type=21 and @outreach_date_bncd is null then @outreach_date_bncd := encounter_datetime - when is_clinical_encounter then @outreach_date_bncd := null - when @prev_id != @cur_id then @outreach_date_bncd := null - else @outreach_date_bncd - end as next_outreach_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @outreach_death_date_bncd - else null - end as outreach_death_date_bncd, - - case - when encounter_type=21 and @outreach_death_date_bncd is null then @outreach_death_date_bncd := death_date - when is_clinical_encounter then @outreach_death_date_bncd := null - when @prev_id != @cur_id then @outreach_death_date_bncd := null - else @outreach_death_date_bncd - end as next_outreach_death_date_bncd, - - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then cast(@outreach_patient_care_status_bncd as unsigned) - else null - end as outreach_patient_care_status_bncd, - - case - when encounter_type=21 and @outreach_patient_care_status_bncd is null then @outreach_patient_care_status_bncd := patient_care_status - when is_clinical_encounter then @outreach_patient_care_status_bncd := null - when @prev_id != @cur_id then @outreach_patient_care_status_bncd := null - else @outreach_patient_care_status_bncd - end as next_outreach_patient_care_status_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_date_bncd - else null - end as transfer_date_bncd, - - case - when encounter_type=116 and @transfer_date_bncd is null then @transfer_date_bncd := encounter_datetime - when is_clinical_encounter then @transfer_date_bncd := null - when @prev_id != @cur_id then @transfer_date_bncd := null - else @transfer_date_bncd - end as next_transfer_date_bncd, - - case - when @prev_id != @cur_id then null - when is_clinical_encounter then @transfer_transfer_out_bncd - else null - end as transfer_transfer_out_bncd, - - case - when encounter_type=116 and @transfer_transfer_out_bncd is null then @transfer_transfer_out_bncd := encounter_datetime - when is_clinical_encounter then @transfer_transfer_out_bncd := null - when @prev_id != @cur_id then @transfer_transfer_out_bncd := null - else @transfer_transfer_out_bncd - end as next_transfer_transfer_out_bncd - - from flat_hiv_summary_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_hiv_summary_3; - create temporary table flat_hiv_summary_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_hiv, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - -/* Moved to flat_hiv_summary_1 - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, -*/ - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id, - - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id - -/* MOVED to flat_hiv_summary_1 - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date -*/ - from flat_hiv_summary_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - alter table flat_hiv_summary_3 drop prev_id, drop cur_id; - - set @prev_id = null; - set @cur_id = null; - set @transfer_in = null; - set @transfer_in_date = null; - set @transfer_in_location_id = null; - set @transfer_out = null; - set @transfer_out_date = null; - set @transfer_out_location_id = null; - - drop temporary table if exists flat_hiv_summary_4; - - create temporary table flat_hiv_summary_4 ( index person_enc (person_id, encounter_datetime)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - - - case - when obs regexp "!!7015=" then @transfer_in := 1 - when prev_clinical_location_id != location_id 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 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 then @transfer_in_location_id := prev_clinical_location_id - when @cur_id = @prev_id then @transfer_in_location_id - else @transfer_in_location_id := null - end transfer_in_location_id, - - - - - - - - - - - - case - when obs regexp "!!1285=!!" then @transfer_out := 1 - when obs regexp "!!1596=1594!!" then @transfer_out := 1 - when obs regexp "!!9082=(1287|1594|9068|9504|1285)!!" then @transfer_out := 1 - when next_clinical_location_id != location_id then @transfer_out := 1 - else @transfer_out := null - end as transfer_out, - - case - when obs regexp "!!1285=(1287|9068|2050)!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9999 - when obs regexp "!!1285=1286!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9998 - when next_clinical_location_id != location_id then @transfer_out_location_id := next_clinical_location_id - else @transfer_out_location_id := null - end transfer_out_location_id, - - - case - when @transfer_out and next_clinical_datetime_hiv is null then @transfer_out_date := date(cur_rtc_date) - when next_clinical_location_id != location_id then @transfer_out_date := date(next_clinical_datetime_hiv) - when transfer_transfer_out_bncd then @transfer_out_date := date(transfer_transfer_out_bncd) - else @transfer_out_date := null - end transfer_out_date - - - - from flat_hiv_summary_3 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - select count(*) into @new_encounter_rows from flat_hiv_summary_4; - - select @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; - select @total_rows_written; - - - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - t1.uuid, - visit_id, - encounter_id, - encounter_datetime, - encounter_type, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - visit_num, - enrollment_date, - enrollment_location_id, - hiv_start_date, - death_date, - scheduled_visit, - transfer_in, - transfer_in_location_id, - transfer_in_date, - transfer_out, - transfer_out_location_id, - transfer_out_date, - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date as rtc_date, - arv_first_regimen, - arv_first_regimen_location_id, - arv_first_regimen_start_date, - arv_first_regimen_start_date_flex, - prev_arv_meds, - cur_arv_meds, - cur_arv_meds_strict, - arv_start_date, - arv_start_location_id, - prev_arv_start_date, - prev_arv_end_date, - prev_arv_line, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - is_pregnant, - edd, - tb_screen, - tb_screening_result, - tb_screening_datetime, - on_ipt, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - on_tb_tx, - tb_tx_start_date, - tb_tx_end_date, - pcp_prophylaxis_start_date, - condoms_provided_date, - modern_contraceptive_method_start_date, - contraceptive_method, - cur_who_stage, - discordant_status, - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - vl_order_date, - cd4_order_date, - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_location_id, - next_clinical_location_id, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv, - outreach_date_bncd, - outreach_death_date_bncd, - outreach_patient_care_status_bncd, - transfer_date_bncd, - transfer_transfer_out_bncd - - from flat_hiv_summary_4 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_hiv_summary_build_queue__0 t2 using (person_id);'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - - select @person_ids_count as 'persons remaining', @cycle_length as 'Cycle time (s)', ceil(@person_ids_count / cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_v15_8.sql b/etl-scripts/stored-procedures/generate_hiv_summary_v15_8.sql deleted file mode 100644 index e46212e..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_v15_8.sql +++ /dev/null @@ -1,1964 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_v15_8`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN - set @primary_table := "flat_hiv_summary_v15b"; - set @query_type = query_type; - set @queue_table = ""; - set @total_rows_written = 0; - - set @start = now(); - set @table_version = "flat_hiv_summary_v2.17"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - - create table if not exists flat_hiv_summary_v15b ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - is_clinical_encounter int, - location_id int, - location_uuid varchar(100), - visit_num int, - enrollment_date datetime, - enrollment_location_id int, - hiv_start_date datetime, - death_date datetime, - scheduled_visit int, - transfer_in tinyint, - transfer_in_location_id int, - transfer_in_date datetime, - transfer_out tinyint, - transfer_out_location_id int, - transfer_out_date datetime, - patient_care_status int, - out_of_care int, - prev_rtc_date datetime, - rtc_date datetime, - arv_first_regimen varchar(500), - arv_first_regimen_location_id int, - arv_first_regimen_start_date datetime, - arv_first_regimen_start_date_flex datetime, - prev_arv_meds varchar(500), - cur_arv_meds varchar(500), - cur_arv_meds_strict varchar(500), - arv_start_date datetime, - arv_start_location_id int, - prev_arv_start_date datetime, - prev_arv_end_date datetime, - prev_arv_line int, - cur_arv_line int, - cur_arv_line_strict int, - cur_arv_line_reported tinyint, - prev_arv_adherence varchar(200), - cur_arv_adherence varchar(200), - hiv_status_disclosed int, - is_pregnant boolean, - edd datetime, - tb_screen boolean, - tb_screening_result int, - tb_screening_datetime datetime, - on_ipt boolean, - ipt_start_date datetime, - ipt_stop_date datetime, - ipt_completion_date datetime, - on_tb_tx boolean, - tb_tx_start_date datetime, - tb_tx_end_date datetime, - pcp_prophylaxis_start_date datetime, - condoms_provided_date datetime, - modern_contraceptive_method_start_date datetime, - contraceptive_method int, - cur_who_stage int, - discordant_status int, - cd4_resulted double, - cd4_resulted_date datetime, - cd4_1 double, - cd4_1_date datetime, - cd4_2 double, - cd4_2_date datetime, - cd4_percent_1 double, - cd4_percent_1_date datetime, - cd4_percent_2 double, - cd4_percent_2_date datetime, - vl_resulted int, - vl_resulted_date datetime, - vl_1 int, - vl_1_date datetime, - vl_2 int, - vl_2_date datetime, - vl_order_date datetime, - cd4_order_date datetime, - hiv_dna_pcr_order_date datetime, - hiv_dna_pcr_resulted int, - hiv_dna_pcr_resulted_date datetime, - hiv_dna_pcr_1 int, - hiv_dna_pcr_1_date datetime, - hiv_dna_pcr_2 int, - hiv_dna_pcr_2_date datetime, - hiv_rapid_test_resulted int, - hiv_rapid_test_resulted_date datetime, - prev_encounter_datetime_hiv datetime, - next_encounter_datetime_hiv datetime, - prev_encounter_type_hiv mediumint, - next_encounter_type_hiv mediumint, - prev_clinical_datetime_hiv datetime, - next_clinical_datetime_hiv datetime, - prev_clinical_location_id mediumint, - next_clinical_location_id mediumint, - prev_clinical_rtc_date_hiv datetime, - next_clinical_rtc_date_hiv datetime, - outreach_date_bncd datetime, - outreach_death_date_bncd datetime, - outreach_patient_care_status_bncd int, - transfer_date_bncd datetime, - transfer_transfer_out_bncd datetime, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_hiv), - index encounter_type (encounter_type), - index date_created (date_created) - - ); - - - - 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 @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_hiv_summary_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from flat_hiv_summary_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_hiv_summary_v15b"; - set @queue_table = "flat_hiv_summary_sync_queue"; - create table if not exists flat_hiv_summary_sync_queue (person_id int primary key); - - - - set @last_update = null; - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; - - replace into flat_hiv_summary_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into flat_hiv_summary_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - - end if; - - - - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - select @person_ids_count as 'num patients to sync'; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - - drop temporary table if exists flat_hiv_summary_build_queue__0; - - - - SET @dyn_sql=CONCAT('create temporary table flat_hiv_summary_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - drop temporary table if exists flat_hiv_summary_0a; - create temporary table flat_hiv_summary_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - - 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,129,138,140,153,154,158,162,163) then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,115,117,120,127,128,138, 140, 153,154,158,162,163,186) then 10 - when t1.encounter_type in (129) then 5 - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_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,186) - AND NOT obs regexp "!!5303=(822|664|1067)!!" - AND NOT obs regexp "!!9082=9036!!" - ); - - insert into flat_hiv_summary_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_hiv_summary_build_queue__0 t0 using (person_id) - ); - - drop temporary table if exists flat_hiv_summary_0; - create temporary table flat_hiv_summary_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_date = null; - set @cur_encounter_date = null; - set @enrollment_date = null; - set @hiv_start_date = null; - set @cur_location = null; - set @cur_rtc_date = null; - set @prev_rtc_date = null; - set @hiv_start_date = null; - set @prev_arv_start_date = null; - set @arv_start_date = null; - set @prev_arv_end_date = null; - set @arv_start_location_id = null; - set @arv_first_regimen_start_date = null; - set @arv_first_regimen_start_date_flex = null; - set @arv_first_regimen = null; - set @prev_arv_line = null; - set @cur_arv_line = null; - set @prev_arv_adherence = null; - set @cur_arv_adherence = null; - set @hiv_status_disclosed = null; - set @is_pregnant = null; - set @edd = null; - set @prev_arv_meds = null; - set @cur_arv_meds = null; - set @ipt_start_date = null; - set @ipt_end_date = null; - set @ipt_completion_date = null; - - set @on_tb_tx = null; - set @tb_tx_start_date = null; - set @tb_tx_end_date = null; - set @pcp_prophylaxis_start_date = null; - set @tb_screen = null; - set @tb_screening_result = null; - set @tb_screening_datetime = null; - - set @death_date = null; - - set @patient_care_status=null; - - set @condoms_provided_date = null; - set @modern_contraceptive_method_start_date = null; - set @contraceptive_method = null; - - - set @cur_who_stage = null; - - set @vl_1=null; - set @vl_2=null; - set @vl_1_date=null; - set @vl_2_date=null; - set @vl_resulted=null; - set @vl_resulted_date=null; - - set @cd4_resulted=null; - set @cd4_resulted_date=null; - set @cd4_1=null; - set @cd4_1_date=null; - set @cd4_2=null; - set @cd4_2_date=null; - set @cd4_percent_1=null; - set @cd4_percent_1_date=null; - set @cd4_percent_2=null; - set @cd4_percent_2_date=null; - set @vl_order_date = null; - set @cd4_order_date = null; - - set @hiv_dna_pcr_order_date = null; - set @hiv_dna_pcr_1=null; - set @hiv_dna_pcr_2=null; - set @hiv_dna_pcr_1_date=null; - set @hiv_dna_pcr_2_date=null; - - set @hiv_rapid_test_resulted=null; - set @hiv_rapid_test_resulted_date= null; - - - - - - - drop temporary table if exists flat_hiv_summary_1; - create temporary table flat_hiv_summary_1 (index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.encounter_id, - @prev_encounter_date := date(@cur_encounter_date) as prev_encounter_date, - @cur_encounter_date := date(encounter_datetime) as cur_encounter_date, - t1.encounter_datetime, - t1.encounter_type, - t1.is_clinical_encounter, - CASE - WHEN - (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND !(@prev_id <=> @cur_id))) - AND obs REGEXP '!!7013=' - THEN - @enrollment_date:=REPLACE(REPLACE((SUBSTRING_INDEX(SUBSTRING(obs, LOCATE('!!7013=', obs)), - @sep, - 1)), - '!!7013=', - ''), - '!!', - '') - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND !(@prev_id <=> @cur_id))) - THEN - @enrollment_date:='1900-01-01' - WHEN - t1.encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND !(@prev_id <=> @cur_id))) - THEN - @enrollment_date:=DATE(encounter_datetime) - WHEN @prev_id <=> @cur_id THEN @enrollment_date - ELSE @enrollment_date:=NULL - END AS enrollment_date, - - - - CASE - WHEN - (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND !(@prev_id <=> @cur_id))) - AND obs REGEXP '!!7030=5622' - THEN - @enrollment_location_id:=9999 - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND !(@prev_id <=> @cur_id))) - THEN - @enrollmen_location_id:=9999 - WHEN - encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND !(@prev_id <=> @cur_id))) - THEN - @enrollment_location_id:= location_id - WHEN @prev_id <=> @cur_id THEN @enrollment_location_id - ELSE @enrollment_location_id:=NULL - END AS enrollment_location_id, - - - - - - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when location_id then @cur_location := location_id - when @prev_id <=> @cur_id then @cur_location - else null - end as 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 - end as visit_num, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id <=> @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - - - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504|1285)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - - - - - - - - - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when !(@prev_id <=> @cur_id) or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := encounter_datetime - else @death_date := null - end - else @death_date - end as death_date, - - - - - - case - when @death_date <= encounter_datetime then @patient_care_status := 159 - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - - case - when obs regexp "!!9203=" then @hiv_start_date := replace(replace((substring_index(substring(obs,locate("!!9203=",obs)),@sep,1)),"!!9203=",""),"!!","") - when obs regexp "!!7015=" then @hiv_start_date := "1900-01-01" - when @hiv_start_date is null then @hiv_start_date := date(encounter_datetime) - when @prev_id <=> @cur_id then @hiv_start_date - else @hiv_start_date := null - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_location_id := location_id - when @prev_id <=> @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location_id := location_id - else @arv_start_location_id := null - end as arv_start_location_id, - - - case - when @prev_id=@cur_id and @cur_arv_meds is not null then @prev_arv_meds := @cur_arv_meds - when @prev_id=@cur_id then @prev_arv_meds - else @prev_arv_meds := null - end as prev_arv_meds, - - - - - #2154 : PATIENT REPORTED CURRENT ANTIRETROVIRAL TREATMENT - #2157 : PATIENT REPORTED PAST ANTIRETROVIRAL TREATMENT - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - when @prev_id <=> @cur_id then @cur_arv_meds - else @cur_arv_meds:= null - end as cur_arv_meds, - - - case - when 1 then null - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - else null - end as cur_arv_meds_strict, - - - case - when @prev_id <=> @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id <=> @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, - - - case - when @prev_id <=> @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := @cur_rtc_date - when @prev_id <=> @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date, - - - CASE - WHEN - (@arv_first_regimen_start_date IS NULL || !(@prev_id <=> @cur_id)) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_start_date:=GetValues(obs,1499) - - WHEN - (@arv_first_regimen_start_date IS NULL || !(@prev_id <=> @cur_id)) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_start_date:='1900-01-01' - - WHEN - (@arv_first_regimen_start_date IS NULL || !(@prev_id <=> @cur_id)) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_start_date:=DATE(encounter_datetime) - - - WHEN - (@arv_first_regimen_start_date IS NULL || !(@prev_id <=> @cur_id)) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_start_date:= "1900-01-01" #DATE(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date IS NULL || !(@prev_id <=> @cur_id)) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_start_date:= "1900-01-01" # DATE(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date IS NULL || !(@prev_id <=> @cur_id)) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_start_date:='1900-01-01' - WHEN @prev_id <=> @cur_id THEN @arv_first_regimen_start_date - ELSE @arv_first_regimen_start_date:=NULL - END AS arv_first_regimen_start_date, - - - - - /* - CASE - WHEN - (@arv_first_regimen_start_date_flex IS NULL || !(@prev_id <=> @cur_id)) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_start_date_flex := GetValues(obs,1499) - WHEN - (@arv_first_regimen_start_date_flex IS NULL || !(@prev_id <=> @cur_id)) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_start_date_flex :='1900-01-01' - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || !(@prev_id <=> @cur_id)) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_start_date_flex:=DATE(encounter_datetime) - - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || !(@prev_id <=> @cur_id)) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen_start_date_flex :=date(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || !(@prev_id <=> @cur_id)) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen_start_date_flex :=date(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || !(@prev_id <=> @cur_id)) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_start_date_flex:='1900-01-01' - WHEN @prev_id <=> @cur_id THEN @arv_first_regimen_start_date_flex - WHEN !(@prev_id <=> @cur_id) THEN @arv_first_regimen_start_date_flex:=NULL - ELSE @arv_first_regimen_start_date_flex - END - */ - null AS arv_first_regimen_start_date_flex, - - - -/* - case - when @arv_first_regimen is null and obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @arv_first_regimen := normalize_arvs(obs,'2157') - when obs regexp "!!7015=" and @arv_first_regimen is null then @arv_first_regimen := "unknown" - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id <=> @cur_id then @arv_first_regimen - when !(@prev_id <=> @cur_id) then @arv_first_regimen := @cur_arv_meds - else "-1" - end as arv_first_regimen, -*/ - - CASE - WHEN - (@arv_first_regimen IS NULL || !(@prev_id <=> @cur_id)) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen:= "unknown" - - WHEN - (@arv_first_regimen IS NULL || !(@prev_id <=> @cur_id)) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen:= "unknown" - - WHEN - (@arv_first_regimen IS NULL || !(@prev_id <=> @cur_id)) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen := @cur_arv_meds - - - WHEN - (@arv_first_regimen IS NULL || !(@prev_id <=> @cur_id)) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen:= "unknown" #@cur_arv_meds - - WHEN - (@arv_first_regimen IS NULL || !(@prev_id <=> @cur_id)) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen:= "unknown" #@cur_arv_meds - - WHEN - (@arv_first_regimen IS NULL || !(@prev_id <=> @cur_id)) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen := "unknown" - - WHEN @prev_id <=> @cur_id THEN @arv_first_regimen - ELSE @arv_first_regimen:=NULL - END AS arv_first_regimen, - - -/* - case - when @arv_first_regimen is null and obs regexp "!!1499=" then @arv_first_regimen_location_id := 9999 - when !(@prev_id <=> @cur_id) and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @arv_first_regimen_location_id is null and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @prev_id <=> @cur_id then @arv_first_regimen_location_id - when !(@prev_id <=> @cur_id) then @arv_first_regimen_location_id := null - else "-1" - end as arv_first_regimen_location_id, -*/ - - CASE - WHEN - (@arv_first_regimen_location_id IS NULL || !(@prev_id <=> @cur_id)) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_location_id := 9999 - - WHEN - (@arv_first_regimen_location_id IS NULL || !(@prev_id <=> @cur_id)) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_location_id:=9999 - - WHEN - (@arv_first_regimen_location_id IS NULL || !(@prev_id <=> @cur_id)) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_location_id:=location_id - - - WHEN - (@arv_first_regimen_location_id IS NULL || !(@prev_id <=> @cur_id)) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_location_id:= 9999 #location_id - - WHEN - (@arv_first_regimen_location_id IS NULL || !(@prev_id <=> @cur_id)) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - THEN - @arv_first_regimen_location_id:= 9999 #location_id - - WHEN - (@arv_first_regimen_location_id IS NULL || !(@prev_id <=> @cur_id)) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_location_id:=9999 - WHEN @prev_id <=> @cur_id THEN @arv_first_regimen_location_id - ELSE @arv_first_regimen_location_id:=NULL - END AS arv_first_regimen_location_id, - - - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := null - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=(6156)!!" then @cur_arv_line := 3 - when @prev_id <=> @cur_id then @cur_arv_line - else @cur_arv_line := null - end as cur_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1250=(6156)!!" then 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1088=(6156)!!" then 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then 2 - when obs regexp "!!2154=(6156)!!" then 3 - else null - end as cur_arv_line_strict, - - - - - - - - - - case - when obs regexp "!!6976=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6976=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6976=6695!!" then @cur_arv_line_reported := 3 - - when obs regexp "!!6744=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6744=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6744=6695!!" then @cur_arv_line_reported := 3 - when @prev_id <=> @cur_id then @cur_arv_line_reported - else @cur_arv_line_reported := null - end as cur_arv_line_reported, - - - - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := null - end as prev_arv_start_date, - - - - - - - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - - when @cur_arv_meds != @prev_arv_meds then @arv_start_date := date(t1.encounter_datetime) - - when !(@prev_id <=> @cur_id) then @arv_start_date := null - else @arv_start_date - end as arv_start_date, - - - case - when @prev_arv_start_date != @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end as prev_arv_end_date, - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := null - end as prev_arv_adherence, - - - - - - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id <=> @cur_id then @cur_arv_adherence - else @cur_arv_adherence := null - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when @prev_id <=> @cur_id then @hiv_status_disclosed - else @hiv_status_disclosed := null - end as hiv_status_disclosed, - - - - - - - - - - - - - - - - - - case - when obs regexp "!!8351=(48|50|1066|1624|6971|9608)!!" then @is_pregnant := null - when !(@prev_id <=> @cur_id) then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" or obs regexp "!!8351=(1065|1484)!!" then @is_pregnant := true - else @is_pregnant := null - end - when @is_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") or obs regexp "!!8351=(1065|1484)!!"then @is_pregnant := true - when @is_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@is_pregnant,encounter_datetime) > 40 or timestampdiff(week,@edd,encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @is_pregnant := null - else @is_pregnant - end as is_pregnant, - - - - - - - - - case - when !(@prev_id <=> @cur_id) then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd := null - end - when @edd is null then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - - - case - when obs regexp "!!6174=" then @tb_screen := true - when obs regexp "!!2022=1065!!" then @tb_screen := true - when obs regexp "!!307=" then @tb_screen := true - when obs regexp "!!12=" then @tb_screen := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screen := true - when orders regexp "(12|307|8064|2311|2323)" then @tb_screen := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screen := true - when obs regexp "!!5958=1077!!" then @tb_screen := true - when obs regexp "!!2020=1065!!" then @tb_screen := true - when obs regexp "!!2021=1065!!" then @tb_screen := true - when obs regexp "!!2028=" then @tb_screen := true - when obs regexp "!!1268=(1256|1850)!!" then @tb_screen := true - when obs regexp "!!5959=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!5971=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!1492=107!!" then @tb_screen := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screen := true - end as tb_screen, - - case - when obs regexp "!!8292=" then @tb_screening_result := - replace(replace((substring_index(substring(obs,locate("!!8292=",obs)),@sep,1)),"!!8292=",""),"!!","") - when !(@prev_id <=> @cur_id) then @tb_screening_result := null - else @tb_screening_result - end as tb_screening_result, - - case - when obs regexp "!!6174=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2022=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!307=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!12=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime - when orders regexp "(12|307|8064|2311|2323)" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5958=1077!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2020=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2021=1065!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!2028=" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1268=(1256|1850)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5959=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!5971=(1073|1074)!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1492=107!!" then @tb_screening_datetime := encounter_datetime - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screening_datetime := encounter_datetime - when @cur_id = @prev_id then @tb_screening_datetime - else @tb_screening_datetime := null - end as tb_screening_datetime, - - - case - when obs regexp "!!1265=(1107|1260)!!" then @on_ipt := 0 - when obs regexp "!!1265=!!" then @on_ipt := 1 - when obs regexp "!!1110=656!!" then @on_ipt := 1 - when @cur_id != @prev_id then @on_ipt := null - else @on_ipt - end as on_ipt, - - - - case - when obs regexp "!!1265=(1256|1850)!!" then @ipt_start_date := encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" and @ipt_start_date is null then @ipt_start_date := encounter_datetime - when @cur_id != @prev_id then @ipt_start_date := null - else @ipt_start_date - end as ipt_start_date, - - - - case - when obs regexp "!!1266=" then @ipt_stop_date := encounter_datetime - when @cur_id = @prev_id then @ipt_stop_date - when @cur_id != @prev_id then @ipt_stop_date := null - else @ipt_stop_date - end as ipt_stop_date, - - case - when obs regexp "!!1266=1267!!" then @ipt_completion_date := encounter_datetime - when @cur_id = @prev_id then @ipt_completion_date - when @cur_id != @prev_id then @ipt_completion_date := null - else @ipt_completion_date - end as ipt_completion_date, - - - case - when obs regexp "!!1268=(1107|1260)!!" then @on_tb_tx := 0 - when obs regexp "!!1268=" then @on_tb_tx := 1 - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @on_tb_tx := 1 - else @on_tb_tx := 0 - end as on_tb_tx, - - - - case - when obs regexp "!!1113=" then @tb_tx_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @tb_tx_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" and obs regexp "!!7015=" and @tb_tx_start_date is null then @tb_tx_start_date := null - when obs regexp "!!1268=(1257|1259|1849|981)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" and @tb_tx_start_date is null then @tb_tx_start_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_start_date - else @tb_tx_start_date := null - end as tb_tx_start_date, - - - - - case - when obs regexp "!!2041=" then @tb_tx_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_tx_end_date := encounter_datetime - when @cur_id = @prev_id then @tb_tx_end_date - else @tb_tx_end_date := null - end as tb_tx_end_date, - - - - - - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1261=1257!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1193=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := encounter_datetime - when @prev_id=@cur_id then @pcp_prophylaxis_start_date - else @pcp_prophylaxis_start_date := null - end as pcp_prophylaxis_start_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - else @cd4_2:=null - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - else @cd4_2_date:=null - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(encounter_datetime) - when @prev_id <=> @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id <=> @cur_id and date(encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=null - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=null - end as cd4_1_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - else @cd4_percent_2:=null - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - else @cd4_percent_2_date:=null - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=null - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=null - end as cd4_percent_1_date, - - - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <> date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - else @vl_2:=null - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - else @vl_2_date:=null - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(encounter_datetime) - when @prev_id <=> @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id <=> @cur_id and date(encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=null - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date:=null - end as vl_1_date, - - - - - - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(encounter_datetime) - when orders regexp "856" then @vl_order_date := date(encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - else @vl_order_date := null - end as vl_order_date, - - - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := null - end as cd4_order_date, - - - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := null - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - else @hiv_dna_pcr_2:=null - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - else @hiv_dna_pcr_2_date:=null - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=null - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=null - end as hiv_dna_pcr_1_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - case - when obs regexp "!!8302=8305!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided_date := encounter_datetime - when obs regexp "!!6579=" then @condoms_provided_date := encounter_datetime - when @prev_id <=> @cur_id then @condoms_provided_date - else @condoms_provided_date := null - end as condoms_provided_date, - - - - - - - case - when obs regexp "!!7240=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(encounter_datetime) - when obs regexp "!!7240=!!" - then @modern_contraceptive_method_start_date := null - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" and obs regexp "!!1190=" - then @modern_contraceptive_method_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1190=",obs)),@sep,1)),"!!1190=",""),"!!","")) - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(encounter_datetime) - when obs regexp "!!374=!!" - then @modern_contraceptive_method_start_date := null - when @prev_id <=> @cur_id then @modern_contraceptive_method_start_date - else @modern_contraceptive_method_start_date := null - end as modern_contraceptive_method_start_date, - - - case - when obs regexp "!!7240=1107!!" then @contraceptive_method := null - when obs regexp "!!7240=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!7240=",obs)),@sep,1)),"!!7240=",""),"!!","") - when obs regexp "!!374=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!374=",obs)),@sep,1)),"!!374=",""),"!!","") - when @prev_id <=> @cur_id then @contraceptive_method - else @contraceptive_method := null - end as contraceptive_method, - - - - - - - case - when obs regexp "!!5356=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!8287=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!8287=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!8287=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!8287=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!8307=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!8307=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!8307=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!8307=(1207|1223)!!" then @cur_who_stage := 4 - when @prev_id <=> @cur_id then @cur_who_stage - else @cur_who_stage := null - end as cur_who_stage, - - - case - when obs regexp "!!6096=1065" then @discordant_status := "Yes" - when obs regexp "!!6096=1066" then @discordant_status := "No" - when obs regexp "!!6096=1067" then @discordant_status := "Unknown" - when obs regexp "!!6096=1175" then @discordant_status := "N/A" - when obs regexp "!!6096=6826" then @discordant_status := "Concordant Couple Positive" - when obs regexp "!!6096=6827" then @discordant_status := "Concordant Couple Negative" - when @prev_id <=> @cur_id then @discordant_status - else @discordant_status := null - end as discordant_status - - - - - from flat_hiv_summary_0 t1 - join amrs.person p using (person_id) - ); - - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_hiv_summary_1 drop prev_id, drop cur_id, drop cur_clinical_datetime, drop cur_clinic_rtc_date; - - drop table if exists flat_hiv_summary_2; - create temporary table flat_hiv_summary_2 - (select *, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id <=> @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id <=> @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when @prev_id <=> @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id <=> @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id <=> @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id <=> @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id <=> @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when !(@prev_id <=> @cur_id) then null - when is_clinical_encounter then @outreach_date_bncd - else null - end as outreach_date_bncd, - - case - when encounter_type=21 and @outreach_date_bncd is null then @outreach_date_bncd := encounter_datetime - when is_clinical_encounter then @outreach_date_bncd := null - when !(@prev_id <=> @cur_id) then @outreach_date_bncd := null - else @outreach_date_bncd - end as next_outreach_date_bncd, - - case - when !(@prev_id <=> @cur_id) then null - when is_clinical_encounter then @outreach_death_date_bncd - else null - end as outreach_death_date_bncd, - - case - when encounter_type=21 and @outreach_death_date_bncd is null then @outreach_death_date_bncd := death_date - when is_clinical_encounter then @outreach_death_date_bncd := null - when !(@prev_id <=> @cur_id) then @outreach_death_date_bncd := null - else @outreach_death_date_bncd - end as next_outreach_death_date_bncd, - - - case - when !(@prev_id <=> @cur_id) then null - when is_clinical_encounter then cast(@outreach_patient_care_status_bncd as unsigned) - else null - end as outreach_patient_care_status_bncd, - - case - when encounter_type=21 and @outreach_patient_care_status_bncd is null then @outreach_patient_care_status_bncd := patient_care_status - when is_clinical_encounter then @outreach_patient_care_status_bncd := null - when !(@prev_id <=> @cur_id) then @outreach_patient_care_status_bncd := null - else @outreach_patient_care_status_bncd - end as next_outreach_patient_care_status_bncd, - - case - when !(@prev_id <=> @cur_id) then null - when is_clinical_encounter then @transfer_date_bncd - else null - end as transfer_date_bncd, - - case - when encounter_type=116 and @transfer_date_bncd is null then @transfer_date_bncd := encounter_datetime - when is_clinical_encounter then @transfer_date_bncd := null - when !(@prev_id <=> @cur_id) then @transfer_date_bncd := null - else @transfer_date_bncd - end as next_transfer_date_bncd, - - case - when !(@prev_id <=> @cur_id) then null - when is_clinical_encounter then @transfer_transfer_out_bncd - else null - end as transfer_transfer_out_bncd, - - case - when encounter_type=116 and @transfer_transfer_out_bncd is null then @transfer_transfer_out_bncd := encounter_datetime - when is_clinical_encounter then @transfer_transfer_out_bncd := null - when !(@prev_id <=> @cur_id) then @transfer_transfer_out_bncd := null - else @transfer_transfer_out_bncd - end as next_transfer_transfer_out_bncd - - from flat_hiv_summary_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_hiv_summary_3; - create temporary table flat_hiv_summary_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:=null - end as prev_encounter_type_hiv, - @cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as prev_encounter_datetime_hiv, - - @cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - -/* Moved to flat_hiv_summary_1 - case - when @prev_id <=> @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id <=> @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, -*/ - - case - when @prev_id <=> @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as prev_clinical_location_id, - - - case - when is_clinical_encounter then @cur_clinical_location_id := location_id - when @prev_id <=> @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinical_location_id - -/* MOVED to flat_hiv_summary_1 - case - when @prev_id <=> @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id <=> @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date -*/ - from flat_hiv_summary_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - alter table flat_hiv_summary_3 drop prev_id, drop cur_id; - - set @prev_id = null; - set @cur_id = null; - set @transfer_in = null; - set @transfer_in_date = null; - set @transfer_in_location_id = null; - set @transfer_out = null; - set @transfer_out_date = null; - set @transfer_out_location_id = null; - - drop temporary table if exists flat_hiv_summary_4; - - create temporary table flat_hiv_summary_4 ( index person_enc (person_id, encounter_datetime)) - (select - *, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - - - case - when obs regexp "!!7015=" then @transfer_in := 1 - when prev_clinical_location_id != location_id 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 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 then @transfer_in_location_id := prev_clinical_location_id - when @cur_id = @prev_id then @transfer_in_location_id - else @transfer_in_location_id := null - end transfer_in_location_id, - - - - - - - - - - - - case - when obs regexp "!!1285=!!" then @transfer_out := 1 - when obs regexp "!!1596=1594!!" then @transfer_out := 1 - when obs regexp "!!9082=(1287|1594|9068|9504|1285)!!" then @transfer_out := 1 - when next_clinical_location_id != location_id then @transfer_out := 1 - else @transfer_out := null - end as transfer_out, - - case - when obs regexp "!!1285=(1287|9068|2050)!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9999 - when obs regexp "!!1285=1286!!" and next_clinical_datetime_hiv is null then @transfer_out_location_id := 9998 - when next_clinical_location_id != location_id then @transfer_out_location_id := next_clinical_location_id - else @transfer_out_location_id := null - end transfer_out_location_id, - - - case - when @transfer_out and next_clinical_datetime_hiv is null then @transfer_out_date := date(cur_rtc_date) - when next_clinical_location_id != location_id then @transfer_out_date := date(next_clinical_datetime_hiv) - when transfer_transfer_out_bncd then @transfer_out_date := date(transfer_transfer_out_bncd) - else @transfer_out_date := null - end transfer_out_date - - - - from flat_hiv_summary_3 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - select count(*) into @new_encounter_rows from flat_hiv_summary_4; - - select @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; - select @total_rows_written; - - - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - t1.uuid, - visit_id, - encounter_id, - encounter_datetime, - encounter_type, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - visit_num, - enrollment_date, - enrollment_location_id, - hiv_start_date, - death_date, - scheduled_visit, - transfer_in, - transfer_in_location_id, - transfer_in_date, - transfer_out, - transfer_out_location_id, - transfer_out_date, - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date as rtc_date, - arv_first_regimen, - arv_first_regimen_location_id, - arv_first_regimen_start_date, - arv_first_regimen_start_date_flex, - prev_arv_meds, - cur_arv_meds, - cur_arv_meds_strict, - arv_start_date, - arv_start_location_id, - prev_arv_start_date, - prev_arv_end_date, - prev_arv_line, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - is_pregnant, - edd, - tb_screen, - tb_screening_result, - tb_screening_datetime, - on_ipt, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - on_tb_tx, - tb_tx_start_date, - tb_tx_end_date, - pcp_prophylaxis_start_date, - condoms_provided_date, - modern_contraceptive_method_start_date, - contraceptive_method, - cur_who_stage, - discordant_status, - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - vl_order_date, - cd4_order_date, - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_location_id, - next_clinical_location_id, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv, - outreach_date_bncd, - outreach_death_date_bncd, - outreach_patient_care_status_bncd, - transfer_date_bncd, - transfer_transfer_out_bncd - - from flat_hiv_summary_4 t1 - join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_hiv_summary_build_queue__0 t2 using (person_id);'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - - select @person_ids_count as 'persons remaining', @cycle_length as 'Cycle time (s)', ceil(@person_ids_count / cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_v15_9.sql b/etl-scripts/stored-procedures/generate_hiv_summary_v15_9.sql deleted file mode 100644 index 594ce15..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_v15_9.sql +++ /dev/null @@ -1,2172 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_v15_9`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) -BEGIN -/**/ -set @primary_table := "foo_flat_hiv_summary"; -#set @primary_table := "flat_hiv_summary_v15b"; - - set @query_type = query_type; - set @queue_table = ""; - set @total_rows_written = 0; - - set @start = now(); - set @table_version = "flat_hiv_summary_v2.17"; -set @table_version = "foo_flat_hiv_summary"; - - set session sort_buffer_size=512000000; - - set @sep = " ## "; - set @lab_encounter_type = 99999; - set @death_encounter_type = 31; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - - - create table if not exists flat_hiv_summary_v15b ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - is_clinical_encounter int, - location_id int, - location_uuid varchar(100), - visit_num int, - enrollment_date datetime, - enrollment_location_id int, - hiv_start_date datetime, - death_date datetime, - scheduled_visit int, - transfer_in tinyint, - transfer_in_location_id int, - transfer_in_date datetime, - transfer_out tinyint, - transfer_out_location_id int, - transfer_out_date datetime, - patient_care_status int, - out_of_care int, - prev_rtc_date datetime, - rtc_date datetime, - arv_first_regimen varchar(500), - arv_first_regimen_location_id int, - arv_first_regimen_start_date datetime, - arv_first_regimen_start_date_flex datetime, - prev_arv_meds varchar(500), - cur_arv_meds varchar(500), - cur_arv_meds_strict varchar(500), - arv_start_date datetime, - arv_start_location_id int, - prev_arv_start_date datetime, - prev_arv_end_date datetime, - prev_arv_line int, - cur_arv_line int, - cur_arv_line_strict int, - cur_arv_line_reported tinyint, - prev_arv_adherence varchar(200), - cur_arv_adherence varchar(200), - hiv_status_disclosed int, - is_pregnant boolean, - edd datetime, - tb_screen boolean, - tb_screening_result int, - tb_screening_datetime datetime, - on_ipt boolean, - ipt_start_date datetime, - ipt_stop_date datetime, - ipt_completion_date datetime, - on_tb_tx boolean, - tb_tx_start_date datetime, - tb_tx_end_date datetime, - pcp_prophylaxis_start_date datetime, - condoms_provided_date datetime, - modern_contraceptive_method_start_date datetime, - contraceptive_method int, - cur_who_stage int, - discordant_status int, - cd4_resulted double, - cd4_resulted_date datetime, - cd4_1 double, - cd4_1_date datetime, - cd4_2 double, - cd4_2_date datetime, - cd4_percent_1 double, - cd4_percent_1_date datetime, - cd4_percent_2 double, - cd4_percent_2_date datetime, - vl_resulted int, - vl_resulted_date datetime, - vl_1 int, - vl_1_date datetime, - vl_2 int, - vl_2_date datetime, - vl_order_date datetime, - cd4_order_date datetime, - hiv_dna_pcr_order_date datetime, - hiv_dna_pcr_resulted int, - hiv_dna_pcr_resulted_date datetime, - hiv_dna_pcr_1 int, - hiv_dna_pcr_1_date datetime, - hiv_dna_pcr_2 int, - hiv_dna_pcr_2_date datetime, - hiv_rapid_test_resulted int, - hiv_rapid_test_resulted_date datetime, - prev_encounter_datetime_hiv datetime, - next_encounter_datetime_hiv datetime, - prev_encounter_type_hiv mediumint, - next_encounter_type_hiv mediumint, - prev_clinical_datetime_hiv datetime, - next_clinical_datetime_hiv datetime, - prev_clinical_location_id mediumint, - next_clinical_location_id mediumint, - prev_clinical_rtc_date_hiv datetime, - next_clinical_rtc_date_hiv datetime, - outreach_date_bncd datetime, - outreach_death_date_bncd datetime, - outreach_patient_care_status_bncd int, - transfer_date_bncd datetime, - transfer_transfer_out_bncd datetime, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_hiv), - index encounter_type (encounter_type), - index date_created (date_created) - - ); - - - - - 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 @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,' (select * from flat_hiv_summary_build_queue limit ', queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @dyn_sql=CONCAT('delete t1 from flat_hiv_summary_build_queue t1 join ',@queue_table, ' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - /* - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - */ - - drop temporary table if exists prev_rows; - create temporary table prev_rows like flat_hiv_summary_v15b; - - end if; - - - if (@query_type="sync") then - select 'SYNCING..........................................'; - set @write_table = "flat_hiv_summary_v15b"; -set @write_table = "foo_flat_hiv_summary"; - -set @queue_table = "foo_flat_hiv_summary_sync_queue"; -#set @cycle_size=500; -#create table if not exists flat_hiv_summary_sync_queue (person_id int primary key); - -create table if not exists foo_flat_hiv_summary_sync_queue (person_id int primary key, min_datetime datetime, index person_date (person_id, min_datetime)); - - - set @last_update = null; - select max(date_updated) into @last_update from etl.flat_log where table_name=@table_version; -#set @last_update = "2020-02-22 23:00"; - - - - - drop temporary table if exists mins; - create temporary table mins (person_id int, min_datetime datetime, index person_date (person_id, min_datetime)) - (select person_id, min(encounter_datetime) as min_datetime from etl.flat_obs where max_date_created> @last_update group by person_id); - - - insert into mins - (select person_id, min(obs_datetime) as min_obs_datetime - from amrs.obs where voided=1 and date_voided > @last_update and date_created <= @last_update group by person_id); - - insert into mins - (select patient_id, min(encounter_datetime) as min_encounter_datetime - from amrs.encounter where voided=1 and date_voided > @last_update and date_created <= @last_update group by patient_id); - - - insert into mins - (select patient_id, min(encounter_datetime) - from amrs.encounter - where date_changed > @last_update - group by patient_id - ); - - insert into mins - (select distinct person_id, min(test_datetime) - from etl.flat_lab_obs - where max_date_created > @last_update - group by person_id - ); - - insert into mins - (select person_id, min(encounter_datetime) - from etl.flat_orders - where max_date_created > @last_update - group by person_id - ); - - insert into mins - (select person_id, "1900-01-01" - from amrs.person - where date_changed > @last_update); - - - insert into mins - (select distinct person_id, "1900-01-01" from - amrs.person - where date_voided > @last_update - ); - - replace into foo_flat_hiv_summary_sync_queue - (select person_id, min(min_datetime) as min_datetime from mins group by person_id); - -/* - drop temporary table if exists prev_rows; - create temporary table prev_rows - (select * from flat_hiv_summary_v15b t1 -join foo_flat_hiv_summary_sync_queue t2 using (person_id) - where - t1.encounter_datetime < t2.min_datetime - and t1.is_clinical_encounter = 1 - and (t1.next_clinical_datetime_hiv >= t2.min_datetime or t1.next_clinical_datetime_hiv is null) - ); -*/ - - - drop temporary table if exists prev_rows; - create temporary table prev_rows - (select t1.person_id as p_id, - ifnull(t2.encounter_datetime,"1800-01-01") as min_datetime, - t2.* - from foo_flat_hiv_summary_sync_queue t1 -# left outer join flat_hiv_summary_v15b t2 -left outer join foo_flat_hiv_summary t2 - - on t1.person_id=t2.person_id - and t2.encounter_datetime < t1.min_datetime - and t2.is_clinical_encounter = 1 - and (t2.next_clinical_datetime_hiv >= t1.min_datetime or t2.next_clinical_datetime_hiv is null) - ); - - alter table prev_rows drop person_id, change column p_id person_id int; - - - drop temporary table if exists duplicates; - create temporary table duplicates - (select person_id, count(*) as n from prev_rows group by person_id having n > 1); - - delete t1 - from prev_rows t1 - join duplicates using (person_id) - where min_datetime != encounter_datetime; - - - - delete t1 - #from flat_hiv_summary_v15b t1 - #join flat_hiv_summary_sync_queue t2 using (person_id) -from foo_flat_hiv_summary t1 -join prev_rows t2 using (person_id) - where t1.encounter_datetime >= t2.min_datetime; - - - /* - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id) where t1.encounter_datetime >= t2.min_datetime ;'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - */ - - -#select encounter_datetime from etl.foo_flat_hiv_summary where person_id=1020; - end if; - - - - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - select @person_ids_count as 'num patients to sync'; - - - -/* - SET @dyn_sql=CONCAT('delete t1 from ',@primary_table, ' t1 join ',@queue_table,' t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; -*/ - - set @total_time=0; - set @cycle_number = 0; - - - while @person_ids_count > 0 do - - set @loop_start_time = now(); - - - drop temporary table if exists flat_hiv_summary_build_queue__0; - -#create temporary table flat_hiv_summary_build_queue__0 (person_id int primary key) -#(select * from etl.flat_hiv_summary_sync_queue); - - SET @dyn_sql=CONCAT('create temporary table flat_hiv_summary_build_queue__0 (person_id int primary key) (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - drop temporary table if exists flat_hiv_summary_0a; - create temporary table flat_hiv_summary_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - - 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,129,138,140,153,154,158,162,163) then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,115,117,120,127,128,138, 140, 153,154,158,162,163) then 10 - when t1.encounter_type in (129) then 5 - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join prev_rows t3 on t1.person_id=t3.person_id and t1.encounter_datetime >= t3.min_datetime - join flat_hiv_summary_build_queue__0 t0 on t1.person_id = t0.person_id - left outer join etl.flat_orders t2 on t1.encounter_id = t2.encounter_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) - AND NOT obs regexp "!!5303=(822|664|1067)!!" - AND NOT obs regexp "!!9082=9036!!" - ); - - - insert into flat_hiv_summary_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join prev_rows t3 on t1.person_id=t3.person_id and t1.test_datetime >= t3.min_datetime - join flat_hiv_summary_build_queue__0 t0 on t1.person_id = t0.person_id - - ); - - - - drop temporary table if exists flat_hiv_summary_0; - create temporary table flat_hiv_summary_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_date = null; - set @cur_encounter_date = null; - set @enrollment_date = null; - set @hiv_start_date = null; - set @cur_location = null; - set @cur_rtc_date = null; - set @prev_rtc_date = null; - set @hiv_start_date = null; - set @prev_arv_start_date = null; - set @arv_start_date = null; - set @prev_arv_end_date = null; - set @arv_start_location_id = null; - set @arv_first_regimen_start_date = null; - set @arv_first_regimen_start_date_flex = null; - set @arv_first_regimen = null; - set @prev_arv_line = null; - set @cur_arv_line = null; - set @prev_arv_adherence = null; - set @cur_arv_adherence = null; - set @hiv_status_disclosed = null; - set @is_pregnant = null; - set @edd = null; - set @prev_arv_meds = null; - set @cur_arv_meds = null; - set @ipt_start_date = null; - set @ipt_end_date = null; - set @ipt_completion_date = null; - - set @on_tb_tx = null; - set @tb_tx_start_date = null; - set @tb_tx_end_date = null; - set @pcp_prophylaxis_start_date = null; - set @tb_screen = null; - set @tb_screening_result = null; - set @tb_screening_datetime = null; - - set @death_date = null; - - set @patient_care_status=null; - - set @condoms_provided_date = null; - set @modern_contraceptive_method_start_date = null; - set @contraceptive_method = null; - - - set @cur_who_stage = null; - - set @vl_1=null; - set @vl_2=null; - set @vl_1_date=null; - set @vl_2_date=null; - set @vl_resulted=null; - set @vl_resulted_date=null; - - set @cd4_resulted=null; - set @cd4_resulted_date=null; - set @cd4_1=null; - set @cd4_1_date=null; - set @cd4_2=null; - set @cd4_2_date=null; - set @cd4_percent_1=null; - set @cd4_percent_1_date=null; - set @cd4_percent_2=null; - set @cd4_percent_2_date=null; - set @vl_order_date = null; - set @cd4_order_date = null; - - set @hiv_dna_pcr_order_date = null; - set @hiv_dna_pcr_1=null; - set @hiv_dna_pcr_2=null; - set @hiv_dna_pcr_1_date=null; - set @hiv_dna_pcr_2_date=null; - - set @hiv_rapid_test_resulted=null; - set @hiv_rapid_test_resulted_date= null; - - - - - - - drop temporary table if exists flat_hiv_summary_1; - create temporary table flat_hiv_summary_1 (index encounter_id (encounter_id)) - (select - obs, - encounter_type_sort_index, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.encounter_id, - @prev_encounter_date := date(@cur_encounter_date) as prev_encounter_date, - @cur_encounter_date := date(t1.encounter_datetime) as cur_encounter_date, - t1.encounter_datetime, - t1.encounter_type, - t1.is_clinical_encounter, - CASE - WHEN - (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7013=' - THEN - @enrollment_date:=REPLACE(REPLACE((SUBSTRING_INDEX(SUBSTRING(obs, LOCATE('!!7013=', obs)), - @sep, - 1)), - '!!7013=', - ''), - '!!', - '') - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollment_date:='1900-01-01' - WHEN - t1.encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_date IS NULL - || (@enrollment_date IS NOT NULL - AND @prev_id != @cur_id)) - AND t2.enrollment_date is null - THEN - @enrollment_date:=DATE(t1.encounter_datetime) - WHEN @prev_id = @cur_id THEN @enrollment_date - ELSE @enrollment_date:= t2.enrollment_date - END AS enrollment_date, - - - - CASE - WHEN - (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - AND obs REGEXP '!!7030=5622' - THEN - @enrollment_location_id:=9999 - WHEN - obs REGEXP '!!7015=' - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - THEN - @enrollmen_location_id:=9999 - WHEN - t1.encounter_type NOT IN (21 , @lab_encounter_type) - AND (@enrollment_location_id IS NULL - || (@enrollment_location_id IS NOT NULL - AND @prev_id != @cur_id)) - AND t2.enrollment_date is null - THEN - @enrollment_location_id:= t1.location_id - WHEN @prev_id = @cur_id THEN @enrollment_location_id - ELSE @enrollment_location_id:=t2.enrollment_location_id - END AS enrollment_location_id, - - - - - - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when t1.location_id then @cur_location := t1.location_id - when @prev_id = @cur_id then @cur_location - when t2.location_id then@cur_location := t2.location_id - else @cur_location := 16 - end as 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 - end as visit_num, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := t2.prev_rtc_date - end as prev_rtc_date, - - - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > t1.encounter_datetime,@cur_rtc_date,null) - when ifnull(t1.is_clinical_encounter=0,1) then @cur_rtc_date := t2.prev_rtc_date - else @cur_rtc_date := t2.rtc_date - end as cur_rtc_date, - - - - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504|1285)!!" then 1 - when t1.encounter_type = @death_encounter_type then 1 - else null - end as out_of_care, - - - - - - - - - - - case - when p.dead or p.death_date then @death_date := p.death_date - when obs regexp "!!1570=" then @death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@sep,1)),"!!1570=",""),"!!","") - when @prev_id != @cur_id or @death_date is null then - case - when obs regexp "!!(1734|1573)=" then @death_date := t1.encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @death_date := t1.encounter_datetime - else @death_date := t2.death_date - end - else @death_date - end as death_date, - - - - - - case - when @death_date <= t1.encounter_datetime then @patient_care_status := 159 - when obs regexp "!!1946=1065!!" then @patient_care_status := 9036 - when obs regexp "!!1285=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @lab_encounter_type and @cur_id != @prev_id then @patient_care_status := null - when t1.encounter_type = @lab_encounter_type and @cur_id = @prev_id then @patient_care_status - else @patient_care_status := 6101 - end as patient_care_status, - - - case - when obs regexp "!!9203=" then @hiv_start_date := replace(replace((substring_index(substring(obs,locate("!!9203=",obs)),@sep,1)),"!!9203=",""),"!!","") - when obs regexp "!!7015=" then @hiv_start_date := "1900-01-01" - when @prev_id = @cur_id then @hiv_start_date - when t2.hiv_start_date is null then @hiv_start_date := date(t1.encounter_datetime) - else @hiv_start_date := t2.hiv_start_date - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null and @prev_id=@cur_id) then @arv_start_location_id := t1.location_id - when @prev_id = @cur_id and obs regexp "!!(1250|1088|2154)=" and @arv_start_date is null then @arv_start_location_id := t1.location_id - when @prev_id = @cur_id then @arv_start_location_id - else @arv_start_location_id := t2.arv_start_location_id - end as arv_start_location_id, - - - case - when @prev_id=@cur_id and @cur_arv_meds is not null then @prev_arv_meds := @cur_arv_meds - when @prev_id=@cur_id then @prev_arv_meds - else @prev_arv_meds := t2.prev_arv_meds - end as prev_arv_meds, - - - - - #2154 : PATIENT REPORTED CURRENT ANTIRETROVIRAL TREATMENT - #2157 : PATIENT REPORTED PAST ANTIRETROVIRAL TREATMENT - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_meds := null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - when @prev_id = @cur_id then @cur_arv_meds - else @cur_arv_meds:= t2.cur_arv_meds - end as cur_arv_meds, - - /* - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=" then @cur_arv_meds := normalize_arvs(obs,'1250') - - - when obs regexp "!!1088=" then @cur_arv_meds := normalize_arvs(obs,'1088') - - - when obs regexp "!!2154=" then @cur_arv_meds := normalize_arvs(obs,'2154') - when obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @cur_arv_meds := normalize_arvs(obs,'2157') - - else null - end - */ - null as cur_arv_meds_strict, - - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := t2.prev_clinical_datetime_hiv - end as prev_clinical_datetime_hiv, - - case - when t1.is_clinical_encounter then @cur_clinical_datetime := t1.encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - when t2.is_clinical_encounter then @cur_clinical_datetime := t2.encounter_datetime - else @cur_clinical_datetime := t2.prev_clinical_datetime_hiv - end as cur_clinical_datetime, - - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := t2.prev_clinical_rtc_date_hiv - end as prev_clinical_rtc_date_hiv, - - - case - when t1.is_clinical_encounter then @cur_clinical_rtc_date := @cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - when t2.is_clinical_encounter AND t1.is_clinical_encounter then @cur_clinical_rtc_date := t2.rtc_date - else @cur_clinical_rtc_date:= t2.prev_clinical_rtc_date_hiv - end as cur_clinic_rtc_date, - - - CASE - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - AND t2.arv_first_regimen_start_date is null - THEN - @arv_first_regimen_start_date:=GetValues(obs,1499) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - AND t2.arv_first_regimen_start_date is null - THEN - @arv_first_regimen_start_date:='1900-01-01' - - WHEN - ((@arv_first_regimen_start_date IS NULL AND t2.arv_first_regimen_start_date is null) || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - AND t2.arv_first_regimen_start_date is null - THEN - @arv_first_regimen_start_date:=DATE(t1.encounter_datetime) - - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),t1.encounter_datetime) < 90) - AND t2.arv_first_regimen_start_date is null - THEN - @arv_first_regimen_start_date:= "1900-01-01" #DATE(t1.encounter_datetime) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),t1.encounter_datetime) < 90) - AND t2.arv_first_regimen_start_date is null - THEN - @arv_first_regimen_start_date:= "1900-01-01" # DATE(encounter_datetime) - - WHEN - (@arv_first_regimen_start_date IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - AND t2.arv_first_regimen_start_date is null - THEN - @arv_first_regimen_start_date:='1900-01-01' - WHEN @prev_id = @cur_id THEN @arv_first_regimen_start_date - else @arv_first_regimen_start_date := t2.arv_first_regimen_start_date - END AS arv_first_regimen_start_date, - - - - - /* - CASE - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - THEN - @arv_first_regimen_start_date_flex := GetValues(obs,1499) - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - THEN - @arv_first_regimen_start_date_flex :='1900-01-01' - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - THEN - @arv_first_regimen_start_date_flex:=DATE(t1.encounter_datetime) - - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen_start_date_flex :=date(t1.encounter_datetime) - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),encounter_datetime) < 90) - - THEN - @arv_first_regimen_start_date_flex :=date(t1.encounter_datetime) - - WHEN - (@arv_first_regimen_start_date_flex IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - THEN - @arv_first_regimen_start_date_flex:='1900-01-01' - WHEN @prev_id = @cur_id THEN @arv_first_regimen_start_date_flex - WHEN @prev_id != @cur_id THEN @arv_first_regimen_start_date_flex:= t2.arv_first_regimen_start_date_flex - ELSE @arv_first_regimen_start_date_flex - END */ - null AS arv_first_regimen_start_date_flex, - - - -/* - case - when @arv_first_regimen is null and obs regexp "!!2157=" and not obs regexp "!!2157=1066" then @arv_first_regimen := normalize_arvs(obs,'2157') - when obs regexp "!!7015=" and @arv_first_regimen is null then @arv_first_regimen := "unknown" - when @arv_first_regimen is null and @cur_arv_meds is not null then @arv_first_regimen := @cur_arv_meds - when @prev_id = @cur_id then @arv_first_regimen - when @prev_id != @cur_id then @arv_first_regimen := @cur_arv_meds - else "-1" - end as arv_first_regimen, -*/ - - CASE - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - AND t2.arv_first_regimen is null - THEN - @arv_first_regimen:= "unknown" - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - AND t2.arv_first_regimen is null - THEN - @arv_first_regimen:= "unknown" - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id ) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - AND t2.arv_first_regimen is null - THEN - @arv_first_regimen := @cur_arv_meds - - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),t1.encounter_datetime) < 90) - AND t2.arv_first_regimen is null - THEN - @arv_first_regimen:= "unknown" #@cur_arv_meds - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),t1.encounter_datetime) < 90) - AND t2.arv_first_regimen is null - - THEN - @arv_first_regimen:= "unknown" #@cur_arv_meds - - WHEN - (@arv_first_regimen IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - AND t2.arv_first_regimen is null - THEN - @arv_first_regimen := "unknown" - - WHEN @prev_id = @cur_id THEN @arv_first_regimen - ELSE @arv_first_regimen:=t2.arv_first_regimen - END AS arv_first_regimen, - - -/* - case - when @arv_first_regimen is null and obs regexp "!!1499=" then @arv_first_regimen_location_id := 9999 - when @prev_id != @cur_id and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @arv_first_regimen_location_id is null and @cur_arv_meds is not null then @arv_first_regimen_location_id := location_id - when @prev_id = @cur_id then @arv_first_regimen_location_id - when @prev_id != @cur_id then @arv_first_regimen_location_id := null - else "-1" - end as arv_first_regimen_location_id, -*/ - - CASE - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs REGEXP '!!1499=' - AND t2.arv_first_regimen_location_id is null - THEN - @arv_first_regimen_location_id := 9999 - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2157=' AND NOT obs regexp '!!2157=1066!!' - AND t2.arv_first_regimen_location_id is null - THEN - @arv_first_regimen_location_id:=9999 - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND (obs REGEXP '!!1255=(1256)!!' || obs REGEXP '!!1250=') - AND t2.arv_first_regimen_location_id is null - THEN - @arv_first_regimen_location_id:=t1.location_id - - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!1088=' AND NOT obs regexp '!!1088=1107!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),t1.encounter_datetime) < 90) - AND t2.arv_first_regimen_location_id is null - THEN - @arv_first_regimen_location_id:= 9999 #location_id - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND obs regexp '!!2154=' AND NOT obs regexp '!!2154=1066!!' - AND not obs regexp'!!7015=' - AND (@prev_clinical_datetime is null - or timestampdiff(day,ifnull(@prev_clinical_rtc_date,date_add(@prev_clinical_datetime, interval 90 day)),t1.encounter_datetime) < 90) - AND t2.arv_first_regimen_location_id is null - THEN - @arv_first_regimen_location_id:= 9999 #location_id - - WHEN - (@arv_first_regimen_location_id IS NULL || @prev_id != @cur_id) - AND @cur_arv_meds IS NOT NULL - AND t2.arv_first_regimen_location_id is null - THEN - @arv_first_regimen_location_id:=9999 - WHEN @prev_id = @cur_id THEN @arv_first_regimen_location_id - ELSE @arv_first_regimen_location_id:=t2.arv_first_regimen_location_id - END AS arv_first_regimen_location_id, - - - - case - when @prev_id=@cur_id then @prev_arv_line := @cur_arv_line - else @prev_arv_line := t2.prev_arv_line - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1250=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!1088=(6156)!!" then @cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then @cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @cur_arv_line := 2 - when obs regexp "!!2154=(6156)!!" then @cur_arv_line := 3 - when @prev_id = @cur_id then @cur_arv_line - else @cur_arv_line := t2.cur_arv_line - end as cur_arv_line, - - /* - case - when obs regexp "!!1255=(1107|1260)!!" then null - when obs regexp "!!1250=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1250=(6156)!!" then 3 - when obs regexp "!!1088=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then 2 - when obs regexp "!!1088=(6156)!!" then 3 - when obs regexp "!!2154=(6467|6964|792|633|631|9759)!!" then 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then 2 - when obs regexp "!!2154=(6156)!!" then 3 - else null - end - */ - null as cur_arv_line_strict, - - - - - - - - - - case - when obs regexp "!!6976=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6976=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6976=6695!!" then @cur_arv_line_reported := 3 - - when obs regexp "!!6744=6693!!" then @cur_arv_line_reported := 1 - when obs regexp "!!6744=6694!!" then @cur_arv_line_reported := 2 - when obs regexp "!!6744=6695!!" then @cur_arv_line_reported := 3 - when @prev_id = @cur_id then @cur_arv_line_reported - else @cur_arv_line_reported := t2.cur_arv_line_reported - end as cur_arv_line_reported, - - - - - case - when @prev_id=@cur_id then @prev_arv_start_date := @arv_start_date - else @prev_arv_start_date := t2.prev_arv_start_date - end as prev_arv_start_date, - - - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @arv_start_date is null and @prev_id=@cur_id ) then @arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @arv_start_date := null - - when @cur_arv_meds != @prev_arv_meds then @arv_start_date := date(t1.encounter_datetime) - when @prev_id = @cur_id then @arv_start_date - else @arv_start_date := t2.arv_start_date - end as arv_start_date, - - - # THIS INDICATOR MAY HAVE AN ERRONEOUS DEFINITION - case - when @prev_id = @cur_id then - case - when @prev_arv_start_date = @arv_start_date then @prev_arv_end_date := date(t1.encounter_datetime) - else @prev_arv_end_date - end - else @prev_arv_end_date := t2.prev_arv_end_date - end as prev_arv_end_date, - - - case - when @prev_id=@cur_id then @prev_arv_adherence := @cur_arv_adherence - else @prev_arv_adherence := t2.prev_arv_adherence - end as prev_arv_adherence, - - - - - - case - when obs regexp "!!8288=6343!!" then @cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @cur_arv_adherence := 'POOR' - when @prev_id = @cur_id then @cur_arv_adherence - else @cur_arv_adherence := t2.cur_arv_adherence - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when @prev_id = @cur_id then @hiv_status_disclosed - else @hiv_status_disclosed := t2.hiv_status_disclosed - end as hiv_status_disclosed, - - - - - - case - when obs regexp "!!8351=(48|50|1066|1624|6971|9608)!!" then @is_pregnant := null - when @prev_id != @cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" or obs regexp "!!8351=(1065|1484)!!" then @is_pregnant := true - else @is_pregnant := t2.is_pregnant - end - when @is_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") or obs regexp "!!8351=(1065|1484)!!"then @is_pregnant := true - when @is_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@is_pregnant,t1.encounter_datetime) > 40 or timestampdiff(week,@edd,t1.encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @is_pregnant := null - else @is_pregnant - end as is_pregnant, - - - - - - - - - case - when @prev_id != @cur_id then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(t1.encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd := t2.edd - end - when @edd is null then - case - when @is_pregnant and obs regexp "!!1836=" then @edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @edd := - date_add(t1.encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@sep,1)),"!!5596=",""),"!!","") - else @edd - end - when @edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@edd,t1.encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @edd := null - else @edd - end as edd, - - - - case - when obs regexp "!!6174=" then @tb_screen := true - when obs regexp "!!2022=1065!!" then @tb_screen := true - when obs regexp "!!307=" then @tb_screen := true - when obs regexp "!!12=" then @tb_screen := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screen := true - when orders regexp "(12|307|8064|2311|2323)" then @tb_screen := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screen := true - when obs regexp "!!5958=1077!!" then @tb_screen := true - when obs regexp "!!2020=1065!!" then @tb_screen := true - when obs regexp "!!2021=1065!!" then @tb_screen := true - when obs regexp "!!2028=" then @tb_screen := true - when obs regexp "!!1268=(1256|1850)!!" then @tb_screen := true - when obs regexp "!!5959=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!5971=(1073|1074)!!" then @tb_screen := true - when obs regexp "!!1492=107!!" then @tb_screen := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screen := true - end as tb_screen, - - case - when obs regexp "!!8292=" then @tb_screening_result := - replace(replace((substring_index(substring(obs,locate("!!8292=",obs)),@sep,1)),"!!8292=",""),"!!","") - when @prev_id = @cur_id then @tb_screening_result - else @tb_screening_result := t2.tb_screening_result - end as tb_screening_result, - - case - when obs regexp "!!6174=" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!2022=1065!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!307=" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!12=" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := t1.encounter_datetime - when orders regexp "(12|307|8064|2311|2323)" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!5958=1077!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!2020=1065!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!2021=1065!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!2028=" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!1268=(1256|1850)!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!5959=(1073|1074)!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!5971=(1073|1074)!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!1492=107!!" then @tb_screening_datetime := t1.encounter_datetime - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @tb_screening_datetime := t1.encounter_datetime - when @cur_id = @prev_id then @tb_screening_datetime - else @tb_screening_datetime := t2.tb_screening_datetime - end as tb_screening_datetime, - - - case - when obs regexp "!!1265=(1107|1260)!!" then @on_ipt := 0 - when obs regexp "!!1265=!!" then @on_ipt := 1 - when obs regexp "!!1110=656!!" then @on_ipt := 1 - when @cur_id = @prev_id then @on_ipt - else @on_ipt := t2.on_ipt - end as on_ipt, - - - - case - when obs regexp "!!1265=(1256|1850)!!" then @ipt_start_date := t1.encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" and @ipt_start_date is null then @ipt_start_date := t1.encounter_datetime - when @cur_id = @prev_id then @ipt_start_date - else @ipt_start_date := t2.ipt_start_date - end as ipt_start_date, - - - - case - when obs regexp "!!1266=" then @ipt_stop_date := t1.encounter_datetime - when @cur_id = @prev_id then @ipt_stop_date - else @ipt_stop_date := t2.ipt_stop_date - end as ipt_stop_date, - - case - when obs regexp "!!1266=1267!!" then @ipt_completion_date := t1.encounter_datetime - when @cur_id = @prev_id then @ipt_completion_date - when @cur_id = @prev_id then @ipt_completion_date - else @ipt_completion_date := t2.ipt_completion_date - end as ipt_completion_date, - - - case - when obs regexp "!!1268=(1107|1260)!!" then @on_tb_tx := 0 - when obs regexp "!!1268=" then @on_tb_tx := 1 - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @on_tb_tx := 1 - else @on_tb_tx := 0 - end as on_tb_tx, - - - - case - when obs regexp "!!1113=" then @tb_tx_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @tb_tx_start_date := t1.encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" and obs regexp "!!7015=" and @tb_tx_start_date is null then @tb_tx_start_date := null - when obs regexp "!!1268=(1257|1259|1849|981)!!" and @tb_tx_start_date is null then @tb_tx_start_date := t1.encounter_datetime - - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" and @tb_tx_start_date is null then @tb_tx_start_date := t1.encounter_datetime - when @cur_id = @prev_id then @tb_tx_start_date - else @tb_tx_start_date := t2.tb_tx_start_date - end as tb_tx_start_date, - - - - - case - when obs regexp "!!2041=" then @tb_tx_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @tb_tx_end_date := t1.encounter_datetime - when @cur_id = @prev_id then @tb_tx_end_date - else @tb_tx_end_date := t2.tb_tx_end_date - end as tb_tx_end_date, - - - - - - case - when obs regexp "!!1261=(1107|1260)!!" then @pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @pcp_prophylaxis_start_date := t1.encounter_datetime - when @prev_id = @cur_id then - case - when obs regexp "!!1261=1257!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := t1.encounter_datetime - when obs regexp "!!1109=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := t1.encounter_datetime - when obs regexp "!!1193=(916|92)!!" and @pcp_prophylaxis_start_date is null then @pcp_prophylaxis_start_date := t1.encounter_datetime - else @pcp_prophylaxis_start_date - end - else @pcp_prophylaxis_start_date := t2.pcp_prophylaxis_start_date - end as pcp_prophylaxis_start_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 and date(t1.encounter_datetime)<>@cd4_1_date then @cd4_2:= @cd4_1 - else @cd4_2 - end - when - t1.encounter_type = @lab_encounter_type - and obs regexp "!!5497=[0-9]" - and t2.cd4_1 >= 0 - and date(t1.encounter_datetime)<>t2.cd4_1_date - then @cd4_2:= t2.cd4_1 - - else @cd4_2:=t2.cd4_2 - end as cd4_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!5497=[0-9]" and @cd4_1 >= 0 then @cd4_2_date:= @cd4_1_date - else @cd4_2_date - end - - when t1.encounter_type=@lab_encounter_type - and obs regexp "!!5497=[0-9]" - and t2.cd4_1 >= 0 - then @cd4_2_date:= t2.cd4_1_date - - else @cd4_2_date:=t2.cd4_2_date - end as cd4_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_date_resulted := date(t1.encounter_datetime) - when @prev_id = @cur_id and date(t1.encounter_datetime) = @cd4_date_resulted then @cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(t1.encounter_datetime) = @cd4_date_resulted then @cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@sep,1)),"!!5497=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_1 - else @cd4_1:=t2.cd4_1 - end as cd4_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!5497=[0-9]" then @cd4_1_date:=date(t1.encounter_datetime) - when @prev_id=@cur_id then @cd4_1_date - else @cd4_1_date:=t2.cd4_1_date - end as cd4_1_date, - - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!730=[0-9]" and @cd4_percent_1 >= 0 - then @cd4_percent_2:= @cd4_percent_1 - else @cd4_percent_2 - end - when t1.encounter_type=@lab_encounter_type - and obs regexp "!!730=[0-9]" - and t2.cd4_percent_1 >= 0 - then @cd4_percent_2:= t2.cd4_percent_1 - - else @cd4_percent_2:=t2.cd4_percent_2 - end as cd4_percent_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type and @cd4_percent_1 >= 0 then @cd4_percent_2_date:= @cd4_percent_1_date - else @cd4_percent_2_date - end - - when obs regexp "!!730=[0-9]" - and t1.encounter_type = @lab_encounter_type - and t2.cd4_percent_1 >= 0 - then @cd4_percent_2_date:= t2.cd4_percent_1_date - - else @cd4_percent_2_date:=t2.cd4_percent_2_date - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!730=[0-9]" - then @cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@sep,1)),"!!730=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @cd4_percent_1 - else @cd4_percent_1:=t2.cd4_percent_1 - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @lab_encounter_type then @cd4_percent_1_date:=date(t1.encounter_datetime) - when @prev_id=@cur_id then @cd4_percent_1_date - else @cd4_percent_1_date:=t2.cd4_percent_1_date - end as cd4_percent_1_date, - - - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,t1.encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <> date(@vl_1_date) then @vl_2:= @vl_1 - else @vl_2 - end - when obs regexp "!!856=[0-9]" - and t2.vl_1 >= 0 - - and - if(obs_datetimes is null,t1.encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <>date(t2.vl_1_date) - then @vl_2:= t2.vl_1 - else @vl_2:=t2.vl_2 - end as vl_2, - - case - when @prev_id=@cur_id then - case - when obs regexp "!!856=[0-9]" and @vl_1 >= 0 - and - if(obs_datetimes is null,t1.encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <>date(@vl_1_date) then @vl_2_date:= @vl_1_date - else @vl_2_date - end - when obs regexp "!!856=[0-9]" - and t2.vl_1 >= 0 - - and - if(obs_datetimes is null,t1.encounter_datetime, - date(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""))) <>date(t2.vl_1_date) - - then @vl_2_date:= t2.vl_1_date - - else @vl_2_date:=t2.vl_2_date - end as vl_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_date_resulted := date(t1.encounter_datetime) - when @prev_id = @cur_id and date(t1.encounter_datetime) = @vl_date_resulted then @vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!856=[0-9]" then @vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id = @cur_id and date(t1.encounter_datetime) = @vl_date_resulted then @vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@sep,1)),"!!856=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @vl_1 - else @vl_1:=t2.vl_1 - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @lab_encounter_type then @vl_1_date:= t1.encounter_datetime - when obs regexp "!!856=[0-9]" - and (@vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!",""),@vl_1_date)) > 30) - and (@vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","")) > @vl_1_date) - then @vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@sep,1)),"!!856=",""),"!!","") - when @prev_id=@cur_id then @vl_1_date - else @vl_1_date := t2.vl_1_date - end as vl_1_date, - - - - - - case - when obs regexp "!!1271=856!!" then @vl_order_date := date(t1.encounter_datetime) - when orders regexp "856" then @vl_order_date := date(t1.encounter_datetime) - when @prev_id=@cur_id and (@vl_1_date is null or @vl_1_date < @vl_order_date) then @vl_order_date - when @prev_id != @cur_id then @vl_order_date := t2.vl_order_date - else @vl_order_date := null - end as vl_order_date, - - - case - when obs regexp "!!1271=657!!" then @cd4_order_date := date(t1.encounter_datetime) - when orders regexp "657" then @cd4_order_date := date(t1.encounter_datetime) - when @prev_id=@cur_id then @cd4_order_date - else @cd4_order_date := t2.cd4_order_date - end as cd4_order_date, - - - case - when obs regexp "!!1271=1030!!" then @hiv_dna_pcr_order_date := date(t1.encounter_datetime) - when orders regexp "1030" then @hiv_dna_pcr_order_date := date(t1.encounter_datetime) - when @prev_id=@cur_id then @hiv_dna_pcr_order_date - else @hiv_dna_pcr_order_date := t2.hiv_dna_pcr_order_date - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then t1.encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(t1.encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2:= @hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2 := @hiv_dna_pcr_1 - else @hiv_dna_pcr_2 - end - - when t1.encounter_type = @lab_encounter_type - and obs regexp "!!1030=[0-9]" - and t2.hiv_dna_pcr_1 >= 0 - and date(t1.encounter_datetime)<>t2.hiv_dna_pcr_1_date - then @hiv_dna_pcr_2:= t2.hiv_dna_pcr_1 - - - when obs regexp "!!1030=[0-9]" - and t2.hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),t2.hiv_dna_pcr_1_date)) > 30 - then @hiv_dna_pcr_2 := t2.hiv_dna_pcr_1 - - - else @hiv_dna_pcr_2:=t2.hiv_dna_pcr_2 - end as hiv_dna_pcr_2, - - case - when @prev_id=@cur_id then - case - when t1.encounter_type=@lab_encounter_type and obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 and date(t1.encounter_datetime)<>@hiv_dna_pcr_1_date then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30 then @hiv_dna_pcr_2_date:= @hiv_dna_pcr_1_date - else @hiv_dna_pcr_2_date - end - - when t1.encounter_type=@lab_encounter_type - and obs regexp "!!1030=[0-9]" - and t2.hiv_dna_pcr_1 >= 0 - and date(t1.encounter_datetime)<>t2.hiv_dna_pcr_1_date - then @hiv_dna_pcr_2_date:= t2.hiv_dna_pcr_1_date - - when obs regexp "!!1030=[0-9]" - and t2.hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@sep,1)),"1030=",""),"!!",""),t2.hiv_dna_pcr_1_date)) > 30 - then @hiv_dna_pcr_2_date:= t2.hiv_dna_pcr_1_date - - else @hiv_dna_pcr_2_date:=t2.hiv_dna_pcr_2_date - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!",""),@hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@sep,1)),"!!1030=",""),"!!","") as unsigned) - when @prev_id=@cur_id then @hiv_dna_pcr_1 - else @hiv_dna_pcr_1:=t2.hiv_dna_pcr_1 - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!1030=[0-9]" then @hiv_dna_pcr_1_date:=date(t1.encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") ,@hiv_dna_pcr_1_date)) > 30) - then @hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@sep,1)),"!!1030=",""),"!!","") - when @prev_id=@cur_id then @hiv_dna_pcr_1_date - else @hiv_dna_pcr_1_date:=t2.hiv_dna_pcr_1_date - end as hiv_dna_pcr_1_date, - - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then t1.encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - case - when obs regexp "!!8302=8305!!" then @condoms_provided_date := t1.encounter_datetime - when obs regexp "!!374=(190|6717|6718)!!" then @condoms_provided_date := t1.encounter_datetime - when obs regexp "!!6579=" then @condoms_provided_date := t1.encounter_datetime - when @prev_id = @cur_id then @condoms_provided_date - else @condoms_provided_date := t2.condoms_provided_date - end as condoms_provided_date, - - - - - - - case - when obs regexp "!!7240=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(t1.encounter_datetime) - when obs regexp "!!7240=!!" - then @modern_contraceptive_method_start_date := null - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" and obs regexp "!!1190=" - then @modern_contraceptive_method_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1190=",obs)),@sep,1)),"!!1190=",""),"!!","")) - when obs regexp "!!374=(5275|6220|780|5279|907|6218|6700|6701|5274|9510|9511|9734|9735|6217)!!" - then @modern_contraceptive_method_start_date := date(t1.encounter_datetime) - when obs regexp "!!374=!!" - then @modern_contraceptive_method_start_date := null - when @prev_id = @cur_id then @modern_contraceptive_method_start_date - else @modern_contraceptive_method_start_date := t2.modern_contraceptive_method_start_date - end as modern_contraceptive_method_start_date, - - - case - when obs regexp "!!7240=1107!!" then @contraceptive_method := null - when obs regexp "!!7240=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!7240=",obs)),@sep,1)),"!!7240=",""),"!!","") - when obs regexp "!!374=" - then @contraceptive_method := replace(replace((substring_index(substring(obs,locate("!!374=",obs)),@sep,1)),"!!374=",""),"!!","") - when @prev_id = @cur_id then @contraceptive_method - else @contraceptive_method := t2.contraceptive_method - end as contraceptive_method, - - - - - - - case - when obs regexp "!!5356=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!5356=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!5356=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!5356=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!8287=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!8287=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!8287=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!8287=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!1224=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!1224=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!1224=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!1224=(1207|1223)!!" then @cur_who_stage := 4 - when obs regexp "!!8307=(1204|1220)!!" then @cur_who_stage := 1 - when obs regexp "!!8307=(1205|1221)!!" then @cur_who_stage := 2 - when obs regexp "!!8307=(1206|1222)!!" then @cur_who_stage := 3 - when obs regexp "!!8307=(1207|1223)!!" then @cur_who_stage := 4 - when @prev_id = @cur_id then @cur_who_stage - else @cur_who_stage := t2.cur_who_stage - end as cur_who_stage, - - - case - when obs regexp "!!6096=1065" then @discordant_status := "Yes" - when obs regexp "!!6096=1066" then @discordant_status := "No" - when obs regexp "!!6096=1067" then @discordant_status := "Unknown" - when obs regexp "!!6096=1175" then @discordant_status := "N/A" - when obs regexp "!!6096=6826" then @discordant_status := "Concordant Couple Positive" - when obs regexp "!!6096=6827" then @discordant_status := "Concordant Couple Negative" - when @prev_id = @cur_id then @discordant_status - else @discordant_status := t2.discordant_status - end as discordant_status - - - - - from flat_hiv_summary_0 t1 - join amrs.person p using (person_id) - left outer join prev_rows t2 using (person_id) - ); - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - - set @next_encounter_type = null; - set @cur_encounter_type = null; - - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - - alter table flat_hiv_summary_1 drop prev_id, drop cur_id, drop cur_clinical_datetime, drop cur_clinic_rtc_date; - - drop temporary table if exists flat_hiv_summary_1b; - create temporary table flat_hiv_summary_1b - (select * from flat_hiv_summary_1 t1 - order by person_id, date(t1.encounter_datetime) desc, encounter_type_sort_index desc - ); - - - drop table if exists flat_hiv_summary_2; - create temporary table flat_hiv_summary_2 - (select t1.*, - @prev_id := @cur_id as prev_id, - @cur_id := person_id as cur_id, - - case - when @prev_id = @cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @cur_encounter_datetime := t1.encounter_datetime as cur_encounter_datetime, - - case - when @prev_id=@cur_id then @next_encounter_type := @cur_encounter_type - else @next_encounter_type := null - end as next_encounter_type_hiv, - - @cur_encounter_type := t1.encounter_type as cur_encounter_type, - - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - else @prev_clinical_location_id := null - end as next_clinical_location_id, - - case - when t1.is_clinical_encounter then @cur_clinical_datetime := t1.encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when t1.is_clinical_encounter then @cur_clinical_location_id := t1.location_id - when @prev_id = @cur_id then @cur_clinical_location_id - else @cur_clinical_location_id := null - end as cur_clinic_location_id, - - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when t1.is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @prev_id != @cur_id then null - when t1.is_clinical_encounter then @outreach_date_bncd - else null - end as outreach_date_bncd, - - case - when t1.encounter_type=21 and @outreach_date_bncd is null then @outreach_date_bncd := t1.encounter_datetime - when t1.is_clinical_encounter then @outreach_date_bncd := null - when @prev_id = @cur_id then @outreach_date_bncd - else @outreach_date_bncd := t2.outreach_date_bncd - end as next_outreach_date_bncd, - - case - when @prev_id = @cur_id and t1.is_clinical_encounter then @outreach_death_date_bncd - when @prev_id = @cur_id then null - else null - end as outreach_death_date_bncd, - - case - when t1.encounter_type=21 and @outreach_death_date_bncd is null then @outreach_death_date_bncd := t1.death_date - when t1.is_clinical_encounter then @outreach_death_date_bncd := null - when @prev_id = @cur_id then @outreach_death_date_bncd - else @outreach_death_date_bncd := t2.outreach_death_date_bncd - end as next_outreach_death_date_bncd, - - - case - when @prev_id != @cur_id then null - when t1.is_clinical_encounter then cast(@outreach_patient_care_status_bncd as unsigned) - else null - end as outreach_patient_care_status_bncd, - - case - when t1.encounter_type=21 and @outreach_patient_care_status_bncd is null then @outreach_patient_care_status_bncd := t1.patient_care_status - when t1.is_clinical_encounter then @outreach_patient_care_status_bncd := null - when @prev_id = @cur_id then @outreach_patient_care_status_bncd - else @outreach_patient_care_status_bncd := null - end as next_outreach_patient_care_status_bncd, - - case - when @prev_id != @cur_id then null - when t1.is_clinical_encounter then @transfer_date_bncd - else null - end as transfer_date_bncd, - - case - when t1.encounter_type=116 and @transfer_date_bncd is null then @transfer_date_bncd := t1.encounter_datetime - when t1.is_clinical_encounter then @transfer_date_bncd := null - when @prev_id = @cur_id then @transfer_date_bncd - else @transfer_date_bncd := null - end as next_transfer_date_bncd, - - case - when @prev_id != @cur_id then null - when t1.is_clinical_encounter then @transfer_transfer_out_bncd - else null - end as transfer_transfer_out_bncd, - - case - when t1.encounter_type=116 and @transfer_transfer_out_bncd is null then @transfer_transfer_out_bncd := t1.encounter_datetime - when t1.is_clinical_encounter then @transfer_transfer_out_bncd := null - when @prev_id = @cur_id then @transfer_transfer_out_bncd - else @transfer_transfer_out_bncd := null - end as next_transfer_transfer_out_bncd - - from flat_hiv_summary_1b t1 - left outer join prev_rows t2 using (person_id) - order by person_id, date(t1.encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - drop temporary table if exists flat_hiv_summary_2b; - create temporary table flat_hiv_summary_2b - (select * from flat_hiv_summary_2 t1 order by person_id, date(t1.encounter_datetime) asc, encounter_type_sort_index); - - - set @prev_id = null; - set @cur_id = null; - set @prev_encounter_type = null; - set @cur_encounter_type = null; - set @prev_encounter_datetime = null; - set @cur_encounter_datetime = null; - set @prev_clinical_datetime = null; - set @cur_clinical_datetime = null; - set @prev_clinical_location_id = null; - set @cur_clinical_location_id = null; - - drop temporary table if exists flat_hiv_summary_3; - create temporary table flat_hiv_summary_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - t1.*, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - case - when @prev_id=@cur_id then @prev_encounter_type := @cur_encounter_type - else @prev_encounter_type:= t2.prev_encounter_type_hiv - end as prev_encounter_type_hiv, - - @cur_encounter_type := t1.encounter_type as cur_encounter_type, - - case - when @prev_id=@cur_id then @prev_encounter_datetime := @cur_encounter_datetime - else @prev_encounter_datetime := t2.prev_encounter_datetime_hiv - end as prev_encounter_datetime_hiv, - - @cur_encounter_datetime := t1.encounter_datetime as cur_encounter_datetime, - -/* Moved to flat_hiv_summary_1 - case - when @prev_id = @cur_id then @prev_clinical_datetime := @cur_clinical_datetime - else @prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when is_clinical_encounter then @cur_clinical_datetime := encounter_datetime - when @prev_id = @cur_id then @cur_clinical_datetime - else @cur_clinical_datetime := null - end as cur_clinical_datetime, -*/ - - case - when @prev_id = @cur_id then @prev_clinical_location_id := @cur_clinical_location_id - #when t2.is_clinical_encounter then @prev_clinical_location_id := t2.location_id - else @prev_clinical_location_id := t2.prev_clinical_location_id - end as prev_clinical_location_id, - - - case - when t1.is_clinical_encounter then @cur_clinical_location_id := t1.location_id - when @prev_id = @cur_id then @cur_clinical_location_id - - when t2.is_clinical_encounter then @cur_clinical_location_id := t2.location_id - else @cur_clinical_location_id := t2.prev_clinical_location_id - end as cur_clinical_location_id - -/* MOVED to flat_hiv_summary_1 - case - when @prev_id = @cur_id then @prev_clinical_rtc_date := @cur_clinical_rtc_date - else @prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @cur_clinical_rtc_date := cur_rtc_date - when @prev_id = @cur_id then @cur_clinical_rtc_date - else @cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date -*/ - from flat_hiv_summary_2b t1 - left outer join prev_rows t2 using (person_id) - order by person_id, t1.encounter_datetime asc, encounter_type_sort_index - ); - - alter table flat_hiv_summary_3 drop prev_id, drop cur_id; - - set @prev_id = null; - set @cur_id = null; - set @transfer_in = null; - set @transfer_in_date = null; - set @transfer_in_location_id = null; - set @transfer_out = null; - set @transfer_out_date = null; - set @transfer_out_location_id = null; - - drop temporary table if exists flat_hiv_summary_4; - create temporary table flat_hiv_summary_4 ( index person_enc (person_id, encounter_datetime)) - (select - t1.*, - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - - - - case - when obs regexp "!!7015=" then @transfer_in := 1 - when t1.prev_clinical_location_id != t1.location_id then @transfer_in := 1 - else @transfer_in := null - end as transfer_in, - - case - when obs regexp "!!7015=" then @transfer_in_date := date(t1.encounter_datetime) - when t1.prev_clinical_location_id != t1.location_id then @transfer_in_date := date(t1.encounter_datetime) - when @cur_id = @prev_id then @transfer_in_date - else @transfer_in_date := t2.transfer_in_date - end transfer_in_date, - - case - when obs regexp "!!7015=1287" then @transfer_in_location_id := 9999 - when t1.prev_clinical_location_id != t1.location_id then @transfer_in_location_id := t1.prev_clinical_location_id - when @cur_id = @prev_id then @transfer_in_location_id - else @transfer_in_location_id := t2.transfer_in_location_id - end transfer_in_location_id, - - - - - case - when obs regexp "!!1285=!!" then @transfer_out := 1 - when obs regexp "!!1596=1594!!" then @transfer_out := 1 - when obs regexp "!!9082=(1287|1594|9068|9504|1285)!!" then @transfer_out := 1 - when t1.next_clinical_location_id != t1.location_id then @transfer_out := 1 - else @transfer_out := t2.transfer_out - end as transfer_out, - - case - when obs regexp "!!1285=(1287|9068|2050)!!" and t1.next_clinical_datetime_hiv is null then @transfer_out_location_id := 9999 - when obs regexp "!!1285=1286!!" and t1.next_clinical_datetime_hiv is null then @transfer_out_location_id := 9998 - when t1.next_clinical_location_id != t1.location_id then @transfer_out_location_id := t1.next_clinical_location_id - else @transfer_out_location_id := t2.transfer_out_location_id - end transfer_out_location_id, - - - case - when @transfer_out and t1.next_clinical_datetime_hiv is null then @transfer_out_date := date(t1.cur_rtc_date) - when t1.next_clinical_location_id != t1.location_id then @transfer_out_date := date(t1.next_clinical_datetime_hiv) - when t1.transfer_transfer_out_bncd then @transfer_out_date := date(t1.transfer_transfer_out_bncd) - else @transfer_out_date := t2.transfer_out_date - end transfer_out_date - - - - from flat_hiv_summary_3 t1 - left outer join prev_rows t2 using (person_id) - order by person_id, date(t1.encounter_datetime), encounter_type_sort_index - ); - - - select count(*) into @new_encounter_rows from flat_hiv_summary_4; - - select @new_encounter_rows; - set @total_rows_written = @total_rows_written + @new_encounter_rows; - select @total_rows_written; - - -#select encounter_id, encounter_datetime from flat_hiv_summary_4; - - SET @dyn_sql=CONCAT('replace into ',@write_table, - '(select - null, - person_id, - t1.uuid, - visit_id, - encounter_id, - encounter_datetime, - encounter_type, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - visit_num, - enrollment_date, - enrollment_location_id, - hiv_start_date, - death_date, - scheduled_visit, - transfer_in, - transfer_in_location_id, - transfer_in_date, - transfer_out, - transfer_out_location_id, - transfer_out_date, - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date as rtc_date, - arv_first_regimen, - arv_first_regimen_location_id, - arv_first_regimen_start_date, - arv_first_regimen_start_date_flex, - prev_arv_meds, - cur_arv_meds, - cur_arv_meds_strict, - arv_start_date, - arv_start_location_id, - prev_arv_start_date, - prev_arv_end_date, - prev_arv_line, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - is_pregnant, - edd, - tb_screen, - tb_screening_result, - tb_screening_datetime, - on_ipt, - ipt_start_date, - ipt_stop_date, - ipt_completion_date, - on_tb_tx, - tb_tx_start_date, - tb_tx_end_date, - pcp_prophylaxis_start_date, - condoms_provided_date, - modern_contraceptive_method_start_date, - contraceptive_method, - cur_who_stage, - discordant_status, - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - vl_order_date, - cd4_order_date, - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_location_id, - next_clinical_location_id, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv, - outreach_date_bncd, - outreach_death_date_bncd, - outreach_patient_care_status_bncd, - transfer_date_bncd, - transfer_transfer_out_bncd - from flat_hiv_summary_4 t1 - left outer join amrs.location t2 using (location_id))'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join flat_hiv_summary_build_queue__0 t2 using (person_id);'); - - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - - select @person_ids_count as 'persons remaining', @cycle_length as 'Cycle time (s)', ceil(@person_ids_count / cycle_size) as remaining_cycles, @remaining_time as 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - end if; - - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - - select - count(*) as t, - count(if(min_datetime = "1800-01-01",1,null)) as total_rebuild - from prev_rows; - - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_hiv_summary_v213.sql b/etl-scripts/stored-procedures/generate_hiv_summary_v213.sql deleted file mode 100644 index 2ce0417..0000000 --- a/etl-scripts/stored-procedures/generate_hiv_summary_v213.sql +++ /dev/null @@ -1,1315 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_hiv_summary_v213`() -BEGIN - select @_query_type := "sync"; - select @_start := now(); - select @_start := now(); - select @_table_version := "flat_hiv_summary_v2.13"; - - set session sort_buffer_size=512000000; - - select @_sep := " ## "; - select @_lab_encounter_type := 99999; - select @_death_encounter_type := 31; - select @_last_date_created := (select max(max_date_created) from etl.flat_obs); - - - - create table if not exists flat_hiv_summary_v213 ( - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - is_clinical_encounter int, - location_id int, - location_uuid varchar(100), - visit_num int, - enrollment_date datetime, - hiv_start_date datetime, - death_date datetime, - scheduled_visit int, - transfer_out int, - transfer_in int, - - patient_care_status int, - out_of_care int, - prev_rtc_date datetime, - rtc_date datetime, - - arv_start_location int, - arv_first_regimen_start_date datetime, - arv_start_date datetime, - prev_arv_start_date datetime, - prev_arv_end_date datetime, - - arv_first_regimen varchar(500), - prev_arv_meds varchar(500), - cur_arv_meds varchar(500), - prev_arv_line int, - cur_arv_line int, - - - prev_arv_adherence varchar(200), - cur_arv_adherence varchar(200), - hiv_status_disclosed int, - - first_evidence_patient_pregnant datetime, - edd datetime, - screened_for_tb boolean, - tb_screening_result boolean, - tb_prophylaxis_start_date datetime, - tb_prophylaxis_end_date datetime, - tb_tx_start_date datetime, - tb_tx_end_date datetime, - pcp_prophylaxis_start_date datetime, - cd4_resulted double, - cd4_resulted_date datetime, - cd4_1 double, - cd4_1_date datetime, - cd4_2 double, - cd4_2_date datetime, - cd4_percent_1 double, - cd4_percent_1_date datetime, - cd4_percent_2 double, - cd4_percent_2_date datetime, - vl_resulted int, - vl_resulted_date datetime, - vl_1 int, - vl_1_date datetime, - vl_2 int, - vl_2_date datetime, - vl_order_date datetime, - cd4_order_date datetime, - - - hiv_dna_pcr_order_date datetime, - hiv_dna_pcr_resulted int, - hiv_dna_pcr_resulted_date datetime, - hiv_dna_pcr_1 int, - hiv_dna_pcr_1_date datetime, - hiv_dna_pcr_2 int, - hiv_dna_pcr_2_date datetime, - - - hiv_rapid_test_resulted int, - hiv_rapid_test_resulted_date datetime, - - condoms_provided int, - using_modern_contraceptive_method int, - - cur_who_stage int, - prev_encounter_datetime_hiv datetime, - next_encounter_datetime_hiv datetime, - prev_encounter_type_hiv mediumint, - next_encounter_type_hiv mediumint, - prev_clinical_datetime_hiv datetime, - next_clinical_datetime_hiv datetime, - prev_clinical_rtc_date_hiv datetime, - next_clinical_rtc_date_hiv datetime, - - - outreach_date_bncd datetime, - outreach_death_date_bncd datetime, - outreach_patient_care_status_bncd int, - transfer_date_bncd datetime, - transfer_transfer_out_bncd int, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index location_rtc (location_uuid,rtc_date), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index loc_id_enc_date_next_clinical (location_id, encounter_datetime, next_clinical_datetime_hiv), - index encounter_type (encounter_type) - ); - - select @_last_update := (select max(date_updated) from etl.flat_log where table_name=@_table_version); - - - select @_last_update := - if(@_last_update is null, - (select max(date_created) from amrs.encounter e join etl.flat_hiv_summary_v213 using (encounter_id)), - @_last_update); - - - select @_last_update := if(@_last_update,@_last_update,'1900-01-01'); - - - - - create table if not exists flat_hiv_summary_v213_queue(person_id int, primary key (person_id)); - - - - select @_num_ids := (select count(*) from flat_hiv_summary_v213_queue limit 1); - - if (@_num_ids=0 or @_query_type="sync") then - - replace into flat_hiv_summary_v213_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @_last_update - ); - - - replace into flat_hiv_summary_v213_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @_last_update - - - ); - - replace into flat_hiv_summary_v213_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @_last_update - ); - - replace into flat_hiv_summary_v213_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @_last_update - ); - end if; - - select @_person_ids_count := (select count(*) from flat_hiv_summary_v213_queue); - - delete t1 from flat_hiv_summary_v213 t1 join flat_hiv_summary_v213_queue t2 using (person_id); - - while @_person_ids_count > 0 do - - - drop table if exists flat_hiv_summary_v213_queue_0; - - create temporary table flat_hiv_summary_v213_queue_0 (select * from flat_hiv_summary_v213_queue limit 5000); - - - select @_person_ids_count := (select count(*) from flat_hiv_summary_v213_queue); - - drop table if exists flat_hiv_summary_v213_0a; - create temporary table flat_hiv_summary_v213_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes, - - 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,115,117,120,127,128,129) then 1 - else null - end as is_clinical_encounter, - - case - when t1.encounter_type in (116) then 20 - when t1.encounter_type in (1,2,3,4,10,14,15,17,19,26,32,33,34,47,105,106,112,113,114,115,117,120,127,128,129) then 10 - else 1 - end as encounter_type_sort_index, - - t2.orders - from etl.flat_obs t1 - join flat_hiv_summary_v213_queue_0 t0 using (person_id) - left join etl.flat_orders t2 using(encounter_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,115,116,117,120,127,128,129) - ); - - insert into flat_hiv_summary_v213_0a - (select - t1.person_id, - null, - t1.encounter_id, - t1.test_datetime, - t1.encounter_type, - null, - t1.obs, - null, - - 0 as is_clinical_encounter, - 1 as encounter_type_sort_index, - null - from etl.flat_lab_obs t1 - join flat_hiv_summary_v213_queue_0 t0 using (person_id) - ); - - drop table if exists flat_hiv_summary_v213_0; - create temporary table flat_hiv_summary_v213_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_hiv_summary_v213_0a - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - - select @_prev_id := null; - select @_cur_id := null; - select @_enrollment_date := null; - select @_hiv_start_date := null; - select @_cur_location := null; - select @_cur_rtc_date := null; - select @_prev_rtc_date := null; - select @_hiv_start_date := null; - select @_prev_arv_start_date := null; - select @_arv_start_date := null; - select @_prev_arv_end_date := null; - select @_arv_start_location := null; - select @_art_first_regimen_start_date := null; - select @_arv_first_regimen := null; - select @_prev_arv_line := null; - select @_cur_arv_line := null; - select @_prev_arv_adherence := null; - select @_cur_arv_adherence := null; - select @_hiv_status_disclosed := null; - select @_first_evidence_pt_pregnant := null; - select @_edd := null; - select @_prev_arv_meds := null; - select @_cur_arv_meds := null; - select @_tb_prophylaxis_start_date := null; - select @_tb_prophylaxis_end_date := null; - select @_tb_treatment_start_date := null; - select @_tb_treatment_end_date := null; - select @_pcp_prophylaxis_start_date := null; - select @_screened_for_tb := null; - select @_tb_screening_result := null; - select @_death_date := null; - select @_vl_1:=null; - select @_vl_2:=null; - select @_vl_1_date:=null; - select @_vl_2_date:=null; - select @_vl_resulted:=null; - select @_vl_resulted_date:=null; - - select @_cd4_resulted:=null; - select @_cd4_resulted_date:=null; - select @_cd4_1:=null; - select @_cd4_1_date:=null; - select @_cd4_2:=null; - select @_cd4_2_date:=null; - select @_cd4_percent_1:=null; - select @_cd4_percent_1_date:=null; - select @_cd4_percent_2:=null; - select @_cd4_percent_2_date:=null; - select @_vl_order_date := null; - select @_cd4_order_date := null; - - select @_hiv_dna_pcr_order_date := null; - select @_hiv_dna_pcr_1:=null; - select @_hiv_dna_pcr_2:=null; - select @_hiv_dna_pcr_1_date:=null; - select @_hiv_dna_pcr_2_date:=null; - - select @_hiv_rapid_test_resulted:=null; - select @_hiv_rapid_test_resulted_date:= null; - - select @_patient_care_status:=null; - - select @_condoms_provided := null; - select @_using_modern_contraceptive_method := null; - - - select @_cur_who_stage := null; - - - - - - drop temporary table if exists flat_hiv_summary_v213_1; - create temporary table flat_hiv_summary_v213_1 (index encounter_id (encounter_id)) - (select - encounter_type_sort_index, - @_prev_id := @_cur_id as prev_id, - @_cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.is_clinical_encounter, - - case - when @_prev_id != @_cur_id and t1.encounter_type in (21,@_lab_encounter_type) then @_enrollment_date := null - when @_prev_id != @_cur_id then @_enrollment_date := encounter_datetime - when t1.encounter_type not in (21,@_lab_encounter_type) and @_enrollment_date is null then @_enrollment_date := encounter_datetime - else @_enrollment_date - end as enrollment_date, - - - - - - if(obs regexp "!!1839=" - ,replace(replace((substring_index(substring(obs,locate("!!1839=",obs)),@_sep,1)),"!!1839=",""),"!!","") - ,null) as scheduled_visit, - - case - when location_id then @_cur_location := location_id - when @_prev_id = @_cur_id then @_cur_location - else null - end as 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 - end as visit_num, - - case - when @_prev_id=@_cur_id then @_prev_rtc_date := @_cur_rtc_date - else @_prev_rtc_date := null - end as prev_rtc_date, - - - case - when obs regexp "!!5096=" then @_cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@_sep,1)),"!!5096=",""),"!!","") - when @_prev_id = @_cur_id then if(@_cur_rtc_date > encounter_datetime,@_cur_rtc_date,null) - else @_cur_rtc_date := null - end as cur_rtc_date, - - - case - when obs regexp "!!7015=" then @_transfer_in := replace(replace((substring_index(substring(obs,locate("!!7015=",obs)),@_sep,1)),"!!7015=",""),"!!","") - else @_transfer_in := null - end as transfer_in, - - - - - - case - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=1594!!" then 1 - when obs regexp "!!9082=(1287|1594|9068|9504|1285)!!" then 1 - else null - end as transfer_out, - - - case - when obs regexp "!!1946=1065!!" then 1 - when obs regexp "!!1285=(1287|9068)!!" then 1 - when obs regexp "!!1596=" then 1 - when obs regexp "!!9082=(159|9036|9083|1287|9068|9079|9504|1285)!!" then 1 - when t1.encounter_type = @_death_encounter_type then 1 - else null - end as out_of_care, - - - - - - case - when obs regexp "!!1946=1065!!" then @_patient_care_status := 9036 - when obs regexp "!!1285=" then @_patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1285=",obs)),@_sep,1)),"!!1285=",""),"!!","") - when obs regexp "!!1596=" then @_patient_care_status := replace(replace((substring_index(substring(obs,locate("!!1596=",obs)),@_sep,1)),"!!1596=",""),"!!","") - when obs regexp "!!9082=" then @_patient_care_status := replace(replace((substring_index(substring(obs,locate("!!9082=",obs)),@_sep,1)),"!!9082=",""),"!!","") - - when t1.encounter_type = @_death_encounter_type then @_patient_care_status := 159 - when t1.encounter_type = @_lab_encounter_type and @_cur_id != @_prev_id then @_patient_care_status := null - when t1.encounter_type = @_lab_encounter_type and @_cur_id = @_prev_id then @_patient_care_status - else @_patient_care_status := 6101 - end as patient_care_status, - - - - - - - - case - when obs regexp "!!1946=1065!!" then @_hiv_start_date := null - when t1.encounter_type=@_lab_encounter_type and obs regexp "!!(1040|1030)=664!!" then @_hiv_start_date:=null - when @_prev_id != @_cur_id or @_hiv_start_date is null then - case - when obs regexp "!!(1040|1030)=664!!" then @_hiv_start_date := date(encounter_datetime) - when obs regexp "!!(1088|1255)=" then @_hiv_start_date := date(t1.encounter_datetime) - else @_hiv_start_date := null - end - else @_hiv_start_date - end as hiv_start_date, - - case - when obs regexp "!!1255=1256!!" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @_arv_start_date is null ) then @_arv_start_location := location_id - when @_prev_id = @_cur_id and obs regexp "!!(1250|1088|2154)=" and @_arv_start_date is null then @_arv_start_location := location_id - when @_prev_id != @_cur_id then @_arv_start_location := null - else @_arv_start_location - end as arv_start_location, - - case - when @_prev_id=@_cur_id then @_prev_arv_meds := @_cur_arv_meds - else @_prev_arv_meds := null - end as prev_arv_meds, - - - - - case - when obs regexp "!!1255=(1107|1260)!!" then @_cur_arv_meds := null - when obs regexp "!!1250=" then @_cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!1250=",obs)),@_sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1250=", "") ) ) / LENGTH("!!1250=") ))),"!!1250=",""),"!!","") - when obs regexp "!!1088=" then @_cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!1088=",obs)),@_sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!1088=", "") ) ) / LENGTH("!!1088=") ))),"!!1088=",""),"!!","") - when obs regexp "!!2154=" then @_cur_arv_meds := - replace(replace((substring_index(substring(obs,locate("!!2154=",obs)),@_sep,ROUND ((LENGTH(obs) - LENGTH( REPLACE ( obs, "!!2154=", "") ) ) / LENGTH("!!2154=") ))),"!!2154=",""),"!!","") - when @_prev_id=@_cur_id then @_cur_arv_meds - else @_cur_arv_meds:= null - end as cur_arv_meds, - - - case - when @_arv_first_regimen is null and @_cur_arv_meds is not null then @_arv_first_regimen := @_cur_arv_meds - when @_prev_id = @_cur_id then @_arv_first_regimen - else @_arv_first_regimen := null - end as arv_first_regimen, - - case - when @_arv_first_regimen_start_date is null and (obs regexp "!!1255=(1256|1259|1850)" or obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!") then @_arv_first_regimen_start_date := date(t1.encounter_datetime) - when @_prev_id != @_cur_id then @_arv_first_regimen_start_date := null - else @_arv_first_regimen_start_date - end as arv_first_regimen_start_date, - - case - when @_prev_id=@_cur_id then @_prev_arv_line := @_cur_arv_line - else @_prev_arv_line := null - end as prev_arv_line, - - case - when obs regexp "!!1255=(1107|1260)!!" then @_cur_arv_line := null - when obs regexp "!!1250=(6467|6964|792|633|631)!!" then @_cur_arv_line := 1 - when obs regexp "!!1250=(794|635|6160|6159)!!" then @_cur_arv_line := 2 - when obs regexp "!!1250=6156!!" then @_cur_arv_line := 3 - when obs regexp "!!1088=(6467|6964|792|633|631)!!" then @_cur_arv_line := 1 - when obs regexp "!!1088=(794|635|6160|6159)!!" then @_cur_arv_line := 2 - when obs regexp "!!1088=6156!!" then @_cur_arv_line := 3 - when obs regexp "!!2154=(6467|6964|792|633|631)!!" then @_cur_arv_line := 1 - when obs regexp "!!2154=(794|635|6160|6159)!!" then @_cur_arv_line := 2 - when obs regexp "!!2154=6156!!" then @_cur_arv_line := 3 - when @_prev_id = @_cur_id then @_cur_arv_line - else @_cur_arv_line := null - end as cur_arv_line, - - case - when @_prev_id=@_cur_id then @_prev_arv_start_date := @_arv_start_date - else @_prev_arv_start_date := null - end as prev_arv_start_date, - - - - - - - - case - when obs regexp "!!1255=(1256|1259|1850)" or (obs regexp "!!1255=(1257|1259|981|1258|1849|1850)!!" and @_arv_start_date is null ) then @_arv_start_date := date(t1.encounter_datetime) - when obs regexp "!!1255=(1107|1260)!!" then @_arv_start_date := null - when @_cur_arv_meds != @_prev_arv_meds and @_cur_arv_line != @_prev_arv_line then @_arv_start_date := date(t1.encounter_datetime) - when @_prev_id != @_cur_id then @_arv_start_date := null - else @_arv_start_date - end as arv_start_date, - - case - when @_prev_arv_start_date != @_arv_start_date then @_prev_arv_end_date := date(t1.encounter_datetime) - else @_prev_arv_end_date - end as prev_arv_end_date, - - case - when @_prev_id=@_cur_id then @_prev_arv_adherence := @_cur_arv_adherence - else @_prev_arv_adherence := null - end as prev_arv_adherence, - - - - - - case - when obs regexp "!!8288=6343!!" then @_cur_arv_adherence := 'GOOD' - when obs regexp "!!8288=6655!!" then @_cur_arv_adherence := 'FAIR' - when obs regexp "!!8288=6656!!" then @_cur_arv_adherence := 'POOR' - when @_prev_id = @_cur_id then @_cur_arv_adherence - else @_cur_arv_adherence := null - end as cur_arv_adherence, - - case - when obs regexp "!!6596=(6594|1267|6595)!!" then @_hiv_status_disclosed := 1 - when obs regexp "!!6596=1118!!" then 0 - when obs regexp "!!6596=" then @_hiv_status_disclosed := null - when @_prev_id != @_cur_id then @_hiv_status_disclosed := null - else @_hiv_status_disclosed - end as hiv_status_disclosed, - - - - - - - case - when @_prev_id != @_cur_id then - case - when t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=" then @_first_evidence_pt_pregnant := encounter_datetime - else @_first_evidence_pt_pregnant := null - end - when @_first_evidence_pt_pregnant is null and (t1.encounter_type in (32,33,44,10) or obs regexp "!!(1279|5596)=") then @_first_evidence_pt_pregnant := encounter_datetime - when @_first_evidence_pt_pregnant and (t1.encounter_type in (11,47,34) or timestampdiff(week,@_first_evidence_pt_pregnant,encounter_datetime) > 40 or timestampdiff(week,@_edd,encounter_datetime) > 40 or obs regexp "!!5599=|!!1156=1065!!") then @_first_evidence_pt_pregnant := null - else @_first_evidence_pt_pregnant - end as first_evidence_patient_pregnant, - - - - - - - - case - when @_prev_id != @_cur_id then - case - when @_first_evidence_patient_pregnant and obs regexp "!!1836=" then @_edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@_sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @_edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@_sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @_edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@_sep,1)),"!!5596=",""),"!!","") - when @_first_evidence_pt_pregnant then @_edd := date_add(@_first_evidence_pt_pregnant,interval 6 month) - else @_edd := null - end - when @_edd is null or @_edd = @_first_evidence_pt_pregnant then - case - when @_first_evidence_pt_pregnant then @_edd := date_add(@_first_evidence_pt_pregnant,interval 6 month) - when @_first_evidence_patient_pregnant and obs regexp "!!1836=" then @_edd := - date_add(replace(replace((substring_index(substring(obs,locate("!!1836=",obs)),@_sep,1)),"!!1836=",""),"!!",""),interval 280 day) - when obs regexp "!!1279=" then @_edd := - date_add(encounter_datetime,interval (40-replace(replace((substring_index(substring(obs,locate("!!1279=",obs)),@_sep,1)),"!!1279=",""),"!!","")) week) - when obs regexp "!!5596=" then @_edd := - replace(replace((substring_index(substring(obs,locate("!!5596=",obs)),@_sep,1)),"!!5596=",""),"!!","") - when @_first_evidence_pt_pregnant then @_edd := date_add(@_first_evidence_pt_pregnant,interval 6 month) - else @_edd - end - when @_edd and (t1.encounter_type in (11,47,34) or timestampdiff(week,@_edd,encounter_datetime) > 4 or obs regexp "!!5599|!!1145=1065!!") then @_edd := null - else @_edd - end as edd, - - - - - - - - - case - when obs regexp "!!6174=" then @_screened_for_tb := true - when obs regexp "!!2022=1065!!" then @_screened_for_tb := true - when obs regexp "!!307=" then @_screened_for_tb := true - when obs regexp "!!12=" then @_screened_for_tb := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @_screened_for_tb := true - when orders regexp "(12|307|8064|2311|2323)" then @_screened_for_tb := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @_screened_for_tb := true - when obs regexp "!!5958=1077!!" then @_screened_for_tb := true - when obs regexp "!!2020=1065!!" then @_screened_for_tb := true - when obs regexp "!!2021=1065!!" then @_screened_for_tb := true - when obs regexp "!!2028=" then @_screened_for_tb := true - when obs regexp "!!1268=(1256|1850)!!" then @_screened_for_tb := true - when obs regexp "!!5959=(1073|1074)!!" then @_screened_for_tb := true - when obs regexp "!!5971=(1073|1074)!!" then @_screened_for_tb := true - when obs regexp "!!1492=107!!" then @_screened_for_tb := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @_screened_for_tb := true - end as screened_for_tb, - - - - - - - - - case - - when obs regexp "!!2022=1065!!" then @_tb_screening_result := true - when obs regexp "!!307=" then @_tb_screening_result := true - when obs regexp "!!12=" then @_tb_screening_result := true - when obs regexp "!!1271=(12|307|8064|2311|2323)!!" then @_tb_screening_result := true - when orders regexp "(12|307|8064|2311|2323)" then @_tb_screening_result := true - when obs regexp "!!1866=(12|307|8064|2311|2323)!!" then @_tb_screening_result := true - when obs regexp "!!5958=1077!!" then @_tb_screening_result := true - when obs regexp "!!2020=1065!!" then @_tb_screening_result := true - when obs regexp "!!2021=1065!!" then @_tb_screening_result := true - when obs regexp "!!2028=" then @_tb_screening_result := true - when obs regexp "!!1268=(1256|1850)!!" then @_tb_screening_result := true - when obs regexp "!!5959=(1073|1074)!!" then @_tb_screening_result := true - when obs regexp "!!5971=(1073|1074)!!" then @_tb_screening_result := true - when obs regexp "!!1492=107!!" then @_tb_screening_result := true - when obs regexp "!!1270=" and obs not regexp "!!1268=1257!!" then @_tb_screening_result := true - when obs not regexp "!!6174=1107" then @_tb_screening_result := true - else @_tb_screening_result := false - end as tb_screening_result, - - case - when obs regexp "!!1265=(1256|1257|1850)!!" then @_on_tb_prophylaxis := 1 - when obs regexp "!!1110=656!!" then @_on_tb_prophylaxis := 1 - when @_prev_id = @_cur_id then @_on_tb_prophylaxis - else null - end as on_tb_prophylaxis, - - - - - - case - when @_cur_id != @_prev_id then - case - when obs regexp "!!1265=(1256|1850)!!" then @_tb_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1265=(1257|981|1406|1849)!!" then @_tb_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1110=656!!" then @_tb_prophylaxis_start_date := encounter_datetime - else @_tb_prophylaxis_start_date := null - end - when @_cur_id = @_prev_id then - case - when obs regexp "!!1265=(1256|1850)!!" then @_tb_prophylaxis_start_date := encounter_datetime - when @_tb_prophylaxis_start_date is not null then @_tb_prophylaxis_start_date - when obs regexp "!!1265=(1257|981|1406|1849)!!" then @_tb_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1110=656!!" then @_tb_prophylaxis_start_date := encounter_datetime - end - end as tb_prophylaxis_start_date, - - - - - - case - when @_cur_id != @_prev_id then - case - when obs regexp "!!1265=1260!!" then @_tb_prophylaxis_end_date := encounter_datetime - else @_tb_prophylaxis_end_date := null - end - when @_cur_id = @_prev_id then - case - when obs regexp "!!1265=1260!!" then @_tb_prophylaxis_end_date := encounter_datetime - when @_tb_prophylaxis_end_date is not null then @_tb_prophylaxis_end_date - when @_tb_prophylaxis_start_date is not null and obs regexp "!!1110=1107!!" and obs regexp "!!1265=" and obs regexp "!!1265=(1107|1260)!!" then @_tb_prophylaxis_end_date := encounter_datetime - end - end as tb_prophylaxis_end_date, - - - - - - case - when @_cur_id != @_prev_id then - case - when obs regexp "!!1113=" then @_tb_treatment_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@_sep,1)),"!!1113=",""),"!!","")) - when obs regexp "!!1268=1256!!" then @_tb_treatment_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" then @_tb_treatment_start_date := encounter_datetime - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @_tb_treatment_start_date := encounter_datetime - else @_tb_treatment_start_date := null - end - when @_cur_id = @_prev_id then - case - when obs regexp "!!1113=" then @_tb_treatment_start_date := date(replace(replace((substring_index(substring(obs,locate("!!1113=",obs)),@_sep,1)),"!!1113=",""),"!!","")) - when @_tb_treatment_start_date is not null then @_tb_treatment_start_date - when obs regexp "!!1268=1256!!" then @_tb_treatment_start_date := encounter_datetime - when obs regexp "!!1268=(1257|1259|1849|981)!!" then @_tb_treatment_start_date := encounter_datetime - when obs regexp "!!1111=" and obs not regexp "!!1111=(1267|1107)!!" then @_tb_treatment_start_date := encounter_datetime - end - end as tb_tx_start_date, - - - - - case - when @_cur_id != @_prev_id then - case - when obs regexp "!!2041=" then @_tb_treatment_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@_sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @_tb_treatment_end_date := encounter_datetime - else @_tb_treatment_end_date := null - end - when @_cur_id = @_prev_id then - case - when obs regexp "!!2041=" then @_tb_treatment_end_date := date(replace(replace((substring_index(substring(obs,locate("!!2041=",obs)),@_sep,1)),"!!2041=",""),"!!","")) - when obs regexp "!!1268=1260!!" then @_tb_treatment_end_date := encounter_datetime - when @_tb_treatment_end_date is not null then @_tb_treatment_end_date - when @_tb_treatment_start_date is not null and obs regexp "!!6176=1066!!" and !(obs regexp "!!1268=(1256|1257|1259|1849|981)!!") then @_tb_treatment_end_date := encounter_datetime - end - end as tb_tx_end_date, - - - - - case - when obs regexp "!!1261=(1107|1260)!!" then @_pcp_prophylaxis_start_date := null - when obs regexp "!!1261=(1256|1850)!!" then @_pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1261=1257!!" then - case - when @_prev_id!=@_cur_id or @_pcp_prophylaxis_start_date is null then @_pcp_prophylaxis_start_date := encounter_datetime - else @_pcp_prophylaxis_start_date - end - when obs regexp "!!1109=(916|92)!!" and @_pcp_prophylaxis_start_date is null then @_pcp_prophylaxis_start_date := encounter_datetime - when obs regexp "!!1193=916!!" and @_pcp_prophylaxis_start_date is null then @_pcp_prophylaxis_start_date := encounter_datetime - when @_prev_id=@_cur_id then @_pcp_prophylaxis_start_date - else @_pcp_prophylaxis_start_date := null - end as pcp_prophylaxis_start_date, - - - - - - - - - case - when p.dead or p.death_date then @_death_date := p.death_date - when obs regexp "!!1570=" then @_death_date := replace(replace((substring_index(substring(obs,locate("!!1570=",obs)),@_sep,1)),"!!1570=",""),"!!","") - when @_prev_id != @_cur_id or @_death_date is null then - case - when obs regexp "!!(1734|1573)=" then @_death_date := encounter_datetime - when obs regexp "!!(1733|9082|6206)=159!!" or t1.encounter_type=31 then @_death_date := encounter_datetime - else @_death_date := null - end - else @_death_date - end as death_date, - - - case - when @_prev_id=@_cur_id then - case - when t1.encounter_type = @_lab_encounter_type and obs regexp "!!5497=[0-9]" and @_cd4_1 >= 0 and date(encounter_datetime)<>@_cd4_1_date then @_cd4_2:= @_cd4_1 - else @_cd4_2 - end - else @_cd4_2:=null - end as cd4_2, - - case - when @_prev_id=@_cur_id then - case - when t1.encounter_type=@_lab_encounter_type and obs regexp "!!5497=[0-9]" and @_cd4_1 >= 0 then @_cd4_2_date:= @_cd4_1_date - else @_cd4_2_date - end - else @_cd4_2_date:=null - end as cd4_2_date, - - case - when t1.encounter_type = @_lab_encounter_type and obs regexp "!!5497=[0-9]" then @_cd4_date_resulted := date(encounter_datetime) - when @_prev_id = @_cur_id and date(encounter_datetime) = @_cd4_date_resulted then @_cd4_date_resulted - end as cd4_resulted_date, - - case - when t1.encounter_type = @_lab_encounter_type and obs regexp "!!5497=[0-9]" then @_cd4_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@_sep,1)),"!!5497=",""),"!!","") as unsigned) - when @_prev_id = @_cur_id and date(encounter_datetime) = @_cd4_date_resulted then @_cd4_resulted - end as cd4_resulted, - - - - case - when t1.encounter_type = @_lab_encounter_type and obs regexp "!!5497=[0-9]" then @_cd4_1:= cast(replace(replace((substring_index(substring(obs,locate("!!5497=",obs)),@_sep,1)),"!!5497=",""),"!!","") as unsigned) - when @_prev_id=@_cur_id then @_cd4_1 - else @_cd4_1:=null - end as cd4_1, - - - case - when t1.encounter_type = @_lab_encounter_type and obs regexp "!!5497=[0-9]" then @_cd4_1_date:=date(encounter_datetime) - when @_prev_id=@_cur_id then @_cd4_1_date - else @_cd4_1_date:=null - end as cd4_1_date, - - - case - when @_prev_id=@_cur_id then - case - when t1.encounter_type=@_lab_encounter_type and obs regexp "!!730=[0-9]" and @_cd4_percent_1 >= 0 - then @_cd4_percent_2:= @_cd4_percent_1 - else @_cd4_percent_2 - end - else @_cd4_percent_2:=null - end as cd4_percent_2, - - case - when @_prev_id=@_cur_id then - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @_lab_encounter_type and @_cd4_percent_1 >= 0 then @_cd4_percent_2_date:= @_cd4_percent_1_date - else @_cd4_percent_2_date - end - else @_cd4_percent_2_date:=null - end as cd4_percent_2_date, - - - case - when t1.encounter_type = @_lab_encounter_type and obs regexp "!!730=[0-9]" - then @_cd4_percent_1:= cast(replace(replace((substring_index(substring(obs,locate("!!730=",obs)),@_sep,1)),"!!730=",""),"!!","") as unsigned) - when @_prev_id=@_cur_id then @_cd4_percent_1 - else @_cd4_percent_1:=null - end as cd4_percent_1, - - case - when obs regexp "!!730=[0-9]" and t1.encounter_type = @_lab_encounter_type then @_cd4_percent_1_date:=date(encounter_datetime) - when @_prev_id=@_cur_id then @_cd4_percent_1_date - else @_cd4_percent_1_date:=null - end as cd4_percent_1_date, - - - - case - when @_prev_id=@_cur_id then - case - when obs regexp "!!856=[0-9]" and @_vl_1 >= 0 - and (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@_sep,1)),"!!856=",""),"!!","")) <>date(@_vl_1_date) then @_vl_2:= @_vl_1 - else @_vl_2 - end - else @_vl_2:=null - end as vl_2, - - case - when @_prev_id=@_cur_id then - case - when obs regexp "!!856=[0-9]" and @_vl_1 >= 0 - and (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@_sep,1)),"!!856=",""),"!!","")) <>date(@_vl_1_date) then @_vl_2_date:= @_vl_1_date - else @_vl_2_date - end - else @_vl_2_date:=null - end as vl_2_date, - - case - when t1.encounter_type = @_lab_encounter_type and obs regexp "!!856=[0-9]" then @_vl_date_resulted := date(encounter_datetime) - when @_prev_id = @_cur_id and date(encounter_datetime) = @_vl_date_resulted then @_vl_date_resulted - end as vl_resulted_date, - - case - when t1.encounter_type = @_lab_encounter_type and obs regexp "!!856=[0-9]" then @_vl_resulted := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@_sep,1)),"!!856=",""),"!!","") as unsigned) - when @_prev_id = @_cur_id and date(encounter_datetime) = @_vl_date_resulted then @_vl_resulted - end as vl_resulted, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @_lab_encounter_type then @_vl_1:=cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@_sep,1)),"!!856=",""),"!!","") as unsigned) - when obs regexp "!!856=[0-9]" - and (@_vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@_sep,1)),"!!856=",""),"!!",""),@_vl_1_date)) > 30) - and (@_vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@_sep,1)),"!!856=",""),"!!","")) > @_vl_1_date) - then @_vl_1 := cast(replace(replace((substring_index(substring(obs,locate("!!856=",obs)),@_sep,1)),"!!856=",""),"!!","") as unsigned) - when @_prev_id=@_cur_id then @_vl_1 - else @_vl_1:=null - end as vl_1, - - case - when obs regexp "!!856=[0-9]" and t1.encounter_type = @_lab_encounter_type then @_vl_1_date:= encounter_datetime - when obs regexp "!!856=[0-9]" - and (@_vl_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@_sep,1)),"!!856=",""),"!!",""),@_vl_1_date)) > 30) - and (@_vl_1_date is null or (replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@_sep,1)),"!!856=",""),"!!","")) > @_vl_1_date) - then @_vl_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!856=",obs_datetimes)),@_sep,1)),"!!856=",""),"!!","") - when @_prev_id=@_cur_id then @_vl_1_date - else @_vl_1_date:=null - end as vl_1_date, - - - - - - case - when obs regexp "!!1271=856!!" then @_vl_order_date := date(encounter_datetime) - when orders regexp "856" then @_vl_order_date := date(encounter_datetime) - when @_prev_id=@_cur_id and (@_vl_1_date is null or @_vl_1_date < @_vl_order_date) then @_vl_order_date - else @_vl_order_date := null - end as vl_order_date, - - - case - when obs regexp "!!1271=657!!" then @_cd4_order_date := date(encounter_datetime) - when orders regexp "657" then @_cd4_order_date := date(encounter_datetime) - when @_prev_id=@_cur_id then @_cd4_order_date - else @_cd4_order_date := null - end as cd4_order_date, - - - case - when obs regexp "!!1271=1030!!" then @_hiv_dna_pcr_order_date := date(encounter_datetime) - when orders regexp "1030" then @_hiv_dna_pcr_order_date := date(encounter_datetime) - when @_prev_id=@_cur_id then @_hiv_dna_pcr_order_date - else @_hiv_dna_pcr_order_date := null - end as hiv_dna_pcr_order_date, - - case - when t1.encounter_type = @_lab_encounter_type and obs regexp "!!1030=[0-9]" then encounter_datetime - when obs regexp "!!1030=[0-9]" - and (@_hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@_sep,1)),"!!1030=",""),"!!",""),@_hiv_dna_pcr_1_date)) > 30) - then replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@_sep,1)),"1030=",""),"!!","") - end as hiv_dna_pcr_resulted_date, - - case - when @_prev_id=@_cur_id then - case - when t1.encounter_type = @_lab_encounter_type and obs regexp "!!1030=[0-9]" and @_hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@_hiv_dna_pcr_1_date then @_hiv_dna_pcr_2:= @_hiv_dna_pcr_1 - when obs regexp "!!1030=[0-9]" and @_hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@_sep,1)),"!!1030=",""),"!!",""),@_hiv_dna_pcr_1_date)) > 30 then @_hiv_dna_pcr_2 := @_hiv_dna_pcr_1 - else @_hiv_dna_pcr_2 - end - else @_hiv_dna_pcr_2:=null - end as hiv_dna_pcr_2, - - case - when @_prev_id=@_cur_id then - case - when t1.encounter_type=@_lab_encounter_type and obs regexp "!!1030=[0-9]" and @_hiv_dna_pcr_1 >= 0 and date(encounter_datetime)<>@_hiv_dna_pcr_1_date then @_hiv_dna_pcr_2_date:= @_hiv_dna_pcr_1_date - when obs regexp "!!1030=[0-9]" and @_hiv_dna_pcr_1 >= 0 - and abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("1030=",obs_datetimes)),@_sep,1)),"1030=",""),"!!",""),@_hiv_dna_pcr_1_date)) > 30 then @_hiv_dna_pcr_2_date:= @_hiv_dna_pcr_1_date - else @_hiv_dna_pcr_2_date - end - else @_hiv_dna_pcr_2_date:=null - end as hiv_dna_pcr_2_date, - - case - when t1.encounter_type = @_lab_encounter_type and obs regexp "!!1030=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@_sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@_hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@_sep,1)),"!!1030=",""),"!!",""),@_hiv_dna_pcr_1_date)) > 30) - then cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@_sep,1)),"!!1030=",""),"!!","") as unsigned) - end as hiv_dna_pcr_resulted, - - case - when t1.encounter_type = @_lab_encounter_type and obs regexp "!!1030=[0-9]" then @_hiv_dna_pcr_1:= cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@_sep,1)),"!!1030=",""),"!!","") as unsigned) - when obs regexp "!!1030=[0-9]" - and (@_hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@_sep,1)),"!!1030=",""),"!!","") ,@_hiv_dna_pcr_1_date)) > 30) - then @_hiv_dna_pcr_1 := cast(replace(replace((substring_index(substring(obs,locate("!!1030=",obs)),@_sep,1)),"!!1030=",""),"!!","") as unsigned) - when @_prev_id=@_cur_id then @_hiv_dna_pcr_1 - else @_hiv_dna_pcr_1:=null - end as hiv_dna_pcr_1, - - - case - when t1.encounter_type = @_lab_encounter_type and obs regexp "!!1030=[0-9]" then @_hiv_dna_pcr_1_date:=date(encounter_datetime) - when obs regexp "!!1030=[0-9]" - and (@_hiv_dna_pcr_1_date is null or abs(datediff(replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@_sep,1)),"!!1030=",""),"!!","") ,@_hiv_dna_pcr_1_date)) > 30) - then @_hiv_dna_pcr_1_date := replace(replace((substring_index(substring(obs_datetimes,locate("!!1030=",obs_datetimes)),@_sep,1)),"!!1030=",""),"!!","") - when @_prev_id=@_cur_id then @_hiv_dna_pcr_1_date - else @_hiv_dna_pcr_1_date:=null - end as hiv_dna_pcr_1_date, - - - case - when t1.encounter_type = @_lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then encounter_datetime - end as hiv_rapid_test_resulted_date, - - case - when t1.encounter_type = @_lab_encounter_type and obs regexp "!!(1040|1042)=[0-9]" then cast(replace(replace((substring_index(substring(obs,locate("!!(1040|1042)=",obs)),@_sep,1)),"!!(1040|1042)=",""),"!!","") as unsigned) - end as hiv_rapid_test_resulted, - - - case - when obs regexp "!!8302=8305!!" then @_condoms_provided := 1 - when obs regexp "!!374=(190|6717|6718)!!" then @_condoms_provided := 1 - when obs regexp "!!6579=" then @_condoms_provided := 1 - else null - end as condoms_provided, - - case - when obs regexp "!!374=(5275|6220|780|5279)!!" then @_using_modern_conctaceptive_method := 1 - else null - end as using_modern_contraceptive_method, - - - - - - - - - - - - - case - when obs regexp "!!5356=(1204)!!" then @_cur_who_stage := 1 - when obs regexp "!!5356=(1205)!!" then @_cur_who_stage := 2 - when obs regexp "!!5356=(1206)!!" then @_cur_who_stage := 3 - when obs regexp "!!5356=(1207)!!" then @_cur_who_stage := 4 - when obs regexp "!!1224=(1220)!!" then @_cur_who_stage := 1 - when obs regexp "!!1224=(1221)!!" then @_cur_who_stage := 2 - when obs regexp "!!1224=(1222)!!" then @_cur_who_stage := 3 - when obs regexp "!!1224=(1223)!!" then @_cur_who_stage := 4 - when @_prev_id = @_cur_id then @_cur_who_stage - else @_cur_who_stage := null - end as cur_who_stage - - from flat_hiv_summary_v213_0 t1 - join amrs.person p using (person_id) - ); - - select @_prev_id := null; - select @_cur_id := null; - select @_prev_encounter_datetime := null; - select @_cur_encounter_datetime := null; - - select @_prev_clinical_datetime := null; - select @_cur_clinical_datetime := null; - - select @_next_encounter_type := null; - select @_cur_encounter_type := null; - - - alter table flat_hiv_summary_v213_1 drop prev_id, drop cur_id; - - drop table if exists flat_hiv_summary_v213_2; - create temporary table flat_hiv_summary_v213_2 - (select *, - @_prev_id := @_cur_id as prev_id, - @_cur_id := person_id as cur_id, - - case - when @_prev_id = @_cur_id then @_prev_encounter_datetime := @_cur_encounter_datetime - else @_prev_encounter_datetime := null - end as next_encounter_datetime_hiv, - - @_cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @_prev_id=@_cur_id then @_next_encounter_type := @_cur_encounter_type - else @_next_encounter_type := null - end as next_encounter_type_hiv, - - @_cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @_prev_id = @_cur_id then @_prev_clinical_datetime := @_cur_clinical_datetime - else @_prev_clinical_datetime := null - end as next_clinical_datetime_hiv, - - case - when is_clinical_encounter then @_cur_clinical_datetime := encounter_datetime - when @_prev_id = @_cur_id then @_cur_clinical_datetime - else @_cur_clinical_datetime := null - end as cur_clinic_datetime, - - case - when @_prev_id = @_cur_id then @_prev_clinical_rtc_date := @_cur_clinical_rtc_date - else @_prev_clinical_rtc_date := null - end as next_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @_cur_clinical_rtc_date := cur_rtc_date - when @_prev_id = @_cur_id then @_cur_clinical_rtc_date - else @_cur_clinical_rtc_date:= null - end as cur_clinical_rtc_date, - - case - when @_prev_id != @_cur_id then null - when is_clinical_encounter then @_outreach_date_bncd - else null - end as outreach_date_bncd, - - case - when encounter_type=21 and @_outreach_date_bncd is null then @_outreach_date_bncd := encounter_datetime - when is_clinical_encounter then @_outreach_date_bncd := null - when @_prev_id != @_cur_id then @_outreach_date_bncd := null - else @_outreach_date_bncd - end as next_outreach_date_bncd, - - case - when @_prev_id != @_cur_id then null - when is_clinical_encounter then @_outreach_death_date_bncd - else null - end as outreach_death_date_bncd, - - case - when encounter_type=21 and @_outreach_death_date_bncd is null then @_outreach_death_date_bncd := death_date - when is_clinical_encounter then @_outreach_death_date_bncd := null - when @_prev_id != @_cur_id then @_outreach_death_date_bncd := null - else @_outreach_death_date_bncd - end as next_outreach_death_date_bncd, - - - case - when @_prev_id != @_cur_id then null - when is_clinical_encounter then cast(@_outreach_patient_care_status_bncd as unsigned) - else null - end as outreach_patient_care_status_bncd, - - case - when encounter_type=21 and @_outreach_patient_care_status_bncd is null then @_outreach_patient_care_status_bncd := patient_care_status - when is_clinical_encounter then @_outreach_patient_care_status_bncd := null - when @_prev_id != @_cur_id then @_outreach_patient_care_status_bncd := null - else @_outreach_patient_care_status_bncd - end as next_outreach_patient_care_status_bncd, - - case - when @_prev_id != @_cur_id then null - when is_clinical_encounter then @_transfer_date_bncd - else null - end as transfer_date_bncd, - - case - when encounter_type=116 and @_transfer_date_bncd is null then @_transfer_date_bncd := encounter_datetime - when is_clinical_encounter then @_transfer_date_bncd := null - when @_prev_id != @_cur_id then @_transfer_date_bncd := null - else @_transfer_date_bncd - end as next_transfer_date_bncd, - - case - when @_prev_id != @_cur_id then null - when is_clinical_encounter then @_transfer_transfer_out_bncd - else null - end as transfer_transfer_out_bncd, - - case - when encounter_type=116 and @_transfer_transfer_out_bncd is null then @_transfer_transfer_out_bncd := encounter_datetime - when is_clinical_encounter then @_transfer_transfer_out_bncd := null - when @_prev_id != @_cur_id then @_transfer_transfer_out_bncd := null - else @_transfer_transfer_out_bncd - end as next_transfer_transfer_out_bncd - - from flat_hiv_summary_v213_1 - order by person_id, date(encounter_datetime) desc, encounter_type_sort_index desc - ); - - alter table flat_hiv_summary_v213_2 drop prev_id, drop cur_id, drop cur_encounter_type, drop cur_encounter_datetime, drop cur_clinical_rtc_date; - - select @_prev_id := null; - select @_cur_id := null; - select @_prev_encounter_type := null; - select @_cur_encounter_type := null; - select @_prev_encounter_datetime := null; - select @_cur_encounter_datetime := null; - select @_prev_clinical_datetime := null; - select @_cur_clinical_datetime := null; - - drop temporary table if exists flat_hiv_summary_v213_3; - create temporary table flat_hiv_summary_v213_3 (prev_encounter_datetime datetime, prev_encounter_type int, index person_enc (person_id, encounter_datetime desc)) - (select - *, - @_prev_id := @_cur_id as prev_id, - @_cur_id := t1.person_id as cur_id, - - case - when @_prev_id=@_cur_id then @_prev_encounter_type := @_cur_encounter_type - else @_prev_encounter_type:=null - end as prev_encounter_type_hiv, @_cur_encounter_type := encounter_type as cur_encounter_type, - - case - when @_prev_id=@_cur_id then @_prev_encounter_datetime := @_cur_encounter_datetime - else @_prev_encounter_datetime := null - end as prev_encounter_datetime_hiv, @_cur_encounter_datetime := encounter_datetime as cur_encounter_datetime, - - case - when @_prev_id = @_cur_id then @_prev_clinical_datetime := @_cur_clinical_datetime - else @_prev_clinical_datetime := null - end as prev_clinical_datetime_hiv, - - case - when is_clinical_encounter then @_cur_clinical_datetime := encounter_datetime - when @_prev_id = @_cur_id then @_cur_clinical_datetime - else @_cur_clinical_datetime := null - end as cur_clinical_datetime, - - case - when @_prev_id = @_cur_id then @_prev_clinical_rtc_date := @_cur_clinical_rtc_date - else @_prev_clinical_rtc_date := null - end as prev_clinical_rtc_date_hiv, - - case - when is_clinical_encounter then @_cur_clinical_rtc_date := cur_rtc_date - when @_prev_id = @_cur_id then @_cur_clinical_rtc_date - else @_cur_clinical_rtc_date:= null - end as cur_clinic_rtc_date - - from flat_hiv_summary_v213_2 t1 - order by person_id, date(encounter_datetime), encounter_type_sort_index - ); - - replace into flat_hiv_summary_v213 - (select - person_id, - t1.uuid, - t1.visit_id, - encounter_id, - encounter_datetime, - encounter_type, - is_clinical_encounter, - location_id, - t2.uuid as location_uuid, - visit_num, - enrollment_date, - hiv_start_date, - death_date, - scheduled_visit, - transfer_out, - transfer_in, - patient_care_status, - out_of_care, - prev_rtc_date, - cur_rtc_date, - arv_start_location, - arv_first_regimen_start_date, - arv_start_date, - prev_arv_start_date, - prev_arv_end_date, - arv_first_regimen, - prev_arv_meds, - cur_arv_meds, - prev_arv_line, - cur_arv_line, - prev_arv_adherence, - cur_arv_adherence, - hiv_status_disclosed, - first_evidence_patient_pregnant, - edd, - screened_for_tb, - tb_screening_result, - tb_prophylaxis_start_date, - tb_prophylaxis_end_date, - tb_tx_start_date, - tb_tx_end_date, - pcp_prophylaxis_start_date, - cd4_resulted, - cd4_resulted_date, - cd4_1, - cd4_1_date, - cd4_2, - cd4_2_date, - cd4_percent_1, - cd4_percent_1_date, - cd4_percent_2, - cd4_percent_2_date, - vl_resulted, - vl_resulted_date, - vl_1, - vl_1_date, - vl_2, - vl_2_date, - vl_order_date, - cd4_order_date, - hiv_dna_pcr_order_date, - hiv_dna_pcr_resulted, - hiv_dna_pcr_resulted_date, - hiv_dna_pcr_1, - hiv_dna_pcr_1_date, - hiv_dna_pcr_2, - hiv_dna_pcr_2_date, - hiv_rapid_test_resulted, - hiv_rapid_test_resulted_date, - condoms_provided, - using_modern_contraceptive_method, - cur_who_stage, - prev_encounter_datetime_hiv, - next_encounter_datetime_hiv, - prev_encounter_type_hiv, - next_encounter_type_hiv, - prev_clinical_datetime_hiv, - next_clinical_datetime_hiv, - prev_clinical_rtc_date_hiv, - next_clinical_rtc_date_hiv, - outreach_date_bncd, - outreach_death_date_bncd, - outreach_patient_care_status_bncd, - transfer_date_bncd, - transfer_transfer_out_bncd - - from flat_hiv_summary_v213_3 t1 - join amrs.location t2 using (location_id)); - - delete from flat_hiv_summary_v213_queue where person_id in (select person_id from flat_hiv_summary_v213_queue_0); - - end while; - - select @_end := now(); - insert into etl.flat_log values (@_start,@_last_date_created,@_table_version,timestampdiff(second,@_start,@_end)); - select concat(@_table_version," : Time to complete: ",timestampdiff(minute, @_start, @_end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_kenya_emr_hiv_summary_v15_12.sql b/etl-scripts/stored-procedures/generate_kenya_emr_hiv_summary.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_kenya_emr_hiv_summary_v15_12.sql rename to etl-scripts/stored-procedures/generate_kenya_emr_hiv_summary.sql index 3bcf446..26fce0b 100644 --- a/etl-scripts/stored-procedures/generate_kenya_emr_hiv_summary_v15_12.sql +++ b/etl-scripts/stored-procedures/generate_kenya_emr_hiv_summary.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_kenya_emr_hiv_summary_v15_12`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) +CREATE PROCEDURE `generate_kenya_emr_hiv_summary`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int) BEGIN set @primary_table := "flat_kenya_emr_hiv_summary_v15b"; set @query_type = query_type; diff --git a/etl-scripts/stored-procedures/generate_pep_summary.sql b/etl-scripts/stored-procedures/generate_pep_summary.sql deleted file mode 100644 index d910fc1..0000000 --- a/etl-scripts/stored-procedures/generate_pep_summary.sql +++ /dev/null @@ -1,205 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_pep_summary`() -BEGIN - - select @start := now(); - select @start := now(); - select @table_version := "flat_pep_summary_v1.0"; - - set session sort_buffer_size=512000000; - - select @sep := " ## "; - select @last_date_created := (select max(max_date_created) from etl.flat_obs); - - #drop table if exists flat_pep_summary; - #delete from flat_log where table_name="flat_pep_summary"; - create table if not exists flat_pep_summary ( - person_id int, - uuid varchar(100), - visit_id int, - encounter_id int, - encounter_datetime datetime, - encounter_type int, - location_id int, - location_uuid varchar(100), - enrollment_date datetime, - prev_rtc_date datetime, - rtc_date datetime, - hiv_exposed_occupational boolean, - pep_start_date date, - - primary key encounter_id (encounter_id), - index person_date (person_id, encounter_datetime), - index location_rtc (location_uuid,rtc_date), - index person_uuid (uuid), - index location_enc_date (location_uuid,encounter_datetime), - index enc_date_location (encounter_datetime, location_uuid), - index location_id_rtc_date (location_id,rtc_date), - index location_uuid_rtc_date (location_uuid,rtc_date), - index encounter_type (encounter_type) - ); - - select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); - - # then use the max_date_created from amrs.encounter. This takes about 10 seconds and is better to avoid. - select @last_update := - if(@last_update is null, - (select max(date_created) from amrs.encounter e join etl.flat_pep_summary using (encounter_id)), - @last_update); - - #otherwise set to a date before any encounters had been created (i.g. we will get all encounters) - select @last_update := if(@last_update,@last_update,'1900-01-01'); - #select @last_update := "2016-09-12"; #date(now()); - #select @last_date_created := "2015-11-17"; #date(now()); - - drop table if exists new_data_person_ids; - create temporary table new_data_person_ids(person_id int, primary key (person_id)); - replace into new_data_person_ids - (select distinct patient_id #, min(encounter_datetime) as start_date - from amrs.encounter - where date_changed > @last_update - ); - - replace into new_data_person_ids - (select distinct person_id #, min(encounter_datetime) as start_date - from etl.flat_obs - where max_date_created > @last_update - # group by person_id - # limit 10 - ); - - # This is for terminal display only - (select distinct person_id #, min(encounter_datetime) as start_date - from etl.flat_obs - where max_date_created > @last_update - # group by person_id - # limit 10 - ); - - select @person_ids_count := (select count(*) from new_data_person_ids); - - delete t1 from flat_pep_summary t1 join new_data_person_ids t2 using (person_id); - - while @person_ids_count > 0 do - - #create temp table with a set of person ids - drop table if exists new_data_person_ids_0; - - create temporary table new_data_person_ids_0 (select * from new_data_person_ids limit 10000); #TODO - change this when data_fetch_size changes - - delete from new_data_person_ids where person_id in (select person_id from new_data_person_ids_0); - - select @person_ids_count := (select count(*) from new_data_person_ids); - - drop table if exists flat_pep_summary_0a; - create temporary table flat_pep_summary_0a - (select - t1.person_id, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - t1.location_id, - t1.obs, - t1.obs_datetimes - from etl.flat_obs t1 - join new_data_person_ids_0 t0 using (person_id) - where t1.encounter_type in (56,57) - ); - - drop table if exists flat_pep_summary_0; - create temporary table flat_pep_summary_0(index encounter_id (encounter_id), index person_enc (person_id,encounter_datetime)) - (select * from flat_pep_summary_0a - order by person_id, date(encounter_datetime) - ); - - - select @prev_id := null; - select @cur_id := null; - select @enrollment_date := null; - select @cur_location := null; - select @cur_rtc_date := null; - select @prev_rtc_date := null; - - #OCCUPATIONAL PEP - select @hiv_exposed_occupational := null; - select @pep_start_date := null; - - drop temporary table if exists flat_pep_summary_1; - create temporary table flat_pep_summary_1 (index encounter_id (encounter_id)) - (select - @prev_id := @cur_id as prev_id, - @cur_id := t1.person_id as cur_id, - t1.person_id, - p.uuid as uuid, - t1.visit_id, - t1.encounter_id, - t1.encounter_datetime, - t1.encounter_type, - - case - when @prev_id != @cur_id then @enrollment_date := encounter_datetime - when @enrollment_date is null then @enrollment_date := encounter_datetime - else @enrollment_date - end as enrollment_date, - - case - when location_id then @cur_location := location_id - when @prev_id = @cur_id then @cur_location - else null - end as location_id, - - case - when @prev_id=@cur_id then @prev_rtc_date := @cur_rtc_date - else @prev_rtc_date := null - end as prev_rtc_date, - - # 5096 = return visit date - case - when obs regexp "!!5096=" then @cur_rtc_date := replace(replace((substring_index(substring(obs,locate("!!5096=",obs)),@sep,1)),"!!5096=",""),"!!","") - when @prev_id = @cur_id then if(@cur_rtc_date > encounter_datetime,@cur_rtc_date,null) - else @cur_rtc_date := null - end as cur_rtc_date, - - case - when obs regexp "!!1972=(5619|5507|1496|6280|8710|8713|8714)" then @hiv_exposed_occupational := 1 - when !(obs regexp "!!1972=(5619|5507|1496|6280|8710|8713|8714)") then @hiv_exposed_occupational := 0 - when @prev_id = @cur_id then @hiv_exposed_occupational - else @hiv_exposed_occupational := null - end as hiv_exposed_occupational, - case - when encounter_type = 56 and (obs regexp "!!1705=1149" or obs regexp "!!7123=") then @pep_start_date := encounter_datetime - when @prev_id = @cur_id then @pep_start_date - else @pep_start_date := null - end as pep_start_date - - from flat_pep_summary_0 t1 - join amrs.person p using (person_id) - ); - - replace into flat_pep_summary - (select - person_id, - t1.uuid, - t1.visit_id, - encounter_id, - encounter_datetime, - encounter_type, - location_id, - t2.uuid as location_uuid, - enrollment_date, - prev_rtc_date, - cur_rtc_date, - hiv_exposed_occupational, - pep_start_date - from flat_pep_summary_1 t1 - join amrs.location t2 using (location_id)); - - end while; - - select @end := now(); - insert into etl.flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes"); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_prep_summary_v1_1_prod.sql b/etl-scripts/stored-procedures/generate_prep_summary.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_prep_summary_v1_1_prod.sql rename to etl-scripts/stored-procedures/generate_prep_summary.sql index 22ec38f..5e021af 100644 --- a/etl-scripts/stored-procedures/generate_prep_summary_v1_1_prod.sql +++ b/etl-scripts/stored-procedures/generate_prep_summary.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_prep_summary_v1_1_prod`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int , IN log boolean) +CREATE PROCEDURE `generate_prep_summary`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int , IN log boolean) BEGIN select @start := now(); diff --git a/etl-scripts/stored-procedures/generate_surge_daily_report_dataset_v1_0_prod.sql b/etl-scripts/stored-procedures/generate_surge_daily_report_dataset.sql similarity index 99% rename from etl-scripts/stored-procedures/generate_surge_daily_report_dataset_v1_0_prod.sql rename to etl-scripts/stored-procedures/generate_surge_daily_report_dataset.sql index 775c747..49d15c1 100644 --- a/etl-scripts/stored-procedures/generate_surge_daily_report_dataset_v1_0_prod.sql +++ b/etl-scripts/stored-procedures/generate_surge_daily_report_dataset.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE PROCEDURE `generate_surge_daily_report_dataset_v1_0_prod`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int , IN log boolean) +CREATE PROCEDURE `generate_surge_daily_report_dataset`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int , IN log boolean) BEGIN set @start = now(); set @table_version = "surge_daily_report_dataset_v1.0"; diff --git a/etl-scripts/stored-procedures/generate_surge_weekly_report_dataset.sql b/etl-scripts/stored-procedures/generate_surge_weekly_report_dataset.sql index bc16178..02f4e32 100644 --- a/etl-scripts/stored-procedures/generate_surge_weekly_report_dataset.sql +++ b/etl-scripts/stored-procedures/generate_surge_weekly_report_dataset.sql @@ -1,1859 +1,1832 @@ -CREATE PROCEDURE `etl`.`generate_surge_weekly_report_dataset_v2`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int , IN log boolean) -BEGIN - set @primary_table := "surge_weekly_report_dataset_v7"; - set @query_type = query_type; - set @queue_table = ""; - - set @total_rows_written = 0; - set @start = now(); - set @table_version = "surge_weekly_report_dataset_v7"; - - set @last_update = null; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - set @may_2019 = '2019-05-11'; - set @may_2015 = '2019-05-11'; - set @october_2018 = '2018-10-01'; - - - -CREATE TABLE IF NOT EXISTS surge_weekly_report_dataset_v7 ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL, - elastic_id BIGINT, - person_uuid CHAR(38), - person_id INT, - year_week VARCHAR(10), - encounter_yw INT, - encounter_id INT, - encounter_datetime DATETIME, - encounter_date DATETIME, - end_date DATE, - start_date DATE, - birthdate DATE, - age DECIMAL(23 , 2 ), - gender VARCHAR(100), - clinical_visit_number INT(11), - prev_rtc_date DATETIME, - rtc_date DATETIME, - visit_this_week INT(1), - on_schedule INT(1), - early_appointment INT(1), - early_appointment_this_week INT(1), - late_appointment_this_week INT(11), - days_since_rtc_date BIGINT(21), - scheduled_this_week INT(1), - unscheduled_this_week INT(0), - tx2_visit_this_week INT(11), - missed_tx2_visit_this_week INT(11), - tx2_scheduled_this_week_but_came_early INT(11), - death_date DATETIME, - missed_appointment_this_week INT(11), - ltfu INT(1), - defaulted INT(1), - missed INT(1), - next_status BINARY(0), - active_in_care_this_week INT(11), - cur_arv_adherence VARCHAR(200), - cur_who_stage INT(11), - is_pre_art_this_week INT(11), - arv_first_regimen_location_id INT(11), - arv_first_regimen VARCHAR(500), - arv_first_regimen_names VARCHAR(500), - arv_first_regimen_start_date DATETIME, - days_since_starting_arvs BIGINT(21), - started_art_this_week INT(1), - enrollment_date DATETIME, - enrolled_this_week INT(1), - art_revisit_this_week INT(0), - cur_arv_meds VARCHAR(500), - cur_arv_meds_names VARCHAR(500), - cur_arv_meds_strict VARCHAR(500), - cur_arv_line INT(11), - cur_arv_line_strict INT(11), - cur_arv_line_reported TINYINT(4), - on_art_this_week BINARY(0), - vl_1 INT(11), - vl_1_date DATETIME, - vl_2 INT(11), - vl_2_date DATETIME, - has_vl_this_week INT(1), - is_suppressed INT(1), - is_un_suppressed INT(1), - days_since_last_vl BIGINT(21), - due_for_vl_this_week INT(0), - reason_for_needing_vl_this_week BINARY(0), - cd4_1 DOUBLE, - cd4_1_date DATETIME, - child_hiv_status_disclosure_status BINARY(0), - transfer_in_this_week INT(0), - transfer_in_location_id INT(11), - transfer_in_date DATETIME, - transfer_out_this_week INT(0), - transfer_out_location_id INT(11), - transfer_out_date DATETIME, - status VARCHAR(12), - dc_eligible_cumulative INT(1), - started_dc_this_week INT(1), - location_id INT(11), - tx2_scheduled_this_week INT(1), - tx2_scheduled_honored INT(1), - prev_id BIGINT(20), - cur_id INT(11), - prev_enc_id BIGINT(20), - cur_enc_id INT(11), - clinical_visit_num BIGINT(63), - prev_status LONGTEXT, - cur_status VARCHAR(12), - cur_prep_this_week INT(11), - new_prep_this_week INT(11), - height DOUBLE, - weight DOUBLE, - bmi DOUBLE, - scheduled_this_week_and_due_for_vl INT(0), - unscheduled_this_week_and_due_for_vl INT(0), - overdue_for_vl_active INT(0), - due_for_vl_has_vl_order INT(0), - due_for_vl_dont_have_order INT(0), - ltfu_this_week INT(0), - missed_this_week INT(0), - all_ltfus INT(0), - ltfu_surge_baseline INT(0), - surge_ltfu_and_ltfu_after_may INT(0), - surge_ltfu_and_still_ltfu INT(0), - newly_ltfu_this_week INT(0), - ltfu_cumulative_outcomes_death INT(0), - ltfu_cumulative_outcomes_transfer_out INT(0), - ltfu_cumulative_outcomes_active INT(0), - active_to_ltfu_count INT(0), - defaulted_this_week INT(0), - due_for_vl_this_week_active INT(0), - on_schedule_this_week INT(0), - ltfu_cumulative_outcomes_total INT(0), - old_ltfus_to_active_this_week INT(0), - tx2_unscheduled_this_week INT(0), - dead_this_week INT(0), - non_ltfu_dead_this_week INT(0), - cumulative_dead INT(0), - dc_eligible_this_week INT(0), - dc_eligible_and_scheduled_this_week INT(0), - active_on_dc INT(0), - is_ltfu_surge_baseline INT(0), - - baseline_location VARCHAR(100), - was_ltfu_may_19 INT(0), - was_active_october_18 INT(0), - not_elligible_for_dc INT(0), - eligible_and_on_dc INT(0), - eligible_not_on_dc INT(0), - eligible_not_on_dc_and_scheduled_this_week INT(0), - eligible_not_on_dc_and_unscheduled_this_week INT(0), - eligible_and_on_dc_and_scheduled_this_week INT(0), - eligible_and_on_dc_and_unscheduled_this_week INT(0), - elligible_total INT(0), - not_elligible_and_on_dc INT(0), - elligible_total_revised INT(0), - ltfu_transfer_out_this_week INT(0), - ltfu_death_this_week INT(0), - ltfu_active_this_week INT(0), - is_ltfu_after_may INT(0), - - - is_ltfu_after_may_total INT(0), - - - week_patient_became_active VARCHAR(100), - med_pickup_rtc_date VARCHAR(100), - days_since_med_pickup_rtc_date BIGINT(21), - days_diff_med_rtc_and_next_clinical_date BIGINT(21), - - was_ltfu_before_october_2018 INT(0), - old_ltfus_outcome_total INT(0), - old_ltfus_to_active INT(0), - intervention_done_this_week INT(0), - interventions INT(0), - due_for_vl_has_vl_order_scheduled INT(0), - due_for_vl_has_vl_order_unscheduled INT(0), - due_for_vl_dont_have_order_scheduled INT(0), - due_for_vl_dont_have_order_unscheduled INT(0), - has_vl_this_week_and_suppressed INT(0), - has_vl_this_week_and_unsuppressed INT(0), - missed_to_active_this_week INT(0), - defaulter_to_active_this_week INT(0), - missed_cumulative INT(0), - med_surge_ltfus_cumulative INT(0), - med_surge_ltfu_and_med_ltfu_after_may INT(0), - med_defaulters INT(0), - newly_med_ltfu_this_week INT(0), - med_surge_ltfus_outcomes INT(0), - med_surge_ltfus_outcomes_this_week INT(0), - column_1 INT(0), - column_2 INT(0), - column_3 INT(0), - column_4 VARCHAR(100), - column_5 VARCHAR(100), - column_6 VARCHAR(100), - PRIMARY KEY elastic_id (elastic_id), - INDEX person_enc_date (person_id , encounter_date), - INDEX person_year_week (person_id , year_week), - INDEX date_created (date_created), - INDEX encounter_id (encounter_id), - INDEX person_report_date (person_id , end_date), - INDEX endDate_location_id (end_date , location_id), - INDEX location_id (location_id), - INDEX year_week (year_week), - INDEX person_id (person_id), - INDEX year_week_location_id (year_week , location_id) -); - - if (query_type = "build") then - SELECT 'BUILDING.......................'; - set @write_table = concat("surge_weekly_report_dataset_v7_temp",queue_number); - set @queue_table = concat("surge_weekly_report_dataset_build_queue_",queue_number); - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(person_id int primary key) (select * from surge_weekly_report_dataset_build_queue limit ',queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('delete t1 from surge_weekly_report_dataset_build_queue t1 join ',@queue_table, ' t2 using (person_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - if (@query_type="sync") then - SELECT 'SYNCING.......................'; - set @write_table = concat("surge_weekly_report_dataset_v7"); - set @queue_table = "surge_weekly_report_dataset_sync_queue"; - create table if not exists surge_weekly_report_dataset_sync_queue (person_id int primary key); - - select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); - - select "Finding patients in amrs.encounters..."; - - replace into surge_weekly_report_dataset_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - - select "Finding patients in flat_obs..."; - - replace into surge_weekly_report_dataset_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - - select "Finding patients in flat_lab_obs..."; - replace into surge_weekly_report_dataset_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - select "Finding patients in flat_orders..."; - - replace into surge_weekly_report_dataset_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into surge_weekly_report_dataset_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into surge_weekly_report_dataset_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - end if; - - # Remove test patients - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @num_ids := 0; - SET @dyn_sql=CONCAT('select count(*) into @num_ids from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - -SELECT CONCAT('Patients in queue: ', @person_ids_count); - -set @total_time=0; -set @cycle_number = 0; - -while @person_ids_count > 0 do - - set @loop_start_time = now(); - drop temporary table if exists surge_weekly_report_dataset_temporary_build_queue; - create temporary table surge_weekly_report_dataset_temporary_build_queue (person_id int primary key); - - SET @dyn_sql=CONCAT('insert into surge_weekly_report_dataset_temporary_build_queue (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SELECT CONCAT('creating patient_vl ...'); - - CREATE Temporary TABLE if not exists patient_vl_status ( - person_id int(11) DEFAULT NULL, - vl_1 int(11) DEFAULT NULL, - vl_2 int(11) DEFAULT NULL, - vl_2_date datetime DEFAULT NULL, - vl_1_date datetime DEFAULT NULL, - end_date date NOT NULL, - week varchar(10) NOT NULL DEFAULT '', - is_suppressed int(1) NOT NULL DEFAULT '0', - is_un_suppressed int(1) NOT NULL DEFAULT '0', - has_vl_this_week int(1) NOT NULL DEFAULT '0', - days_since_last_vl int(11) DEFAULT NULL, - Primary Key person_week (person_id,week), - KEY person_id (person_id) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8; - - - replace into patient_vl_status( - SELECT - person_id, - vl_1, - vl_2, - vl_2_date, - vl_1_date, - end_date, - week, - if(vl_1=0 or (vl_1 >= 0 and vl_1 <= 400),1,0 ) as is_suppressed, - if(vl_1>400,1,0 ) as is_un_suppressed, - if(yearweek(vl_1_date)=week,1,0 ) as has_vl_this_week, - TIMESTAMPDIFF(DAY, - vl_1_date, - end_date) AS days_since_last_vl - - FROM - surge_week t1 - JOIN - (SELECT t1a.person_id, vl_1,vl_2,vl_2_date, vl_1_date,arv_start_date,encounter_datetime FROM - etl.flat_hiv_summary_v15b t1a Inner JOIN surge_weekly_report_dataset_temporary_build_queue t3 on t1a.person_id=t3.person_id - WHERE t1a.vl_1_date IS NOT NULL) t2 - - WHERE t2.encounter_datetime < DATE_ADD(t1.end_date, INTERVAL 1 DAY)); - - - SELECT CONCAT('creating surge_vitals ...'); - - CREATE TEMPORARY TABLE if not exists surge_vitals ( - prev_id bigint(20) DEFAULT NULL, - cur_id int(11) DEFAULT NULL, - person_id int(11) DEFAULT NULL, - encounter_id int(11) NOT NULL DEFAULT '0', - encounter_datetime datetime DEFAULT NULL, - height double DEFAULT NULL, - weight double DEFAULT NULL, - bmi double DEFAULT NULL, - Primary KEY encounter_id (encounter_id), - KEY person_id (person_id), - KEY encounter_id (encounter_id), - KEY person_id_2 (person_id,encounter_datetime), - KEY encounter_datetime (encounter_datetime) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8; - - replace into surge_vitals ( - SELECT - @prev_id := @cur_id AS prev_id, - @cur_id := t1.person_id AS cur_id, - t1.person_id as person_id, - t1.encounter_id, - encounter_datetime, - CASE - WHEN @prev_id != @cur_id THEN @height := height - WHEN @prev_id = @cur_id and @height is null and height is not null THEN @height := height - WHEN @prev_id = @cur_id and @height and height THEN @height := height - ELSE @height - END AS height, - CASE - WHEN @prev_id != @cur_id THEN @weight := weight - WHEN @prev_id = @cur_id and @weight is null and weight is not null THEN @weight := weight - WHEN @prev_id = @cur_id and @weight and weight THEN @weight := weight - ELSE @weight - END AS weight, - (@weight / (@height * @height) * 10000) as bmi - - from - (select encounter_id, height,weight,encounter_datetime,tv.person_id from etl.flat_vitals tv - inner join surge_weekly_report_dataset_temporary_build_queue t2 on tv.person_id=t2.person_id - order by t2.person_id , encounter_datetime) t1); - - set @age =null; - set @status = null; - - - - SELECT CONCAT('creating surge_weekly_report_dataset_0 ...'); - drop temporary table if exists surge_weekly_report_dataset_0; - create temporary table surge_weekly_report_dataset_0 - (index (person_id), index(start_date), index(end_date),index(encounter_date)) - (SELECT - concat(t1.week,t2.person_id) as elastic_id, - t2.uuid AS person_uuid, - t2.person_id, - t1.week AS year_week, - YEARWEEK(t2.encounter_datetime) encounter_yw, - t2.encounter_id, - t2.encounter_datetime, - t2.next_clinical_datetime_hiv, - if(rtc_date >= @october_2018 <= @may_2019, rtc_date, null) as base_rtc_date, - DATE(t2.encounter_datetime) AS encounter_date, - t1.end_date, - t1.start_date, - DATE(birthdate) AS birthdate, - gender, - CASE - WHEN - TIMESTAMPDIFF(YEAR, birthdate, end_date) > 0 - THEN - @age:=ROUND(TIMESTAMPDIFF(YEAR, birthdate, end_date), - 0) - ELSE @age:=ROUND(TIMESTAMPDIFF(MONTH, - birthdate, - end_date) / 12, - 2) - END AS age, - prev_clinical_rtc_date_hiv as prev_rtc_date , - rtc_date, - - - CASE - WHEN - med_pickup_rtc_date IS NOT NULL - AND visit_type NOT IN (24) - - THEN - @med_pickup_rtc_date := med_pickup_rtc_date - - ELSE @med_pickup_rtc_date := NULL - END AS med_pickup_rtc_date, - - # select med_pickup_rtc_date, year_week from surge_weekly_report_dataset_0 where person_id = 21417 - - - IF(YEARWEEK(t2.encounter_datetime) = t1.week, - 1, - 0) AS visit_this_week, - IF(DATE(t2.encounter_datetime) = DATE(prev_clinical_rtc_date_hiv), - 1, - 0) AS on_schedule, - IF((yearweek(prev_clinical_rtc_date_hiv)=t1.week OR yearweek(rtc_date)=t1.week OR (prev_clinical_rtc_date_hiv is null and yearweek(rtc_date)=t1.week)) and - YEARWEEK(t2.encounter_datetime) = t1.week, - 1, - 0) AS on_schedule_this_week, - IF(DATE(t2.encounter_datetime) < DATE(prev_clinical_rtc_date_hiv), - 1, - 0) AS early_appointment, - - IF(YEARWEEK(t2.encounter_datetime) = t1.week AND - (t1.week < YEARWEEK(prev_clinical_rtc_date_hiv) OR (prev_clinical_rtc_date_hiv is null and t1.week < YEARWEEK(rtc_date))) - ,1,0) AS early_appointment_this_week, - - IF(YEARWEEK(t2.encounter_datetime) = t1.week - AND (t1.week > YEARWEEK(prev_clinical_rtc_date_hiv) OR ( prev_clinical_rtc_date_hiv is null and t1.week > YEARWEEK(rtc_date))) - ,1,0) AS late_appointment_this_week, - - TIMESTAMPDIFF(DAY, - rtc_date, - end_date) AS days_since_rtc_date, - - - TIMESTAMPDIFF(DAY, - prev_clinical_rtc_date_hiv, - t2.encounter_datetime) AS days_diff_enc_date_and_prev_rtc, - - TIMESTAMPDIFF(DAY, - @med_pickup_rtc_date, - end_date) AS days_since_med_pickup_rtc_date, - - TIMESTAMPDIFF(DAY, - DATE(med_pickup_rtc_date), - DATE(next_clinical_datetime_hiv) - ) AS days_diff_med_rtc_and_next_clinical_date, - - IF(YEARWEEK(prev_clinical_rtc_date_hiv) = t1.week - OR (prev_clinical_rtc_date_hiv is null and t1.week = YEARWEEK(rtc_date)) OR yearweek(rtc_date)=t1.week, - 1, - 0) AS scheduled_this_week, - CASE - WHEN - YEARWEEK(t2.encounter_datetime) = t1.week - AND (yearweek(prev_clinical_rtc_date_hiv) != t1.week OR (prev_clinical_rtc_date_hiv is null and yearweek(rtc_date) != t1.week)) - AND ((DATE(t2.encounter_datetime) != DATE(prev_clinical_rtc_date_hiv)) OR ( prev_clinical_rtc_date_hiv is null and (DATE(t2.encounter_datetime) != DATE(rtc_date)))) - THEN - 1 - ELSE 0 - END AS unscheduled_this_week, - - # NULL AS tx2_visit_this_week, - # NULL AS missed_tx2_visit_this_week, - t2.death_date, - NULL active_in_care_this_week, - cur_arv_adherence, - NULL cur_who_stage, - NULL is_pre_art_this_week, - arv_first_regimen_location_id, - arv_first_regimen, - etl.get_arv_names(arv_first_regimen) as arv_first_regimen_names, - arv_first_regimen_start_date, - TIMESTAMPDIFF(DAY, - arv_first_regimen_start_date, - end_date) AS days_since_starting_arvs, - IF(YEARWEEK(arv_first_regimen_start_date) = t1.week, - 1, - 0) AS started_art_this_week, - enrollment_date, - IF(YEARWEEK(enrollment_date) = t1.week, - 1, - 0) AS enrolled_this_week, - CASE - WHEN - YEARWEEK(t2.encounter_datetime) = t1.week - AND YEARWEEK(arv_first_regimen_start_date) != t1.week - AND cur_arv_meds IS NOT NULL - AND arv_first_regimen_start_date IS NOT NULL - THEN - 1 - ELSE 0 - END AS art_revisit_this_week, - cur_arv_meds, - etl.get_arv_names(cur_arv_meds) AS cur_arv_meds_names, - cur_arv_meds_strict, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - NULL AS on_art_this_week, - cd4_1, - cd4_1_date, - NULL AS child_hiv_status_disclosure_status, - CASE - WHEN YEARWEEK(transfer_in_date) = t1.week THEN 1 - ELSE 0 - END AS transfer_in_this_week, - transfer_in_location_id, - transfer_in_date, - CASE - WHEN YEARWEEK(transfer_out_date) = t1.week THEN 1 - ELSE 0 - END AS transfer_out_this_week, - transfer_out_location_id, - transfer_out_date, - - t2.location_id, - IF((DATE(arv_first_regimen_start_date) = DATE(t2.encounter_datetime) - AND YEARWEEK(rtc_date) = t1.week) or (DATE(arv_first_regimen_start_date) = DATE(prev_clinical_datetime_hiv) - AND YEARWEEK(prev_rtc_date) = t1.week) , - 1, - 0) AS tx2_scheduled_this_week, - IF(DATE(arv_first_regimen_start_date) = DATE(prev_clinical_datetime_hiv) - AND YEARWEEK(t2.encounter_datetime) = t1.week, - 1, - 0) AS tx2_visit_this_week, - IF(YEARWEEK(arv_first_regimen_start_date) = YEARWEEK(prev_clinical_datetime_hiv) - AND YEARWEEK(t2.encounter_datetime) = t1.week AND YEARWEEK(prev_rtc_date) = t1.week, - 1, - 0) AS tx2_scheduled_honored, - - - is_pregnant, - edd, - @date_pregnant := if(edd is not null,DATE_ADD(edd, INTERVAL -9 MONTH),null) as date_confirmed_pregnant, - if(edd is not null and yearweek(edd) >= yearweek(t1.end_date),1,0) as is_still_pregnant, - if(@date_pregnant is not null and YEARWEEK(@date_pregnant) > yearweek(arv_first_regimen_start_date),1,0) as started_arv_b4_pregnancy, - if(@date_pregnant is not null and YEARWEEK(@date_pregnant) < yearweek(arv_first_regimen_start_date),1,0) as started_arv_after_pregnancy, - if(arv_first_regimen_start_date is not null and yearweek(arv_first_regimen_start_date) <= yearweek(t1.end_date) ,1,0) as patient_is_on_art, - breast_feeding, - breast_feeding_encounter_date, - - - - tb_tx_start_date, - TIMESTAMPDIFF(MONTH, - tb_tx_start_date, - end_date) AS months_since_tb_tx_start_date, - CASE - WHEN YEARWEEK(vl_order_date) = t1.week THEN 1 - ELSE 0 - END AS vl_ordered_this_week, - if(YEARWEEK(t2.encounter_datetime)= t1.week and encounter_type=21,1,0) intervention_done_this_week, - if( encounter_type=21,1,0) interventions, - arv_start_date - FROM - surge_week t1 - JOIN etl.flat_hiv_summary_v15b t2 - JOIN surge_weekly_report_dataset_temporary_build_queue t5 USING (person_id) - left join ndwr.patient_breast_feeding bf USING (person_id)#ON(t2.person_id = bf.person_id AND breast_feeding_encounter_date < DATE_ADD(t1.end_date, INTERVAL 1 DAY)) - JOIN - amrs.person t3 USING (person_id) - - WHERE - t2.encounter_datetime < DATE_ADD(t1.end_date, INTERVAL 1 DAY) - AND ((t2.next_clinical_datetime_hiv IS NULL - OR t2.next_clinical_datetime_hiv >= DATE_ADD(t1.end_date, INTERVAL 1 DAY)) || (t2.rtc_date IS NULL - OR t2.rtc_date >= DATE_ADD(t1.end_date, INTERVAL 1 DAY))) - AND t2.encounter_datetime < DATE_ADD(t1.end_date, INTERVAL 1 DAY) - AND t2.is_clinical_encounter = 1 - ORDER BY t2.person_id , t2.encounter_datetime , rtc_date , end_date); - - drop temporary table if exists surge_weekly_report_dataset_01; - CREATE TEMPORARY TABLE `surge_weekly_report_dataset_01` ( - `elastic_id` varchar(21) CHARACTER SET latin1 DEFAULT NULL, - `person_uuid` varchar(100) CHARACTER SET latin1 DEFAULT NULL, - `person_id` int(11) DEFAULT NULL, - `year_week` varchar(10) CHARACTER SET latin1 NOT NULL DEFAULT '', - `encounter_yw` int(6) DEFAULT NULL, - `encounter_id` int(11) NOT NULL DEFAULT '0', - `encounter_datetime` datetime DEFAULT NULL, - `next_clinical_datetime_hiv` datetime DEFAULT NULL, - `base_rtc_date` datetime DEFAULT NULL, - `encounter_date` date DEFAULT NULL, - `end_date` date NOT NULL, - `start_date` date NOT NULL, - `birthdate` date DEFAULT NULL, - `gender` varchar(50) DEFAULT '', - `age` decimal(23,2) DEFAULT NULL, - `prev_rtc_date` datetime DEFAULT NULL, - `rtc_date` datetime DEFAULT NULL, - `med_pickup_rtc_date` varchar(19) DEFAULT NULL, - `visit_this_week` int(1) NOT NULL DEFAULT '0', - `on_schedule` int(1) NOT NULL DEFAULT '0', - `on_schedule_this_week` int(1) NOT NULL DEFAULT '0', - `early_appointment` int(1) NOT NULL DEFAULT '0', - `early_appointment_this_week` int(1) NOT NULL DEFAULT '0', - `late_appointment_this_week` int(1) NOT NULL DEFAULT '0', - `days_since_rtc_date` bigint(21) DEFAULT NULL, - `days_diff_enc_date_and_prev_rtc` bigint(21) DEFAULT NULL, - `days_since_med_pickup_rtc_date` bigint(21) DEFAULT NULL, - `days_diff_med_rtc_and_next_clinical_date` bigint(21) DEFAULT NULL, - `scheduled_this_week` int(1) NOT NULL DEFAULT '0', - `unscheduled_this_week` int(0) DEFAULT NULL, - `death_date` datetime DEFAULT NULL, - `active_in_care_this_week` binary(0) DEFAULT NULL, - `cur_arv_adherence` varchar(200) CHARACTER SET latin1 DEFAULT NULL, - `cur_who_stage` binary(0) DEFAULT NULL, - `is_pre_art_this_week` binary(0) DEFAULT NULL, - `arv_first_regimen_location_id` int(11) DEFAULT NULL, - `arv_first_regimen` varchar(500) CHARACTER SET latin1 DEFAULT NULL, - `arv_first_regimen_names` text CHARACTER SET latin1, - `arv_first_regimen_start_date` datetime DEFAULT NULL, - `days_since_starting_arvs` bigint(21) DEFAULT NULL, - `started_art_this_week` int(1) NOT NULL DEFAULT '0', - `enrollment_date` datetime DEFAULT NULL, - `enrolled_this_week` int(1) NOT NULL DEFAULT '0', - `art_revisit_this_week` int(0) DEFAULT NULL, - `cur_arv_meds` varchar(500) CHARACTER SET latin1 DEFAULT NULL, - `cur_arv_meds_names` text CHARACTER SET latin1, - `cur_arv_meds_strict` varchar(500) CHARACTER SET latin1 DEFAULT NULL, - `cur_arv_line` int(11) DEFAULT NULL, - `cur_arv_line_strict` int(11) DEFAULT NULL, - `cur_arv_line_reported` tinyint(4) DEFAULT NULL, - `on_art_this_week` binary(0) DEFAULT NULL, - `cd4_1` double DEFAULT NULL, - `cd4_1_date` datetime DEFAULT NULL, - `child_hiv_status_disclosure_status` binary(0) DEFAULT NULL, - `transfer_in_this_week` int(0) DEFAULT NULL, - `transfer_in_location_id` int(11) DEFAULT NULL, - `transfer_in_date` datetime DEFAULT NULL, - `transfer_out_this_week` int(0) DEFAULT NULL, - `transfer_out_location_id` int(11) DEFAULT NULL, - `transfer_out_date` datetime DEFAULT NULL, - `location_id` int(11) DEFAULT NULL, - `tx2_scheduled_this_week` int(1) NOT NULL DEFAULT '0', - `tx2_visit_this_week` int(1) NOT NULL DEFAULT '0', - `tx2_scheduled_honored` int(1) NOT NULL DEFAULT '0', - `is_pregnant` tinyint(1) DEFAULT NULL, - `edd` datetime DEFAULT NULL, - `date_confirmed_pregnant` varchar(19) DEFAULT NULL, - `is_still_pregnant` int(1) NOT NULL DEFAULT '0', - `started_arv_b4_pregnancy` int(1) NOT NULL DEFAULT '0', - `started_arv_after_pregnancy` int(1) NOT NULL DEFAULT '0', - `patient_is_on_art` int(1) NOT NULL DEFAULT '0', - `breast_feeding` int(11) DEFAULT NULL, - `breast_feeding_encounter_date` datetime DEFAULT NULL, - `tb_tx_start_date` datetime DEFAULT NULL, - `months_since_tb_tx_start_date` bigint(21) DEFAULT NULL, - `vl_ordered_this_week` int(0) DEFAULT NULL, - `intervention_done_this_week` int(1) NOT NULL DEFAULT '0', - `interventions` int(1) NOT NULL DEFAULT '0', - `arv_start_date` datetime DEFAULT NULL, - PRIMARY KEY(person_id, year_week), - KEY `person_id` (`person_id`), - KEY `start_date` (`start_date`), - KEY `end_date` (`end_date`), - KEY `encounter_date` (`encounter_date`) - - - ) ENGINE=InnoDB DEFAULT CHARSET=utf8; - - replace into surge_weekly_report_dataset_01(select * - from surge_weekly_report_dataset_0); - SELECT - COUNT(*) patients_for_24 - FROM - surge_weekly_report_dataset_0 - WHERE - year_week = 201924; - - SELECT CONCAT('creating surge_weekly_report_dataset__1 ...'); - - drop temporary table if exists surge_weekly_report_dataset_1; - - CREATE temporary TABLE surge_weekly_report_dataset_1 (SELECT sw0.*, - vl.vl_1, - vl.vl_1_date, - vl.vl_2, - vl.vl_2_date, - vl.days_since_last_vl, - - CASE - WHEN vl_1 > 999 - AND vl_1_date > arv_start_date - AND TIMESTAMPDIFF(MONTH,vl_1_date,sw0.end_date) >=3 - THEN - 1 - #is pregnant and started_arv_b4_pregnancy - WHEN is_still_pregnant=1 AND started_arv_b4_pregnancy=1 and vl_1_date is null then 1 - WHEN is_still_pregnant=1 AND started_arv_b4_pregnancy=1 and TIMESTAMPDIFF(MONTH,vl_1_date,date_confirmed_pregnant)>=1 then 1 - WHEN is_still_pregnant=1 AND started_arv_b4_pregnancy=1 and yearweek(vl_1_date) >= yearweek(date_confirmed_pregnant) and TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date)>=6 then 1 - - #is pregnant and started_arv_after_pregnancy - WHEN is_still_pregnant=1 AND patient_is_on_art=1 AND started_arv_after_pregnancy=1 and TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=3 and vl_1_date is null then 1 - WHEN is_still_pregnant=1 AND patient_is_on_art=1 AND started_arv_after_pregnancy=1 and TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=3 and TIMESTAMPDIFF(MONTH,vl_1_date,date_confirmed_pregnant)>=1 then 1 - WHEN is_still_pregnant=1 AND patient_is_on_art=1 AND started_arv_after_pregnancy=1 and TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=3 and TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date)>=6 then 1 - - #is 24 and below years - WHEN age<=24 AND patient_is_on_art=1 AND (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=6 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date)>=6) and vl_1_date is null then 1 - WHEN age <=24 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=6 then 1 - - #is 25 and above years - WHEN age >=25 AND patient_is_on_art=1 AND (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=6 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date)>=6) and vl_1_date is null then 1 - WHEN age >=25 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=6 and (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date) <=12 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) <= 12 ) then 1 - WHEN age >=25 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=12 and (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date) > 12 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) > 12 )then 1 - - # breast feeding - WHEN breast_feeding=1065 AND patient_is_on_art=1 and (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date) >=6 - or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) >=6) and vl_1_date is null then 1 - WHEN breast_feeding=1065 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=6 then 1 - - # drug substitution and no vl ever - WHEN arv_first_regimen_start_date is not null and arv_start_date is not null - and arv_first_regimen_start_date < arv_start_date - AND TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) >= 3 - AND vl_1_date is null then 1 - - # drug substitution and vl was done before change - WHEN arv_first_regimen_start_date is not null and arv_start_date is not null - and arv_first_regimen_start_date < arv_start_date - AND TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) >= 3 - AND TIMESTAMPDIFF(MONTH,vl_1_date,arv_start_date)>=1 then 1 - - - else 0 - - END AS patients_due_for_vl, - - vl.has_vl_this_week, - vl.is_un_suppressed, - vl.is_suppressed, - IF( YEARWEEK(death_date)=year_week , 1,0) as non_ltfu_dead_this_week, - IF( death_date>=@may_2019, 1,0) as cumulative_dead, - CASE - WHEN death_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(death_date) THEN @status:='dead' - WHEN transfer_out_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(transfer_out_date) THEN @status:='transfer_out' - - WHEN days_since_rtc_date > 0 AND days_since_rtc_date < 5 THEN @status:='missed' - - WHEN days_since_rtc_date > 4 AND days_since_rtc_date <= 28 THEN @status:='defaulter' - - WHEN days_since_rtc_date <= 0 THEN @status:='active' - - WHEN days_since_rtc_date > 28 THEN @status:='ltfu' - ELSE @status:='unknown' - END AS status, - - CASE - WHEN death_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(death_date) THEN @status:='unknown' - WHEN transfer_out_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(transfer_out_date) THEN @status:='unknown' - WHEN next_clinical_datetime_hiv IS NULL - AND days_since_med_pickup_rtc_date > 0 - AND days_since_med_pickup_rtc_date < 5 THEN @status:='missed' - - WHEN - next_clinical_datetime_hiv IS NULL - AND days_since_med_pickup_rtc_date > 4 - AND days_since_med_pickup_rtc_date <= 28 THEN @status:='defaulter' - - WHEN next_clinical_datetime_hiv IS NULL - AND days_since_med_pickup_rtc_date > 28 THEN @status:='ltfu' - - WHEN next_clinical_datetime_hiv IS NOT NULL - AND days_diff_med_rtc_and_next_clinical_date > 28 - AND days_since_med_pickup_rtc_date > 28 THEN @status:='ltfu' - - ELSE @status:='unknown' - END AS med_status, - - - - IF(scheduled_this_week =1 and on_schedule_this_week=0 and days_since_rtc_date >0 and days_since_rtc_date <5 - - AND death_date IS NULL AND transfer_out_date IS NULL, 1, 0) AS missed_appointment_this_week, - - IF(days_since_rtc_date >=0 and days_since_rtc_date <5 and @status not in('transfer_out','dead'), 1, 0) AS missed_cumulative , - - IF(days_since_rtc_date >4 and days_since_rtc_date <=28 - AND death_date IS NULL AND transfer_out_date IS NULL, 1, 0) AS defaulted , - - null as ltfu, - null AS missed, - IF(YEARWEEK(t4.date_enrolled) = year_week - AND (t4.program_id = 3 OR t4.program_id = 9), - 1, - 0) AS started_dc_this_week, - IF(t4.program_id = 3 OR t4.program_id = 9, 1, 0) AS active_on_dc, - IF(t4.program_id = 3 OR t4.program_id = 9, - 1, - 0) AS on_dc_this_week, - IF(YEARWEEK(t4.date_enrolled) = year_week - AND t4.program_id = 10, - 1, - 0) AS new_prep_this_week, - IF(t4.program_id = 10 , 1, 0) AS cur_prep_this_week, - t6.weight AS weight, - t6.height AS height, - fd.encounter_datetime as death_reporting_date, - fd.next_encounter_datetime as fd_next_encounter_datetime, - ft.encounter_datetime as transfer_reporting_date, - ft.next_encounter_datetime as ft_next_encounter_datetime, - fe.encounter_datetime as exit_reporting_date, - fe.next_encounter_datetime as fe_next_encounter_datetime, - t6.bmi FROM - surge_weekly_report_dataset_01 sw0 - LEFT OUTER JOIN patient_vl_status vl ON vl.person_id=sw0.person_id and vl.week = sw0.year_week - LEFT OUTER JOIN - amrs.patient_program t4 ON t4.patient_id = sw0.person_id - AND t4.program_id IN (3 , 9, 10, 11) - AND t4.date_completed IS NULL - LEFT OUTER JOIN - surge_vitals t6 ON t6.encounter_id = sw0.encounter_id - LEFT JOIN etl.flat_appointment fd ON (sw0.person_id = fd.person_id and fd.encounter_type = 31) - LEFT JOIN etl.flat_appointment ft ON (sw0.person_id = ft.person_id and ft.encounter_type = 116) - LEFT JOIN etl.flat_appointment fe ON (sw0.person_id = fe.person_id and fe.encounter_type = 157) - ORDER BY sw0.person_id , year_week); - - - set @prev_id = -1; - set @cur_id = -1; - set @prev_enc_id = -1; - set @cur_enc_id = -1; - set @cur_status = ''; - set @prev_status = ''; - set @cur_med_status = ''; - set @prev_med_status = ''; - set @cur_arv_meds = null; - set @prev_arv_meds = null; - set @cur_location_id = null; - set @prev_location_id = null; - Set @clinic_visit_number = 1; - set @prev_patients_due_for_vl = 0; - set @prev_defaulted = 0; - set @cur_defaulted = 0; - set @prev_missed = 0; - set @cur_missed = 0; - set @prev_ltfu = 0; - set @cur_ltfu = 0; - set @base_rtc = null; - - - set @was_active_october_18:= null; - set @was_ltfu_may_19 := null; - set @is_ltfu_surge_baseline := null; - - - - SELECT CONCAT('creating surge_weekly_report_dataset__2 ...'); - - - drop temporary table if exists surge_weekly_report_dataset__2; - CREATE temporary TABLE surge_weekly_report_dataset__2 (SELECT *, - @prev_id:=@cur_id AS prev_id, - @cur_id:=person_id AS cur_id, - @prev_enc_id:=@cur_enc_id AS prev_enc_id, - @cur_enc_id:=encounter_id AS cur_enc_id, - CASE - WHEN - @prev_id = @cur_id - AND @prev_enc_id != @cur_enc_id - THEN - @clinic_visit_number:=@clinic_visit_number + 1 - WHEN - @prev_id = @cur_id - AND @prev_enc_id = @cur_enc_id - THEN - @clinic_visit_number - ELSE @clinic_visit_number:=1 - END AS clinical_visit_num, - - CASE - WHEN @prev_id = @cur_id THEN @prev_status:=@cur_status - ELSE @prev_status:='' - END AS prev_status, - - @cur_status:=status AS cur_status, - - CASE - WHEN @prev_id = @cur_id THEN @prev_med_status:=@cur_med_status - ELSE @prev_med_status:='' - END AS prev_med_status, - - @cur_med_status:=med_status AS cur_med_status, - - CASE - WHEN - (@prev_id != @cur_id - OR @was_active_october_18 IS NULL) - AND year_week > 201838 - AND year_week < 201918 - AND (@cur_status = 'active' OR @cur_status = 'missed' OR @cur_status = 'defaulter') - AND ((death_date IS NULL OR DATE(death_reporting_date) > '2019-05-11') AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > '2019-05-11')) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > '2019-05-11') AND fe_next_encounter_datetime IS NULL) - - THEN - @was_active_october_18:=1 - WHEN @prev_id != @cur_id THEN @was_active_october_18:=NULL - ELSE @was_active_october_18 - END AS was_active_october_18, - - CASE - WHEN - (@prev_id != @cur_id - OR @was_ltfu_may_19 IS NULL) - AND year_week = 201918 - AND @cur_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > '2019-05-11') AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > '2019-05-11')) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > '2019-05-11') AND fe_next_encounter_datetime IS NULL) - THEN - @was_ltfu_may_19:=1 - WHEN @prev_id != @cur_id THEN @was_ltfu_may_19:=NULL - ELSE @was_ltfu_may_19 - END AS was_ltfu_may_19, - - CASE - WHEN - (@prev_id != @cur_id - OR @is_ltfu_surge_baseline IS NULL) - AND @was_active_october_18 = 1 - AND @was_ltfu_may_19 = 1 - AND year_week <= 201918 - - THEN - @is_ltfu_surge_baseline:=1 - WHEN - (@prev_id != @cur_id - OR @is_ltfu_surge_baseline IS NULL) - AND NOT (@was_active_october_18 = NULL - AND @was_ltfu_may_19 = NULL) - AND year_week <= 201918 - - THEN - @is_ltfu_surge_baseline:=0 - WHEN @prev_id != @cur_id THEN @is_ltfu_surge_baseline:=NULL - ELSE @is_ltfu_surge_baseline - END AS is_ltfu_surge_baseline, - - - CASE - WHEN @prev_id != @cur_id - AND @is_ltfu_surge_baseline = 1 - AND @baseline_location IS NULL - THEN @baseline_location:= location_id - - WHEN @prev_id != @cur_id - AND @is_ltfu_surge_baseline = 1 - AND @baseline_location IS NOT NULL - THEN @baseline_location - - WHEN @prev_id = @cur_id - AND @is_ltfu_surge_baseline = 1 - AND @baseline_location IS NULL - THEN @baseline_location:= location_id - - WHEN @prev_id = @cur_id - AND @is_ltfu_surge_baseline = 1 - AND @baseline_location IS NOT NULL - THEN @baseline_location - - ELSE @baseline_location:= null - END as baseline_location, - - CASE - WHEN - (@prev_id != @cur_id - OR @active_after_may_19 IS NULL) - AND year_week >=201918 - AND (@cur_status = 'active' OR @cur_status = 'missed' OR @cur_status = 'defaulter') - AND ((death_date IS NULL OR DATE(death_reporting_date) > '2019-05-11') AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > '2019-05-11')) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > '2019-05-11') AND fe_next_encounter_datetime IS NULL) - - THEN - @active_after_may_19:=1 - WHEN @prev_id != @cur_id THEN @active_after_may_19:=NULL - ELSE @active_after_may_19 - END AS active_after_may_19, - - CASE - WHEN - (@prev_id != @cur_id - OR @is_ltfu_after_may_revised IS NULL) - AND @active_after_may_19 = 1 - AND @cur_status = 'ltfu' - AND year_week > 201918 - THEN - @is_ltfu_after_may_revised:=1 - WHEN - (@prev_id != @cur_id - OR @is_ltfu_after_may_revised IS NULL) - AND NOT (@active_after_may_19 = NULL) - AND year_week > 201918 - - THEN - @is_ltfu_after_may_revised:=0 - WHEN @prev_id != @cur_id THEN @is_ltfu_after_may_revised:=NULL - ELSE @is_ltfu_after_may_revised - END AS is_ltfu_after_may_revised, - - - - - CASE - WHEN - (@prev_id != @cur_id - OR @is_med_ltfu_after_may_revised IS NULL) - AND @active_after_may_19 = 1 - AND @cur_med_status = 'ltfu' - AND year_week > 201918 - THEN - @is_med_ltfu_after_may_revised:=1 - - WHEN @prev_id != @cur_id THEN @is_med_ltfu_after_may_revised:=NULL - ELSE @is_med_ltfu_after_may_revised - END AS is_med_ltfu_after_may_revised, - - - CASE - WHEN - (@prev_id != @cur_id - OR @was_med_ltfu_may_19 IS NULL) - AND year_week = 201918 - AND @cur_med_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > '2019-05-11') AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > '2019-05-11')) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > '2019-05-11') AND fe_next_encounter_datetime IS NULL) - THEN - @was_med_ltfu_may_19:=1 - WHEN @prev_id != @cur_id THEN @was_med_ltfu_may_19:=NULL - ELSE @was_med_ltfu_may_19 - END AS was_med_ltfu_may_19, - - - CASE - WHEN - (@prev_id != @cur_id - OR @is_med_ltfu_surge_baseline IS NULL) - AND @was_active_october_18 = 1 - AND @was_med_ltfu_may_19 = 1 - AND year_week <= 201918 - - THEN - @is_med_ltfu_surge_baseline:=1 - WHEN - (@prev_id != @cur_id - OR @is_med_ltfu_surge_baseline IS NULL) - AND NOT (@was_active_october_18 = NULL - AND @was_med_ltfu_may_19 = NULL) - AND year_week <= 201918 - - THEN - @is_med_ltfu_surge_baseline:=0 - WHEN @prev_id != @cur_id THEN @is_med_ltfu_surge_baseline:=NULL - ELSE @is_med_ltfu_surge_baseline - END AS is_med_ltfu_surge_baseline, - - IF((@is_med_ltfu_surge_baseline = 1 OR @is_med_ltfu_after_may_revised = 1), - 1, 0) AS med_surge_ltfus_cumulative, - - IF((@is_med_ltfu_surge_baseline = 1 OR @is_med_ltfu_after_may_revised = 1) AND @cur_med_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, 0) AS med_surge_ltfu_and_med_ltfu_after_may, - - CASE - WHEN - next_clinical_datetime_hiv IS NULL - AND days_since_med_pickup_rtc_date > 4 - AND days_since_med_pickup_rtc_date <= 28 - AND @cur_status not in('transfer_out','dead') - - THEN - 1 - ELSE 0 - END AS med_defaulters, - - IF(@prev_med_status = 'defaulter' - AND @cur_med_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, - 0) AS newly_med_ltfu_this_week, - - IF((@is_med_ltfu_surge_baseline = 1 OR @is_med_ltfu_after_may_revised = 1) - AND @prev_med_status = 'ltfu' - AND (@cur_med_status = 'unknown' - OR @cur_med_status = 'missed' - OR @cur_med_status = 'defaulter'), - 1, 0) AS med_surge_ltfus_outcomes_this_week, - - IF((@is_med_ltfu_surge_baseline = 1 OR @is_med_ltfu_after_may_revised = 1) - AND (@cur_med_status = 'unknown' - OR @cur_med_status = 'missed' - OR @cur_med_status = 'defaulter'), - 1, 0) AS med_surge_ltfus_outcomes, - - - CASE - WHEN @prev_id = @cur_id - AND ( @is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1) - AND @prev_status = 'ltfu' AND @cur_status = 'active' - AND @week_patient_became_active IS NULL - AND encounter_datetime > '2019-05-11' - THEN @week_patient_became_active:= encounter_datetime - - WHEN @prev_id = @cur_id - AND ( @is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1) - AND @prev_status = 'defaulter' AND @cur_status = 'ltfu' - AND @week_patient_became_active IS NOT NULL - THEN @week_patient_became_active:= NULL - - WHEN @prev_id = @cur_id - AND ( @is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1) - AND @prev_status = 'ltfu' AND @cur_status = 'ltfu' - THEN @week_patient_became_active:= NULL - - WHEN @prev_id != @cur_id - AND ( @is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1) - AND @prev_status = 'ltfu' AND @cur_status = 'active' - AND @week_patient_became_active IS NULL - AND encounter_datetime > '2019-05-11' - THEN @week_patient_became_active:= encounter_datetime - - WHEN @prev_id != @cur_id - AND ( @is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1) - AND @prev_status = 'defaulter' AND @cur_status = 'ltfu' - AND @week_patient_became_active IS NOT NULL - THEN @week_patient_became_active:= NULL - - WHEN @prev_id != @cur_id - AND ( @is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1) - AND @prev_status = 'ltfu' AND @cur_status = 'ltfu' - THEN @week_patient_became_active:= NULL - - ELSE @week_patient_became_active - END as week_patient_became_active, - - - IF(@prev_status = 'missed' - AND @cur_status = 'active', - 1, - 0) AS missed_to_active_this_week, - - IF(@prev_status = 'defaulter' - AND @cur_status = 'active', - 1, - 0) AS defaulter_to_active_this_week, - - IF(status = 'ltfu', 1, 0) AS all_ltfus, - - IF(@is_ltfu_surge_baseline = 1 AND @cur_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date) OR (transfer_out_date > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, - 0) AS surge_ltfu_and_still_ltfu, - - IF((@is_ltfu_after_may_revised = 1 OR - (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))) - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND ((transfer_out_date IS NULL AND transfer_out_location_id IS NULL) OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, - 0) AS is_ltfu_after_may_total, - - IF((@is_ltfu_after_may_revised = 1 OR - (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))) - AND @cur_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, - 0) AS is_ltfu_after_may, - - IF((@is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1) AND @cur_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND ((transfer_out_date IS NULL AND transfer_out_location_id IS NULL) OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, 0) AS surge_ltfu_and_ltfu_after_may, - - IF( YEARWEEK(death_date)=year_week, 1,0) as dead_this_week, - - IF(@prev_status = 'defaulter' - AND (@cur_status = 'ltfu' OR days_since_rtc_date > 28 OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)) - - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, - 0) AS newly_ltfu_this_week, - - #cumulative outcomes - IF((@is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1 ) - AND @cur_status = 'dead', - 1, - 0) AS ltfu_cumulative_outcomes_death, - - IF((@is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1 ) - AND @cur_status = 'transfer_out', - 1, - 0) AS ltfu_cumulative_outcomes_transfer_out, - - IF((@is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1 ) - AND (@cur_status = 'active' - OR @cur_status = 'transfer_in' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter'), - 1, - 0) AS ltfu_cumulative_outcomes_active, - - IF((@is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1 ) - AND (@cur_status = 'active' - OR @cur_status = 'transfer_in' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_out' - OR @cur_status = 'dead'), - 1, - 0) AS ltfu_cumulative_outcomes_total, - - #weekly outcomes - IF(((@is_ltfu_surge_baseline = 1 AND @prev_status = 'ltfu') OR (@is_ltfu_after_may_revised = 1 AND @prev_status = 'ltfu') - OR (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <= 0 )))) - AND @cur_status = 'dead', - 1, - 0) AS ltfu_death_this_week, - - IF(((@is_ltfu_surge_baseline = 1 AND @prev_status = 'ltfu') OR (@is_ltfu_after_may_revised = 1 AND @prev_status = 'ltfu') - OR (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <= 0 )))) - AND @cur_status = 'transfer_out', - 1, - 0) AS ltfu_transfer_out_this_week, - - - IF(((@is_ltfu_surge_baseline = 1 AND @prev_status = 'ltfu') OR (@is_ltfu_after_may_revised = 1 AND @prev_status = 'ltfu') - OR (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <= 0 )))) - AND (@cur_status = 'active' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_in'), - 1, - 0) AS ltfu_active_this_week, - - CASE - WHEN - (@prev_id != @cur_id - OR @was_ltfu_before_october_2018 IS NULL) - AND year_week = 201839 - AND @cur_status = 'ltfu' - AND @was_active_october_18 = 0 - THEN - @was_ltfu_before_october_2018:=1 - WHEN @prev_id != @cur_id THEN @was_ltfu_before_october_2018:=NULL - ELSE @was_ltfu_before_october_2018 - END AS was_ltfu_before_october_2018, - - - - #self returnies total - IF(@was_ltfu_before_october_2018 = 1 - AND (@cur_status = 'active' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_in' - OR @cur_status = 'transfer_out' - OR @cur_status = 'dead'), - 1, - 0) AS old_ltfus_outcome_total, - - IF(@was_ltfu_before_october_2018 = 1 - AND (@cur_status = 'active' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_out' - OR @cur_status = 'transfer_in' - OR @cur_status = 'dead'), - 1, - 0) AS old_ltfus_to_active, - - - #self returnies this week - IF(@was_ltfu_before_october_2018 = 1 - AND @prev_status = 'ltfu' - AND (@cur_status = 'active' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_in' - OR @cur_status = 'transfer_out'), - 1, - 0) AS old_ltfus_to_active_this_week, - - - CASE - WHEN - @prev_id = @cur_id - AND @prev_status = 'active' - AND @cur_status = 'ltfu' - THEN - @active_to_ltfu_count:=@active_to_ltfu_count + 1 - ELSE @active_to_ltfu_count:=0 - END AS active_to_ltfu_count, - - CASE - WHEN @prev_id != @cur_id THEN @height:=height - WHEN - @prev_id = @cur_id AND @height IS NULL - AND height IS NOT NULL - THEN - @height:=height - WHEN - @prev_id = @cur_id AND @height - AND height - THEN - @height:=height - ELSE @height - END AS revised_height, - CASE - WHEN @prev_id != @cur_id THEN @weight:=weight - WHEN - @prev_id = @cur_id AND @weight IS NULL - AND weight IS NOT NULL - THEN - @weight:=weight - WHEN - @prev_id = @cur_id AND @weight - AND weight - THEN - @weight:=weight - ELSE @weight - END AS revised_weight, - CASE - WHEN @prev_id != @cur_id THEN @bmi:=bmi - WHEN - @prev_id = @cur_id AND @bmi IS NULL - AND bmi IS NOT NULL - THEN - @bmi:=bmi - WHEN @prev_id = @cur_id AND @bmi AND bmi THEN @bmi:=bmi - ELSE @bmi - END AS revised_bmi, - CASE - WHEN @prev_id = @cur_id THEN @prev_patients_due_for_vl:=@cur_patients_due_for_vl - ELSE @prev_patients_due_for_vl:=0 - END AS prev_patients_due_for_vl, - @cur_patients_due_for_vl:=patients_due_for_vl AS cur_patients_due_for_vl, - IF(tx2_scheduled_this_week = 1 - AND year_week != YEARWEEK(encounter_date) AND prev_rtc_date IS NULL, - 1, - 0) AS tx2_missed_this_week, - IF(tx2_scheduled_this_week = 1 - AND YEARWEEK(encounter_date) < year_week AND prev_rtc_date IS NOT NULL , - 1, - 0) AS tx2_scheduled_this_week_but_came_early, - IF(tx2_scheduled_this_week = 0 - AND tx2_visit_this_week = 1, - 1, - 0) AS tx2_unscheduled_this_week, - - CASE - WHEN days_since_last_vl <= 365 - AND vl_2_date IS NOT NULL - AND (months_since_tb_tx_start_date IS NULL - OR months_since_tb_tx_start_date >= 6) - AND (is_pregnant = 0 OR is_pregnant IS NULL) - AND @bmi >= 18.5 - AND age >= 20 - AND @cur_status = 'active' - AND days_since_starting_arvs > 364 - AND vl_1 >= 0 - AND vl_1 <= 400 - THEN @not_elligible_for_dc := 0 - ELSE @not_elligible_for_dc := 1 - END AS not_elligible_for_dc, - - - CASE - WHEN days_since_last_vl <= 365 - AND vl_2_date IS NOT NULL - AND (months_since_tb_tx_start_date IS NULL - OR months_since_tb_tx_start_date >= 6) - AND on_dc_this_week = 1 - AND (is_pregnant = 0 OR is_pregnant IS NULL) - AND @bmi >= 18.5 - AND age >= 20 - AND @cur_status = 'active' - AND days_since_starting_arvs > 364 - AND vl_1 >= 0 - AND vl_1 <= 400 - THEN @eligible_and_on_dc := 1 - ELSE @eligible_and_on_dc := 0 - END AS eligible_and_on_dc, - - CASE - WHEN days_since_last_vl <= 365 - AND vl_2_date IS NOT NULL - AND (months_since_tb_tx_start_date IS NULL - OR months_since_tb_tx_start_date >= 6) - AND on_dc_this_week != 1 - AND (is_pregnant = 0 OR is_pregnant IS NULL) - AND @bmi >= 18.5 - AND age >= 20 - AND @cur_status = 'active' - AND days_since_starting_arvs > 364 - AND vl_1 >= 0 - AND vl_1 <= 400 - THEN @eligible_not_on_dc := 1 - ELSE - @eligible_not_on_dc := 0 - END AS eligible_not_on_dc, - - - IF(scheduled_this_week = 1 AND @eligible_not_on_dc = 1, 1, 0 ) as eligible_not_on_dc_and_scheduled_this_week, - IF(scheduled_this_week = 0 AND @eligible_not_on_dc = 1, 1, 0 ) as eligible_not_on_dc_and_unscheduled_this_week, - IF(scheduled_this_week = 1 AND @eligible_and_on_dc = 1, 1, 0 ) as eligible_and_on_dc_and_scheduled_this_week, - IF(scheduled_this_week = 0 AND @eligible_and_on_dc = 1, 1, 0 ) as eligible_and_on_dc_and_unscheduled_this_week, - IF(@eligible_not_on_dc = 1 OR @eligible_and_on_dc = 1, 1, 0 ) as elligible_total, - IF(@not_elligible_for_dc = 1 AND on_dc_this_week = 1, 1, 0 ) as not_elligible_and_on_dc, - IF(@eligible_not_on_dc = 1 OR on_dc_this_week = 1, 1, 0 ) as elligible_total_revised, - - CASE - WHEN - @prev_id != @cur_id - THEN @previous_dc_eligibility := NULL - ELSE - @previous_dc_eligibility := @dc_eligible_cumulative - END as previous_dc_eligibility, - - CASE - WHEN days_since_last_vl <= 365 - AND vl_2_date IS NOT NULL - AND (months_since_tb_tx_start_date IS NULL - OR months_since_tb_tx_start_date >= 6) - AND on_dc_this_week != 1 - AND (is_pregnant = 0 OR is_pregnant IS NULL) - AND @bmi >= 18.5 - AND age >= 20 - AND @cur_status = 'active' - AND days_since_starting_arvs >= 364 - AND vl_1 >= 0 - AND vl_1 <= 400 - THEN @dc_eligible_cumulative := 1 - ELSE - @dc_eligible_cumulative := 0 - END AS dc_eligible_cumulative, - - IF(@prev_id = @cur_id AND (@previous_dc_eligibility = 0 OR @previous_dc_eligibility = null) AND @dc_eligible_cumulative = 1, 1, 0 ) as dc_eligible_this_week, - - IF(scheduled_this_week = 1 AND @dc_eligible_cumulative = 1, 1, 0 ) as dc_eligible_and_scheduled_this_week, - IF(patients_due_for_vl = 1 AND @cur_status = 'active' - AND scheduled_this_week = 1, - 1, - 0) AS scheduled_this_week_and_due_for_vl, - IF(patients_due_for_vl = 1 - AND @cur_status = 'active' - AND scheduled_this_week = 0, - 1, - 0) AS unscheduled_this_week_and_due_for_vl, - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 1 - AND @cur_status = 'active', - 1, - 0) AS due_for_vl_has_vl_order, - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 0 - AND @cur_status = 'active', - 1, - 0) AS due_for_vl_dont_have_order, - IF(patients_due_for_vl = 1 AND @cur_status = 'active', - 1, - 0) AS due_for_vl_this_week_active, - IF( patients_due_for_vl= 1 - AND @cur_status = 'active', - 1, - 0) AS overdue_for_vl_active, - IF(is_suppressed = 1 - AND @cur_status = 'active', - 1, - 0) AS is_suppressed_active, - IF(is_un_suppressed = 1 - AND @cur_status = 'active', - 1, - 0) AS is_un_suppressed_active, - - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 1 - AND @cur_status = 'active' - AND scheduled_this_week = 1, - 1, - 0) AS due_for_vl_has_vl_order_scheduled, - - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 1 - AND @cur_status = 'active' - AND scheduled_this_week = 0, - 1, - 0) AS due_for_vl_has_vl_order_unscheduled, - - - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 0 - AND @cur_status = 'active' - AND scheduled_this_week = 1, - 1, - 0) AS due_for_vl_dont_have_order_scheduled, - - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 0 - AND @cur_status = 'active' - AND scheduled_this_week = 0, - 1, - 0) AS due_for_vl_dont_have_order_unscheduled, - - IF(is_suppressed = 1 - AND has_vl_this_week = 1, - 1, - 0) AS has_vl_this_week_and_suppressed, - - IF(is_un_suppressed = 1 - AND has_vl_this_week = 1, - 1, - 0) AS has_vl_this_week_and_unsuppressed, - - - - CASE - WHEN @prev_id = @cur_id THEN @prev_defaulted:=@cur_defaulted - ELSE @prev_defaulted:=0 - END AS prev_defaulted, - @cur_defaulted:=defaulted AS cur_defaulted, - IF(@cur_defaulted = 0 - AND @cur_defaulted = 1, - 1, - 0) AS defaulted_this_week, - CASE - WHEN @prev_id = @cur_id THEN @prev_missed:=@cur_missed - ELSE @prev_missed:=0 - END AS prev_missed, - @cur_missed:=missed AS cur_missed, - IF(@prev_missed = 0 AND @cur_missed = 1, - 1, - 0) AS missed_this_week, - CASE - WHEN @prev_id = @cur_id THEN @prev_ltfu:=@cur_ltfu - ELSE @prev_ltfu:=0 - END AS prev_ltfu, - @cur_ltfu:=ltfu AS cur_ltfu, - IF(@cur_ltfu = 0 AND @cur_ltfu = 1, - 1, - 0) AS ltfu_this_week FROM - surge_weekly_report_dataset_1); - - - SELECT CONCAT('replacing into surge_weekly_report_dataset ...'); - replace into surge_weekly_report_dataset_v7 - (select - now(), - elastic_id , - person_uuid , - person_id , - year_week , - encounter_yw , - encounter_id , - encounter_datetime, - encounter_date , - end_date , - start_date , - birthdate , - age , - gender, - clinical_visit_num as clinical_visit_number, - prev_rtc_date , - rtc_date , - visit_this_week , - on_schedule , - early_appointment, - early_appointment_this_week , - late_appointment_this_week , - days_since_rtc_date , - scheduled_this_week , - unscheduled_this_week , - tx2_visit_this_week, - tx2_missed_this_week as missed_tx2_visit_this_week , - tx2_scheduled_this_week_but_came_early, - death_date , - missed_appointment_this_week , - ltfu , - defaulted , - missed , - null as next_status , - active_in_care_this_week , - cur_arv_adherence , - cur_who_stage , - is_pre_art_this_week , - arv_first_regimen_location_id , - arv_first_regimen , - arv_first_regimen_names , - arv_first_regimen_start_date , - days_since_starting_arvs , - started_art_this_week , - enrollment_date , - enrolled_this_week, - art_revisit_this_week , - cur_arv_meds , - cur_arv_meds_names , - cur_arv_meds_strict , - cur_arv_line , - cur_arv_line_strict , - cur_arv_line_reported , - on_art_this_week , - vl_1 , - vl_1_date , - vl_2 , - vl_2_date, - has_vl_this_week , - is_suppressed_active as is_suppressed , - is_un_suppressed_active as is_un_suppressed , - days_since_last_vl , - null as due_for_vl_this_week, - null as reason_for_needing_vl_this_week , - cd4_1 , - cd4_1_date , - child_hiv_status_disclosure_status , - transfer_in_this_week , - transfer_in_location_id , - transfer_in_date , - transfer_out_this_week , - transfer_out_location_id, - transfer_out_date , - status , - dc_eligible_cumulative , - started_dc_this_week , - location_id, - tx2_scheduled_this_week , - tx2_scheduled_honored , - prev_id, - cur_id , - prev_enc_id , - cur_enc_id , - clinical_visit_num , - prev_status , - cur_status , - cur_prep_this_week , - new_prep_this_week, - revised_height as height , - revised_weight as weight , - revised_bmi as bmi , - scheduled_this_week_and_due_for_vl , - unscheduled_this_week_and_due_for_vl , - overdue_for_vl_active , - due_for_vl_has_vl_order, - due_for_vl_dont_have_order, - ltfu_this_week , - missed_this_week , - all_ltfus , - null as ltfu_surge_baseline , - surge_ltfu_and_ltfu_after_may, - surge_ltfu_and_still_ltfu , - newly_ltfu_this_week , - ltfu_cumulative_outcomes_death , - ltfu_cumulative_outcomes_transfer_out , - ltfu_cumulative_outcomes_active, - active_to_ltfu_count , - defaulted_this_week, - due_for_vl_this_week_active , - on_schedule_this_week , - ltfu_cumulative_outcomes_total, - old_ltfus_to_active_this_week, - tx2_unscheduled_this_week , - dead_this_week, - non_ltfu_dead_this_week , - cumulative_dead , - dc_eligible_this_week, - dc_eligible_and_scheduled_this_week, - active_on_dc, - is_ltfu_surge_baseline, - baseline_location, - was_ltfu_may_19 , - was_active_october_18, - not_elligible_for_dc, - eligible_and_on_dc, - eligible_not_on_dc, - eligible_not_on_dc_and_scheduled_this_week, - eligible_not_on_dc_and_unscheduled_this_week, - eligible_and_on_dc_and_scheduled_this_week, - eligible_and_on_dc_and_unscheduled_this_week, - elligible_total, - not_elligible_and_on_dc, - elligible_total_revised, - ltfu_transfer_out_this_week, - ltfu_death_this_week, - ltfu_active_this_week, - - is_ltfu_after_may, - is_ltfu_after_may_total, - week_patient_became_active, - - med_pickup_rtc_date, - days_since_med_pickup_rtc_date, - days_diff_med_rtc_and_next_clinical_date, - - was_ltfu_before_october_2018, - old_ltfus_outcome_total, - old_ltfus_to_active, - intervention_done_this_week, - interventions, - due_for_vl_has_vl_order_scheduled, - due_for_vl_has_vl_order_unscheduled, - due_for_vl_dont_have_order_scheduled, - due_for_vl_dont_have_order_unscheduled, - has_vl_this_week_and_suppressed, - has_vl_this_week_and_unsuppressed, - missed_to_active_this_week, - defaulter_to_active_this_week, - missed_cumulative, - med_surge_ltfus_cumulative, - med_surge_ltfu_and_med_ltfu_after_may, - med_defaulters, - newly_med_ltfu_this_week, - med_surge_ltfus_outcomes, - med_surge_ltfus_outcomes_this_week, - null as column_1, - null as column_2, - null as column_3, - null as column_4, - null as column_5, - null as column_6 - - - from surge_weekly_report_dataset__2); - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join surge_weekly_report_dataset_temporary_build_queue t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count := (select count(*) from @queue_table); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - SELECT - @person_ids_count as 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - if(log = true) then - insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - end if; -SELECT - CONCAT(@table_version, - ' : Time to complete: ', - TIMESTAMPDIFF(MINUTE, @start, @end), - ' minutes'); - - END \ No newline at end of file +DELIMITER $$ +CREATE PROCEDURE `generate_surge_weekly_report_dataset`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int , IN log boolean) +BEGIN + set @primary_table := "surge_weekly_report_dataset"; + set @query_type = query_type; + set @queue_table = ""; + + set @total_rows_written = 0; + set @start = now(); + set @table_version = "surge_weekly_report_dataset"; + + set @last_update = null; + set @last_date_created = (select max(max_date_created) from etl.flat_obs); + set @may_2019 = '2019-05-11'; + set @may_2015 = '2019-05-11'; + set @october_2018 = '2018-10-01'; + + + +CREATE TABLE IF NOT EXISTS surge_weekly_report_dataset( + date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL, + elastic_id BIGINT, + person_uuid CHAR(38), + person_id INT, + year_week VARCHAR(10), + encounter_yw INT, + encounter_id INT, + encounter_datetime DATETIME, + encounter_date DATETIME, + end_date DATE, + start_date DATE, + birthdate DATE, + age DECIMAL(23 , 2 ), + gender VARCHAR(100), + clinical_visit_number INT(11), + prev_rtc_date DATETIME, + rtc_date DATETIME, + visit_this_week INT(1), + on_schedule INT(1), + early_appointment INT(1), + early_appointment_this_week INT(1), + late_appointment_this_week INT(11), + days_since_rtc_date BIGINT(21), + scheduled_this_week INT(1), + unscheduled_this_week INT(0), + tx2_visit_this_week INT(11), + missed_tx2_visit_this_week INT(11), + tx2_scheduled_this_week_but_came_early INT(11), + death_date DATETIME, + missed_appointment_this_week INT(11), + ltfu INT(1), + defaulted INT(1), + missed INT(1), + next_status BINARY(0), + active_in_care_this_week INT(11), + cur_arv_adherence VARCHAR(200), + cur_who_stage INT(11), + is_pre_art_this_week INT(11), + arv_first_regimen_location_id INT(11), + arv_first_regimen VARCHAR(500), + arv_first_regimen_names VARCHAR(500), + arv_first_regimen_start_date DATETIME, + days_since_starting_arvs BIGINT(21), + started_art_this_week INT(1), + enrollment_date DATETIME, + enrolled_this_week INT(1), + art_revisit_this_week INT(0), + cur_arv_meds VARCHAR(500), + cur_arv_meds_names VARCHAR(500), + cur_arv_meds_strict VARCHAR(500), + cur_arv_line INT(11), + cur_arv_line_strict INT(11), + cur_arv_line_reported TINYINT(4), + on_art_this_week BINARY(0), + vl_1 INT(11), + vl_1_date DATETIME, + vl_2 INT(11), + vl_2_date DATETIME, + has_vl_this_week INT(1), + is_suppressed INT(1), + is_un_suppressed INT(1), + days_since_last_vl BIGINT(21), + due_for_vl_this_week INT(0), + reason_for_needing_vl_this_week BINARY(0), + cd4_1 DOUBLE, + cd4_1_date DATETIME, + child_hiv_status_disclosure_status BINARY(0), + transfer_in_this_week INT(0), + transfer_in_location_id INT(11), + transfer_in_date DATETIME, + transfer_out_this_week INT(0), + transfer_out_location_id INT(11), + transfer_out_date DATETIME, + status VARCHAR(12), + dc_eligible_cumulative INT(1), + started_dc_this_week INT(1), + location_id INT(11), + tx2_scheduled_this_week INT(1), + tx2_scheduled_honored INT(1), + prev_id BIGINT(20), + cur_id INT(11), + prev_enc_id BIGINT(20), + cur_enc_id INT(11), + clinical_visit_num BIGINT(63), + prev_status LONGTEXT, + cur_status VARCHAR(12), + cur_prep_this_week INT(11), + new_prep_this_week INT(11), + height DOUBLE, + weight DOUBLE, + bmi DOUBLE, + scheduled_this_week_and_due_for_vl INT(0), + unscheduled_this_week_and_due_for_vl INT(0), + overdue_for_vl_active INT(0), + due_for_vl_has_vl_order INT(0), + due_for_vl_dont_have_order INT(0), + ltfu_this_week INT(0), + missed_this_week INT(0), + all_ltfus INT(0), + ltfu_surge_baseline INT(0), + surge_ltfu_and_ltfu_after_may INT(0), + surge_ltfu_and_still_ltfu INT(0), + newly_ltfu_this_week INT(0), + ltfu_cumulative_outcomes_death INT(0), + ltfu_cumulative_outcomes_transfer_out INT(0), + ltfu_cumulative_outcomes_active INT(0), + active_to_ltfu_count INT(0), + defaulted_this_week INT(0), + due_for_vl_this_week_active INT(0), + on_schedule_this_week INT(0), + ltfu_cumulative_outcomes_total INT(0), + old_ltfus_to_active_this_week INT(0), + tx2_unscheduled_this_week INT(0), + dead_this_week INT(0), + non_ltfu_dead_this_week INT(0), + cumulative_dead INT(0), + dc_eligible_this_week INT(0), + dc_eligible_and_scheduled_this_week INT(0), + active_on_dc INT(0), + is_ltfu_surge_baseline INT(0), + + baseline_location VARCHAR(100), + was_ltfu_may_19 INT(0), + was_active_october_18 INT(0), + not_elligible_for_dc INT(0), + eligible_and_on_dc INT(0), + eligible_not_on_dc INT(0), + eligible_not_on_dc_and_scheduled_this_week INT(0), + eligible_not_on_dc_and_unscheduled_this_week INT(0), + eligible_and_on_dc_and_scheduled_this_week INT(0), + eligible_and_on_dc_and_unscheduled_this_week INT(0), + elligible_total INT(0), + not_elligible_and_on_dc INT(0), + elligible_total_revised INT(0), + ltfu_transfer_out_this_week INT(0), + ltfu_death_this_week INT(0), + ltfu_active_this_week INT(0), + + is_ltfu_after_may INT(0), + is_ltfu_after_may_total INT(0), + + + week_patient_became_active VARCHAR(100), + med_pickup_rtc_date VARCHAR(100), + days_since_med_pickup_rtc_date BIGINT(21), + days_diff_med_rtc_and_next_clinical_date BIGINT(21), + + was_ltfu_before_october_2018 INT(0), + old_ltfus_outcome_total INT(0), + old_ltfus_to_active INT(0), + intervention_done_this_week INT(0), + interventions INT(0), + due_for_vl_has_vl_order_scheduled INT(0), + due_for_vl_has_vl_order_unscheduled INT(0), + due_for_vl_dont_have_order_scheduled INT(0), + due_for_vl_dont_have_order_unscheduled INT(0), + has_vl_this_week_and_suppressed INT(0), + has_vl_this_week_and_unsuppressed INT(0), + missed_to_active_this_week INT(0), + defaulter_to_active_this_week INT(0), + missed_cumulative INT(0), + med_surge_ltfus_cumulative INT(0), + med_surge_ltfu_and_med_ltfu_after_may INT(0), + med_defaulters INT(0), + newly_med_ltfu_this_week INT(0), + med_surge_ltfus_outcomes INT(0), + med_surge_ltfus_outcomes_this_week INT(0), + column_1 INT(0), + column_2 INT(0), + column_3 INT(0), + column_4 VARCHAR(100), + column_5 VARCHAR(100), + column_6 VARCHAR(100), + PRIMARY KEY elastic_id (elastic_id), + INDEX person_enc_date (person_id , encounter_date), + INDEX person_year_week (person_id , year_week), + INDEX date_created (date_created), + INDEX encounter_id (encounter_id), + INDEX person_report_date (person_id , end_date), + INDEX endDate_location_id (end_date , location_id), + INDEX location_id (location_id), + INDEX year_week (year_week), + INDEX person_id (person_id), + INDEX year_week_location_id (year_week , location_id) +); + + if (query_type = "build") then + SELECT 'BUILDING.......................'; + set @write_table = concat("surge_weekly_report_dataset_temp",queue_number); + set @queue_table = concat("surge_weekly_report_dataset_build_queue_",queue_number); + + SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(person_id int primary key) (select * from surge_weekly_report_dataset_build_queue limit ',queue_size, ');'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SET @dyn_sql=CONCAT('delete t1 from surge_weekly_report_dataset_build_queue t1 join ',@queue_table, ' t2 using (person_id)'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + end if; + + if (@query_type="sync") then + SELECT 'SYNCING.......................'; + set @write_table = concat("surge_weekly_report_dataset"); + set @queue_table = "surge_weekly_report_dataset_sync_queue"; + create table if not exists surge_weekly_report_dataset_sync_queue (person_id int primary key); + + select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); + + select "Finding patients in amrs.encounters..."; + + replace into surge_weekly_report_dataset_sync_queue + (select distinct patient_id + from amrs.encounter + where date_changed > @last_update + ); + + + select "Finding patients in flat_obs..."; + + replace into surge_weekly_report_dataset_sync_queue + (select distinct person_id + from etl.flat_obs + where max_date_created > @last_update + ); + + + select "Finding patients in flat_lab_obs..."; + replace into surge_weekly_report_dataset_sync_queue + (select distinct person_id + from etl.flat_lab_obs + where max_date_created > @last_update + ); + + select "Finding patients in flat_orders..."; + + replace into surge_weekly_report_dataset_sync_queue + (select distinct person_id + from etl.flat_orders + where max_date_created > @last_update + ); + + replace into surge_weekly_report_dataset_sync_queue + (select person_id from + amrs.person + where date_voided > @last_update); + + + replace into surge_weekly_report_dataset_sync_queue + (select person_id from + amrs.person + where date_changed > @last_update); + + end if; + + # Remove test patients + SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 + join amrs.person_attribute t2 using (person_id) + where t2.person_attribute_type_id=28 and value="true" and voided=0'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + + SET @num_ids := 0; + SET @dyn_sql=CONCAT('select count(*) into @num_ids from ',@queue_table,';'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SET @person_ids_count = 0; + SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + +SELECT CONCAT('Patients in queue: ', @person_ids_count); + +set @total_time=0; +set @cycle_number = 0; + +while @person_ids_count > 0 do + + set @loop_start_time = now(); + drop temporary table if exists surge_weekly_report_dataset_temporary_build_queue; + create temporary table surge_weekly_report_dataset_temporary_build_queue (person_id int primary key); + + SET @dyn_sql=CONCAT('insert into surge_weekly_report_dataset_temporary_build_queue (select * from ',@queue_table,' limit ',cycle_size,');'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SELECT CONCAT('creating patient_vl ...'); + + CREATE Temporary TABLE if not exists patient_vl_status ( + person_id int(11) DEFAULT NULL, + vl_1 int(11) DEFAULT NULL, + vl_2 int(11) DEFAULT NULL, + vl_2_date datetime DEFAULT NULL, + vl_1_date datetime DEFAULT NULL, + end_date date NOT NULL, + week varchar(10) NOT NULL DEFAULT '', + is_suppressed int(1) NOT NULL DEFAULT '0', + is_un_suppressed int(1) NOT NULL DEFAULT '0', + has_vl_this_week int(1) NOT NULL DEFAULT '0', + days_since_last_vl int(11) DEFAULT NULL, + Primary Key person_week (person_id,week), + KEY person_id (person_id) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + + + replace into patient_vl_status( + SELECT + person_id, + vl_1, + vl_2, + vl_2_date, + vl_1_date, + end_date, + week, + if(vl_1=0 or (vl_1 >= 0 and vl_1 <= 400),1,0 ) as is_suppressed, + if(vl_1>400,1,0 ) as is_un_suppressed, + if(yearweek(vl_1_date)=week,1,0 ) as has_vl_this_week, + TIMESTAMPDIFF(DAY, + vl_1_date, + end_date) AS days_since_last_vl + + FROM + surge_week t1 + JOIN + (SELECT t1a.person_id, vl_1,vl_2,vl_2_date, vl_1_date,arv_start_date,encounter_datetime FROM + etl.flat_hiv_summary_v15b t1a Inner JOIN surge_weekly_report_dataset_temporary_build_queue t3 on t1a.person_id=t3.person_id + WHERE t1a.vl_1_date IS NOT NULL) t2 + + WHERE t2.encounter_datetime < DATE_ADD(t1.end_date, INTERVAL 1 DAY)); + + + SELECT CONCAT('creating surge_vitals ...'); + + CREATE TEMPORARY TABLE if not exists surge_vitals ( + prev_id bigint(20) DEFAULT NULL, + cur_id int(11) DEFAULT NULL, + person_id int(11) DEFAULT NULL, + encounter_id int(11) NOT NULL DEFAULT '0', + encounter_datetime datetime DEFAULT NULL, + height double DEFAULT NULL, + weight double DEFAULT NULL, + bmi double DEFAULT NULL, + Primary KEY encounter_id (encounter_id), + KEY person_id (person_id), + KEY encounter_id (encounter_id), + KEY person_id_2 (person_id,encounter_datetime), + KEY encounter_datetime (encounter_datetime) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + + replace into surge_vitals ( + SELECT + @prev_id := @cur_id AS prev_id, + @cur_id := t1.person_id AS cur_id, + t1.person_id as person_id, + t1.encounter_id, + encounter_datetime, + CASE + WHEN @prev_id != @cur_id THEN @height := height + WHEN @prev_id = @cur_id and @height is null and height is not null THEN @height := height + WHEN @prev_id = @cur_id and @height and height THEN @height := height + ELSE @height + END AS height, + CASE + WHEN @prev_id != @cur_id THEN @weight := weight + WHEN @prev_id = @cur_id and @weight is null and weight is not null THEN @weight := weight + WHEN @prev_id = @cur_id and @weight and weight THEN @weight := weight + ELSE @weight + END AS weight, + (@weight / (@height * @height) * 10000) as bmi + + from + (select encounter_id, height,weight,encounter_datetime,tv.person_id from etl.flat_vitals tv + inner join surge_weekly_report_dataset_temporary_build_queue t2 on tv.person_id=t2.person_id + order by t2.person_id , encounter_datetime) t1); + + set @age =null; + set @status = null; + + + + SELECT CONCAT('creating surge_weekly_report_dataset_0 ...'); + drop temporary table if exists surge_weekly_report_dataset_0; + create temporary table surge_weekly_report_dataset_0 + (index (person_id), index(start_date), index(end_date),index(encounter_date)) + (SELECT + concat(t1.week,t2.person_id) as elastic_id, + t2.uuid AS person_uuid, + t2.person_id, + t1.week AS year_week, + YEARWEEK(t2.encounter_datetime) encounter_yw, + t2.encounter_id, + t2.encounter_datetime, + t2.next_clinical_datetime_hiv, + if(rtc_date >= @october_2018 <= @may_2019, rtc_date, null) as base_rtc_date, + DATE(t2.encounter_datetime) AS encounter_date, + t1.end_date, + t1.start_date, + DATE(birthdate) AS birthdate, + gender, + CASE + WHEN + TIMESTAMPDIFF(YEAR, birthdate, end_date) > 0 + THEN + @age:=ROUND(TIMESTAMPDIFF(YEAR, birthdate, end_date), + 0) + ELSE @age:=ROUND(TIMESTAMPDIFF(MONTH, + birthdate, + end_date) / 12, + 2) + END AS age, + prev_clinical_rtc_date_hiv as prev_rtc_date , + rtc_date, + + + CASE + WHEN + med_pickup_rtc_date IS NOT NULL + AND visit_type NOT IN (24) + + THEN + @med_pickup_rtc_date := med_pickup_rtc_date + + ELSE @med_pickup_rtc_date := NULL + END AS med_pickup_rtc_date, + + # select med_pickup_rtc_date, year_week from surge_weekly_report_dataset_0 where person_id = 21417 + + + IF(YEARWEEK(t2.encounter_datetime) = t1.week, + 1, + 0) AS visit_this_week, + IF(DATE(t2.encounter_datetime) = DATE(prev_clinical_rtc_date_hiv), + 1, + 0) AS on_schedule, + IF((yearweek(prev_clinical_rtc_date_hiv)=t1.week OR yearweek(rtc_date)=t1.week OR (prev_clinical_rtc_date_hiv is null and yearweek(rtc_date)=t1.week)) and + YEARWEEK(t2.encounter_datetime) = t1.week, + 1, + 0) AS on_schedule_this_week, + IF(DATE(t2.encounter_datetime) < DATE(prev_clinical_rtc_date_hiv), + 1, + 0) AS early_appointment, + + IF(YEARWEEK(t2.encounter_datetime) = t1.week AND + (t1.week < YEARWEEK(prev_clinical_rtc_date_hiv) OR (prev_clinical_rtc_date_hiv is null and t1.week < YEARWEEK(rtc_date))) + ,1,0) AS early_appointment_this_week, + + IF(YEARWEEK(t2.encounter_datetime) = t1.week + AND (t1.week > YEARWEEK(prev_clinical_rtc_date_hiv) OR ( prev_clinical_rtc_date_hiv is null and t1.week > YEARWEEK(rtc_date))) + ,1,0) AS late_appointment_this_week, + + TIMESTAMPDIFF(DAY, + rtc_date, + end_date) AS days_since_rtc_date, + + + TIMESTAMPDIFF(DAY, + prev_clinical_rtc_date_hiv, + t2.encounter_datetime) AS days_diff_enc_date_and_prev_rtc, + + TIMESTAMPDIFF(DAY, + @med_pickup_rtc_date, + end_date) AS days_since_med_pickup_rtc_date, + + TIMESTAMPDIFF(DAY, + DATE(med_pickup_rtc_date), + DATE(next_clinical_datetime_hiv) + ) AS days_diff_med_rtc_and_next_clinical_date, + + IF(YEARWEEK(prev_clinical_rtc_date_hiv) = t1.week + OR (prev_clinical_rtc_date_hiv is null and t1.week = YEARWEEK(rtc_date)) OR yearweek(rtc_date)=t1.week, + 1, + 0) AS scheduled_this_week, + CASE + WHEN + YEARWEEK(t2.encounter_datetime) = t1.week + AND (yearweek(prev_clinical_rtc_date_hiv) != t1.week OR (prev_clinical_rtc_date_hiv is null and yearweek(rtc_date) != t1.week)) + AND ((DATE(t2.encounter_datetime) != DATE(prev_clinical_rtc_date_hiv)) OR ( prev_clinical_rtc_date_hiv is null and (DATE(t2.encounter_datetime) != DATE(rtc_date)))) + THEN + 1 + ELSE 0 + END AS unscheduled_this_week, + + # NULL AS tx2_visit_this_week, + # NULL AS missed_tx2_visit_this_week, + t2.death_date, + NULL active_in_care_this_week, + cur_arv_adherence, + NULL cur_who_stage, + NULL is_pre_art_this_week, + arv_first_regimen_location_id, + arv_first_regimen, + etl.get_arv_names(arv_first_regimen) as arv_first_regimen_names, + arv_first_regimen_start_date, + TIMESTAMPDIFF(DAY, + arv_first_regimen_start_date, + end_date) AS days_since_starting_arvs, + IF(YEARWEEK(arv_first_regimen_start_date) = t1.week, + 1, + 0) AS started_art_this_week, + enrollment_date, + IF(YEARWEEK(enrollment_date) = t1.week, + 1, + 0) AS enrolled_this_week, + CASE + WHEN + YEARWEEK(t2.encounter_datetime) = t1.week + AND YEARWEEK(arv_first_regimen_start_date) != t1.week + AND cur_arv_meds IS NOT NULL + AND arv_first_regimen_start_date IS NOT NULL + THEN + 1 + ELSE 0 + END AS art_revisit_this_week, + cur_arv_meds, + etl.get_arv_names(cur_arv_meds) AS cur_arv_meds_names, + cur_arv_meds_strict, + cur_arv_line, + cur_arv_line_strict, + cur_arv_line_reported, + NULL AS on_art_this_week, + cd4_1, + cd4_1_date, + NULL AS child_hiv_status_disclosure_status, + CASE + WHEN YEARWEEK(transfer_in_date) = t1.week THEN 1 + ELSE 0 + END AS transfer_in_this_week, + transfer_in_location_id, + transfer_in_date, + CASE + WHEN YEARWEEK(transfer_out_date) = t1.week THEN 1 + ELSE 0 + END AS transfer_out_this_week, + transfer_out_location_id, + transfer_out_date, + + t2.location_id, + IF((DATE(arv_first_regimen_start_date) = DATE(t2.encounter_datetime) + AND YEARWEEK(rtc_date) = t1.week) or (DATE(arv_first_regimen_start_date) = DATE(prev_clinical_datetime_hiv) + AND YEARWEEK(prev_rtc_date) = t1.week) , + 1, + 0) AS tx2_scheduled_this_week, + IF(DATE(arv_first_regimen_start_date) = DATE(prev_clinical_datetime_hiv) + AND YEARWEEK(t2.encounter_datetime) = t1.week, + 1, + 0) AS tx2_visit_this_week, + IF(YEARWEEK(arv_first_regimen_start_date) = YEARWEEK(prev_clinical_datetime_hiv) + AND YEARWEEK(t2.encounter_datetime) = t1.week AND YEARWEEK(prev_rtc_date) = t1.week, + 1, + 0) AS tx2_scheduled_honored, + + + is_pregnant, + edd, + @date_pregnant := if(edd is not null,DATE_ADD(edd, INTERVAL -9 MONTH),null) as date_confirmed_pregnant, + if(edd is not null and yearweek(edd) >= yearweek(t1.end_date),1,0) as is_still_pregnant, + if(@date_pregnant is not null and YEARWEEK(@date_pregnant) > yearweek(arv_first_regimen_start_date),1,0) as started_arv_b4_pregnancy, + if(@date_pregnant is not null and YEARWEEK(@date_pregnant) < yearweek(arv_first_regimen_start_date),1,0) as started_arv_after_pregnancy, + if(arv_first_regimen_start_date is not null and yearweek(arv_first_regimen_start_date) <= yearweek(t1.end_date) ,1,0) as patient_is_on_art, + breast_feeding, + breast_feeding_encounter_date, + + + + tb_tx_start_date, + TIMESTAMPDIFF(MONTH, + tb_tx_start_date, + end_date) AS months_since_tb_tx_start_date, + CASE + WHEN YEARWEEK(vl_order_date) = t1.week THEN 1 + ELSE 0 + END AS vl_ordered_this_week, + if(YEARWEEK(t2.encounter_datetime)= t1.week and encounter_type=21,1,0) intervention_done_this_week, + if( encounter_type=21,1,0) interventions, + arv_start_date + FROM + surge_week t1 + JOIN etl.flat_hiv_summary_v15b t2 + JOIN surge_weekly_report_dataset_temporary_build_queue t5 USING (person_id) + left join ndwr.patient_breast_feeding bf USING (person_id)#ON(t2.person_id = bf.person_id AND breast_feeding_encounter_date < DATE_ADD(t1.end_date, INTERVAL 1 DAY)) + JOIN + amrs.person t3 USING (person_id) + + WHERE + t2.encounter_datetime < DATE_ADD(t1.end_date, INTERVAL 1 DAY) + AND ((t2.next_clinical_datetime_hiv IS NULL + OR t2.next_clinical_datetime_hiv >= DATE_ADD(t1.end_date, INTERVAL 1 DAY)) || (t2.rtc_date IS NULL + OR t2.rtc_date >= DATE_ADD(t1.end_date, INTERVAL 1 DAY))) + AND t2.encounter_datetime < DATE_ADD(t1.end_date, INTERVAL 1 DAY) + AND t2.is_clinical_encounter = 1 + ORDER BY t2.person_id , t2.encounter_datetime , rtc_date , end_date); + + drop temporary table if exists surge_weekly_report_dataset_01; + CREATE TEMPORARY TABLE `surge_weekly_report_dataset_01` ( + `elastic_id` varchar(21) CHARACTER SET latin1 DEFAULT NULL, + `person_uuid` varchar(100) CHARACTER SET latin1 DEFAULT NULL, + `person_id` int(11) DEFAULT NULL, + `year_week` varchar(10) CHARACTER SET latin1 NOT NULL DEFAULT '', + `encounter_yw` int(6) DEFAULT NULL, + `encounter_id` int(11) NOT NULL DEFAULT '0', + `encounter_datetime` datetime DEFAULT NULL, + `next_clinical_datetime_hiv` datetime DEFAULT NULL, + `base_rtc_date` datetime DEFAULT NULL, + `encounter_date` date DEFAULT NULL, + `end_date` date NOT NULL, + `start_date` date NOT NULL, + `birthdate` date DEFAULT NULL, + `gender` varchar(50) DEFAULT '', + `age` decimal(23,2) DEFAULT NULL, + `prev_rtc_date` datetime DEFAULT NULL, + `rtc_date` datetime DEFAULT NULL, + `med_pickup_rtc_date` varchar(19) DEFAULT NULL, + `visit_this_week` int(1) NOT NULL DEFAULT '0', + `on_schedule` int(1) NOT NULL DEFAULT '0', + `on_schedule_this_week` int(1) NOT NULL DEFAULT '0', + `early_appointment` int(1) NOT NULL DEFAULT '0', + `early_appointment_this_week` int(1) NOT NULL DEFAULT '0', + `late_appointment_this_week` int(1) NOT NULL DEFAULT '0', + `days_since_rtc_date` bigint(21) DEFAULT NULL, + `days_diff_enc_date_and_prev_rtc` bigint(21) DEFAULT NULL, + `days_since_med_pickup_rtc_date` bigint(21) DEFAULT NULL, + `days_diff_med_rtc_and_next_clinical_date` bigint(21) DEFAULT NULL, + `scheduled_this_week` int(1) NOT NULL DEFAULT '0', + `unscheduled_this_week` int(0) DEFAULT NULL, + `death_date` datetime DEFAULT NULL, + `active_in_care_this_week` binary(0) DEFAULT NULL, + `cur_arv_adherence` varchar(200) CHARACTER SET latin1 DEFAULT NULL, + `cur_who_stage` binary(0) DEFAULT NULL, + `is_pre_art_this_week` binary(0) DEFAULT NULL, + `arv_first_regimen_location_id` int(11) DEFAULT NULL, + `arv_first_regimen` varchar(500) CHARACTER SET latin1 DEFAULT NULL, + `arv_first_regimen_names` text CHARACTER SET latin1, + `arv_first_regimen_start_date` datetime DEFAULT NULL, + `days_since_starting_arvs` bigint(21) DEFAULT NULL, + `started_art_this_week` int(1) NOT NULL DEFAULT '0', + `enrollment_date` datetime DEFAULT NULL, + `enrolled_this_week` int(1) NOT NULL DEFAULT '0', + `art_revisit_this_week` int(0) DEFAULT NULL, + `cur_arv_meds` varchar(500) CHARACTER SET latin1 DEFAULT NULL, + `cur_arv_meds_names` text CHARACTER SET latin1, + `cur_arv_meds_strict` varchar(500) CHARACTER SET latin1 DEFAULT NULL, + `cur_arv_line` int(11) DEFAULT NULL, + `cur_arv_line_strict` int(11) DEFAULT NULL, + `cur_arv_line_reported` tinyint(4) DEFAULT NULL, + `on_art_this_week` binary(0) DEFAULT NULL, + `cd4_1` double DEFAULT NULL, + `cd4_1_date` datetime DEFAULT NULL, + `child_hiv_status_disclosure_status` binary(0) DEFAULT NULL, + `transfer_in_this_week` int(0) DEFAULT NULL, + `transfer_in_location_id` int(11) DEFAULT NULL, + `transfer_in_date` datetime DEFAULT NULL, + `transfer_out_this_week` int(0) DEFAULT NULL, + `transfer_out_location_id` int(11) DEFAULT NULL, + `transfer_out_date` datetime DEFAULT NULL, + `location_id` int(11) DEFAULT NULL, + `tx2_scheduled_this_week` int(1) NOT NULL DEFAULT '0', + `tx2_visit_this_week` int(1) NOT NULL DEFAULT '0', + `tx2_scheduled_honored` int(1) NOT NULL DEFAULT '0', + `is_pregnant` tinyint(1) DEFAULT NULL, + `edd` datetime DEFAULT NULL, + `date_confirmed_pregnant` varchar(19) DEFAULT NULL, + `is_still_pregnant` int(1) NOT NULL DEFAULT '0', + `started_arv_b4_pregnancy` int(1) NOT NULL DEFAULT '0', + `started_arv_after_pregnancy` int(1) NOT NULL DEFAULT '0', + `patient_is_on_art` int(1) NOT NULL DEFAULT '0', + `breast_feeding` int(11) DEFAULT NULL, + `breast_feeding_encounter_date` datetime DEFAULT NULL, + `tb_tx_start_date` datetime DEFAULT NULL, + `months_since_tb_tx_start_date` bigint(21) DEFAULT NULL, + `vl_ordered_this_week` int(0) DEFAULT NULL, + `intervention_done_this_week` int(1) NOT NULL DEFAULT '0', + `interventions` int(1) NOT NULL DEFAULT '0', + `arv_start_date` datetime DEFAULT NULL, + PRIMARY KEY(person_id, year_week), + KEY `person_id` (`person_id`), + KEY `start_date` (`start_date`), + KEY `end_date` (`end_date`), + KEY `encounter_date` (`encounter_date`) + + + ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + + replace into surge_weekly_report_dataset_01(select * + from surge_weekly_report_dataset_0); + SELECT + COUNT(*) patients_for_24 + FROM + surge_weekly_report_dataset_0 + WHERE + year_week = 201924; + + SELECT CONCAT('creating surge_weekly_report_dataset__1 ...'); + + drop temporary table if exists surge_weekly_report_dataset_1; + + CREATE temporary TABLE surge_weekly_report_dataset_1 (SELECT sw0.*, + vl.vl_1, + vl.vl_1_date, + vl.vl_2, + vl.vl_2_date, + vl.days_since_last_vl, + + CASE + WHEN vl_1 > 999 + AND vl_1_date > arv_start_date + AND TIMESTAMPDIFF(MONTH,vl_1_date,sw0.end_date) >=3 + THEN + 1 + #is pregnant and started_arv_b4_pregnancy + WHEN is_still_pregnant=1 AND started_arv_b4_pregnancy=1 and vl_1_date is null then 1 + WHEN is_still_pregnant=1 AND started_arv_b4_pregnancy=1 and TIMESTAMPDIFF(MONTH,vl_1_date,date_confirmed_pregnant)>=1 then 1 + WHEN is_still_pregnant=1 AND started_arv_b4_pregnancy=1 and yearweek(vl_1_date) >= yearweek(date_confirmed_pregnant) and TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date)>=6 then 1 + + #is pregnant and started_arv_after_pregnancy + WHEN is_still_pregnant=1 AND patient_is_on_art=1 AND started_arv_after_pregnancy=1 and TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=3 and vl_1_date is null then 1 + WHEN is_still_pregnant=1 AND patient_is_on_art=1 AND started_arv_after_pregnancy=1 and TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=3 and TIMESTAMPDIFF(MONTH,vl_1_date,date_confirmed_pregnant)>=1 then 1 + WHEN is_still_pregnant=1 AND patient_is_on_art=1 AND started_arv_after_pregnancy=1 and TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=3 and TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date)>=6 then 1 + + #is 24 and below years + WHEN age<=24 AND patient_is_on_art=1 AND (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=6 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date)>=6) and vl_1_date is null then 1 + WHEN age <=24 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=6 then 1 + + #is 25 and above years + WHEN age >=25 AND patient_is_on_art=1 AND (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=6 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date)>=6) and vl_1_date is null then 1 + WHEN age >=25 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=6 and (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date) <=12 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) <= 12 ) then 1 + WHEN age >=25 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=12 and (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date) > 12 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) > 12 )then 1 + + # breast feeding + WHEN breast_feeding=1065 AND patient_is_on_art=1 and (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date) >=6 + or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) >=6) and vl_1_date is null then 1 + WHEN breast_feeding=1065 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=6 then 1 + + # drug substitution and no vl ever + WHEN arv_first_regimen_start_date is not null and arv_start_date is not null + and arv_first_regimen_start_date < arv_start_date + AND TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) >= 3 + AND vl_1_date is null then 1 + + # drug substitution and vl was done before change + WHEN arv_first_regimen_start_date is not null and arv_start_date is not null + and arv_first_regimen_start_date < arv_start_date + AND TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) >= 3 + AND TIMESTAMPDIFF(MONTH,vl_1_date,arv_start_date)>=1 then 1 + + + else 0 + + END AS patients_due_for_vl, + + vl.has_vl_this_week, + vl.is_un_suppressed, + vl.is_suppressed, + IF( YEARWEEK(death_date)=year_week , 1,0) as non_ltfu_dead_this_week, + IF( death_date>=@may_2019, 1,0) as cumulative_dead, + CASE + WHEN death_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(death_date) THEN @status:='dead' + WHEN transfer_out_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(transfer_out_date) THEN @status:='transfer_out' + + WHEN days_since_rtc_date > 0 AND days_since_rtc_date < 5 THEN @status:='missed' + + WHEN days_since_rtc_date > 4 AND days_since_rtc_date <= 28 THEN @status:='defaulter' + + WHEN days_since_rtc_date <= 0 THEN @status:='active' + + WHEN days_since_rtc_date > 28 THEN @status:='ltfu' + ELSE @status:='unknown' + END AS status, + + CASE + WHEN death_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(death_date) THEN @status:='unknown' + WHEN transfer_out_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(transfer_out_date) THEN @status:='unknown' + WHEN next_clinical_datetime_hiv IS NULL + AND days_since_med_pickup_rtc_date > 0 + AND days_since_med_pickup_rtc_date < 5 THEN @status:='missed' + + WHEN + next_clinical_datetime_hiv IS NULL + AND days_since_med_pickup_rtc_date > 4 + AND days_since_med_pickup_rtc_date <= 28 THEN @status:='defaulter' + + WHEN next_clinical_datetime_hiv IS NULL + AND days_since_med_pickup_rtc_date > 28 THEN @status:='ltfu' + + WHEN next_clinical_datetime_hiv IS NOT NULL + AND days_diff_med_rtc_and_next_clinical_date > 28 + AND days_since_med_pickup_rtc_date > 28 THEN @status:='ltfu' + + ELSE @status:='unknown' + END AS med_status, + + + + IF(scheduled_this_week =1 and on_schedule_this_week=0 and days_since_rtc_date >0 and days_since_rtc_date <5 + + AND death_date IS NULL AND transfer_out_date IS NULL, 1, 0) AS missed_appointment_this_week, + + IF(days_since_rtc_date >=0 and days_since_rtc_date <5 and @status not in('transfer_out','dead'), 1, 0) AS missed_cumulative , + + IF(days_since_rtc_date >4 and days_since_rtc_date <=28 + AND death_date IS NULL AND transfer_out_date IS NULL, 1, 0) AS defaulted , + + null as ltfu, + null AS missed, + IF(YEARWEEK(t4.date_enrolled) = year_week + AND (t4.program_id = 3 OR t4.program_id = 9), + 1, + 0) AS started_dc_this_week, + IF(t4.program_id = 3 OR t4.program_id = 9, 1, 0) AS active_on_dc, + IF(t4.program_id = 3 OR t4.program_id = 9, + 1, + 0) AS on_dc_this_week, + IF(YEARWEEK(t4.date_enrolled) = year_week + AND t4.program_id = 10, + 1, + 0) AS new_prep_this_week, + IF(t4.program_id = 10 , 1, 0) AS cur_prep_this_week, + t6.weight AS weight, + t6.height AS height, + fd.encounter_datetime as death_reporting_date, + fd.next_encounter_datetime as fd_next_encounter_datetime, + ft.encounter_datetime as transfer_reporting_date, + ft.next_encounter_datetime as ft_next_encounter_datetime, + fe.encounter_datetime as exit_reporting_date, + fe.next_encounter_datetime as fe_next_encounter_datetime, + t6.bmi FROM + surge_weekly_report_dataset_01 sw0 + LEFT OUTER JOIN patient_vl_status vl ON vl.person_id=sw0.person_id and vl.week = sw0.year_week + LEFT OUTER JOIN + amrs.patient_program t4 ON t4.patient_id = sw0.person_id + AND t4.program_id IN (3 , 9, 10, 11) + AND t4.date_completed IS NULL + LEFT OUTER JOIN + surge_vitals t6 ON t6.encounter_id = sw0.encounter_id + LEFT JOIN etl.flat_appointment fd ON (sw0.person_id = fd.person_id and fd.encounter_type = 31) + LEFT JOIN etl.flat_appointment ft ON (sw0.person_id = ft.person_id and ft.encounter_type = 116) + LEFT JOIN etl.flat_appointment fe ON (sw0.person_id = fe.person_id and fe.encounter_type = 157) + ORDER BY sw0.person_id , year_week); + + + set @prev_id = -1; + set @cur_id = -1; + set @prev_enc_id = -1; + set @cur_enc_id = -1; + set @cur_status = ''; + set @prev_status = ''; + set @cur_med_status = ''; + set @prev_med_status = ''; + set @cur_arv_meds = null; + set @prev_arv_meds = null; + set @cur_location_id = null; + set @prev_location_id = null; + Set @clinic_visit_number = 1; + set @prev_patients_due_for_vl = 0; + set @prev_defaulted = 0; + set @cur_defaulted = 0; + set @prev_missed = 0; + set @cur_missed = 0; + set @prev_ltfu = 0; + set @cur_ltfu = 0; + set @base_rtc = null; + + + set @was_active_october_18:= null; + set @was_ltfu_may_19 := null; + set @is_ltfu_surge_baseline := null; + + + + SELECT CONCAT('creating surge_weekly_report_dataset__2 ...'); + + + drop temporary table if exists surge_weekly_report_dataset__2; + CREATE temporary TABLE surge_weekly_report_dataset__2 (SELECT *, + @prev_id:=@cur_id AS prev_id, + @cur_id:=person_id AS cur_id, + @prev_enc_id:=@cur_enc_id AS prev_enc_id, + @cur_enc_id:=encounter_id AS cur_enc_id, + CASE + WHEN + @prev_id = @cur_id + AND @prev_enc_id != @cur_enc_id + THEN + @clinic_visit_number:=@clinic_visit_number + 1 + WHEN + @prev_id = @cur_id + AND @prev_enc_id = @cur_enc_id + THEN + @clinic_visit_number + ELSE @clinic_visit_number:=1 + END AS clinical_visit_num, + + CASE + WHEN @prev_id = @cur_id THEN @prev_status:=@cur_status + ELSE @prev_status:='' + END AS prev_status, + + @cur_status:=status AS cur_status, + + CASE + WHEN @prev_id = @cur_id THEN @prev_med_status:=@cur_med_status + ELSE @prev_med_status:='' + END AS prev_med_status, + + @cur_med_status:=med_status AS cur_med_status, + + CASE + WHEN + (@prev_id != @cur_id + OR @was_active_october_18 IS NULL) + AND year_week < 201938 + AND (@cur_status = 'active' OR @cur_status = 'missed' OR @cur_status = 'defaulter') + AND ((death_date IS NULL OR DATE(death_reporting_date) > '2019-09-30') AND fd_next_encounter_datetime IS NULL) + AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > '2019-09-30')) + AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > '2019-09-30') AND fe_next_encounter_datetime IS NULL) + + THEN + @was_active_october_18:=1 + WHEN @prev_id != @cur_id THEN @was_active_october_18:=NULL + ELSE @was_active_october_18 + END AS was_active_october_18, + + CASE + WHEN + (@prev_id != @cur_id + OR @was_ltfu_may_19 IS NULL) + AND year_week = 201918 + AND @cur_status = 'ltfu' + AND ((death_date IS NULL OR DATE(death_reporting_date) > '2019-05-11') AND fd_next_encounter_datetime IS NULL) + AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > '2019-05-11')) + AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > '2019-05-11') AND fe_next_encounter_datetime IS NULL) + THEN + @was_ltfu_may_19:=1 + WHEN @prev_id != @cur_id THEN @was_ltfu_may_19:=NULL + ELSE @was_ltfu_may_19 + END AS was_ltfu_may_19, + + CASE + WHEN + (@prev_id != @cur_id + OR @is_ltfu_surge_baseline IS NULL) + AND @was_active_october_18 = 1 + AND @was_ltfu_may_19 = 1 + AND year_week <= 201918 + + THEN + @is_ltfu_surge_baseline:=1 + WHEN + (@prev_id != @cur_id + OR @is_ltfu_surge_baseline IS NULL) + AND NOT (@was_active_october_18 = NULL + AND @was_ltfu_may_19 = NULL) + AND year_week <= 201918 + + THEN + @is_ltfu_surge_baseline:=0 + WHEN @prev_id != @cur_id THEN @is_ltfu_surge_baseline:=NULL + ELSE @is_ltfu_surge_baseline + END AS is_ltfu_surge_baseline, + + + CASE + WHEN @prev_id != @cur_id + AND @is_ltfu_surge_baseline = 1 + AND @baseline_location IS NULL + THEN @baseline_location:= location_id + + WHEN @prev_id != @cur_id + AND @is_ltfu_surge_baseline = 1 + AND @baseline_location IS NOT NULL + THEN @baseline_location + + WHEN @prev_id = @cur_id + AND @is_ltfu_surge_baseline = 1 + AND @baseline_location IS NULL + THEN @baseline_location:= location_id + + WHEN @prev_id = @cur_id + AND @is_ltfu_surge_baseline = 1 + AND @baseline_location IS NOT NULL + THEN @baseline_location + + ELSE @baseline_location:= null + END as baseline_location, + + CASE + WHEN + (@prev_id != @cur_id + OR @ever_active_after_sep_19 IS NULL) + AND year_week >=201938 + AND (@cur_status = 'active' OR @cur_status = 'missed' OR @cur_status = 'defaulter') + AND ((death_date IS NULL OR DATE(death_reporting_date) > '2019-09-22') AND fd_next_encounter_datetime IS NULL) + AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > '2019-09-22')) + AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > '2019-09-22') AND fe_next_encounter_datetime IS NULL) + + THEN + @ever_active_after_sep_19:=1 + WHEN @prev_id != @cur_id THEN @ever_active_after_sep_19:=NULL + ELSE @ever_active_after_sep_19 + END AS ever_active_after_sep_19, #active_after_may_19 + + CASE + WHEN + (@prev_id != @cur_id + OR @is_ltfu_after_sep_19 IS NULL) + AND @ever_active_after_sep_19 = 1 + AND @cur_status = 'ltfu' + AND year_week > 201938 + THEN + @is_ltfu_after_sep_19:=1 + WHEN + (@prev_id != @cur_id + OR @is_ltfu_after_sep_19 IS NULL) + AND NOT (@ever_active_after_sep_19 = NULL) + AND year_week > 201938 + + THEN + @is_ltfu_after_sep_19:=0 + WHEN @prev_id != @cur_id THEN @is_ltfu_after_sep_19:=NULL + ELSE @is_ltfu_after_sep_19 + END AS is_ltfu_after_sep_19, #is_ltfu_after_may_revised + + IF((@is_ltfu_after_sep_19 = 1 OR + (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))), + 1, + 0) AS is_ltfu_after_sep_total, + + IF((@is_ltfu_after_sep_19 = 1 OR + (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))) + AND @cur_status = 'ltfu' + AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) + AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) + AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), + 1, + 0) AS is_ltfu_after_sep_not_resolved, + + + #MEDICATION Refill indicators + + CASE + WHEN + (@prev_id != @cur_id + OR @is_med_ltfu_after_sep19 IS NULL) + AND @ever_active_after_sep_19 = 1 + AND @cur_med_status = 'ltfu' + AND year_week > 201938 + THEN + @is_med_ltfu_after_sep19:=1 + WHEN + (@prev_id != @cur_id + OR @is_med_ltfu_after_sep19 IS NULL) + AND NOT (@ever_active_after_sep_19 = NULL) + AND year_week > 201938 + + THEN + @is_med_ltfu_after_sep19:=0 + WHEN @prev_id != @cur_id THEN @is_med_ltfu_after_sep19:=NULL + ELSE @is_med_ltfu_after_sep19 + END AS is_med_ltfu_after_sep19, #med revised + + IF((@is_med_ltfu_after_sep19 = 1), + 1, 0) AS is_med_ltfu_after_sep19_cumulative, + + IF(@is_med_ltfu_after_sep19 = 1 AND @cur_med_status = 'ltfu' + AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) + AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) + AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), + 1, 0) AS is_med_ltfu_after_sep19_unresolved, + + + CASE + WHEN + next_clinical_datetime_hiv IS NULL + AND days_since_med_pickup_rtc_date > 4 + AND days_since_med_pickup_rtc_date <= 28 + AND @cur_status not in('transfer_out','dead') + + THEN + 1 + ELSE 0 + END AS med_defaulters, + + IF(@prev_med_status = 'defaulter' + AND @cur_med_status = 'ltfu' + AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) + AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) + AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), + 1, + 0) AS newly_med_ltfu_this_week, + + IF(@is_med_ltfu_after_sep19 = 1 AND @prev_med_status = 'ltfu' + AND (@cur_med_status = 'unknown' + OR @cur_med_status = 'missed' + OR @cur_med_status = 'defaulter'), + 1, 0) AS med_surge_ltfus_outcomes_this_week, + + IF(@is_med_ltfu_after_sep19 = 1 AND (@cur_med_status = 'unknown' + OR @cur_med_status = 'missed' + OR @cur_med_status = 'defaulter'), + 1, 0) AS med_surge_ltfus_outcomes, + + #MEDICATION Refill indicators final + + + CASE + WHEN @prev_id = @cur_id + AND @is_ltfu_after_sep19 = 1 + AND @prev_status = 'ltfu' AND @cur_status = 'active' + AND @week_patient_became_active IS NULL + AND encounter_datetime > '2019-05-11' + THEN @week_patient_became_active:= encounter_datetime + + WHEN @prev_id = @cur_id + AND @is_ltfu_after_sep19 = 1 + AND @prev_status = 'defaulter' AND @cur_status = 'ltfu' + AND @week_patient_became_active IS NOT NULL + THEN @week_patient_became_active:= NULL + + WHEN @prev_id = @cur_id + AND @is_ltfu_after_sep19 = 1 + AND @prev_status = 'ltfu' AND @cur_status = 'ltfu' + THEN @week_patient_became_active:= NULL + + WHEN @prev_id != @cur_id + AND @is_ltfu_after_sep19 = 1 + AND @prev_status = 'ltfu' AND @cur_status = 'active' + AND @week_patient_became_active IS NULL + AND encounter_datetime > '2019-05-11' + THEN @week_patient_became_active:= encounter_datetime + + WHEN @prev_id != @cur_id + AND @is_ltfu_after_sep19 = 1 + AND @prev_status = 'defaulter' AND @cur_status = 'ltfu' + AND @week_patient_became_active IS NOT NULL + THEN @week_patient_became_active:= NULL + + WHEN @prev_id != @cur_id + AND @is_ltfu_after_sep19 = 1 + AND @prev_status = 'ltfu' AND @cur_status = 'ltfu' + THEN @week_patient_became_active:= NULL + + ELSE @week_patient_became_active + END as week_patient_became_active, + + + IF(@prev_status = 'missed' + AND @cur_status = 'active', + 1, + 0) AS missed_to_active_this_week, + + IF(@prev_status = 'defaulter' + AND @cur_status = 'active', + 1, + 0) AS defaulter_to_active_this_week, + IF((@cur_status = 'ltfu' OR days_since_rtc_date > 28 OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)) + AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) + AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) + AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), + 1, + 0) AS all_ltfus, + + IF( YEARWEEK(death_date)=year_week, 1,0) as dead_this_week, + + IF(@prev_status = 'defaulter' + AND (@cur_status = 'ltfu' OR days_since_rtc_date > 28 OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)) + + AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) + AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) + AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), + 1, + 0) AS newly_ltfu_this_week, + + + # OUTCOMES cumulative outcomes + IF((@is_ltfu_after_sep_19 = 1 OR + (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))) + AND @cur_status = 'dead', + 1, + 0) AS ltfu_cumulative_outcomes_death, + + IF((@is_ltfu_after_sep_19 = 1 OR + (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))) + AND @cur_status = 'transfer_out', + 1, + 0) AS ltfu_cumulative_outcomes_transfer_out, + + IF((@is_ltfu_after_sep_19 = 1 OR + (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))) + AND (@cur_status = 'active' + OR @cur_status = 'transfer_in' + OR @cur_status = 'missed' + OR @cur_status = 'defaulter'), + 1, + 0) AS ltfu_cumulative_outcomes_active, + + IF((@is_ltfu_after_sep_19 = 1 OR + (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))) + AND (@cur_status = 'active' + OR @cur_status = 'transfer_in' + OR @cur_status = 'missed' + OR @cur_status = 'defaulter' + OR @cur_status = 'transfer_out' + OR @cur_status = 'dead'), + 1, + 0) AS ltfu_cumulative_outcomes_total, + + #OUTCOMES weekly outcomes + IF(((@is_ltfu_after_sep_19 = 1 AND @prev_status = 'ltfu') + OR (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <= 0 )))) + AND @cur_status = 'dead', + 1, + 0) AS ltfu_death_this_week, + + IF((@is_ltfu_after_sep_19 = 1 AND @prev_status = 'ltfu' + OR (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <= 0 )))) + AND @cur_status = 'transfer_out', + 1, + 0) AS ltfu_transfer_out_this_week, + + + IF(((@is_ltfu_after_sep_19 = 1 AND @prev_status = 'ltfu') + OR (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <= 0 )))) + AND (@cur_status = 'active' + OR @cur_status = 'missed' + OR @cur_status = 'defaulter' + OR @cur_status = 'transfer_in'), + 1, + 0) AS ltfu_active_this_week, + + IF(((@is_ltfu_after_sep_19 = 1 AND @prev_status = 'ltfu') + OR (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <= 0 )))) + AND (@cur_status = 'active' + OR @cur_status = 'missed' + OR @cur_status = 'defaulter' + OR @cur_status = 'transfer_in' + OR @cur_status = 'dead' + OR @cur_status = 'transfer_out'), + 1, + 0) AS ltfu_cumulative_outcomes_this_week_total, + #OUTCOMES weekly outcomes + + CASE + WHEN + (@prev_id != @cur_id + OR @was_ltfu_before_october_2018 IS NULL) + AND year_week = 201938 + AND @cur_status = 'ltfu' + AND @was_active_october_18 = 0 + THEN + @was_ltfu_before_october_2018:=1 + WHEN @prev_id != @cur_id THEN @was_ltfu_before_october_2018:=NULL + ELSE @was_ltfu_before_october_2018 + END AS was_ltfu_before_october_2018, + + + + #self returnies total + IF(@was_ltfu_before_october_2018 = 1 + AND (@cur_status = 'active' + OR @cur_status = 'missed' + OR @cur_status = 'defaulter' + OR @cur_status = 'transfer_in' + OR @cur_status = 'transfer_out' + OR @cur_status = 'dead'), + 1, + 0) AS old_ltfus_outcome_total, + + IF(@was_ltfu_before_october_2018 = 1 + AND (@cur_status = 'active' + OR @cur_status = 'missed' + OR @cur_status = 'defaulter' + OR @cur_status = 'transfer_out' + OR @cur_status = 'transfer_in' + OR @cur_status = 'dead'), + 1, + 0) AS old_ltfus_to_active, + + + #self returnies this week + IF(@was_ltfu_before_october_2018 = 1 + AND @prev_status = 'ltfu' + AND (@cur_status = 'active' + OR @cur_status = 'missed' + OR @cur_status = 'defaulter' + OR @cur_status = 'transfer_in' + OR @cur_status = 'transfer_out'), + 1, + 0) AS old_ltfus_to_active_this_week, + + + CASE + WHEN + @prev_id = @cur_id + AND @prev_status = 'active' + AND @cur_status = 'ltfu' + THEN + @active_to_ltfu_count:=@active_to_ltfu_count + 1 + ELSE @active_to_ltfu_count:=0 + END AS active_to_ltfu_count, + + CASE + WHEN @prev_id != @cur_id THEN @height:=height + WHEN + @prev_id = @cur_id AND @height IS NULL + AND height IS NOT NULL + THEN + @height:=height + WHEN + @prev_id = @cur_id AND @height + AND height + THEN + @height:=height + ELSE @height + END AS revised_height, + CASE + WHEN @prev_id != @cur_id THEN @weight:=weight + WHEN + @prev_id = @cur_id AND @weight IS NULL + AND weight IS NOT NULL + THEN + @weight:=weight + WHEN + @prev_id = @cur_id AND @weight + AND weight + THEN + @weight:=weight + ELSE @weight + END AS revised_weight, + CASE + WHEN @prev_id != @cur_id THEN @bmi:=bmi + WHEN + @prev_id = @cur_id AND @bmi IS NULL + AND bmi IS NOT NULL + THEN + @bmi:=bmi + WHEN @prev_id = @cur_id AND @bmi AND bmi THEN @bmi:=bmi + ELSE @bmi + END AS revised_bmi, + CASE + WHEN @prev_id = @cur_id THEN @prev_patients_due_for_vl:=@cur_patients_due_for_vl + ELSE @prev_patients_due_for_vl:=0 + END AS prev_patients_due_for_vl, + @cur_patients_due_for_vl:=patients_due_for_vl AS cur_patients_due_for_vl, + IF(tx2_scheduled_this_week = 1 + AND year_week != YEARWEEK(encounter_date) AND prev_rtc_date IS NULL, + 1, + 0) AS tx2_missed_this_week, + IF(tx2_scheduled_this_week = 1 + AND YEARWEEK(encounter_date) < year_week AND prev_rtc_date IS NOT NULL , + 1, + 0) AS tx2_scheduled_this_week_but_came_early, + IF(tx2_scheduled_this_week = 0 + AND tx2_visit_this_week = 1, + 1, + 0) AS tx2_unscheduled_this_week, + + CASE + WHEN days_since_last_vl <= 365 + AND vl_2_date IS NOT NULL + AND (months_since_tb_tx_start_date IS NULL + OR months_since_tb_tx_start_date >= 6) + AND (is_pregnant = 0 OR is_pregnant IS NULL) + AND @bmi >= 18.5 + AND age >= 20 + AND @cur_status = 'active' + AND days_since_starting_arvs > 364 + AND vl_1 >= 0 + AND vl_1 <= 400 + THEN @not_elligible_for_dc := 0 + ELSE @not_elligible_for_dc := 1 + END AS not_elligible_for_dc, + + + CASE + WHEN days_since_last_vl <= 365 + AND vl_2_date IS NOT NULL + AND (months_since_tb_tx_start_date IS NULL + OR months_since_tb_tx_start_date >= 6) + AND on_dc_this_week = 1 + AND (is_pregnant = 0 OR is_pregnant IS NULL) + AND @bmi >= 18.5 + AND age >= 20 + AND @cur_status = 'active' + AND days_since_starting_arvs > 364 + AND vl_1 >= 0 + AND vl_1 <= 400 + THEN @eligible_and_on_dc := 1 + ELSE @eligible_and_on_dc := 0 + END AS eligible_and_on_dc, + + CASE + WHEN days_since_last_vl <= 365 + AND vl_2_date IS NOT NULL + AND (months_since_tb_tx_start_date IS NULL + OR months_since_tb_tx_start_date >= 6) + AND on_dc_this_week != 1 + AND (is_pregnant = 0 OR is_pregnant IS NULL) + AND @bmi >= 18.5 + AND age >= 20 + AND @cur_status = 'active' + AND days_since_starting_arvs > 364 + AND vl_1 >= 0 + AND vl_1 <= 400 + THEN @eligible_not_on_dc := 1 + ELSE + @eligible_not_on_dc := 0 + END AS eligible_not_on_dc, + + + IF(scheduled_this_week = 1 AND @eligible_not_on_dc = 1, 1, 0 ) as eligible_not_on_dc_and_scheduled_this_week, + IF(scheduled_this_week = 0 AND @eligible_not_on_dc = 1, 1, 0 ) as eligible_not_on_dc_and_unscheduled_this_week, + IF(scheduled_this_week = 1 AND @eligible_and_on_dc = 1, 1, 0 ) as eligible_and_on_dc_and_scheduled_this_week, + IF(scheduled_this_week = 0 AND @eligible_and_on_dc = 1, 1, 0 ) as eligible_and_on_dc_and_unscheduled_this_week, + IF(@eligible_not_on_dc = 1 OR @eligible_and_on_dc = 1, 1, 0 ) as elligible_total, + IF(@not_elligible_for_dc = 1 AND on_dc_this_week = 1, 1, 0 ) as not_elligible_and_on_dc, + IF(@eligible_not_on_dc = 1 OR on_dc_this_week = 1, 1, 0 ) as elligible_total_revised, + + CASE + WHEN + @prev_id != @cur_id + THEN @previous_dc_eligibility := NULL + ELSE + @previous_dc_eligibility := @dc_eligible_cumulative + END as previous_dc_eligibility, + + CASE + WHEN days_since_last_vl <= 365 + AND vl_2_date IS NOT NULL + AND (months_since_tb_tx_start_date IS NULL + OR months_since_tb_tx_start_date >= 6) + AND on_dc_this_week != 1 + AND (is_pregnant = 0 OR is_pregnant IS NULL) + AND @bmi >= 18.5 + AND age >= 20 + AND @cur_status = 'active' + AND days_since_starting_arvs >= 364 + AND vl_1 >= 0 + AND vl_1 <= 400 + THEN @dc_eligible_cumulative := 1 + ELSE + @dc_eligible_cumulative := 0 + END AS dc_eligible_cumulative, + + IF(@prev_id = @cur_id AND (@previous_dc_eligibility = 0 OR @previous_dc_eligibility = null) AND @dc_eligible_cumulative = 1, 1, 0 ) as dc_eligible_this_week, + + IF(scheduled_this_week = 1 AND @dc_eligible_cumulative = 1, 1, 0 ) as dc_eligible_and_scheduled_this_week, + IF(patients_due_for_vl = 1 AND @cur_status = 'active' + AND scheduled_this_week = 1, + 1, + 0) AS scheduled_this_week_and_due_for_vl, + IF(patients_due_for_vl = 1 + AND @cur_status = 'active' + AND scheduled_this_week = 0, + 1, + 0) AS unscheduled_this_week_and_due_for_vl, + IF(patients_due_for_vl = 1 + AND vl_ordered_this_week = 1 + AND @cur_status = 'active', + 1, + 0) AS due_for_vl_has_vl_order, + IF(patients_due_for_vl = 1 + AND vl_ordered_this_week = 0 + AND @cur_status = 'active', + 1, + 0) AS due_for_vl_dont_have_order, + IF(patients_due_for_vl = 1 AND @cur_status = 'active', + 1, + 0) AS due_for_vl_this_week_active, + IF( patients_due_for_vl= 1 + AND @cur_status = 'active', + 1, + 0) AS overdue_for_vl_active, + IF(is_suppressed = 1 + AND @cur_status = 'active', + 1, + 0) AS is_suppressed_active, + IF(is_un_suppressed = 1 + AND @cur_status = 'active', + 1, + 0) AS is_un_suppressed_active, + + IF(patients_due_for_vl = 1 + AND vl_ordered_this_week = 1 + AND @cur_status = 'active' + AND scheduled_this_week = 1, + 1, + 0) AS due_for_vl_has_vl_order_scheduled, + + IF(patients_due_for_vl = 1 + AND vl_ordered_this_week = 1 + AND @cur_status = 'active' + AND scheduled_this_week = 0, + 1, + 0) AS due_for_vl_has_vl_order_unscheduled, + + + IF(patients_due_for_vl = 1 + AND vl_ordered_this_week = 0 + AND @cur_status = 'active' + AND scheduled_this_week = 1, + 1, + 0) AS due_for_vl_dont_have_order_scheduled, + + IF(patients_due_for_vl = 1 + AND vl_ordered_this_week = 0 + AND @cur_status = 'active' + AND scheduled_this_week = 0, + 1, + 0) AS due_for_vl_dont_have_order_unscheduled, + + IF(is_suppressed = 1 + AND has_vl_this_week = 1, + 1, + 0) AS has_vl_this_week_and_suppressed, + + IF(is_un_suppressed = 1 + AND has_vl_this_week = 1, + 1, + 0) AS has_vl_this_week_and_unsuppressed, + + + + CASE + WHEN @prev_id = @cur_id THEN @prev_defaulted:=@cur_defaulted + ELSE @prev_defaulted:=0 + END AS prev_defaulted, + @cur_defaulted:=defaulted AS cur_defaulted, + IF(@cur_defaulted = 0 + AND @cur_defaulted = 1, + 1, + 0) AS defaulted_this_week, + CASE + WHEN @prev_id = @cur_id THEN @prev_missed:=@cur_missed + ELSE @prev_missed:=0 + END AS prev_missed, + @cur_missed:=missed AS cur_missed, + IF(@prev_missed = 0 AND @cur_missed = 1, + 1, + 0) AS missed_this_week, + CASE + WHEN @prev_id = @cur_id THEN @prev_ltfu:=@cur_ltfu + ELSE @prev_ltfu:=0 + END AS prev_ltfu, + @cur_ltfu:=ltfu AS cur_ltfu, + IF(@cur_ltfu = 0 AND @cur_ltfu = 1, + 1, + 0) AS ltfu_this_week FROM + surge_weekly_report_dataset_1); + + + SELECT CONCAT('replacing into surge_weekly_report_dataset ...'); + replace into surge_weekly_report_dataset + (select + now(), + elastic_id , + person_uuid , + person_id , + year_week , + encounter_yw , + encounter_id , + encounter_datetime, + encounter_date , + end_date , + start_date , + birthdate , + age , + gender, + clinical_visit_num as clinical_visit_number, + prev_rtc_date , + rtc_date , + visit_this_week , + on_schedule , + early_appointment, + early_appointment_this_week , + late_appointment_this_week , + days_since_rtc_date , + scheduled_this_week , + unscheduled_this_week , + tx2_visit_this_week, + tx2_missed_this_week as missed_tx2_visit_this_week , + tx2_scheduled_this_week_but_came_early, + death_date , + missed_appointment_this_week , + ltfu , + defaulted , + missed , + null as next_status , + active_in_care_this_week , + cur_arv_adherence , + cur_who_stage , + is_pre_art_this_week , + arv_first_regimen_location_id , + arv_first_regimen , + arv_first_regimen_names , + arv_first_regimen_start_date , + days_since_starting_arvs , + started_art_this_week , + enrollment_date , + enrolled_this_week, + art_revisit_this_week , + cur_arv_meds , + cur_arv_meds_names , + cur_arv_meds_strict , + cur_arv_line , + cur_arv_line_strict , + cur_arv_line_reported , + on_art_this_week , + vl_1 , + vl_1_date , + vl_2 , + vl_2_date, + has_vl_this_week , + is_suppressed_active as is_suppressed , + is_un_suppressed_active as is_un_suppressed , + days_since_last_vl , + null as due_for_vl_this_week, + null as reason_for_needing_vl_this_week , + cd4_1 , + cd4_1_date , + child_hiv_status_disclosure_status , + transfer_in_this_week , + transfer_in_location_id , + transfer_in_date , + transfer_out_this_week , + transfer_out_location_id, + transfer_out_date , + status , + dc_eligible_cumulative , + started_dc_this_week , + location_id, + tx2_scheduled_this_week , + tx2_scheduled_honored , + prev_id, + cur_id , + prev_enc_id , + cur_enc_id , + clinical_visit_num , + prev_status , + cur_status , + cur_prep_this_week , + new_prep_this_week, + revised_height as height , + revised_weight as weight , + revised_bmi as bmi , + scheduled_this_week_and_due_for_vl , + unscheduled_this_week_and_due_for_vl , + overdue_for_vl_active , + due_for_vl_has_vl_order, + due_for_vl_dont_have_order, + ltfu_this_week , + missed_this_week , + all_ltfus , + null as ltfu_surge_baseline , + is_ltfu_after_sep_not_resolved as surge_ltfu_and_ltfu_after_may, + null as surge_ltfu_and_still_ltfu , + newly_ltfu_this_week , + ltfu_cumulative_outcomes_death , + ltfu_cumulative_outcomes_transfer_out , + ltfu_cumulative_outcomes_active, + active_to_ltfu_count , + defaulted_this_week, + due_for_vl_this_week_active , + on_schedule_this_week , + ltfu_cumulative_outcomes_total, + old_ltfus_to_active_this_week, + tx2_unscheduled_this_week , + dead_this_week, + non_ltfu_dead_this_week , + cumulative_dead , + dc_eligible_this_week, + dc_eligible_and_scheduled_this_week, + active_on_dc, + is_ltfu_surge_baseline, + baseline_location, + was_ltfu_may_19 , + was_active_october_18, + not_elligible_for_dc, + eligible_and_on_dc, + eligible_not_on_dc, + eligible_not_on_dc_and_scheduled_this_week, + eligible_not_on_dc_and_unscheduled_this_week, + eligible_and_on_dc_and_scheduled_this_week, + eligible_and_on_dc_and_unscheduled_this_week, + elligible_total, + not_elligible_and_on_dc, + elligible_total_revised, + ltfu_transfer_out_this_week, + ltfu_death_this_week, + ltfu_active_this_week, + + is_ltfu_after_sep_not_resolved AS is_ltfu_after_may, + is_ltfu_after_sep_total AS is_ltfu_after_may_total, + week_patient_became_active, + + med_pickup_rtc_date, + days_since_med_pickup_rtc_date, + days_diff_med_rtc_and_next_clinical_date, + + was_ltfu_before_october_2018, + old_ltfus_outcome_total, + old_ltfus_to_active, + intervention_done_this_week, + interventions, + due_for_vl_has_vl_order_scheduled, + due_for_vl_has_vl_order_unscheduled, + due_for_vl_dont_have_order_scheduled, + due_for_vl_dont_have_order_unscheduled, + has_vl_this_week_and_suppressed, + has_vl_this_week_and_unsuppressed, + missed_to_active_this_week, + defaulter_to_active_this_week, + missed_cumulative, + is_med_ltfu_after_sep19_cumulative AS med_surge_ltfus_cumulative, + is_med_ltfu_after_sep19_unresolved AS med_surge_ltfu_and_med_ltfu_after_may, + med_defaulters, + newly_med_ltfu_this_week, + med_surge_ltfus_outcomes, + med_surge_ltfus_outcomes_this_week, + ltfu_cumulative_outcomes_this_week_total as column_1, + null as column_2, + null as column_3, + null as column_4, + null as column_5, + null as column_6 + + + from surge_weekly_report_dataset__2); + + + SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join surge_weekly_report_dataset_temporary_build_queue t2 using (person_id);'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + #select @person_ids_count := (select count(*) from @queue_table); + SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + set @cycle_length = timestampdiff(second,@loop_start_time,now()); + set @total_time = @total_time + @cycle_length; + set @cycle_number = @cycle_number + 1; + + set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); + + SELECT + @person_ids_count as 'persons remaining', + @cycle_length AS 'Cycle time (s)', + CEIL(@person_ids_count / cycle_size) AS remaining_cycles, + @remaining_time AS 'Est time remaining (min)'; + + end while; + + if(@query_type="build") then + SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + SET @total_rows_to_write=0; + SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + set @start_write = now(); + select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); + + SET @dyn_sql=CONCAT('replace into ', @primary_table, + '(select * from ',@write_table,');'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + set @finish_write = now(); + set @time_to_write = timestampdiff(second,@start_write,@finish_write); + select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); + + SET @dyn_sql=CONCAT('drop table ',@write_table,';'); + PREPARE s1 from @dyn_sql; + EXECUTE s1; + DEALLOCATE PREPARE s1; + + end if; + + set @ave_cycle_length = ceil(@total_time/@cycle_number); + select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); + + set @end = now(); + if(log = true) then + insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); + end if; +SELECT + CONCAT(@table_version, + ' : Time to complete: ', + TIMESTAMPDIFF(MINUTE, @start, @end), + ' minutes'); + + END$$ +DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_surge_weekly_report_dataset_v1.sql b/etl-scripts/stored-procedures/generate_surge_weekly_report_dataset_v1.sql deleted file mode 100644 index e1e6092..0000000 --- a/etl-scripts/stored-procedures/generate_surge_weekly_report_dataset_v1.sql +++ /dev/null @@ -1,1832 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_surge_weekly_report_dataset_v1`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int , IN log boolean) -BEGIN - set @primary_table := "surge_weekly_report_dataset"; - set @query_type = query_type; - set @queue_table = ""; - - set @total_rows_written = 0; - set @start = now(); - set @table_version = "surge_weekly_report_dataset"; - - set @last_update = null; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - set @may_2019 = '2019-05-11'; - set @may_2015 = '2019-05-11'; - set @october_2018 = '2018-10-01'; - - - -CREATE TABLE IF NOT EXISTS surge_weekly_report_dataset( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL, - elastic_id BIGINT, - person_uuid CHAR(38), - person_id INT, - year_week VARCHAR(10), - encounter_yw INT, - encounter_id INT, - encounter_datetime DATETIME, - encounter_date DATETIME, - end_date DATE, - start_date DATE, - birthdate DATE, - age DECIMAL(23 , 2 ), - gender VARCHAR(100), - clinical_visit_number INT(11), - prev_rtc_date DATETIME, - rtc_date DATETIME, - visit_this_week INT(1), - on_schedule INT(1), - early_appointment INT(1), - early_appointment_this_week INT(1), - late_appointment_this_week INT(11), - days_since_rtc_date BIGINT(21), - scheduled_this_week INT(1), - unscheduled_this_week INT(0), - tx2_visit_this_week INT(11), - missed_tx2_visit_this_week INT(11), - tx2_scheduled_this_week_but_came_early INT(11), - death_date DATETIME, - missed_appointment_this_week INT(11), - ltfu INT(1), - defaulted INT(1), - missed INT(1), - next_status BINARY(0), - active_in_care_this_week INT(11), - cur_arv_adherence VARCHAR(200), - cur_who_stage INT(11), - is_pre_art_this_week INT(11), - arv_first_regimen_location_id INT(11), - arv_first_regimen VARCHAR(500), - arv_first_regimen_names VARCHAR(500), - arv_first_regimen_start_date DATETIME, - days_since_starting_arvs BIGINT(21), - started_art_this_week INT(1), - enrollment_date DATETIME, - enrolled_this_week INT(1), - art_revisit_this_week INT(0), - cur_arv_meds VARCHAR(500), - cur_arv_meds_names VARCHAR(500), - cur_arv_meds_strict VARCHAR(500), - cur_arv_line INT(11), - cur_arv_line_strict INT(11), - cur_arv_line_reported TINYINT(4), - on_art_this_week BINARY(0), - vl_1 INT(11), - vl_1_date DATETIME, - vl_2 INT(11), - vl_2_date DATETIME, - has_vl_this_week INT(1), - is_suppressed INT(1), - is_un_suppressed INT(1), - days_since_last_vl BIGINT(21), - due_for_vl_this_week INT(0), - reason_for_needing_vl_this_week BINARY(0), - cd4_1 DOUBLE, - cd4_1_date DATETIME, - child_hiv_status_disclosure_status BINARY(0), - transfer_in_this_week INT(0), - transfer_in_location_id INT(11), - transfer_in_date DATETIME, - transfer_out_this_week INT(0), - transfer_out_location_id INT(11), - transfer_out_date DATETIME, - status VARCHAR(12), - dc_eligible_cumulative INT(1), - started_dc_this_week INT(1), - location_id INT(11), - tx2_scheduled_this_week INT(1), - tx2_scheduled_honored INT(1), - prev_id BIGINT(20), - cur_id INT(11), - prev_enc_id BIGINT(20), - cur_enc_id INT(11), - clinical_visit_num BIGINT(63), - prev_status LONGTEXT, - cur_status VARCHAR(12), - cur_prep_this_week INT(11), - new_prep_this_week INT(11), - height DOUBLE, - weight DOUBLE, - bmi DOUBLE, - scheduled_this_week_and_due_for_vl INT(0), - unscheduled_this_week_and_due_for_vl INT(0), - overdue_for_vl_active INT(0), - due_for_vl_has_vl_order INT(0), - due_for_vl_dont_have_order INT(0), - ltfu_this_week INT(0), - missed_this_week INT(0), - all_ltfus INT(0), - ltfu_surge_baseline INT(0), - surge_ltfu_and_ltfu_after_may INT(0), - surge_ltfu_and_still_ltfu INT(0), - newly_ltfu_this_week INT(0), - ltfu_cumulative_outcomes_death INT(0), - ltfu_cumulative_outcomes_transfer_out INT(0), - ltfu_cumulative_outcomes_active INT(0), - active_to_ltfu_count INT(0), - defaulted_this_week INT(0), - due_for_vl_this_week_active INT(0), - on_schedule_this_week INT(0), - ltfu_cumulative_outcomes_total INT(0), - old_ltfus_to_active_this_week INT(0), - tx2_unscheduled_this_week INT(0), - dead_this_week INT(0), - non_ltfu_dead_this_week INT(0), - cumulative_dead INT(0), - dc_eligible_this_week INT(0), - dc_eligible_and_scheduled_this_week INT(0), - active_on_dc INT(0), - is_ltfu_surge_baseline INT(0), - - baseline_location VARCHAR(100), - was_ltfu_may_19 INT(0), - was_active_october_18 INT(0), - not_elligible_for_dc INT(0), - eligible_and_on_dc INT(0), - eligible_not_on_dc INT(0), - eligible_not_on_dc_and_scheduled_this_week INT(0), - eligible_not_on_dc_and_unscheduled_this_week INT(0), - eligible_and_on_dc_and_scheduled_this_week INT(0), - eligible_and_on_dc_and_unscheduled_this_week INT(0), - elligible_total INT(0), - not_elligible_and_on_dc INT(0), - elligible_total_revised INT(0), - ltfu_transfer_out_this_week INT(0), - ltfu_death_this_week INT(0), - ltfu_active_this_week INT(0), - - is_ltfu_after_may INT(0), - is_ltfu_after_may_total INT(0), - - - week_patient_became_active VARCHAR(100), - med_pickup_rtc_date VARCHAR(100), - days_since_med_pickup_rtc_date BIGINT(21), - days_diff_med_rtc_and_next_clinical_date BIGINT(21), - - was_ltfu_before_october_2018 INT(0), - old_ltfus_outcome_total INT(0), - old_ltfus_to_active INT(0), - intervention_done_this_week INT(0), - interventions INT(0), - due_for_vl_has_vl_order_scheduled INT(0), - due_for_vl_has_vl_order_unscheduled INT(0), - due_for_vl_dont_have_order_scheduled INT(0), - due_for_vl_dont_have_order_unscheduled INT(0), - has_vl_this_week_and_suppressed INT(0), - has_vl_this_week_and_unsuppressed INT(0), - missed_to_active_this_week INT(0), - defaulter_to_active_this_week INT(0), - missed_cumulative INT(0), - med_surge_ltfus_cumulative INT(0), - med_surge_ltfu_and_med_ltfu_after_may INT(0), - med_defaulters INT(0), - newly_med_ltfu_this_week INT(0), - med_surge_ltfus_outcomes INT(0), - med_surge_ltfus_outcomes_this_week INT(0), - column_1 INT(0), - column_2 INT(0), - column_3 INT(0), - column_4 VARCHAR(100), - column_5 VARCHAR(100), - column_6 VARCHAR(100), - PRIMARY KEY elastic_id (elastic_id), - INDEX person_enc_date (person_id , encounter_date), - INDEX person_year_week (person_id , year_week), - INDEX date_created (date_created), - INDEX encounter_id (encounter_id), - INDEX person_report_date (person_id , end_date), - INDEX endDate_location_id (end_date , location_id), - INDEX location_id (location_id), - INDEX year_week (year_week), - INDEX person_id (person_id), - INDEX year_week_location_id (year_week , location_id) -); - - if (query_type = "build") then - SELECT 'BUILDING.......................'; - set @write_table = concat("surge_weekly_report_dataset_temp",queue_number); - set @queue_table = concat("surge_weekly_report_dataset_build_queue_",queue_number); - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(person_id int primary key) (select * from surge_weekly_report_dataset_build_queue limit ',queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('delete t1 from surge_weekly_report_dataset_build_queue t1 join ',@queue_table, ' t2 using (person_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - if (@query_type="sync") then - SELECT 'SYNCING.......................'; - set @write_table = concat("surge_weekly_report_dataset"); - set @queue_table = "surge_weekly_report_dataset_sync_queue"; - create table if not exists surge_weekly_report_dataset_sync_queue (person_id int primary key); - - select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); - - select "Finding patients in amrs.encounters..."; - - replace into surge_weekly_report_dataset_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - - select "Finding patients in flat_obs..."; - - replace into surge_weekly_report_dataset_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - - select "Finding patients in flat_lab_obs..."; - replace into surge_weekly_report_dataset_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - select "Finding patients in flat_orders..."; - - replace into surge_weekly_report_dataset_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into surge_weekly_report_dataset_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into surge_weekly_report_dataset_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - end if; - - # Remove test patients - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @num_ids := 0; - SET @dyn_sql=CONCAT('select count(*) into @num_ids from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - -SELECT CONCAT('Patients in queue: ', @person_ids_count); - -set @total_time=0; -set @cycle_number = 0; - -while @person_ids_count > 0 do - - set @loop_start_time = now(); - drop temporary table if exists surge_weekly_report_dataset_temporary_build_queue; - create temporary table surge_weekly_report_dataset_temporary_build_queue (person_id int primary key); - - SET @dyn_sql=CONCAT('insert into surge_weekly_report_dataset_temporary_build_queue (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SELECT CONCAT('creating patient_vl ...'); - - CREATE Temporary TABLE if not exists patient_vl_status ( - person_id int(11) DEFAULT NULL, - vl_1 int(11) DEFAULT NULL, - vl_2 int(11) DEFAULT NULL, - vl_2_date datetime DEFAULT NULL, - vl_1_date datetime DEFAULT NULL, - end_date date NOT NULL, - week varchar(10) NOT NULL DEFAULT '', - is_suppressed int(1) NOT NULL DEFAULT '0', - is_un_suppressed int(1) NOT NULL DEFAULT '0', - has_vl_this_week int(1) NOT NULL DEFAULT '0', - days_since_last_vl int(11) DEFAULT NULL, - Primary Key person_week (person_id,week), - KEY person_id (person_id) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8; - - - replace into patient_vl_status( - SELECT - person_id, - vl_1, - vl_2, - vl_2_date, - vl_1_date, - end_date, - week, - if(vl_1=0 or (vl_1 >= 0 and vl_1 <= 400),1,0 ) as is_suppressed, - if(vl_1>400,1,0 ) as is_un_suppressed, - if(yearweek(vl_1_date)=week,1,0 ) as has_vl_this_week, - TIMESTAMPDIFF(DAY, - vl_1_date, - end_date) AS days_since_last_vl - - FROM - surge_week t1 - JOIN - (SELECT t1a.person_id, vl_1,vl_2,vl_2_date, vl_1_date,arv_start_date,encounter_datetime FROM - etl.flat_hiv_summary_v15b t1a Inner JOIN surge_weekly_report_dataset_temporary_build_queue t3 on t1a.person_id=t3.person_id - WHERE t1a.vl_1_date IS NOT NULL) t2 - - WHERE t2.encounter_datetime < DATE_ADD(t1.end_date, INTERVAL 1 DAY)); - - - SELECT CONCAT('creating surge_vitals ...'); - - CREATE TEMPORARY TABLE if not exists surge_vitals ( - prev_id bigint(20) DEFAULT NULL, - cur_id int(11) DEFAULT NULL, - person_id int(11) DEFAULT NULL, - encounter_id int(11) NOT NULL DEFAULT '0', - encounter_datetime datetime DEFAULT NULL, - height double DEFAULT NULL, - weight double DEFAULT NULL, - bmi double DEFAULT NULL, - Primary KEY encounter_id (encounter_id), - KEY person_id (person_id), - KEY encounter_id (encounter_id), - KEY person_id_2 (person_id,encounter_datetime), - KEY encounter_datetime (encounter_datetime) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8; - - replace into surge_vitals ( - SELECT - @prev_id := @cur_id AS prev_id, - @cur_id := t1.person_id AS cur_id, - t1.person_id as person_id, - t1.encounter_id, - encounter_datetime, - CASE - WHEN @prev_id != @cur_id THEN @height := height - WHEN @prev_id = @cur_id and @height is null and height is not null THEN @height := height - WHEN @prev_id = @cur_id and @height and height THEN @height := height - ELSE @height - END AS height, - CASE - WHEN @prev_id != @cur_id THEN @weight := weight - WHEN @prev_id = @cur_id and @weight is null and weight is not null THEN @weight := weight - WHEN @prev_id = @cur_id and @weight and weight THEN @weight := weight - ELSE @weight - END AS weight, - (@weight / (@height * @height) * 10000) as bmi - - from - (select encounter_id, height,weight,encounter_datetime,tv.person_id from etl.flat_vitals tv - inner join surge_weekly_report_dataset_temporary_build_queue t2 on tv.person_id=t2.person_id - order by t2.person_id , encounter_datetime) t1); - - set @age =null; - set @status = null; - - - - SELECT CONCAT('creating surge_weekly_report_dataset_0 ...'); - drop temporary table if exists surge_weekly_report_dataset_0; - create temporary table surge_weekly_report_dataset_0 - (index (person_id), index(start_date), index(end_date),index(encounter_date)) - (SELECT - concat(t1.week,t2.person_id) as elastic_id, - t2.uuid AS person_uuid, - t2.person_id, - t1.week AS year_week, - YEARWEEK(t2.encounter_datetime) encounter_yw, - t2.encounter_id, - t2.encounter_datetime, - t2.next_clinical_datetime_hiv, - if(rtc_date >= @october_2018 <= @may_2019, rtc_date, null) as base_rtc_date, - DATE(t2.encounter_datetime) AS encounter_date, - t1.end_date, - t1.start_date, - DATE(birthdate) AS birthdate, - gender, - CASE - WHEN - TIMESTAMPDIFF(YEAR, birthdate, end_date) > 0 - THEN - @age:=ROUND(TIMESTAMPDIFF(YEAR, birthdate, end_date), - 0) - ELSE @age:=ROUND(TIMESTAMPDIFF(MONTH, - birthdate, - end_date) / 12, - 2) - END AS age, - prev_clinical_rtc_date_hiv as prev_rtc_date , - rtc_date, - - - CASE - WHEN - med_pickup_rtc_date IS NOT NULL - AND visit_type NOT IN (24) - - THEN - @med_pickup_rtc_date := med_pickup_rtc_date - - ELSE @med_pickup_rtc_date := NULL - END AS med_pickup_rtc_date, - - # select med_pickup_rtc_date, year_week from surge_weekly_report_dataset_0 where person_id = 21417 - - - IF(YEARWEEK(t2.encounter_datetime) = t1.week, - 1, - 0) AS visit_this_week, - IF(DATE(t2.encounter_datetime) = DATE(prev_clinical_rtc_date_hiv), - 1, - 0) AS on_schedule, - IF((yearweek(prev_clinical_rtc_date_hiv)=t1.week OR yearweek(rtc_date)=t1.week OR (prev_clinical_rtc_date_hiv is null and yearweek(rtc_date)=t1.week)) and - YEARWEEK(t2.encounter_datetime) = t1.week, - 1, - 0) AS on_schedule_this_week, - IF(DATE(t2.encounter_datetime) < DATE(prev_clinical_rtc_date_hiv), - 1, - 0) AS early_appointment, - - IF(YEARWEEK(t2.encounter_datetime) = t1.week AND - (t1.week < YEARWEEK(prev_clinical_rtc_date_hiv) OR (prev_clinical_rtc_date_hiv is null and t1.week < YEARWEEK(rtc_date))) - ,1,0) AS early_appointment_this_week, - - IF(YEARWEEK(t2.encounter_datetime) = t1.week - AND (t1.week > YEARWEEK(prev_clinical_rtc_date_hiv) OR ( prev_clinical_rtc_date_hiv is null and t1.week > YEARWEEK(rtc_date))) - ,1,0) AS late_appointment_this_week, - - TIMESTAMPDIFF(DAY, - rtc_date, - end_date) AS days_since_rtc_date, - - - TIMESTAMPDIFF(DAY, - prev_clinical_rtc_date_hiv, - t2.encounter_datetime) AS days_diff_enc_date_and_prev_rtc, - - TIMESTAMPDIFF(DAY, - @med_pickup_rtc_date, - end_date) AS days_since_med_pickup_rtc_date, - - TIMESTAMPDIFF(DAY, - DATE(med_pickup_rtc_date), - DATE(next_clinical_datetime_hiv) - ) AS days_diff_med_rtc_and_next_clinical_date, - - IF(YEARWEEK(prev_clinical_rtc_date_hiv) = t1.week - OR (prev_clinical_rtc_date_hiv is null and t1.week = YEARWEEK(rtc_date)) OR yearweek(rtc_date)=t1.week, - 1, - 0) AS scheduled_this_week, - CASE - WHEN - YEARWEEK(t2.encounter_datetime) = t1.week - AND (yearweek(prev_clinical_rtc_date_hiv) != t1.week OR (prev_clinical_rtc_date_hiv is null and yearweek(rtc_date) != t1.week)) - AND ((DATE(t2.encounter_datetime) != DATE(prev_clinical_rtc_date_hiv)) OR ( prev_clinical_rtc_date_hiv is null and (DATE(t2.encounter_datetime) != DATE(rtc_date)))) - THEN - 1 - ELSE 0 - END AS unscheduled_this_week, - - # NULL AS tx2_visit_this_week, - # NULL AS missed_tx2_visit_this_week, - t2.death_date, - NULL active_in_care_this_week, - cur_arv_adherence, - NULL cur_who_stage, - NULL is_pre_art_this_week, - arv_first_regimen_location_id, - arv_first_regimen, - etl.get_arv_names(arv_first_regimen) as arv_first_regimen_names, - arv_first_regimen_start_date, - TIMESTAMPDIFF(DAY, - arv_first_regimen_start_date, - end_date) AS days_since_starting_arvs, - IF(YEARWEEK(arv_first_regimen_start_date) = t1.week, - 1, - 0) AS started_art_this_week, - enrollment_date, - IF(YEARWEEK(enrollment_date) = t1.week, - 1, - 0) AS enrolled_this_week, - CASE - WHEN - YEARWEEK(t2.encounter_datetime) = t1.week - AND YEARWEEK(arv_first_regimen_start_date) != t1.week - AND cur_arv_meds IS NOT NULL - AND arv_first_regimen_start_date IS NOT NULL - THEN - 1 - ELSE 0 - END AS art_revisit_this_week, - cur_arv_meds, - etl.get_arv_names(cur_arv_meds) AS cur_arv_meds_names, - cur_arv_meds_strict, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - NULL AS on_art_this_week, - cd4_1, - cd4_1_date, - NULL AS child_hiv_status_disclosure_status, - CASE - WHEN YEARWEEK(transfer_in_date) = t1.week THEN 1 - ELSE 0 - END AS transfer_in_this_week, - transfer_in_location_id, - transfer_in_date, - CASE - WHEN YEARWEEK(transfer_out_date) = t1.week THEN 1 - ELSE 0 - END AS transfer_out_this_week, - transfer_out_location_id, - transfer_out_date, - - t2.location_id, - IF((DATE(arv_first_regimen_start_date) = DATE(t2.encounter_datetime) - AND YEARWEEK(rtc_date) = t1.week) or (DATE(arv_first_regimen_start_date) = DATE(prev_clinical_datetime_hiv) - AND YEARWEEK(prev_rtc_date) = t1.week) , - 1, - 0) AS tx2_scheduled_this_week, - IF(DATE(arv_first_regimen_start_date) = DATE(prev_clinical_datetime_hiv) - AND YEARWEEK(t2.encounter_datetime) = t1.week, - 1, - 0) AS tx2_visit_this_week, - IF(YEARWEEK(arv_first_regimen_start_date) = YEARWEEK(prev_clinical_datetime_hiv) - AND YEARWEEK(t2.encounter_datetime) = t1.week AND YEARWEEK(prev_rtc_date) = t1.week, - 1, - 0) AS tx2_scheduled_honored, - - - is_pregnant, - edd, - @date_pregnant := if(edd is not null,DATE_ADD(edd, INTERVAL -9 MONTH),null) as date_confirmed_pregnant, - if(edd is not null and yearweek(edd) >= yearweek(t1.end_date),1,0) as is_still_pregnant, - if(@date_pregnant is not null and YEARWEEK(@date_pregnant) > yearweek(arv_first_regimen_start_date),1,0) as started_arv_b4_pregnancy, - if(@date_pregnant is not null and YEARWEEK(@date_pregnant) < yearweek(arv_first_regimen_start_date),1,0) as started_arv_after_pregnancy, - if(arv_first_regimen_start_date is not null and yearweek(arv_first_regimen_start_date) <= yearweek(t1.end_date) ,1,0) as patient_is_on_art, - breast_feeding, - breast_feeding_encounter_date, - - - - tb_tx_start_date, - TIMESTAMPDIFF(MONTH, - tb_tx_start_date, - end_date) AS months_since_tb_tx_start_date, - CASE - WHEN YEARWEEK(vl_order_date) = t1.week THEN 1 - ELSE 0 - END AS vl_ordered_this_week, - if(YEARWEEK(t2.encounter_datetime)= t1.week and encounter_type=21,1,0) intervention_done_this_week, - if( encounter_type=21,1,0) interventions, - arv_start_date - FROM - surge_week t1 - JOIN etl.flat_hiv_summary_v15b t2 - JOIN surge_weekly_report_dataset_temporary_build_queue t5 USING (person_id) - left join ndwr.patient_breast_feeding bf USING (person_id)#ON(t2.person_id = bf.person_id AND breast_feeding_encounter_date < DATE_ADD(t1.end_date, INTERVAL 1 DAY)) - JOIN - amrs.person t3 USING (person_id) - - WHERE - t2.encounter_datetime < DATE_ADD(t1.end_date, INTERVAL 1 DAY) - AND ((t2.next_clinical_datetime_hiv IS NULL - OR t2.next_clinical_datetime_hiv >= DATE_ADD(t1.end_date, INTERVAL 1 DAY)) || (t2.rtc_date IS NULL - OR t2.rtc_date >= DATE_ADD(t1.end_date, INTERVAL 1 DAY))) - AND t2.encounter_datetime < DATE_ADD(t1.end_date, INTERVAL 1 DAY) - AND t2.is_clinical_encounter = 1 - ORDER BY t2.person_id , t2.encounter_datetime , rtc_date , end_date); - - drop temporary table if exists surge_weekly_report_dataset_01; - CREATE TEMPORARY TABLE `surge_weekly_report_dataset_01` ( - `elastic_id` varchar(21) CHARACTER SET latin1 DEFAULT NULL, - `person_uuid` varchar(100) CHARACTER SET latin1 DEFAULT NULL, - `person_id` int(11) DEFAULT NULL, - `year_week` varchar(10) CHARACTER SET latin1 NOT NULL DEFAULT '', - `encounter_yw` int(6) DEFAULT NULL, - `encounter_id` int(11) NOT NULL DEFAULT '0', - `encounter_datetime` datetime DEFAULT NULL, - `next_clinical_datetime_hiv` datetime DEFAULT NULL, - `base_rtc_date` datetime DEFAULT NULL, - `encounter_date` date DEFAULT NULL, - `end_date` date NOT NULL, - `start_date` date NOT NULL, - `birthdate` date DEFAULT NULL, - `gender` varchar(50) DEFAULT '', - `age` decimal(23,2) DEFAULT NULL, - `prev_rtc_date` datetime DEFAULT NULL, - `rtc_date` datetime DEFAULT NULL, - `med_pickup_rtc_date` varchar(19) DEFAULT NULL, - `visit_this_week` int(1) NOT NULL DEFAULT '0', - `on_schedule` int(1) NOT NULL DEFAULT '0', - `on_schedule_this_week` int(1) NOT NULL DEFAULT '0', - `early_appointment` int(1) NOT NULL DEFAULT '0', - `early_appointment_this_week` int(1) NOT NULL DEFAULT '0', - `late_appointment_this_week` int(1) NOT NULL DEFAULT '0', - `days_since_rtc_date` bigint(21) DEFAULT NULL, - `days_diff_enc_date_and_prev_rtc` bigint(21) DEFAULT NULL, - `days_since_med_pickup_rtc_date` bigint(21) DEFAULT NULL, - `days_diff_med_rtc_and_next_clinical_date` bigint(21) DEFAULT NULL, - `scheduled_this_week` int(1) NOT NULL DEFAULT '0', - `unscheduled_this_week` int(0) DEFAULT NULL, - `death_date` datetime DEFAULT NULL, - `active_in_care_this_week` binary(0) DEFAULT NULL, - `cur_arv_adherence` varchar(200) CHARACTER SET latin1 DEFAULT NULL, - `cur_who_stage` binary(0) DEFAULT NULL, - `is_pre_art_this_week` binary(0) DEFAULT NULL, - `arv_first_regimen_location_id` int(11) DEFAULT NULL, - `arv_first_regimen` varchar(500) CHARACTER SET latin1 DEFAULT NULL, - `arv_first_regimen_names` text CHARACTER SET latin1, - `arv_first_regimen_start_date` datetime DEFAULT NULL, - `days_since_starting_arvs` bigint(21) DEFAULT NULL, - `started_art_this_week` int(1) NOT NULL DEFAULT '0', - `enrollment_date` datetime DEFAULT NULL, - `enrolled_this_week` int(1) NOT NULL DEFAULT '0', - `art_revisit_this_week` int(0) DEFAULT NULL, - `cur_arv_meds` varchar(500) CHARACTER SET latin1 DEFAULT NULL, - `cur_arv_meds_names` text CHARACTER SET latin1, - `cur_arv_meds_strict` varchar(500) CHARACTER SET latin1 DEFAULT NULL, - `cur_arv_line` int(11) DEFAULT NULL, - `cur_arv_line_strict` int(11) DEFAULT NULL, - `cur_arv_line_reported` tinyint(4) DEFAULT NULL, - `on_art_this_week` binary(0) DEFAULT NULL, - `cd4_1` double DEFAULT NULL, - `cd4_1_date` datetime DEFAULT NULL, - `child_hiv_status_disclosure_status` binary(0) DEFAULT NULL, - `transfer_in_this_week` int(0) DEFAULT NULL, - `transfer_in_location_id` int(11) DEFAULT NULL, - `transfer_in_date` datetime DEFAULT NULL, - `transfer_out_this_week` int(0) DEFAULT NULL, - `transfer_out_location_id` int(11) DEFAULT NULL, - `transfer_out_date` datetime DEFAULT NULL, - `location_id` int(11) DEFAULT NULL, - `tx2_scheduled_this_week` int(1) NOT NULL DEFAULT '0', - `tx2_visit_this_week` int(1) NOT NULL DEFAULT '0', - `tx2_scheduled_honored` int(1) NOT NULL DEFAULT '0', - `is_pregnant` tinyint(1) DEFAULT NULL, - `edd` datetime DEFAULT NULL, - `date_confirmed_pregnant` varchar(19) DEFAULT NULL, - `is_still_pregnant` int(1) NOT NULL DEFAULT '0', - `started_arv_b4_pregnancy` int(1) NOT NULL DEFAULT '0', - `started_arv_after_pregnancy` int(1) NOT NULL DEFAULT '0', - `patient_is_on_art` int(1) NOT NULL DEFAULT '0', - `breast_feeding` int(11) DEFAULT NULL, - `breast_feeding_encounter_date` datetime DEFAULT NULL, - `tb_tx_start_date` datetime DEFAULT NULL, - `months_since_tb_tx_start_date` bigint(21) DEFAULT NULL, - `vl_ordered_this_week` int(0) DEFAULT NULL, - `intervention_done_this_week` int(1) NOT NULL DEFAULT '0', - `interventions` int(1) NOT NULL DEFAULT '0', - `arv_start_date` datetime DEFAULT NULL, - PRIMARY KEY(person_id, year_week), - KEY `person_id` (`person_id`), - KEY `start_date` (`start_date`), - KEY `end_date` (`end_date`), - KEY `encounter_date` (`encounter_date`) - - - ) ENGINE=InnoDB DEFAULT CHARSET=utf8; - - replace into surge_weekly_report_dataset_01(select * - from surge_weekly_report_dataset_0); - SELECT - COUNT(*) patients_for_24 - FROM - surge_weekly_report_dataset_0 - WHERE - year_week = 201924; - - SELECT CONCAT('creating surge_weekly_report_dataset__1 ...'); - - drop temporary table if exists surge_weekly_report_dataset_1; - - CREATE temporary TABLE surge_weekly_report_dataset_1 (SELECT sw0.*, - vl.vl_1, - vl.vl_1_date, - vl.vl_2, - vl.vl_2_date, - vl.days_since_last_vl, - - CASE - WHEN vl_1 > 999 - AND vl_1_date > arv_start_date - AND TIMESTAMPDIFF(MONTH,vl_1_date,sw0.end_date) >=3 - THEN - 1 - #is pregnant and started_arv_b4_pregnancy - WHEN is_still_pregnant=1 AND started_arv_b4_pregnancy=1 and vl_1_date is null then 1 - WHEN is_still_pregnant=1 AND started_arv_b4_pregnancy=1 and TIMESTAMPDIFF(MONTH,vl_1_date,date_confirmed_pregnant)>=1 then 1 - WHEN is_still_pregnant=1 AND started_arv_b4_pregnancy=1 and yearweek(vl_1_date) >= yearweek(date_confirmed_pregnant) and TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date)>=6 then 1 - - #is pregnant and started_arv_after_pregnancy - WHEN is_still_pregnant=1 AND patient_is_on_art=1 AND started_arv_after_pregnancy=1 and TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=3 and vl_1_date is null then 1 - WHEN is_still_pregnant=1 AND patient_is_on_art=1 AND started_arv_after_pregnancy=1 and TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=3 and TIMESTAMPDIFF(MONTH,vl_1_date,date_confirmed_pregnant)>=1 then 1 - WHEN is_still_pregnant=1 AND patient_is_on_art=1 AND started_arv_after_pregnancy=1 and TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=3 and TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date)>=6 then 1 - - #is 24 and below years - WHEN age<=24 AND patient_is_on_art=1 AND (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=6 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date)>=6) and vl_1_date is null then 1 - WHEN age <=24 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=6 then 1 - - #is 25 and above years - WHEN age >=25 AND patient_is_on_art=1 AND (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=6 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date)>=6) and vl_1_date is null then 1 - WHEN age >=25 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=6 and (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date) <=12 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) <= 12 ) then 1 - WHEN age >=25 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=12 and (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date) > 12 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) > 12 )then 1 - - # breast feeding - WHEN breast_feeding=1065 AND patient_is_on_art=1 and (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date) >=6 - or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) >=6) and vl_1_date is null then 1 - WHEN breast_feeding=1065 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=6 then 1 - - # drug substitution and no vl ever - WHEN arv_first_regimen_start_date is not null and arv_start_date is not null - and arv_first_regimen_start_date < arv_start_date - AND TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) >= 3 - AND vl_1_date is null then 1 - - # drug substitution and vl was done before change - WHEN arv_first_regimen_start_date is not null and arv_start_date is not null - and arv_first_regimen_start_date < arv_start_date - AND TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) >= 3 - AND TIMESTAMPDIFF(MONTH,vl_1_date,arv_start_date)>=1 then 1 - - - else 0 - - END AS patients_due_for_vl, - - vl.has_vl_this_week, - vl.is_un_suppressed, - vl.is_suppressed, - IF( YEARWEEK(death_date)=year_week , 1,0) as non_ltfu_dead_this_week, - IF( death_date>=@may_2019, 1,0) as cumulative_dead, - CASE - WHEN death_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(death_date) THEN @status:='dead' - WHEN transfer_out_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(transfer_out_date) THEN @status:='transfer_out' - - WHEN days_since_rtc_date > 0 AND days_since_rtc_date < 5 THEN @status:='missed' - - WHEN days_since_rtc_date > 4 AND days_since_rtc_date <= 28 THEN @status:='defaulter' - - WHEN days_since_rtc_date <= 0 THEN @status:='active' - - WHEN days_since_rtc_date > 28 THEN @status:='ltfu' - ELSE @status:='unknown' - END AS status, - - CASE - WHEN death_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(death_date) THEN @status:='unknown' - WHEN transfer_out_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(transfer_out_date) THEN @status:='unknown' - WHEN next_clinical_datetime_hiv IS NULL - AND days_since_med_pickup_rtc_date > 0 - AND days_since_med_pickup_rtc_date < 5 THEN @status:='missed' - - WHEN - next_clinical_datetime_hiv IS NULL - AND days_since_med_pickup_rtc_date > 4 - AND days_since_med_pickup_rtc_date <= 28 THEN @status:='defaulter' - - WHEN next_clinical_datetime_hiv IS NULL - AND days_since_med_pickup_rtc_date > 28 THEN @status:='ltfu' - - WHEN next_clinical_datetime_hiv IS NOT NULL - AND days_diff_med_rtc_and_next_clinical_date > 28 - AND days_since_med_pickup_rtc_date > 28 THEN @status:='ltfu' - - ELSE @status:='unknown' - END AS med_status, - - - - IF(scheduled_this_week =1 and on_schedule_this_week=0 and days_since_rtc_date >0 and days_since_rtc_date <5 - - AND death_date IS NULL AND transfer_out_date IS NULL, 1, 0) AS missed_appointment_this_week, - - IF(days_since_rtc_date >=0 and days_since_rtc_date <5 and @status not in('transfer_out','dead'), 1, 0) AS missed_cumulative , - - IF(days_since_rtc_date >4 and days_since_rtc_date <=28 - AND death_date IS NULL AND transfer_out_date IS NULL, 1, 0) AS defaulted , - - null as ltfu, - null AS missed, - IF(YEARWEEK(t4.date_enrolled) = year_week - AND (t4.program_id = 3 OR t4.program_id = 9), - 1, - 0) AS started_dc_this_week, - IF(t4.program_id = 3 OR t4.program_id = 9, 1, 0) AS active_on_dc, - IF(t4.program_id = 3 OR t4.program_id = 9, - 1, - 0) AS on_dc_this_week, - IF(YEARWEEK(t4.date_enrolled) = year_week - AND t4.program_id = 10, - 1, - 0) AS new_prep_this_week, - IF(t4.program_id = 10 , 1, 0) AS cur_prep_this_week, - t6.weight AS weight, - t6.height AS height, - fd.encounter_datetime as death_reporting_date, - fd.next_encounter_datetime as fd_next_encounter_datetime, - ft.encounter_datetime as transfer_reporting_date, - ft.next_encounter_datetime as ft_next_encounter_datetime, - fe.encounter_datetime as exit_reporting_date, - fe.next_encounter_datetime as fe_next_encounter_datetime, - t6.bmi FROM - surge_weekly_report_dataset_01 sw0 - LEFT OUTER JOIN patient_vl_status vl ON vl.person_id=sw0.person_id and vl.week = sw0.year_week - LEFT OUTER JOIN - amrs.patient_program t4 ON t4.patient_id = sw0.person_id - AND t4.program_id IN (3 , 9, 10, 11) - AND t4.date_completed IS NULL - LEFT OUTER JOIN - surge_vitals t6 ON t6.encounter_id = sw0.encounter_id - LEFT JOIN etl.flat_appointment fd ON (sw0.person_id = fd.person_id and fd.encounter_type = 31) - LEFT JOIN etl.flat_appointment ft ON (sw0.person_id = ft.person_id and ft.encounter_type = 116) - LEFT JOIN etl.flat_appointment fe ON (sw0.person_id = fe.person_id and fe.encounter_type = 157) - ORDER BY sw0.person_id , year_week); - - - set @prev_id = -1; - set @cur_id = -1; - set @prev_enc_id = -1; - set @cur_enc_id = -1; - set @cur_status = ''; - set @prev_status = ''; - set @cur_med_status = ''; - set @prev_med_status = ''; - set @cur_arv_meds = null; - set @prev_arv_meds = null; - set @cur_location_id = null; - set @prev_location_id = null; - Set @clinic_visit_number = 1; - set @prev_patients_due_for_vl = 0; - set @prev_defaulted = 0; - set @cur_defaulted = 0; - set @prev_missed = 0; - set @cur_missed = 0; - set @prev_ltfu = 0; - set @cur_ltfu = 0; - set @base_rtc = null; - - - set @was_active_october_18:= null; - set @was_ltfu_may_19 := null; - set @is_ltfu_surge_baseline := null; - - - - SELECT CONCAT('creating surge_weekly_report_dataset__2 ...'); - - - drop temporary table if exists surge_weekly_report_dataset__2; - CREATE temporary TABLE surge_weekly_report_dataset__2 (SELECT *, - @prev_id:=@cur_id AS prev_id, - @cur_id:=person_id AS cur_id, - @prev_enc_id:=@cur_enc_id AS prev_enc_id, - @cur_enc_id:=encounter_id AS cur_enc_id, - CASE - WHEN - @prev_id = @cur_id - AND @prev_enc_id != @cur_enc_id - THEN - @clinic_visit_number:=@clinic_visit_number + 1 - WHEN - @prev_id = @cur_id - AND @prev_enc_id = @cur_enc_id - THEN - @clinic_visit_number - ELSE @clinic_visit_number:=1 - END AS clinical_visit_num, - - CASE - WHEN @prev_id = @cur_id THEN @prev_status:=@cur_status - ELSE @prev_status:='' - END AS prev_status, - - @cur_status:=status AS cur_status, - - CASE - WHEN @prev_id = @cur_id THEN @prev_med_status:=@cur_med_status - ELSE @prev_med_status:='' - END AS prev_med_status, - - @cur_med_status:=med_status AS cur_med_status, - - CASE - WHEN - (@prev_id != @cur_id - OR @was_active_october_18 IS NULL) - AND year_week < 201938 - AND (@cur_status = 'active' OR @cur_status = 'missed' OR @cur_status = 'defaulter') - AND ((death_date IS NULL OR DATE(death_reporting_date) > '2019-09-30') AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > '2019-09-30')) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > '2019-09-30') AND fe_next_encounter_datetime IS NULL) - - THEN - @was_active_october_18:=1 - WHEN @prev_id != @cur_id THEN @was_active_october_18:=NULL - ELSE @was_active_october_18 - END AS was_active_october_18, - - CASE - WHEN - (@prev_id != @cur_id - OR @was_ltfu_may_19 IS NULL) - AND year_week = 201918 - AND @cur_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > '2019-05-11') AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > '2019-05-11')) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > '2019-05-11') AND fe_next_encounter_datetime IS NULL) - THEN - @was_ltfu_may_19:=1 - WHEN @prev_id != @cur_id THEN @was_ltfu_may_19:=NULL - ELSE @was_ltfu_may_19 - END AS was_ltfu_may_19, - - CASE - WHEN - (@prev_id != @cur_id - OR @is_ltfu_surge_baseline IS NULL) - AND @was_active_october_18 = 1 - AND @was_ltfu_may_19 = 1 - AND year_week <= 201918 - - THEN - @is_ltfu_surge_baseline:=1 - WHEN - (@prev_id != @cur_id - OR @is_ltfu_surge_baseline IS NULL) - AND NOT (@was_active_october_18 = NULL - AND @was_ltfu_may_19 = NULL) - AND year_week <= 201918 - - THEN - @is_ltfu_surge_baseline:=0 - WHEN @prev_id != @cur_id THEN @is_ltfu_surge_baseline:=NULL - ELSE @is_ltfu_surge_baseline - END AS is_ltfu_surge_baseline, - - - CASE - WHEN @prev_id != @cur_id - AND @is_ltfu_surge_baseline = 1 - AND @baseline_location IS NULL - THEN @baseline_location:= location_id - - WHEN @prev_id != @cur_id - AND @is_ltfu_surge_baseline = 1 - AND @baseline_location IS NOT NULL - THEN @baseline_location - - WHEN @prev_id = @cur_id - AND @is_ltfu_surge_baseline = 1 - AND @baseline_location IS NULL - THEN @baseline_location:= location_id - - WHEN @prev_id = @cur_id - AND @is_ltfu_surge_baseline = 1 - AND @baseline_location IS NOT NULL - THEN @baseline_location - - ELSE @baseline_location:= null - END as baseline_location, - - CASE - WHEN - (@prev_id != @cur_id - OR @ever_active_after_sep_19 IS NULL) - AND year_week >=201938 - AND (@cur_status = 'active' OR @cur_status = 'missed' OR @cur_status = 'defaulter') - AND ((death_date IS NULL OR DATE(death_reporting_date) > '2019-09-22') AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > '2019-09-22')) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > '2019-09-22') AND fe_next_encounter_datetime IS NULL) - - THEN - @ever_active_after_sep_19:=1 - WHEN @prev_id != @cur_id THEN @ever_active_after_sep_19:=NULL - ELSE @ever_active_after_sep_19 - END AS ever_active_after_sep_19, #active_after_may_19 - - CASE - WHEN - (@prev_id != @cur_id - OR @is_ltfu_after_sep_19 IS NULL) - AND @ever_active_after_sep_19 = 1 - AND @cur_status = 'ltfu' - AND year_week > 201938 - THEN - @is_ltfu_after_sep_19:=1 - WHEN - (@prev_id != @cur_id - OR @is_ltfu_after_sep_19 IS NULL) - AND NOT (@ever_active_after_sep_19 = NULL) - AND year_week > 201938 - - THEN - @is_ltfu_after_sep_19:=0 - WHEN @prev_id != @cur_id THEN @is_ltfu_after_sep_19:=NULL - ELSE @is_ltfu_after_sep_19 - END AS is_ltfu_after_sep_19, #is_ltfu_after_may_revised - - IF((@is_ltfu_after_sep_19 = 1 OR - (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))), - 1, - 0) AS is_ltfu_after_sep_total, - - IF((@is_ltfu_after_sep_19 = 1 OR - (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))) - AND @cur_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, - 0) AS is_ltfu_after_sep_not_resolved, - - - #MEDICATION Refill indicators - - CASE - WHEN - (@prev_id != @cur_id - OR @is_med_ltfu_after_sep19 IS NULL) - AND @ever_active_after_sep_19 = 1 - AND @cur_med_status = 'ltfu' - AND year_week > 201938 - THEN - @is_med_ltfu_after_sep19:=1 - WHEN - (@prev_id != @cur_id - OR @is_med_ltfu_after_sep19 IS NULL) - AND NOT (@ever_active_after_sep_19 = NULL) - AND year_week > 201938 - - THEN - @is_med_ltfu_after_sep19:=0 - WHEN @prev_id != @cur_id THEN @is_med_ltfu_after_sep19:=NULL - ELSE @is_med_ltfu_after_sep19 - END AS is_med_ltfu_after_sep19, #med revised - - IF((@is_med_ltfu_after_sep19 = 1), - 1, 0) AS is_med_ltfu_after_sep19_cumulative, - - IF(@is_med_ltfu_after_sep19 = 1 AND @cur_med_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, 0) AS is_med_ltfu_after_sep19_unresolved, - - - CASE - WHEN - next_clinical_datetime_hiv IS NULL - AND days_since_med_pickup_rtc_date > 4 - AND days_since_med_pickup_rtc_date <= 28 - AND @cur_status not in('transfer_out','dead') - - THEN - 1 - ELSE 0 - END AS med_defaulters, - - IF(@prev_med_status = 'defaulter' - AND @cur_med_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, - 0) AS newly_med_ltfu_this_week, - - IF(@is_med_ltfu_after_sep19 = 1 AND @prev_med_status = 'ltfu' - AND (@cur_med_status = 'unknown' - OR @cur_med_status = 'missed' - OR @cur_med_status = 'defaulter'), - 1, 0) AS med_surge_ltfus_outcomes_this_week, - - IF(@is_med_ltfu_after_sep19 = 1 AND (@cur_med_status = 'unknown' - OR @cur_med_status = 'missed' - OR @cur_med_status = 'defaulter'), - 1, 0) AS med_surge_ltfus_outcomes, - - #MEDICATION Refill indicators final - - - CASE - WHEN @prev_id = @cur_id - AND @is_ltfu_after_sep19 = 1 - AND @prev_status = 'ltfu' AND @cur_status = 'active' - AND @week_patient_became_active IS NULL - AND encounter_datetime > '2019-05-11' - THEN @week_patient_became_active:= encounter_datetime - - WHEN @prev_id = @cur_id - AND @is_ltfu_after_sep19 = 1 - AND @prev_status = 'defaulter' AND @cur_status = 'ltfu' - AND @week_patient_became_active IS NOT NULL - THEN @week_patient_became_active:= NULL - - WHEN @prev_id = @cur_id - AND @is_ltfu_after_sep19 = 1 - AND @prev_status = 'ltfu' AND @cur_status = 'ltfu' - THEN @week_patient_became_active:= NULL - - WHEN @prev_id != @cur_id - AND @is_ltfu_after_sep19 = 1 - AND @prev_status = 'ltfu' AND @cur_status = 'active' - AND @week_patient_became_active IS NULL - AND encounter_datetime > '2019-05-11' - THEN @week_patient_became_active:= encounter_datetime - - WHEN @prev_id != @cur_id - AND @is_ltfu_after_sep19 = 1 - AND @prev_status = 'defaulter' AND @cur_status = 'ltfu' - AND @week_patient_became_active IS NOT NULL - THEN @week_patient_became_active:= NULL - - WHEN @prev_id != @cur_id - AND @is_ltfu_after_sep19 = 1 - AND @prev_status = 'ltfu' AND @cur_status = 'ltfu' - THEN @week_patient_became_active:= NULL - - ELSE @week_patient_became_active - END as week_patient_became_active, - - - IF(@prev_status = 'missed' - AND @cur_status = 'active', - 1, - 0) AS missed_to_active_this_week, - - IF(@prev_status = 'defaulter' - AND @cur_status = 'active', - 1, - 0) AS defaulter_to_active_this_week, - IF((@cur_status = 'ltfu' OR days_since_rtc_date > 28 OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)) - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, - 0) AS all_ltfus, - - IF( YEARWEEK(death_date)=year_week, 1,0) as dead_this_week, - - IF(@prev_status = 'defaulter' - AND (@cur_status = 'ltfu' OR days_since_rtc_date > 28 OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)) - - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, - 0) AS newly_ltfu_this_week, - - - # OUTCOMES cumulative outcomes - IF((@is_ltfu_after_sep_19 = 1 OR - (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))) - AND @cur_status = 'dead', - 1, - 0) AS ltfu_cumulative_outcomes_death, - - IF((@is_ltfu_after_sep_19 = 1 OR - (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))) - AND @cur_status = 'transfer_out', - 1, - 0) AS ltfu_cumulative_outcomes_transfer_out, - - IF((@is_ltfu_after_sep_19 = 1 OR - (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))) - AND (@cur_status = 'active' - OR @cur_status = 'transfer_in' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter'), - 1, - 0) AS ltfu_cumulative_outcomes_active, - - IF((@is_ltfu_after_sep_19 = 1 OR - (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))) - AND (@cur_status = 'active' - OR @cur_status = 'transfer_in' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_out' - OR @cur_status = 'dead'), - 1, - 0) AS ltfu_cumulative_outcomes_total, - - #OUTCOMES weekly outcomes - IF(((@is_ltfu_after_sep_19 = 1 AND @prev_status = 'ltfu') - OR (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <= 0 )))) - AND @cur_status = 'dead', - 1, - 0) AS ltfu_death_this_week, - - IF((@is_ltfu_after_sep_19 = 1 AND @prev_status = 'ltfu' - OR (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <= 0 )))) - AND @cur_status = 'transfer_out', - 1, - 0) AS ltfu_transfer_out_this_week, - - - IF(((@is_ltfu_after_sep_19 = 1 AND @prev_status = 'ltfu') - OR (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <= 0 )))) - AND (@cur_status = 'active' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_in'), - 1, - 0) AS ltfu_active_this_week, - - IF(((@is_ltfu_after_sep_19 = 1 AND @prev_status = 'ltfu') - OR (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <= 0 )))) - AND (@cur_status = 'active' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_in' - OR @cur_status = 'dead' - OR @cur_status = 'transfer_out'), - 1, - 0) AS ltfu_cumulative_outcomes_this_week_total, - #OUTCOMES weekly outcomes - - CASE - WHEN - (@prev_id != @cur_id - OR @was_ltfu_before_october_2018 IS NULL) - AND year_week = 201938 - AND @cur_status = 'ltfu' - AND @was_active_october_18 = 0 - THEN - @was_ltfu_before_october_2018:=1 - WHEN @prev_id != @cur_id THEN @was_ltfu_before_october_2018:=NULL - ELSE @was_ltfu_before_october_2018 - END AS was_ltfu_before_october_2018, - - - - #self returnies total - IF(@was_ltfu_before_october_2018 = 1 - AND (@cur_status = 'active' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_in' - OR @cur_status = 'transfer_out' - OR @cur_status = 'dead'), - 1, - 0) AS old_ltfus_outcome_total, - - IF(@was_ltfu_before_october_2018 = 1 - AND (@cur_status = 'active' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_out' - OR @cur_status = 'transfer_in' - OR @cur_status = 'dead'), - 1, - 0) AS old_ltfus_to_active, - - - #self returnies this week - IF(@was_ltfu_before_october_2018 = 1 - AND @prev_status = 'ltfu' - AND (@cur_status = 'active' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_in' - OR @cur_status = 'transfer_out'), - 1, - 0) AS old_ltfus_to_active_this_week, - - - CASE - WHEN - @prev_id = @cur_id - AND @prev_status = 'active' - AND @cur_status = 'ltfu' - THEN - @active_to_ltfu_count:=@active_to_ltfu_count + 1 - ELSE @active_to_ltfu_count:=0 - END AS active_to_ltfu_count, - - CASE - WHEN @prev_id != @cur_id THEN @height:=height - WHEN - @prev_id = @cur_id AND @height IS NULL - AND height IS NOT NULL - THEN - @height:=height - WHEN - @prev_id = @cur_id AND @height - AND height - THEN - @height:=height - ELSE @height - END AS revised_height, - CASE - WHEN @prev_id != @cur_id THEN @weight:=weight - WHEN - @prev_id = @cur_id AND @weight IS NULL - AND weight IS NOT NULL - THEN - @weight:=weight - WHEN - @prev_id = @cur_id AND @weight - AND weight - THEN - @weight:=weight - ELSE @weight - END AS revised_weight, - CASE - WHEN @prev_id != @cur_id THEN @bmi:=bmi - WHEN - @prev_id = @cur_id AND @bmi IS NULL - AND bmi IS NOT NULL - THEN - @bmi:=bmi - WHEN @prev_id = @cur_id AND @bmi AND bmi THEN @bmi:=bmi - ELSE @bmi - END AS revised_bmi, - CASE - WHEN @prev_id = @cur_id THEN @prev_patients_due_for_vl:=@cur_patients_due_for_vl - ELSE @prev_patients_due_for_vl:=0 - END AS prev_patients_due_for_vl, - @cur_patients_due_for_vl:=patients_due_for_vl AS cur_patients_due_for_vl, - IF(tx2_scheduled_this_week = 1 - AND year_week != YEARWEEK(encounter_date) AND prev_rtc_date IS NULL, - 1, - 0) AS tx2_missed_this_week, - IF(tx2_scheduled_this_week = 1 - AND YEARWEEK(encounter_date) < year_week AND prev_rtc_date IS NOT NULL , - 1, - 0) AS tx2_scheduled_this_week_but_came_early, - IF(tx2_scheduled_this_week = 0 - AND tx2_visit_this_week = 1, - 1, - 0) AS tx2_unscheduled_this_week, - - CASE - WHEN days_since_last_vl <= 365 - AND vl_2_date IS NOT NULL - AND (months_since_tb_tx_start_date IS NULL - OR months_since_tb_tx_start_date >= 6) - AND (is_pregnant = 0 OR is_pregnant IS NULL) - AND @bmi >= 18.5 - AND age >= 20 - AND @cur_status = 'active' - AND days_since_starting_arvs > 364 - AND vl_1 >= 0 - AND vl_1 <= 400 - THEN @not_elligible_for_dc := 0 - ELSE @not_elligible_for_dc := 1 - END AS not_elligible_for_dc, - - - CASE - WHEN days_since_last_vl <= 365 - AND vl_2_date IS NOT NULL - AND (months_since_tb_tx_start_date IS NULL - OR months_since_tb_tx_start_date >= 6) - AND on_dc_this_week = 1 - AND (is_pregnant = 0 OR is_pregnant IS NULL) - AND @bmi >= 18.5 - AND age >= 20 - AND @cur_status = 'active' - AND days_since_starting_arvs > 364 - AND vl_1 >= 0 - AND vl_1 <= 400 - THEN @eligible_and_on_dc := 1 - ELSE @eligible_and_on_dc := 0 - END AS eligible_and_on_dc, - - CASE - WHEN days_since_last_vl <= 365 - AND vl_2_date IS NOT NULL - AND (months_since_tb_tx_start_date IS NULL - OR months_since_tb_tx_start_date >= 6) - AND on_dc_this_week != 1 - AND (is_pregnant = 0 OR is_pregnant IS NULL) - AND @bmi >= 18.5 - AND age >= 20 - AND @cur_status = 'active' - AND days_since_starting_arvs > 364 - AND vl_1 >= 0 - AND vl_1 <= 400 - THEN @eligible_not_on_dc := 1 - ELSE - @eligible_not_on_dc := 0 - END AS eligible_not_on_dc, - - - IF(scheduled_this_week = 1 AND @eligible_not_on_dc = 1, 1, 0 ) as eligible_not_on_dc_and_scheduled_this_week, - IF(scheduled_this_week = 0 AND @eligible_not_on_dc = 1, 1, 0 ) as eligible_not_on_dc_and_unscheduled_this_week, - IF(scheduled_this_week = 1 AND @eligible_and_on_dc = 1, 1, 0 ) as eligible_and_on_dc_and_scheduled_this_week, - IF(scheduled_this_week = 0 AND @eligible_and_on_dc = 1, 1, 0 ) as eligible_and_on_dc_and_unscheduled_this_week, - IF(@eligible_not_on_dc = 1 OR @eligible_and_on_dc = 1, 1, 0 ) as elligible_total, - IF(@not_elligible_for_dc = 1 AND on_dc_this_week = 1, 1, 0 ) as not_elligible_and_on_dc, - IF(@eligible_not_on_dc = 1 OR on_dc_this_week = 1, 1, 0 ) as elligible_total_revised, - - CASE - WHEN - @prev_id != @cur_id - THEN @previous_dc_eligibility := NULL - ELSE - @previous_dc_eligibility := @dc_eligible_cumulative - END as previous_dc_eligibility, - - CASE - WHEN days_since_last_vl <= 365 - AND vl_2_date IS NOT NULL - AND (months_since_tb_tx_start_date IS NULL - OR months_since_tb_tx_start_date >= 6) - AND on_dc_this_week != 1 - AND (is_pregnant = 0 OR is_pregnant IS NULL) - AND @bmi >= 18.5 - AND age >= 20 - AND @cur_status = 'active' - AND days_since_starting_arvs >= 364 - AND vl_1 >= 0 - AND vl_1 <= 400 - THEN @dc_eligible_cumulative := 1 - ELSE - @dc_eligible_cumulative := 0 - END AS dc_eligible_cumulative, - - IF(@prev_id = @cur_id AND (@previous_dc_eligibility = 0 OR @previous_dc_eligibility = null) AND @dc_eligible_cumulative = 1, 1, 0 ) as dc_eligible_this_week, - - IF(scheduled_this_week = 1 AND @dc_eligible_cumulative = 1, 1, 0 ) as dc_eligible_and_scheduled_this_week, - IF(patients_due_for_vl = 1 AND @cur_status = 'active' - AND scheduled_this_week = 1, - 1, - 0) AS scheduled_this_week_and_due_for_vl, - IF(patients_due_for_vl = 1 - AND @cur_status = 'active' - AND scheduled_this_week = 0, - 1, - 0) AS unscheduled_this_week_and_due_for_vl, - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 1 - AND @cur_status = 'active', - 1, - 0) AS due_for_vl_has_vl_order, - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 0 - AND @cur_status = 'active', - 1, - 0) AS due_for_vl_dont_have_order, - IF(patients_due_for_vl = 1 AND @cur_status = 'active', - 1, - 0) AS due_for_vl_this_week_active, - IF( patients_due_for_vl= 1 - AND @cur_status = 'active', - 1, - 0) AS overdue_for_vl_active, - IF(is_suppressed = 1 - AND @cur_status = 'active', - 1, - 0) AS is_suppressed_active, - IF(is_un_suppressed = 1 - AND @cur_status = 'active', - 1, - 0) AS is_un_suppressed_active, - - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 1 - AND @cur_status = 'active' - AND scheduled_this_week = 1, - 1, - 0) AS due_for_vl_has_vl_order_scheduled, - - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 1 - AND @cur_status = 'active' - AND scheduled_this_week = 0, - 1, - 0) AS due_for_vl_has_vl_order_unscheduled, - - - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 0 - AND @cur_status = 'active' - AND scheduled_this_week = 1, - 1, - 0) AS due_for_vl_dont_have_order_scheduled, - - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 0 - AND @cur_status = 'active' - AND scheduled_this_week = 0, - 1, - 0) AS due_for_vl_dont_have_order_unscheduled, - - IF(is_suppressed = 1 - AND has_vl_this_week = 1, - 1, - 0) AS has_vl_this_week_and_suppressed, - - IF(is_un_suppressed = 1 - AND has_vl_this_week = 1, - 1, - 0) AS has_vl_this_week_and_unsuppressed, - - - - CASE - WHEN @prev_id = @cur_id THEN @prev_defaulted:=@cur_defaulted - ELSE @prev_defaulted:=0 - END AS prev_defaulted, - @cur_defaulted:=defaulted AS cur_defaulted, - IF(@cur_defaulted = 0 - AND @cur_defaulted = 1, - 1, - 0) AS defaulted_this_week, - CASE - WHEN @prev_id = @cur_id THEN @prev_missed:=@cur_missed - ELSE @prev_missed:=0 - END AS prev_missed, - @cur_missed:=missed AS cur_missed, - IF(@prev_missed = 0 AND @cur_missed = 1, - 1, - 0) AS missed_this_week, - CASE - WHEN @prev_id = @cur_id THEN @prev_ltfu:=@cur_ltfu - ELSE @prev_ltfu:=0 - END AS prev_ltfu, - @cur_ltfu:=ltfu AS cur_ltfu, - IF(@cur_ltfu = 0 AND @cur_ltfu = 1, - 1, - 0) AS ltfu_this_week FROM - surge_weekly_report_dataset_1); - - - SELECT CONCAT('replacing into surge_weekly_report_dataset ...'); - replace into surge_weekly_report_dataset - (select - now(), - elastic_id , - person_uuid , - person_id , - year_week , - encounter_yw , - encounter_id , - encounter_datetime, - encounter_date , - end_date , - start_date , - birthdate , - age , - gender, - clinical_visit_num as clinical_visit_number, - prev_rtc_date , - rtc_date , - visit_this_week , - on_schedule , - early_appointment, - early_appointment_this_week , - late_appointment_this_week , - days_since_rtc_date , - scheduled_this_week , - unscheduled_this_week , - tx2_visit_this_week, - tx2_missed_this_week as missed_tx2_visit_this_week , - tx2_scheduled_this_week_but_came_early, - death_date , - missed_appointment_this_week , - ltfu , - defaulted , - missed , - null as next_status , - active_in_care_this_week , - cur_arv_adherence , - cur_who_stage , - is_pre_art_this_week , - arv_first_regimen_location_id , - arv_first_regimen , - arv_first_regimen_names , - arv_first_regimen_start_date , - days_since_starting_arvs , - started_art_this_week , - enrollment_date , - enrolled_this_week, - art_revisit_this_week , - cur_arv_meds , - cur_arv_meds_names , - cur_arv_meds_strict , - cur_arv_line , - cur_arv_line_strict , - cur_arv_line_reported , - on_art_this_week , - vl_1 , - vl_1_date , - vl_2 , - vl_2_date, - has_vl_this_week , - is_suppressed_active as is_suppressed , - is_un_suppressed_active as is_un_suppressed , - days_since_last_vl , - null as due_for_vl_this_week, - null as reason_for_needing_vl_this_week , - cd4_1 , - cd4_1_date , - child_hiv_status_disclosure_status , - transfer_in_this_week , - transfer_in_location_id , - transfer_in_date , - transfer_out_this_week , - transfer_out_location_id, - transfer_out_date , - status , - dc_eligible_cumulative , - started_dc_this_week , - location_id, - tx2_scheduled_this_week , - tx2_scheduled_honored , - prev_id, - cur_id , - prev_enc_id , - cur_enc_id , - clinical_visit_num , - prev_status , - cur_status , - cur_prep_this_week , - new_prep_this_week, - revised_height as height , - revised_weight as weight , - revised_bmi as bmi , - scheduled_this_week_and_due_for_vl , - unscheduled_this_week_and_due_for_vl , - overdue_for_vl_active , - due_for_vl_has_vl_order, - due_for_vl_dont_have_order, - ltfu_this_week , - missed_this_week , - all_ltfus , - null as ltfu_surge_baseline , - is_ltfu_after_sep_not_resolved as surge_ltfu_and_ltfu_after_may, - null as surge_ltfu_and_still_ltfu , - newly_ltfu_this_week , - ltfu_cumulative_outcomes_death , - ltfu_cumulative_outcomes_transfer_out , - ltfu_cumulative_outcomes_active, - active_to_ltfu_count , - defaulted_this_week, - due_for_vl_this_week_active , - on_schedule_this_week , - ltfu_cumulative_outcomes_total, - old_ltfus_to_active_this_week, - tx2_unscheduled_this_week , - dead_this_week, - non_ltfu_dead_this_week , - cumulative_dead , - dc_eligible_this_week, - dc_eligible_and_scheduled_this_week, - active_on_dc, - is_ltfu_surge_baseline, - baseline_location, - was_ltfu_may_19 , - was_active_october_18, - not_elligible_for_dc, - eligible_and_on_dc, - eligible_not_on_dc, - eligible_not_on_dc_and_scheduled_this_week, - eligible_not_on_dc_and_unscheduled_this_week, - eligible_and_on_dc_and_scheduled_this_week, - eligible_and_on_dc_and_unscheduled_this_week, - elligible_total, - not_elligible_and_on_dc, - elligible_total_revised, - ltfu_transfer_out_this_week, - ltfu_death_this_week, - ltfu_active_this_week, - - is_ltfu_after_sep_not_resolved AS is_ltfu_after_may, - is_ltfu_after_sep_total AS is_ltfu_after_may_total, - week_patient_became_active, - - med_pickup_rtc_date, - days_since_med_pickup_rtc_date, - days_diff_med_rtc_and_next_clinical_date, - - was_ltfu_before_october_2018, - old_ltfus_outcome_total, - old_ltfus_to_active, - intervention_done_this_week, - interventions, - due_for_vl_has_vl_order_scheduled, - due_for_vl_has_vl_order_unscheduled, - due_for_vl_dont_have_order_scheduled, - due_for_vl_dont_have_order_unscheduled, - has_vl_this_week_and_suppressed, - has_vl_this_week_and_unsuppressed, - missed_to_active_this_week, - defaulter_to_active_this_week, - missed_cumulative, - is_med_ltfu_after_sep19_cumulative AS med_surge_ltfus_cumulative, - is_med_ltfu_after_sep19_unresolved AS med_surge_ltfu_and_med_ltfu_after_may, - med_defaulters, - newly_med_ltfu_this_week, - med_surge_ltfus_outcomes, - med_surge_ltfus_outcomes_this_week, - ltfu_cumulative_outcomes_this_week_total as column_1, - null as column_2, - null as column_3, - null as column_4, - null as column_5, - null as column_6 - - - from surge_weekly_report_dataset__2); - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join surge_weekly_report_dataset_temporary_build_queue t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count := (select count(*) from @queue_table); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - SELECT - @person_ids_count as 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - if(log = true) then - insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - end if; -SELECT - CONCAT(@table_version, - ' : Time to complete: ', - TIMESTAMPDIFF(MINUTE, @start, @end), - ' minutes'); - - END$$ -DELIMITER ; diff --git a/etl-scripts/stored-procedures/generate_surge_weekly_report_dataset_v4.sql b/etl-scripts/stored-procedures/generate_surge_weekly_report_dataset_v4.sql deleted file mode 100644 index 8163e92..0000000 --- a/etl-scripts/stored-procedures/generate_surge_weekly_report_dataset_v4.sql +++ /dev/null @@ -1,1861 +0,0 @@ -DELIMITER $$ -CREATE PROCEDURE `generate_surge_weekly_report_dataset_v4`(IN query_type varchar(50), IN queue_number int, IN queue_size int, IN cycle_size int , IN log boolean) -BEGIN - set @primary_table := "surge_weekly_report_dataset_v7"; - set @query_type = query_type; - set @queue_table = ""; - - set @total_rows_written = 0; - set @start = now(); - set @table_version = "surge_weekly_report_dataset_v7"; - - set @last_update = null; - set @last_date_created = (select max(max_date_created) from etl.flat_obs); - set @may_2019 = '2019-05-11'; - set @may_2015 = '2019-05-11'; - set @october_2018 = '2018-10-01'; - - - -CREATE TABLE IF NOT EXISTS surge_weekly_report_dataset_v7 ( - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL, - elastic_id BIGINT, - person_uuid CHAR(38), - person_id INT, - year_week VARCHAR(10), - encounter_yw INT, - encounter_id INT, - encounter_datetime DATETIME, - encounter_date DATETIME, - end_date DATE, - start_date DATE, - birthdate DATE, - age DECIMAL(23 , 2 ), - gender VARCHAR(100), - clinical_visit_number INT(11), - prev_rtc_date DATETIME, - rtc_date DATETIME, - visit_this_week INT(1), - on_schedule INT(1), - early_appointment INT(1), - early_appointment_this_week INT(1), - late_appointment_this_week INT(11), - days_since_rtc_date BIGINT(21), - scheduled_this_week INT(1), - unscheduled_this_week INT(0), - tx2_visit_this_week INT(11), - missed_tx2_visit_this_week INT(11), - tx2_scheduled_this_week_but_came_early INT(11), - death_date DATETIME, - missed_appointment_this_week INT(11), - ltfu INT(1), - defaulted INT(1), - missed INT(1), - next_status BINARY(0), - active_in_care_this_week INT(11), - cur_arv_adherence VARCHAR(200), - cur_who_stage INT(11), - is_pre_art_this_week INT(11), - arv_first_regimen_location_id INT(11), - arv_first_regimen VARCHAR(500), - arv_first_regimen_names VARCHAR(500), - arv_first_regimen_start_date DATETIME, - days_since_starting_arvs BIGINT(21), - started_art_this_week INT(1), - enrollment_date DATETIME, - enrolled_this_week INT(1), - art_revisit_this_week INT(0), - cur_arv_meds VARCHAR(500), - cur_arv_meds_names VARCHAR(500), - cur_arv_meds_strict VARCHAR(500), - cur_arv_line INT(11), - cur_arv_line_strict INT(11), - cur_arv_line_reported TINYINT(4), - on_art_this_week BINARY(0), - vl_1 INT(11), - vl_1_date DATETIME, - vl_2 INT(11), - vl_2_date DATETIME, - has_vl_this_week INT(1), - is_suppressed INT(1), - is_un_suppressed INT(1), - days_since_last_vl BIGINT(21), - due_for_vl_this_week INT(0), - reason_for_needing_vl_this_week BINARY(0), - cd4_1 DOUBLE, - cd4_1_date DATETIME, - child_hiv_status_disclosure_status BINARY(0), - transfer_in_this_week INT(0), - transfer_in_location_id INT(11), - transfer_in_date DATETIME, - transfer_out_this_week INT(0), - transfer_out_location_id INT(11), - transfer_out_date DATETIME, - status VARCHAR(12), - dc_eligible_cumulative INT(1), - started_dc_this_week INT(1), - location_id INT(11), - tx2_scheduled_this_week INT(1), - tx2_scheduled_honored INT(1), - prev_id BIGINT(20), - cur_id INT(11), - prev_enc_id BIGINT(20), - cur_enc_id INT(11), - clinical_visit_num BIGINT(63), - prev_status LONGTEXT, - cur_status VARCHAR(12), - cur_prep_this_week INT(11), - new_prep_this_week INT(11), - height DOUBLE, - weight DOUBLE, - bmi DOUBLE, - scheduled_this_week_and_due_for_vl INT(0), - unscheduled_this_week_and_due_for_vl INT(0), - overdue_for_vl_active INT(0), - due_for_vl_has_vl_order INT(0), - due_for_vl_dont_have_order INT(0), - ltfu_this_week INT(0), - missed_this_week INT(0), - all_ltfus INT(0), - ltfu_surge_baseline INT(0), - surge_ltfu_and_ltfu_after_may INT(0), - surge_ltfu_and_still_ltfu INT(0), - newly_ltfu_this_week INT(0), - ltfu_cumulative_outcomes_death INT(0), - ltfu_cumulative_outcomes_transfer_out INT(0), - ltfu_cumulative_outcomes_active INT(0), - active_to_ltfu_count INT(0), - defaulted_this_week INT(0), - due_for_vl_this_week_active INT(0), - on_schedule_this_week INT(0), - ltfu_cumulative_outcomes_total INT(0), - old_ltfus_to_active_this_week INT(0), - tx2_unscheduled_this_week INT(0), - dead_this_week INT(0), - non_ltfu_dead_this_week INT(0), - cumulative_dead INT(0), - dc_eligible_this_week INT(0), - dc_eligible_and_scheduled_this_week INT(0), - active_on_dc INT(0), - is_ltfu_surge_baseline INT(0), - - baseline_location VARCHAR(100), - was_ltfu_may_19 INT(0), - was_active_october_18 INT(0), - not_elligible_for_dc INT(0), - eligible_and_on_dc INT(0), - eligible_not_on_dc INT(0), - eligible_not_on_dc_and_scheduled_this_week INT(0), - eligible_not_on_dc_and_unscheduled_this_week INT(0), - eligible_and_on_dc_and_scheduled_this_week INT(0), - eligible_and_on_dc_and_unscheduled_this_week INT(0), - elligible_total INT(0), - not_elligible_and_on_dc INT(0), - elligible_total_revised INT(0), - ltfu_transfer_out_this_week INT(0), - ltfu_death_this_week INT(0), - ltfu_active_this_week INT(0), - is_ltfu_after_may INT(0), - - - is_ltfu_after_may_total INT(0), - - - week_patient_became_active VARCHAR(100), - med_pickup_rtc_date VARCHAR(100), - days_since_med_pickup_rtc_date BIGINT(21), - days_diff_med_rtc_and_next_clinical_date BIGINT(21), - - was_ltfu_before_october_2018 INT(0), - old_ltfus_outcome_total INT(0), - old_ltfus_to_active INT(0), - intervention_done_this_week INT(0), - interventions INT(0), - due_for_vl_has_vl_order_scheduled INT(0), - due_for_vl_has_vl_order_unscheduled INT(0), - due_for_vl_dont_have_order_scheduled INT(0), - due_for_vl_dont_have_order_unscheduled INT(0), - has_vl_this_week_and_suppressed INT(0), - has_vl_this_week_and_unsuppressed INT(0), - missed_to_active_this_week INT(0), - defaulter_to_active_this_week INT(0), - missed_cumulative INT(0), - med_surge_ltfus_cumulative INT(0), - med_surge_ltfu_and_med_ltfu_after_may INT(0), - med_defaulters INT(0), - newly_med_ltfu_this_week INT(0), - med_surge_ltfus_outcomes INT(0), - med_surge_ltfus_outcomes_this_week INT(0), - column_1 INT(0), - column_2 INT(0), - column_3 INT(0), - column_4 VARCHAR(100), - column_5 VARCHAR(100), - column_6 VARCHAR(100), - PRIMARY KEY elastic_id (elastic_id), - INDEX person_enc_date (person_id , encounter_date), - INDEX person_year_week (person_id , year_week), - INDEX date_created (date_created), - INDEX encounter_id (encounter_id), - INDEX person_report_date (person_id , end_date), - INDEX endDate_location_id (end_date , location_id), - INDEX location_id (location_id), - INDEX year_week (year_week), - INDEX person_id (person_id), - INDEX year_week_location_id (year_week , location_id) -); - - if (query_type = "build") then - SELECT 'BUILDING.......................'; - set @write_table = concat("surge_weekly_report_dataset_v7_temp",queue_number); - set @queue_table = concat("surge_weekly_report_dataset_build_queue_",queue_number); - - SET @dyn_sql=CONCAT('Create table if not exists ',@queue_table,'(person_id int primary key) (select * from surge_weekly_report_dataset_build_queue limit ',queue_size, ');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('Create table if not exists ',@write_table,' like ',@primary_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @dyn_sql=CONCAT('delete t1 from surge_weekly_report_dataset_build_queue t1 join ',@queue_table, ' t2 using (person_id)'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - end if; - - if (@query_type="sync") then - SELECT 'SYNCING.......................'; - set @write_table = concat("surge_weekly_report_dataset_v7"); - set @queue_table = "surge_weekly_report_dataset_sync_queue"; - create table if not exists surge_weekly_report_dataset_sync_queue (person_id int primary key); - - select @last_update := (select max(date_updated) from etl.flat_log where table_name=@table_version); - - select "Finding patients in amrs.encounters..."; - - replace into surge_weekly_report_dataset_sync_queue - (select distinct patient_id - from amrs.encounter - where date_changed > @last_update - ); - - - select "Finding patients in flat_obs..."; - - replace into surge_weekly_report_dataset_sync_queue - (select distinct person_id - from etl.flat_obs - where max_date_created > @last_update - ); - - - select "Finding patients in flat_lab_obs..."; - replace into surge_weekly_report_dataset_sync_queue - (select distinct person_id - from etl.flat_lab_obs - where max_date_created > @last_update - ); - - select "Finding patients in flat_orders..."; - - replace into surge_weekly_report_dataset_sync_queue - (select distinct person_id - from etl.flat_orders - where max_date_created > @last_update - ); - - replace into surge_weekly_report_dataset_sync_queue - (select person_id from - amrs.person - where date_voided > @last_update); - - - replace into surge_weekly_report_dataset_sync_queue - (select person_id from - amrs.person - where date_changed > @last_update); - - end if; - - # Remove test patients - SET @dyn_sql=CONCAT('delete t1 FROM ',@queue_table,' t1 - join amrs.person_attribute t2 using (person_id) - where t2.person_attribute_type_id=28 and value="true" and voided=0'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - - SET @num_ids := 0; - SET @dyn_sql=CONCAT('select count(*) into @num_ids from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @person_ids_count = 0; - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - -SELECT CONCAT('Patients in queue: ', @person_ids_count); - -set @total_time=0; -set @cycle_number = 0; - -while @person_ids_count > 0 do - - set @loop_start_time = now(); - drop temporary table if exists surge_weekly_report_dataset_temporary_build_queue; - create temporary table surge_weekly_report_dataset_temporary_build_queue (person_id int primary key); - - SET @dyn_sql=CONCAT('insert into surge_weekly_report_dataset_temporary_build_queue (select * from ',@queue_table,' limit ',cycle_size,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SELECT CONCAT('creating patient_vl ...'); - - CREATE Temporary TABLE if not exists patient_vl_status ( - person_id int(11) DEFAULT NULL, - vl_1 int(11) DEFAULT NULL, - vl_2 int(11) DEFAULT NULL, - vl_2_date datetime DEFAULT NULL, - vl_1_date datetime DEFAULT NULL, - end_date date NOT NULL, - week varchar(10) NOT NULL DEFAULT '', - is_suppressed int(1) NOT NULL DEFAULT '0', - is_un_suppressed int(1) NOT NULL DEFAULT '0', - has_vl_this_week int(1) NOT NULL DEFAULT '0', - days_since_last_vl int(11) DEFAULT NULL, - Primary Key person_week (person_id,week), - KEY person_id (person_id) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8; - - - replace into patient_vl_status( - SELECT - person_id, - vl_1, - vl_2, - vl_2_date, - vl_1_date, - end_date, - week, - if(vl_1=0 or (vl_1 >= 0 and vl_1 <= 400),1,0 ) as is_suppressed, - if(vl_1>400,1,0 ) as is_un_suppressed, - if(yearweek(vl_1_date)=week,1,0 ) as has_vl_this_week, - TIMESTAMPDIFF(DAY, - vl_1_date, - end_date) AS days_since_last_vl - - FROM - surge_week t1 - JOIN - (SELECT t1a.person_id, vl_1,vl_2,vl_2_date, vl_1_date,arv_start_date,encounter_datetime FROM - etl.flat_hiv_summary_v15b t1a Inner JOIN surge_weekly_report_dataset_temporary_build_queue t3 on t1a.person_id=t3.person_id - WHERE t1a.vl_1_date IS NOT NULL) t2 - - WHERE t2.encounter_datetime < DATE_ADD(t1.end_date, INTERVAL 1 DAY)); - - - SELECT CONCAT('creating surge_vitals ...'); - - CREATE TEMPORARY TABLE if not exists surge_vitals ( - prev_id bigint(20) DEFAULT NULL, - cur_id int(11) DEFAULT NULL, - person_id int(11) DEFAULT NULL, - encounter_id int(11) NOT NULL DEFAULT '0', - encounter_datetime datetime DEFAULT NULL, - height double DEFAULT NULL, - weight double DEFAULT NULL, - bmi double DEFAULT NULL, - Primary KEY encounter_id (encounter_id), - KEY person_id (person_id), - KEY encounter_id (encounter_id), - KEY person_id_2 (person_id,encounter_datetime), - KEY encounter_datetime (encounter_datetime) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8; - - replace into surge_vitals ( - SELECT - @prev_id := @cur_id AS prev_id, - @cur_id := t1.person_id AS cur_id, - t1.person_id as person_id, - t1.encounter_id, - encounter_datetime, - CASE - WHEN @prev_id != @cur_id THEN @height := height - WHEN @prev_id = @cur_id and @height is null and height is not null THEN @height := height - WHEN @prev_id = @cur_id and @height and height THEN @height := height - ELSE @height - END AS height, - CASE - WHEN @prev_id != @cur_id THEN @weight := weight - WHEN @prev_id = @cur_id and @weight is null and weight is not null THEN @weight := weight - WHEN @prev_id = @cur_id and @weight and weight THEN @weight := weight - ELSE @weight - END AS weight, - (@weight / (@height * @height) * 10000) as bmi - - from - (select encounter_id, height,weight,encounter_datetime,tv.person_id from etl.flat_vitals tv - inner join surge_weekly_report_dataset_temporary_build_queue t2 on tv.person_id=t2.person_id - order by t2.person_id , encounter_datetime) t1); - - set @age =null; - set @status = null; - - - - SELECT CONCAT('creating surge_weekly_report_dataset_0 ...'); - drop temporary table if exists surge_weekly_report_dataset_0; - create temporary table surge_weekly_report_dataset_0 - (index (person_id), index(start_date), index(end_date),index(encounter_date)) - (SELECT - concat(t1.week,t2.person_id) as elastic_id, - t2.uuid AS person_uuid, - t2.person_id, - t1.week AS year_week, - YEARWEEK(t2.encounter_datetime) encounter_yw, - t2.encounter_id, - t2.encounter_datetime, - t2.next_clinical_datetime_hiv, - if(rtc_date >= @october_2018 <= @may_2019, rtc_date, null) as base_rtc_date, - DATE(t2.encounter_datetime) AS encounter_date, - t1.end_date, - t1.start_date, - DATE(birthdate) AS birthdate, - gender, - CASE - WHEN - TIMESTAMPDIFF(YEAR, birthdate, end_date) > 0 - THEN - @age:=ROUND(TIMESTAMPDIFF(YEAR, birthdate, end_date), - 0) - ELSE @age:=ROUND(TIMESTAMPDIFF(MONTH, - birthdate, - end_date) / 12, - 2) - END AS age, - prev_clinical_rtc_date_hiv as prev_rtc_date , - rtc_date, - - - CASE - WHEN - med_pickup_rtc_date IS NOT NULL - AND visit_type NOT IN (24) - - THEN - @med_pickup_rtc_date := med_pickup_rtc_date - - ELSE @med_pickup_rtc_date := NULL - END AS med_pickup_rtc_date, - - # select med_pickup_rtc_date, year_week from surge_weekly_report_dataset_0 where person_id = 21417 - - - IF(YEARWEEK(t2.encounter_datetime) = t1.week, - 1, - 0) AS visit_this_week, - IF(DATE(t2.encounter_datetime) = DATE(prev_clinical_rtc_date_hiv), - 1, - 0) AS on_schedule, - IF((yearweek(prev_clinical_rtc_date_hiv)=t1.week OR yearweek(rtc_date)=t1.week OR (prev_clinical_rtc_date_hiv is null and yearweek(rtc_date)=t1.week)) and - YEARWEEK(t2.encounter_datetime) = t1.week, - 1, - 0) AS on_schedule_this_week, - IF(DATE(t2.encounter_datetime) < DATE(prev_clinical_rtc_date_hiv), - 1, - 0) AS early_appointment, - - IF(YEARWEEK(t2.encounter_datetime) = t1.week AND - (t1.week < YEARWEEK(prev_clinical_rtc_date_hiv) OR (prev_clinical_rtc_date_hiv is null and t1.week < YEARWEEK(rtc_date))) - ,1,0) AS early_appointment_this_week, - - IF(YEARWEEK(t2.encounter_datetime) = t1.week - AND (t1.week > YEARWEEK(prev_clinical_rtc_date_hiv) OR ( prev_clinical_rtc_date_hiv is null and t1.week > YEARWEEK(rtc_date))) - ,1,0) AS late_appointment_this_week, - - TIMESTAMPDIFF(DAY, - rtc_date, - end_date) AS days_since_rtc_date, - - - TIMESTAMPDIFF(DAY, - prev_clinical_rtc_date_hiv, - t2.encounter_datetime) AS days_diff_enc_date_and_prev_rtc, - - TIMESTAMPDIFF(DAY, - @med_pickup_rtc_date, - end_date) AS days_since_med_pickup_rtc_date, - - TIMESTAMPDIFF(DAY, - DATE(med_pickup_rtc_date), - DATE(next_clinical_datetime_hiv) - ) AS days_diff_med_rtc_and_next_clinical_date, - - IF(YEARWEEK(prev_clinical_rtc_date_hiv) = t1.week - OR (prev_clinical_rtc_date_hiv is null and t1.week = YEARWEEK(rtc_date)) OR yearweek(rtc_date)=t1.week, - 1, - 0) AS scheduled_this_week, - CASE - WHEN - YEARWEEK(t2.encounter_datetime) = t1.week - AND (yearweek(prev_clinical_rtc_date_hiv) != t1.week OR (prev_clinical_rtc_date_hiv is null and yearweek(rtc_date) != t1.week)) - AND ((DATE(t2.encounter_datetime) != DATE(prev_clinical_rtc_date_hiv)) OR ( prev_clinical_rtc_date_hiv is null and (DATE(t2.encounter_datetime) != DATE(rtc_date)))) - THEN - 1 - ELSE 0 - END AS unscheduled_this_week, - - # NULL AS tx2_visit_this_week, - # NULL AS missed_tx2_visit_this_week, - t2.death_date, - NULL active_in_care_this_week, - cur_arv_adherence, - NULL cur_who_stage, - NULL is_pre_art_this_week, - arv_first_regimen_location_id, - arv_first_regimen, - etl.get_arv_names(arv_first_regimen) as arv_first_regimen_names, - arv_first_regimen_start_date, - TIMESTAMPDIFF(DAY, - arv_first_regimen_start_date, - end_date) AS days_since_starting_arvs, - IF(YEARWEEK(arv_first_regimen_start_date) = t1.week, - 1, - 0) AS started_art_this_week, - enrollment_date, - IF(YEARWEEK(enrollment_date) = t1.week, - 1, - 0) AS enrolled_this_week, - CASE - WHEN - YEARWEEK(t2.encounter_datetime) = t1.week - AND YEARWEEK(arv_first_regimen_start_date) != t1.week - AND cur_arv_meds IS NOT NULL - AND arv_first_regimen_start_date IS NOT NULL - THEN - 1 - ELSE 0 - END AS art_revisit_this_week, - cur_arv_meds, - etl.get_arv_names(cur_arv_meds) AS cur_arv_meds_names, - cur_arv_meds_strict, - cur_arv_line, - cur_arv_line_strict, - cur_arv_line_reported, - NULL AS on_art_this_week, - cd4_1, - cd4_1_date, - NULL AS child_hiv_status_disclosure_status, - CASE - WHEN YEARWEEK(transfer_in_date) = t1.week THEN 1 - ELSE 0 - END AS transfer_in_this_week, - transfer_in_location_id, - transfer_in_date, - CASE - WHEN YEARWEEK(transfer_out_date) = t1.week THEN 1 - ELSE 0 - END AS transfer_out_this_week, - transfer_out_location_id, - transfer_out_date, - - t2.location_id, - IF((DATE(arv_first_regimen_start_date) = DATE(t2.encounter_datetime) - AND YEARWEEK(rtc_date) = t1.week) or (DATE(arv_first_regimen_start_date) = DATE(prev_clinical_datetime_hiv) - AND YEARWEEK(prev_rtc_date) = t1.week) , - 1, - 0) AS tx2_scheduled_this_week, - IF(DATE(arv_first_regimen_start_date) = DATE(prev_clinical_datetime_hiv) - AND YEARWEEK(t2.encounter_datetime) = t1.week, - 1, - 0) AS tx2_visit_this_week, - IF(YEARWEEK(arv_first_regimen_start_date) = YEARWEEK(prev_clinical_datetime_hiv) - AND YEARWEEK(t2.encounter_datetime) = t1.week AND YEARWEEK(prev_rtc_date) = t1.week, - 1, - 0) AS tx2_scheduled_honored, - - - is_pregnant, - edd, - @date_pregnant := if(edd is not null,DATE_ADD(edd, INTERVAL -9 MONTH),null) as date_confirmed_pregnant, - if(edd is not null and yearweek(edd) >= yearweek(t1.end_date),1,0) as is_still_pregnant, - if(@date_pregnant is not null and YEARWEEK(@date_pregnant) > yearweek(arv_first_regimen_start_date),1,0) as started_arv_b4_pregnancy, - if(@date_pregnant is not null and YEARWEEK(@date_pregnant) < yearweek(arv_first_regimen_start_date),1,0) as started_arv_after_pregnancy, - if(arv_first_regimen_start_date is not null and yearweek(arv_first_regimen_start_date) <= yearweek(t1.end_date) ,1,0) as patient_is_on_art, - breast_feeding, - breast_feeding_encounter_date, - - - - tb_tx_start_date, - TIMESTAMPDIFF(MONTH, - tb_tx_start_date, - end_date) AS months_since_tb_tx_start_date, - CASE - WHEN YEARWEEK(vl_order_date) = t1.week THEN 1 - ELSE 0 - END AS vl_ordered_this_week, - if(YEARWEEK(t2.encounter_datetime)= t1.week and encounter_type=21,1,0) intervention_done_this_week, - if( encounter_type=21,1,0) interventions, - arv_start_date - FROM - surge_week t1 - JOIN etl.flat_hiv_summary_v15b t2 - JOIN surge_weekly_report_dataset_temporary_build_queue t5 USING (person_id) - left join ndwr.patient_breast_feeding bf USING (person_id)#ON(t2.person_id = bf.person_id AND breast_feeding_encounter_date < DATE_ADD(t1.end_date, INTERVAL 1 DAY)) - JOIN - amrs.person t3 USING (person_id) - - WHERE - t2.encounter_datetime < DATE_ADD(t1.end_date, INTERVAL 1 DAY) - AND ((t2.next_clinical_datetime_hiv IS NULL - OR t2.next_clinical_datetime_hiv >= DATE_ADD(t1.end_date, INTERVAL 1 DAY)) || (t2.rtc_date IS NULL - OR t2.rtc_date >= DATE_ADD(t1.end_date, INTERVAL 1 DAY))) - AND t2.encounter_datetime < DATE_ADD(t1.end_date, INTERVAL 1 DAY) - AND t2.is_clinical_encounter = 1 - ORDER BY t2.person_id , t2.encounter_datetime , rtc_date , end_date); - - drop temporary table if exists surge_weekly_report_dataset_01; - CREATE TEMPORARY TABLE `surge_weekly_report_dataset_01` ( - `elastic_id` varchar(21) CHARACTER SET latin1 DEFAULT NULL, - `person_uuid` varchar(100) CHARACTER SET latin1 DEFAULT NULL, - `person_id` int(11) DEFAULT NULL, - `year_week` varchar(10) CHARACTER SET latin1 NOT NULL DEFAULT '', - `encounter_yw` int(6) DEFAULT NULL, - `encounter_id` int(11) NOT NULL DEFAULT '0', - `encounter_datetime` datetime DEFAULT NULL, - `next_clinical_datetime_hiv` datetime DEFAULT NULL, - `base_rtc_date` datetime DEFAULT NULL, - `encounter_date` date DEFAULT NULL, - `end_date` date NOT NULL, - `start_date` date NOT NULL, - `birthdate` date DEFAULT NULL, - `gender` varchar(50) DEFAULT '', - `age` decimal(23,2) DEFAULT NULL, - `prev_rtc_date` datetime DEFAULT NULL, - `rtc_date` datetime DEFAULT NULL, - `med_pickup_rtc_date` varchar(19) DEFAULT NULL, - `visit_this_week` int(1) NOT NULL DEFAULT '0', - `on_schedule` int(1) NOT NULL DEFAULT '0', - `on_schedule_this_week` int(1) NOT NULL DEFAULT '0', - `early_appointment` int(1) NOT NULL DEFAULT '0', - `early_appointment_this_week` int(1) NOT NULL DEFAULT '0', - `late_appointment_this_week` int(1) NOT NULL DEFAULT '0', - `days_since_rtc_date` bigint(21) DEFAULT NULL, - `days_diff_enc_date_and_prev_rtc` bigint(21) DEFAULT NULL, - `days_since_med_pickup_rtc_date` bigint(21) DEFAULT NULL, - `days_diff_med_rtc_and_next_clinical_date` bigint(21) DEFAULT NULL, - `scheduled_this_week` int(1) NOT NULL DEFAULT '0', - `unscheduled_this_week` int(0) DEFAULT NULL, - `death_date` datetime DEFAULT NULL, - `active_in_care_this_week` binary(0) DEFAULT NULL, - `cur_arv_adherence` varchar(200) CHARACTER SET latin1 DEFAULT NULL, - `cur_who_stage` binary(0) DEFAULT NULL, - `is_pre_art_this_week` binary(0) DEFAULT NULL, - `arv_first_regimen_location_id` int(11) DEFAULT NULL, - `arv_first_regimen` varchar(500) CHARACTER SET latin1 DEFAULT NULL, - `arv_first_regimen_names` text CHARACTER SET latin1, - `arv_first_regimen_start_date` datetime DEFAULT NULL, - `days_since_starting_arvs` bigint(21) DEFAULT NULL, - `started_art_this_week` int(1) NOT NULL DEFAULT '0', - `enrollment_date` datetime DEFAULT NULL, - `enrolled_this_week` int(1) NOT NULL DEFAULT '0', - `art_revisit_this_week` int(0) DEFAULT NULL, - `cur_arv_meds` varchar(500) CHARACTER SET latin1 DEFAULT NULL, - `cur_arv_meds_names` text CHARACTER SET latin1, - `cur_arv_meds_strict` varchar(500) CHARACTER SET latin1 DEFAULT NULL, - `cur_arv_line` int(11) DEFAULT NULL, - `cur_arv_line_strict` int(11) DEFAULT NULL, - `cur_arv_line_reported` tinyint(4) DEFAULT NULL, - `on_art_this_week` binary(0) DEFAULT NULL, - `cd4_1` double DEFAULT NULL, - `cd4_1_date` datetime DEFAULT NULL, - `child_hiv_status_disclosure_status` binary(0) DEFAULT NULL, - `transfer_in_this_week` int(0) DEFAULT NULL, - `transfer_in_location_id` int(11) DEFAULT NULL, - `transfer_in_date` datetime DEFAULT NULL, - `transfer_out_this_week` int(0) DEFAULT NULL, - `transfer_out_location_id` int(11) DEFAULT NULL, - `transfer_out_date` datetime DEFAULT NULL, - `location_id` int(11) DEFAULT NULL, - `tx2_scheduled_this_week` int(1) NOT NULL DEFAULT '0', - `tx2_visit_this_week` int(1) NOT NULL DEFAULT '0', - `tx2_scheduled_honored` int(1) NOT NULL DEFAULT '0', - `is_pregnant` tinyint(1) DEFAULT NULL, - `edd` datetime DEFAULT NULL, - `date_confirmed_pregnant` varchar(19) DEFAULT NULL, - `is_still_pregnant` int(1) NOT NULL DEFAULT '0', - `started_arv_b4_pregnancy` int(1) NOT NULL DEFAULT '0', - `started_arv_after_pregnancy` int(1) NOT NULL DEFAULT '0', - `patient_is_on_art` int(1) NOT NULL DEFAULT '0', - `breast_feeding` int(11) DEFAULT NULL, - `breast_feeding_encounter_date` datetime DEFAULT NULL, - `tb_tx_start_date` datetime DEFAULT NULL, - `months_since_tb_tx_start_date` bigint(21) DEFAULT NULL, - `vl_ordered_this_week` int(0) DEFAULT NULL, - `intervention_done_this_week` int(1) NOT NULL DEFAULT '0', - `interventions` int(1) NOT NULL DEFAULT '0', - `arv_start_date` datetime DEFAULT NULL, - PRIMARY KEY(person_id, year_week), - KEY `person_id` (`person_id`), - KEY `start_date` (`start_date`), - KEY `end_date` (`end_date`), - KEY `encounter_date` (`encounter_date`) - - - ) ENGINE=InnoDB DEFAULT CHARSET=utf8; - - replace into surge_weekly_report_dataset_01(select * - from surge_weekly_report_dataset_0); - SELECT - COUNT(*) patients_for_24 - FROM - surge_weekly_report_dataset_0 - WHERE - year_week = 201924; - - SELECT CONCAT('creating surge_weekly_report_dataset__1 ...'); - - drop temporary table if exists surge_weekly_report_dataset_1; - - CREATE temporary TABLE surge_weekly_report_dataset_1 (SELECT sw0.*, - vl.vl_1, - vl.vl_1_date, - vl.vl_2, - vl.vl_2_date, - vl.days_since_last_vl, - - CASE - WHEN vl_1 > 999 - AND vl_1_date > arv_start_date - AND TIMESTAMPDIFF(MONTH,vl_1_date,sw0.end_date) >=3 - THEN - 1 - #is pregnant and started_arv_b4_pregnancy - WHEN is_still_pregnant=1 AND started_arv_b4_pregnancy=1 and vl_1_date is null then 1 - WHEN is_still_pregnant=1 AND started_arv_b4_pregnancy=1 and TIMESTAMPDIFF(MONTH,vl_1_date,date_confirmed_pregnant)>=1 then 1 - WHEN is_still_pregnant=1 AND started_arv_b4_pregnancy=1 and yearweek(vl_1_date) >= yearweek(date_confirmed_pregnant) and TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date)>=6 then 1 - - #is pregnant and started_arv_after_pregnancy - WHEN is_still_pregnant=1 AND patient_is_on_art=1 AND started_arv_after_pregnancy=1 and TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=3 and vl_1_date is null then 1 - WHEN is_still_pregnant=1 AND patient_is_on_art=1 AND started_arv_after_pregnancy=1 and TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=3 and TIMESTAMPDIFF(MONTH,vl_1_date,date_confirmed_pregnant)>=1 then 1 - WHEN is_still_pregnant=1 AND patient_is_on_art=1 AND started_arv_after_pregnancy=1 and TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=3 and TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date)>=6 then 1 - - #is 24 and below years - WHEN age<=24 AND patient_is_on_art=1 AND (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=6 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date)>=6) and vl_1_date is null then 1 - WHEN age <=24 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=6 then 1 - - #is 25 and above years - WHEN age >=25 AND patient_is_on_art=1 AND (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date)>=6 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date)>=6) and vl_1_date is null then 1 - WHEN age >=25 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=6 and (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date) <=12 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) <= 12 ) then 1 - WHEN age >=25 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=12 and (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date) > 12 or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) > 12 )then 1 - - # breast feeding - WHEN breast_feeding=1065 AND patient_is_on_art=1 and (TIMESTAMPDIFF(MONTH,arv_first_regimen_start_date,sw0.end_date) >=6 - or TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) >=6) and vl_1_date is null then 1 - WHEN breast_feeding=1065 AND patient_is_on_art=1 AND TIMESTAMPDIFF(MONTH,vl_1_date,vl.end_date) >=6 then 1 - - # drug substitution and no vl ever - WHEN arv_first_regimen_start_date is not null and arv_start_date is not null - and arv_first_regimen_start_date < arv_start_date - AND TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) >= 3 - AND vl_1_date is null then 1 - - # drug substitution and vl was done before change - WHEN arv_first_regimen_start_date is not null and arv_start_date is not null - and arv_first_regimen_start_date < arv_start_date - AND TIMESTAMPDIFF(MONTH,arv_start_date,sw0.end_date) >= 3 - AND TIMESTAMPDIFF(MONTH,vl_1_date,arv_start_date)>=1 then 1 - - - else 0 - - END AS patients_due_for_vl, - - vl.has_vl_this_week, - vl.is_un_suppressed, - vl.is_suppressed, - IF( YEARWEEK(death_date)=year_week , 1,0) as non_ltfu_dead_this_week, - IF( death_date>=@may_2019, 1,0) as cumulative_dead, - CASE - WHEN death_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(death_date) THEN @status:='dead' - WHEN transfer_out_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(transfer_out_date) THEN @status:='transfer_out' - - WHEN days_since_rtc_date > 0 AND days_since_rtc_date < 5 THEN @status:='missed' - - WHEN days_since_rtc_date > 4 AND days_since_rtc_date <= 28 THEN @status:='defaulter' - - WHEN days_since_rtc_date <= 0 THEN @status:='active' - - WHEN days_since_rtc_date > 28 THEN @status:='ltfu' - ELSE @status:='unknown' - END AS status, - - CASE - WHEN death_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(death_date) THEN @status:='unknown' - WHEN transfer_out_date IS NOT NULL AND YEARWEEK(sw0.end_date) >= YEARWEEK(transfer_out_date) THEN @status:='unknown' - WHEN next_clinical_datetime_hiv IS NULL - AND days_since_med_pickup_rtc_date > 0 - AND days_since_med_pickup_rtc_date < 5 THEN @status:='missed' - - WHEN - next_clinical_datetime_hiv IS NULL - AND days_since_med_pickup_rtc_date > 4 - AND days_since_med_pickup_rtc_date <= 28 THEN @status:='defaulter' - - WHEN next_clinical_datetime_hiv IS NULL - AND days_since_med_pickup_rtc_date > 28 THEN @status:='ltfu' - - WHEN next_clinical_datetime_hiv IS NOT NULL - AND days_diff_med_rtc_and_next_clinical_date > 28 - AND days_since_med_pickup_rtc_date > 28 THEN @status:='ltfu' - - ELSE @status:='unknown' - END AS med_status, - - - - IF(scheduled_this_week =1 and on_schedule_this_week=0 and days_since_rtc_date >0 and days_since_rtc_date <5 - - AND death_date IS NULL AND transfer_out_date IS NULL, 1, 0) AS missed_appointment_this_week, - - IF(days_since_rtc_date >=0 and days_since_rtc_date <5 and @status not in('transfer_out','dead'), 1, 0) AS missed_cumulative , - - IF(days_since_rtc_date >4 and days_since_rtc_date <=28 - AND death_date IS NULL AND transfer_out_date IS NULL, 1, 0) AS defaulted , - - null as ltfu, - null AS missed, - IF(YEARWEEK(t4.date_enrolled) = year_week - AND (t4.program_id = 3 OR t4.program_id = 9), - 1, - 0) AS started_dc_this_week, - IF(t4.program_id = 3 OR t4.program_id = 9, 1, 0) AS active_on_dc, - IF(t4.program_id = 3 OR t4.program_id = 9, - 1, - 0) AS on_dc_this_week, - IF(YEARWEEK(t4.date_enrolled) = year_week - AND t4.program_id = 10, - 1, - 0) AS new_prep_this_week, - IF(t4.program_id = 10 , 1, 0) AS cur_prep_this_week, - t6.weight AS weight, - t6.height AS height, - fd.encounter_datetime as death_reporting_date, - fd.next_encounter_datetime as fd_next_encounter_datetime, - ft.encounter_datetime as transfer_reporting_date, - ft.next_encounter_datetime as ft_next_encounter_datetime, - fe.encounter_datetime as exit_reporting_date, - fe.next_encounter_datetime as fe_next_encounter_datetime, - t6.bmi FROM - surge_weekly_report_dataset_01 sw0 - LEFT OUTER JOIN patient_vl_status vl ON vl.person_id=sw0.person_id and vl.week = sw0.year_week - LEFT OUTER JOIN - amrs.patient_program t4 ON t4.patient_id = sw0.person_id - AND t4.program_id IN (3 , 9, 10, 11) - AND t4.date_completed IS NULL - LEFT OUTER JOIN - surge_vitals t6 ON t6.encounter_id = sw0.encounter_id - LEFT JOIN etl.flat_appointment fd ON (sw0.person_id = fd.person_id and fd.encounter_type = 31) - LEFT JOIN etl.flat_appointment ft ON (sw0.person_id = ft.person_id and ft.encounter_type = 116) - LEFT JOIN etl.flat_appointment fe ON (sw0.person_id = fe.person_id and fe.encounter_type = 157) - ORDER BY sw0.person_id , year_week); - - - set @prev_id = -1; - set @cur_id = -1; - set @prev_enc_id = -1; - set @cur_enc_id = -1; - set @cur_status = ''; - set @prev_status = ''; - set @cur_med_status = ''; - set @prev_med_status = ''; - set @cur_arv_meds = null; - set @prev_arv_meds = null; - set @cur_location_id = null; - set @prev_location_id = null; - Set @clinic_visit_number = 1; - set @prev_patients_due_for_vl = 0; - set @prev_defaulted = 0; - set @cur_defaulted = 0; - set @prev_missed = 0; - set @cur_missed = 0; - set @prev_ltfu = 0; - set @cur_ltfu = 0; - set @base_rtc = null; - - - set @was_active_october_18:= null; - set @was_ltfu_may_19 := null; - set @is_ltfu_surge_baseline := null; - - - - SELECT CONCAT('creating surge_weekly_report_dataset__2 ...'); - - - drop temporary table if exists surge_weekly_report_dataset__2; - CREATE temporary TABLE surge_weekly_report_dataset__2 (SELECT *, - @prev_id:=@cur_id AS prev_id, - @cur_id:=person_id AS cur_id, - @prev_enc_id:=@cur_enc_id AS prev_enc_id, - @cur_enc_id:=encounter_id AS cur_enc_id, - CASE - WHEN - @prev_id = @cur_id - AND @prev_enc_id != @cur_enc_id - THEN - @clinic_visit_number:=@clinic_visit_number + 1 - WHEN - @prev_id = @cur_id - AND @prev_enc_id = @cur_enc_id - THEN - @clinic_visit_number - ELSE @clinic_visit_number:=1 - END AS clinical_visit_num, - - CASE - WHEN @prev_id = @cur_id THEN @prev_status:=@cur_status - ELSE @prev_status:='' - END AS prev_status, - - @cur_status:=status AS cur_status, - - CASE - WHEN @prev_id = @cur_id THEN @prev_med_status:=@cur_med_status - ELSE @prev_med_status:='' - END AS prev_med_status, - - @cur_med_status:=med_status AS cur_med_status, - - CASE - WHEN - (@prev_id != @cur_id - OR @was_active_october_18 IS NULL) - AND year_week > 201838 - AND year_week < 201918 - AND (@cur_status = 'active' OR @cur_status = 'missed' OR @cur_status = 'defaulter') - AND ((death_date IS NULL OR DATE(death_reporting_date) > '2019-05-11') AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > '2019-05-11')) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > '2019-05-11') AND fe_next_encounter_datetime IS NULL) - - THEN - @was_active_october_18:=1 - WHEN @prev_id != @cur_id THEN @was_active_october_18:=NULL - ELSE @was_active_october_18 - END AS was_active_october_18, - - CASE - WHEN - (@prev_id != @cur_id - OR @was_ltfu_may_19 IS NULL) - AND year_week = 201918 - AND @cur_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > '2019-05-11') AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > '2019-05-11')) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > '2019-05-11') AND fe_next_encounter_datetime IS NULL) - THEN - @was_ltfu_may_19:=1 - WHEN @prev_id != @cur_id THEN @was_ltfu_may_19:=NULL - ELSE @was_ltfu_may_19 - END AS was_ltfu_may_19, - - CASE - WHEN - (@prev_id != @cur_id - OR @is_ltfu_surge_baseline IS NULL) - AND @was_active_october_18 = 1 - AND @was_ltfu_may_19 = 1 - AND year_week <= 201918 - - THEN - @is_ltfu_surge_baseline:=1 - WHEN - (@prev_id != @cur_id - OR @is_ltfu_surge_baseline IS NULL) - AND NOT (@was_active_october_18 = NULL - AND @was_ltfu_may_19 = NULL) - AND year_week <= 201918 - - THEN - @is_ltfu_surge_baseline:=0 - WHEN @prev_id != @cur_id THEN @is_ltfu_surge_baseline:=NULL - ELSE @is_ltfu_surge_baseline - END AS is_ltfu_surge_baseline, - - - CASE - WHEN @prev_id != @cur_id - AND @is_ltfu_surge_baseline = 1 - AND @baseline_location IS NULL - THEN @baseline_location:= location_id - - WHEN @prev_id != @cur_id - AND @is_ltfu_surge_baseline = 1 - AND @baseline_location IS NOT NULL - THEN @baseline_location - - WHEN @prev_id = @cur_id - AND @is_ltfu_surge_baseline = 1 - AND @baseline_location IS NULL - THEN @baseline_location:= location_id - - WHEN @prev_id = @cur_id - AND @is_ltfu_surge_baseline = 1 - AND @baseline_location IS NOT NULL - THEN @baseline_location - - ELSE @baseline_location:= null - END as baseline_location, - - CASE - WHEN - (@prev_id != @cur_id - OR @active_after_may_19 IS NULL) - AND year_week >=201918 - AND (@cur_status = 'active' OR @cur_status = 'missed' OR @cur_status = 'defaulter') - AND ((death_date IS NULL OR DATE(death_reporting_date) > '2019-05-11') AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > '2019-05-11')) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > '2019-05-11') AND fe_next_encounter_datetime IS NULL) - - THEN - @active_after_may_19:=1 - WHEN @prev_id != @cur_id THEN @active_after_may_19:=NULL - ELSE @active_after_may_19 - END AS active_after_may_19, - - CASE - WHEN - (@prev_id != @cur_id - OR @is_ltfu_after_may_revised IS NULL) - AND @active_after_may_19 = 1 - AND @cur_status = 'ltfu' - AND year_week > 201918 - THEN - @is_ltfu_after_may_revised:=1 - WHEN - (@prev_id != @cur_id - OR @is_ltfu_after_may_revised IS NULL) - AND NOT (@active_after_may_19 = NULL) - AND year_week > 201918 - - THEN - @is_ltfu_after_may_revised:=0 - WHEN @prev_id != @cur_id THEN @is_ltfu_after_may_revised:=NULL - ELSE @is_ltfu_after_may_revised - END AS is_ltfu_after_may_revised, - - - - - CASE - WHEN - (@prev_id != @cur_id - OR @is_med_ltfu_after_may_revised IS NULL) - AND @active_after_may_19 = 1 - AND @cur_med_status = 'ltfu' - AND year_week > 201918 - THEN - @is_med_ltfu_after_may_revised:=1 - - WHEN @prev_id != @cur_id THEN @is_med_ltfu_after_may_revised:=NULL - ELSE @is_med_ltfu_after_may_revised - END AS is_med_ltfu_after_may_revised, - - - CASE - WHEN - (@prev_id != @cur_id - OR @was_med_ltfu_may_19 IS NULL) - AND year_week = 201918 - AND @cur_med_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > '2019-05-11') AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > '2019-05-11')) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > '2019-05-11') AND fe_next_encounter_datetime IS NULL) - THEN - @was_med_ltfu_may_19:=1 - WHEN @prev_id != @cur_id THEN @was_med_ltfu_may_19:=NULL - ELSE @was_med_ltfu_may_19 - END AS was_med_ltfu_may_19, - - - CASE - WHEN - (@prev_id != @cur_id - OR @is_med_ltfu_surge_baseline IS NULL) - AND @was_active_october_18 = 1 - AND @was_med_ltfu_may_19 = 1 - AND year_week <= 201918 - - THEN - @is_med_ltfu_surge_baseline:=1 - WHEN - (@prev_id != @cur_id - OR @is_med_ltfu_surge_baseline IS NULL) - AND NOT (@was_active_october_18 = NULL - AND @was_med_ltfu_may_19 = NULL) - AND year_week <= 201918 - - THEN - @is_med_ltfu_surge_baseline:=0 - WHEN @prev_id != @cur_id THEN @is_med_ltfu_surge_baseline:=NULL - ELSE @is_med_ltfu_surge_baseline - END AS is_med_ltfu_surge_baseline, - - IF((@is_med_ltfu_surge_baseline = 1 OR @is_med_ltfu_after_may_revised = 1), - 1, 0) AS med_surge_ltfus_cumulative, - - IF((@is_med_ltfu_surge_baseline = 1 OR @is_med_ltfu_after_may_revised = 1) AND @cur_med_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, 0) AS med_surge_ltfu_and_med_ltfu_after_may, - - CASE - WHEN - next_clinical_datetime_hiv IS NULL - AND days_since_med_pickup_rtc_date > 4 - AND days_since_med_pickup_rtc_date <= 28 - AND @cur_status not in('transfer_out','dead') - - THEN - 1 - ELSE 0 - END AS med_defaulters, - - IF(@prev_med_status = 'defaulter' - AND @cur_med_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, - 0) AS newly_med_ltfu_this_week, - - IF((@is_med_ltfu_surge_baseline = 1 OR @is_med_ltfu_after_may_revised = 1) - AND @prev_med_status = 'ltfu' - AND (@cur_med_status = 'unknown' - OR @cur_med_status = 'missed' - OR @cur_med_status = 'defaulter'), - 1, 0) AS med_surge_ltfus_outcomes_this_week, - - IF((@is_med_ltfu_surge_baseline = 1 OR @is_med_ltfu_after_may_revised = 1) - AND (@cur_med_status = 'unknown' - OR @cur_med_status = 'missed' - OR @cur_med_status = 'defaulter'), - 1, 0) AS med_surge_ltfus_outcomes, - - - CASE - WHEN @prev_id = @cur_id - AND ( @is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1) - AND @prev_status = 'ltfu' AND @cur_status = 'active' - AND @week_patient_became_active IS NULL - AND encounter_datetime > '2019-05-11' - THEN @week_patient_became_active:= encounter_datetime - - WHEN @prev_id = @cur_id - AND ( @is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1) - AND @prev_status = 'defaulter' AND @cur_status = 'ltfu' - AND @week_patient_became_active IS NOT NULL - THEN @week_patient_became_active:= NULL - - WHEN @prev_id = @cur_id - AND ( @is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1) - AND @prev_status = 'ltfu' AND @cur_status = 'ltfu' - THEN @week_patient_became_active:= NULL - - WHEN @prev_id != @cur_id - AND ( @is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1) - AND @prev_status = 'ltfu' AND @cur_status = 'active' - AND @week_patient_became_active IS NULL - AND encounter_datetime > '2019-05-11' - THEN @week_patient_became_active:= encounter_datetime - - WHEN @prev_id != @cur_id - AND ( @is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1) - AND @prev_status = 'defaulter' AND @cur_status = 'ltfu' - AND @week_patient_became_active IS NOT NULL - THEN @week_patient_became_active:= NULL - - WHEN @prev_id != @cur_id - AND ( @is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1) - AND @prev_status = 'ltfu' AND @cur_status = 'ltfu' - THEN @week_patient_became_active:= NULL - - ELSE @week_patient_became_active - END as week_patient_became_active, - - - IF(@prev_status = 'missed' - AND @cur_status = 'active', - 1, - 0) AS missed_to_active_this_week, - - IF(@prev_status = 'defaulter' - AND @cur_status = 'active', - 1, - 0) AS defaulter_to_active_this_week, - - IF(status = 'ltfu', 1, 0) AS all_ltfus, - - IF(@is_ltfu_surge_baseline = 1 AND @cur_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date) OR (transfer_out_date > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, - 0) AS surge_ltfu_and_still_ltfu, - - IF((@is_ltfu_after_may_revised = 1 OR - (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))) - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND ((transfer_out_date IS NULL AND transfer_out_location_id IS NULL) OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, - 0) AS is_ltfu_after_may_total, - - IF((@is_ltfu_after_may_revised = 1 OR - (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)))) - AND @cur_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, - 0) AS is_ltfu_after_may, - - IF((@is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1) AND @cur_status = 'ltfu' - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND ((transfer_out_date IS NULL AND transfer_out_location_id IS NULL) OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, 0) AS surge_ltfu_and_ltfu_after_may, - - IF( YEARWEEK(death_date)=year_week, 1,0) as dead_this_week, - - IF(@prev_status = 'defaulter' - AND (@cur_status = 'ltfu' OR days_since_rtc_date > 28 OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <=0)) - - AND ((death_date IS NULL OR DATE(death_reporting_date) > end_date) AND fd_next_encounter_datetime IS NULL) - AND (transfer_out_date IS NULL OR (DATE(transfer_reporting_date) > end_date)) - AND ((exit_reporting_date IS NULL OR DATE(exit_reporting_date) > end_date) AND fe_next_encounter_datetime IS NULL), - 1, - 0) AS newly_ltfu_this_week, - - #cumulative outcomes - IF((@is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1 ) - AND @cur_status = 'dead', - 1, - 0) AS ltfu_cumulative_outcomes_death, - - IF((@is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1 ) - AND @cur_status = 'transfer_out', - 1, - 0) AS ltfu_cumulative_outcomes_transfer_out, - - IF((@is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1 ) - AND (@cur_status = 'active' - OR @cur_status = 'transfer_in' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter'), - 1, - 0) AS ltfu_cumulative_outcomes_active, - - IF((@is_ltfu_surge_baseline = 1 OR @is_ltfu_after_may_revised = 1 ) - AND (@cur_status = 'active' - OR @cur_status = 'transfer_in' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_out' - OR @cur_status = 'dead'), - 1, - 0) AS ltfu_cumulative_outcomes_total, - - #weekly outcomes - IF(((@is_ltfu_surge_baseline = 1 AND @prev_status = 'ltfu') OR (@is_ltfu_after_may_revised = 1 AND @prev_status = 'ltfu') - OR (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <= 0 )))) - AND @cur_status = 'dead', - 1, - 0) AS ltfu_death_this_week, - - IF(((@is_ltfu_surge_baseline = 1 AND @prev_status = 'ltfu') OR (@is_ltfu_after_may_revised = 1 AND @prev_status = 'ltfu') - OR (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <= 0 )))) - AND @cur_status = 'transfer_out', - 1, - 0) AS ltfu_transfer_out_this_week, - - - IF(((@is_ltfu_surge_baseline = 1 AND @prev_status = 'ltfu') OR (@is_ltfu_after_may_revised = 1 AND @prev_status = 'ltfu') - OR (@prev_status = 'defaulter' AND ((days_since_rtc_date > 28) OR (days_diff_enc_date_and_prev_rtc > 28 and days_since_rtc_date <= 0 )))) - AND (@cur_status = 'active' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_in'), - 1, - 0) AS ltfu_active_this_week, - - CASE - WHEN - (@prev_id != @cur_id - OR @was_ltfu_before_october_2018 IS NULL) - AND year_week = 201839 - AND @cur_status = 'ltfu' - AND @was_active_october_18 = 0 - THEN - @was_ltfu_before_october_2018:=1 - WHEN @prev_id != @cur_id THEN @was_ltfu_before_october_2018:=NULL - ELSE @was_ltfu_before_october_2018 - END AS was_ltfu_before_october_2018, - - - - #self returnies total - IF(@was_ltfu_before_october_2018 = 1 - AND (@cur_status = 'active' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_in' - OR @cur_status = 'transfer_out' - OR @cur_status = 'dead'), - 1, - 0) AS old_ltfus_outcome_total, - - IF(@was_ltfu_before_october_2018 = 1 - AND (@cur_status = 'active' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_out' - OR @cur_status = 'transfer_in' - OR @cur_status = 'dead'), - 1, - 0) AS old_ltfus_to_active, - - - #self returnies this week - IF(@was_ltfu_before_october_2018 = 1 - AND @prev_status = 'ltfu' - AND (@cur_status = 'active' - OR @cur_status = 'missed' - OR @cur_status = 'defaulter' - OR @cur_status = 'transfer_in' - OR @cur_status = 'transfer_out'), - 1, - 0) AS old_ltfus_to_active_this_week, - - - CASE - WHEN - @prev_id = @cur_id - AND @prev_status = 'active' - AND @cur_status = 'ltfu' - THEN - @active_to_ltfu_count:=@active_to_ltfu_count + 1 - ELSE @active_to_ltfu_count:=0 - END AS active_to_ltfu_count, - - CASE - WHEN @prev_id != @cur_id THEN @height:=height - WHEN - @prev_id = @cur_id AND @height IS NULL - AND height IS NOT NULL - THEN - @height:=height - WHEN - @prev_id = @cur_id AND @height - AND height - THEN - @height:=height - ELSE @height - END AS revised_height, - CASE - WHEN @prev_id != @cur_id THEN @weight:=weight - WHEN - @prev_id = @cur_id AND @weight IS NULL - AND weight IS NOT NULL - THEN - @weight:=weight - WHEN - @prev_id = @cur_id AND @weight - AND weight - THEN - @weight:=weight - ELSE @weight - END AS revised_weight, - CASE - WHEN @prev_id != @cur_id THEN @bmi:=bmi - WHEN - @prev_id = @cur_id AND @bmi IS NULL - AND bmi IS NOT NULL - THEN - @bmi:=bmi - WHEN @prev_id = @cur_id AND @bmi AND bmi THEN @bmi:=bmi - ELSE @bmi - END AS revised_bmi, - CASE - WHEN @prev_id = @cur_id THEN @prev_patients_due_for_vl:=@cur_patients_due_for_vl - ELSE @prev_patients_due_for_vl:=0 - END AS prev_patients_due_for_vl, - @cur_patients_due_for_vl:=patients_due_for_vl AS cur_patients_due_for_vl, - IF(tx2_scheduled_this_week = 1 - AND year_week != YEARWEEK(encounter_date) AND prev_rtc_date IS NULL, - 1, - 0) AS tx2_missed_this_week, - IF(tx2_scheduled_this_week = 1 - AND YEARWEEK(encounter_date) < year_week AND prev_rtc_date IS NOT NULL , - 1, - 0) AS tx2_scheduled_this_week_but_came_early, - IF(tx2_scheduled_this_week = 0 - AND tx2_visit_this_week = 1, - 1, - 0) AS tx2_unscheduled_this_week, - - CASE - WHEN days_since_last_vl <= 365 - AND vl_2_date IS NOT NULL - AND (months_since_tb_tx_start_date IS NULL - OR months_since_tb_tx_start_date >= 6) - AND (is_pregnant = 0 OR is_pregnant IS NULL) - AND @bmi >= 18.5 - AND age >= 20 - AND @cur_status = 'active' - AND days_since_starting_arvs > 364 - AND vl_1 >= 0 - AND vl_1 <= 400 - THEN @not_elligible_for_dc := 0 - ELSE @not_elligible_for_dc := 1 - END AS not_elligible_for_dc, - - - CASE - WHEN days_since_last_vl <= 365 - AND vl_2_date IS NOT NULL - AND (months_since_tb_tx_start_date IS NULL - OR months_since_tb_tx_start_date >= 6) - AND on_dc_this_week = 1 - AND (is_pregnant = 0 OR is_pregnant IS NULL) - AND @bmi >= 18.5 - AND age >= 20 - AND @cur_status = 'active' - AND days_since_starting_arvs > 364 - AND vl_1 >= 0 - AND vl_1 <= 400 - THEN @eligible_and_on_dc := 1 - ELSE @eligible_and_on_dc := 0 - END AS eligible_and_on_dc, - - CASE - WHEN days_since_last_vl <= 365 - AND vl_2_date IS NOT NULL - AND (months_since_tb_tx_start_date IS NULL - OR months_since_tb_tx_start_date >= 6) - AND on_dc_this_week != 1 - AND (is_pregnant = 0 OR is_pregnant IS NULL) - AND @bmi >= 18.5 - AND age >= 20 - AND @cur_status = 'active' - AND days_since_starting_arvs > 364 - AND vl_1 >= 0 - AND vl_1 <= 400 - THEN @eligible_not_on_dc := 1 - ELSE - @eligible_not_on_dc := 0 - END AS eligible_not_on_dc, - - - IF(scheduled_this_week = 1 AND @eligible_not_on_dc = 1, 1, 0 ) as eligible_not_on_dc_and_scheduled_this_week, - IF(scheduled_this_week = 0 AND @eligible_not_on_dc = 1, 1, 0 ) as eligible_not_on_dc_and_unscheduled_this_week, - IF(scheduled_this_week = 1 AND @eligible_and_on_dc = 1, 1, 0 ) as eligible_and_on_dc_and_scheduled_this_week, - IF(scheduled_this_week = 0 AND @eligible_and_on_dc = 1, 1, 0 ) as eligible_and_on_dc_and_unscheduled_this_week, - IF(@eligible_not_on_dc = 1 OR @eligible_and_on_dc = 1, 1, 0 ) as elligible_total, - IF(@not_elligible_for_dc = 1 AND on_dc_this_week = 1, 1, 0 ) as not_elligible_and_on_dc, - IF(@eligible_not_on_dc = 1 OR on_dc_this_week = 1, 1, 0 ) as elligible_total_revised, - - CASE - WHEN - @prev_id != @cur_id - THEN @previous_dc_eligibility := NULL - ELSE - @previous_dc_eligibility := @dc_eligible_cumulative - END as previous_dc_eligibility, - - CASE - WHEN days_since_last_vl <= 365 - AND vl_2_date IS NOT NULL - AND (months_since_tb_tx_start_date IS NULL - OR months_since_tb_tx_start_date >= 6) - AND on_dc_this_week != 1 - AND (is_pregnant = 0 OR is_pregnant IS NULL) - AND @bmi >= 18.5 - AND age >= 20 - AND @cur_status = 'active' - AND days_since_starting_arvs >= 364 - AND vl_1 >= 0 - AND vl_1 <= 400 - THEN @dc_eligible_cumulative := 1 - ELSE - @dc_eligible_cumulative := 0 - END AS dc_eligible_cumulative, - - IF(@prev_id = @cur_id AND (@previous_dc_eligibility = 0 OR @previous_dc_eligibility = null) AND @dc_eligible_cumulative = 1, 1, 0 ) as dc_eligible_this_week, - - IF(scheduled_this_week = 1 AND @dc_eligible_cumulative = 1, 1, 0 ) as dc_eligible_and_scheduled_this_week, - IF(patients_due_for_vl = 1 AND @cur_status = 'active' - AND scheduled_this_week = 1, - 1, - 0) AS scheduled_this_week_and_due_for_vl, - IF(patients_due_for_vl = 1 - AND @cur_status = 'active' - AND scheduled_this_week = 0, - 1, - 0) AS unscheduled_this_week_and_due_for_vl, - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 1 - AND @cur_status = 'active', - 1, - 0) AS due_for_vl_has_vl_order, - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 0 - AND @cur_status = 'active', - 1, - 0) AS due_for_vl_dont_have_order, - IF(patients_due_for_vl = 1 AND @cur_status = 'active', - 1, - 0) AS due_for_vl_this_week_active, - IF( patients_due_for_vl= 1 - AND @cur_status = 'active', - 1, - 0) AS overdue_for_vl_active, - IF(is_suppressed = 1 - AND @cur_status = 'active', - 1, - 0) AS is_suppressed_active, - IF(is_un_suppressed = 1 - AND @cur_status = 'active', - 1, - 0) AS is_un_suppressed_active, - - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 1 - AND @cur_status = 'active' - AND scheduled_this_week = 1, - 1, - 0) AS due_for_vl_has_vl_order_scheduled, - - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 1 - AND @cur_status = 'active' - AND scheduled_this_week = 0, - 1, - 0) AS due_for_vl_has_vl_order_unscheduled, - - - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 0 - AND @cur_status = 'active' - AND scheduled_this_week = 1, - 1, - 0) AS due_for_vl_dont_have_order_scheduled, - - IF(patients_due_for_vl = 1 - AND vl_ordered_this_week = 0 - AND @cur_status = 'active' - AND scheduled_this_week = 0, - 1, - 0) AS due_for_vl_dont_have_order_unscheduled, - - IF(is_suppressed = 1 - AND has_vl_this_week = 1, - 1, - 0) AS has_vl_this_week_and_suppressed, - - IF(is_un_suppressed = 1 - AND has_vl_this_week = 1, - 1, - 0) AS has_vl_this_week_and_unsuppressed, - - - - CASE - WHEN @prev_id = @cur_id THEN @prev_defaulted:=@cur_defaulted - ELSE @prev_defaulted:=0 - END AS prev_defaulted, - @cur_defaulted:=defaulted AS cur_defaulted, - IF(@cur_defaulted = 0 - AND @cur_defaulted = 1, - 1, - 0) AS defaulted_this_week, - CASE - WHEN @prev_id = @cur_id THEN @prev_missed:=@cur_missed - ELSE @prev_missed:=0 - END AS prev_missed, - @cur_missed:=missed AS cur_missed, - IF(@prev_missed = 0 AND @cur_missed = 1, - 1, - 0) AS missed_this_week, - CASE - WHEN @prev_id = @cur_id THEN @prev_ltfu:=@cur_ltfu - ELSE @prev_ltfu:=0 - END AS prev_ltfu, - @cur_ltfu:=ltfu AS cur_ltfu, - IF(@cur_ltfu = 0 AND @cur_ltfu = 1, - 1, - 0) AS ltfu_this_week FROM - surge_weekly_report_dataset_1); - - - SELECT CONCAT('replacing into surge_weekly_report_dataset ...'); - replace into surge_weekly_report_dataset_v7 - (select - now(), - elastic_id , - person_uuid , - person_id , - year_week , - encounter_yw , - encounter_id , - encounter_datetime, - encounter_date , - end_date , - start_date , - birthdate , - age , - gender, - clinical_visit_num as clinical_visit_number, - prev_rtc_date , - rtc_date , - visit_this_week , - on_schedule , - early_appointment, - early_appointment_this_week , - late_appointment_this_week , - days_since_rtc_date , - scheduled_this_week , - unscheduled_this_week , - tx2_visit_this_week, - tx2_missed_this_week as missed_tx2_visit_this_week , - tx2_scheduled_this_week_but_came_early, - death_date , - missed_appointment_this_week , - ltfu , - defaulted , - missed , - null as next_status , - active_in_care_this_week , - cur_arv_adherence , - cur_who_stage , - is_pre_art_this_week , - arv_first_regimen_location_id , - arv_first_regimen , - arv_first_regimen_names , - arv_first_regimen_start_date , - days_since_starting_arvs , - started_art_this_week , - enrollment_date , - enrolled_this_week, - art_revisit_this_week , - cur_arv_meds , - cur_arv_meds_names , - cur_arv_meds_strict , - cur_arv_line , - cur_arv_line_strict , - cur_arv_line_reported , - on_art_this_week , - vl_1 , - vl_1_date , - vl_2 , - vl_2_date, - has_vl_this_week , - is_suppressed_active as is_suppressed , - is_un_suppressed_active as is_un_suppressed , - days_since_last_vl , - null as due_for_vl_this_week, - null as reason_for_needing_vl_this_week , - cd4_1 , - cd4_1_date , - child_hiv_status_disclosure_status , - transfer_in_this_week , - transfer_in_location_id , - transfer_in_date , - transfer_out_this_week , - transfer_out_location_id, - transfer_out_date , - status , - dc_eligible_cumulative , - started_dc_this_week , - location_id, - tx2_scheduled_this_week , - tx2_scheduled_honored , - prev_id, - cur_id , - prev_enc_id , - cur_enc_id , - clinical_visit_num , - prev_status , - cur_status , - cur_prep_this_week , - new_prep_this_week, - revised_height as height , - revised_weight as weight , - revised_bmi as bmi , - scheduled_this_week_and_due_for_vl , - unscheduled_this_week_and_due_for_vl , - overdue_for_vl_active , - due_for_vl_has_vl_order, - due_for_vl_dont_have_order, - ltfu_this_week , - missed_this_week , - all_ltfus , - null as ltfu_surge_baseline , - surge_ltfu_and_ltfu_after_may, - surge_ltfu_and_still_ltfu , - newly_ltfu_this_week , - ltfu_cumulative_outcomes_death , - ltfu_cumulative_outcomes_transfer_out , - ltfu_cumulative_outcomes_active, - active_to_ltfu_count , - defaulted_this_week, - due_for_vl_this_week_active , - on_schedule_this_week , - ltfu_cumulative_outcomes_total, - old_ltfus_to_active_this_week, - tx2_unscheduled_this_week , - dead_this_week, - non_ltfu_dead_this_week , - cumulative_dead , - dc_eligible_this_week, - dc_eligible_and_scheduled_this_week, - active_on_dc, - is_ltfu_surge_baseline, - baseline_location, - was_ltfu_may_19 , - was_active_october_18, - not_elligible_for_dc, - eligible_and_on_dc, - eligible_not_on_dc, - eligible_not_on_dc_and_scheduled_this_week, - eligible_not_on_dc_and_unscheduled_this_week, - eligible_and_on_dc_and_scheduled_this_week, - eligible_and_on_dc_and_unscheduled_this_week, - elligible_total, - not_elligible_and_on_dc, - elligible_total_revised, - ltfu_transfer_out_this_week, - ltfu_death_this_week, - ltfu_active_this_week, - - is_ltfu_after_may, - is_ltfu_after_may_total, - week_patient_became_active, - - med_pickup_rtc_date, - days_since_med_pickup_rtc_date, - days_diff_med_rtc_and_next_clinical_date, - - was_ltfu_before_october_2018, - old_ltfus_outcome_total, - old_ltfus_to_active, - intervention_done_this_week, - interventions, - due_for_vl_has_vl_order_scheduled, - due_for_vl_has_vl_order_unscheduled, - due_for_vl_dont_have_order_scheduled, - due_for_vl_dont_have_order_unscheduled, - has_vl_this_week_and_suppressed, - has_vl_this_week_and_unsuppressed, - missed_to_active_this_week, - defaulter_to_active_this_week, - missed_cumulative, - med_surge_ltfus_cumulative, - med_surge_ltfu_and_med_ltfu_after_may, - med_defaulters, - newly_med_ltfu_this_week, - med_surge_ltfus_outcomes, - med_surge_ltfus_outcomes_this_week, - null as column_1, - null as column_2, - null as column_3, - null as column_4, - null as column_5, - null as column_6 - - - from surge_weekly_report_dataset__2); - - - SET @dyn_sql=CONCAT('delete t1 from ',@queue_table,' t1 join surge_weekly_report_dataset_temporary_build_queue t2 using (person_id);'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - #select @person_ids_count := (select count(*) from @queue_table); - SET @dyn_sql=CONCAT('select count(*) into @person_ids_count from ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @cycle_length = timestampdiff(second,@loop_start_time,now()); - set @total_time = @total_time + @cycle_length; - set @cycle_number = @cycle_number + 1; - - set @remaining_time = ceil((@total_time / @cycle_number) * ceil(@person_ids_count / cycle_size) / 60); - - SELECT - @person_ids_count as 'persons remaining', - @cycle_length AS 'Cycle time (s)', - CEIL(@person_ids_count / cycle_size) AS remaining_cycles, - @remaining_time AS 'Est time remaining (min)'; - - end while; - - if(@query_type="build") then - SET @dyn_sql=CONCAT('drop table ',@queue_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - SET @total_rows_to_write=0; - SET @dyn_sql=CONCAT("Select count(*) into @total_rows_to_write from ",@write_table); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @start_write = now(); - select concat(@start_write, " : Writing ",@total_rows_to_write, ' to ',@primary_table); - - SET @dyn_sql=CONCAT('replace into ', @primary_table, - '(select * from ',@write_table,');'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - set @finish_write = now(); - set @time_to_write = timestampdiff(second,@start_write,@finish_write); - select concat(@finish_write, ' : Completed writing rows. Time to write to primary table: ', @time_to_write, ' seconds '); - - SET @dyn_sql=CONCAT('drop table ',@write_table,';'); - PREPARE s1 from @dyn_sql; - EXECUTE s1; - DEALLOCATE PREPARE s1; - - end if; - - set @ave_cycle_length = ceil(@total_time/@cycle_number); - select CONCAT('Average Cycle Length: ', @ave_cycle_length, ' second(s)'); - - set @end = now(); - if(log = true) then - insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end)); - end if; -SELECT - CONCAT(@table_version, - ' : Time to complete: ', - TIMESTAMPDIFF(MINUTE, @start, @end), - ' minutes'); - - END$$ -DELIMITER ;