Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commit #236

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 26 additions & 61 deletions jobs/sql/extractions/all_encounters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ SET @partition = '${partitionNum}';
set sql_safe_updates = 0;
set @next_appt_date_concept_id = CONCEPT_FROM_MAPPING('PIH', 5096);

drop temporary table if exists temp_all_encounters;
create temporary table temp_all_encounters
drop temporary table if exists temp_encounter;
create temporary table temp_encounter
(
encounter_id int,
encounter_datetime datetime,
patient_id int,
visit_id int,
creator int(11),
user_entered varchar(255),
location_id int,
encounter_location varchar(255),
encounter_type int,
encounter_type_name varchar(50),
entered_datetime datetime,
emr_id varchar(15),
Expand All @@ -22,77 +24,40 @@ create temporary table temp_all_encounters
);

-- If there is not a previous watermark, initialize with all encounters
insert into temp_all_encounters (encounter_id)
select encounter_id from encounter
insert into temp_encounter (encounter_id, encounter_datetime, encounter_type, location_id, patient_id, visit_id, creator, entered_datetime, voided)
select encounter_id, encounter_datetime, encounter_type, location_id, patient_id, visit_id, creator, date_created, voided
from encounter
where voided = 0
and @previousWatermark is null;

-- If there is a previous watermark, initialize with only those encounters since that watermark
insert into temp_all_encounters (encounter_id)
select encounter_id from encounter
insert into temp_encounter (encounter_id, encounter_datetime, encounter_type, location_id, patient_id, visit_id, creator, entered_datetime, voided )
select encounter_id, encounter_datetime,
encounter_type, location_id, encounter.patient_id, visit_id, creator, date_created, voided
from encounter
inner join dbevent_patient dp on encounter.patient_id = dp.patient_id
where voided = 0
and @previousWatermark is not null
and dp.last_updated >= @previousWatermark
and dp.last_updated <= @newWatermark;

create index temp_all_encounters_encounterId ON temp_all_encounters (encounter_id);

-- Load the encounters
update temp_all_encounters t
inner join encounter e on t.encounter_id = e.encounter_id
set t.encounter_datetime = e.encounter_datetime,
t.encounter_type_name = encounter_type_name_from_id(e.encounter_type),
t.encounter_location = location_name(e.location_id),
t.patient_id = e.patient_id,
t.visit_id = e.visit_id,
t.user_entered = person_name_of_user(e.creator),
t.entered_datetime = e.date_created,
t.voided = e.voided
;
CREATE INDEX temp_all_encounters_encounterId ON temp_encounter (encounter_id);
CREATE INDEX temp_all_encounters_patientId ON temp_encounter (patient_id);

CREATE INDEX temp_all_encounters_patientId ON temp_all_encounters (patient_id);
UPDATE temp_encounter t SET t.encounter_type_name = encounter_type_name_from_id(t.encounter_type);
UPDATE temp_encounter t SET t.encounter_location = location_name(t.location_id);
UPDATE temp_encounter t SET t.user_entered = person_name_of_user(t.creator);
UPDATE temp_encounter t SET t.emr_id = patient_identifier(t.patient_id, 'ZL EMR ID');

-- next visit date
-- all next appt date obs loaded to temp table before looking up for each encounter
-- this avoids having to access the full obs table for each encounter
DROP TABLE IF EXISTS temp_next_appt_obs;
CREATE TEMPORARY TABLE temp_next_appt_obs
select encounter_id, max(value_datetime) "next_appt_date"
from obs
where concept_id = @next_appt_date_concept_id
and voided = 0
group by encounter_id;
DROP TABLE IF EXISTS temp_obs;
CREATE TEMPORARY TABLE temp_obs AS
SELECT o.person_id, o.encounter_id, o.obs_id , o.concept_id, o.value_datetime, o.voided , o.date_created
FROM temp_encounter te INNER JOIN obs o ON te.encounter_id=o.encounter_id
WHERE o.voided =0
AND concept_id = @next_appt_date_concept_id;

create index temp_next_appt_obs_ei on temp_next_appt_obs (encounter_id);

update temp_all_encounters te
inner join temp_next_appt_obs tvo on tvo.encounter_id = te.encounter_id
set te.next_appt_date = tvo.next_appt_date
where te.voided = 0;

-- emr_id
-- unique patients loaded to temp table before looking up emr_id and joining back in to the temp_all_encounters table
drop temporary table if exists temp_emrids;
create temporary table temp_emrids
(
patient_id int(11),
emr_id varchar(50)
);

insert into temp_emrids(patient_id)
select DISTINCT patient_id
from temp_all_encounters
;
create index temp_emrids_patient_id on temp_emrids (patient_id);

update temp_emrids t
set emr_id = patient_identifier(patient_id, 'ZL EMR ID');

update temp_all_encounters t
inner join temp_emrids te on te.patient_id = t.patient_id
set t.emr_id = te.emr_id
;
UPDATE temp_encounter t SET t.next_appt_date = obs_value_datetime_from_temp(t.encounter_id, 'PIH', '5096');

-- final query
select emr_id,
Expand All @@ -106,5 +71,5 @@ select emr_id,
user_entered,
next_appt_date,
voided
from temp_all_encounters t
ORDER BY t.patient_id, t.encounter_id;
from temp_encounter t
ORDER BY t.patient_id, t.encounter_id;