From 820840d9d46a7683eeba4970b3067ae9049a7c01 Mon Sep 17 00:00:00 2001 From: Sarah Keating Date: Mon, 2 Feb 2026 15:06:28 +0000 Subject: [PATCH 1/4] first query for flow sheets --- .gitignore | 1 + sql_scripts/flow_sheet_values.sql | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 sql_scripts/flow_sheet_values.sql diff --git a/.gitignore b/.gitignore index be3f5ce..5fac6cd 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ wheels/ # IDEs .idea/ +.vscode/ # settings files (should not be in the source tree anyway, but just in case) *.env diff --git a/sql_scripts/flow_sheet_values.sql b/sql_scripts/flow_sheet_values.sql new file mode 100644 index 0000000..77d6cc1 --- /dev/null +++ b/sql_scripts/flow_sheet_values.sql @@ -0,0 +1,13 @@ +select observation_datetime as datetime, +value_as_real, +unit, +value_as_text, +comment, +vo.visit_observation_type_id , +(select display_name +from star.visit_observation_type as vt +where vt.visit_observation_type_id = vo.visit_observation_type_id ) as observation +from star.visit_observation as vo +left join star.visit_observation_type as vt +on vo.visit_observation_type_id = vt.visit_observation_type_id +where hospital_visit_id = 'csn' \ No newline at end of file From fb3d6da7edf40527fdbb5df5a8abf161614e1768 Mon Sep 17 00:00:00 2001 From: Sarah Keating Date: Wed, 4 Feb 2026 10:30:40 +0000 Subject: [PATCH 2/4] building up scripts --- sql_scripts/flow_sheet_values.sql | 2 +- sql_scripts/lab_results.sql | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 sql_scripts/lab_results.sql diff --git a/sql_scripts/flow_sheet_values.sql b/sql_scripts/flow_sheet_values.sql index 77d6cc1..bbfdfb9 100644 --- a/sql_scripts/flow_sheet_values.sql +++ b/sql_scripts/flow_sheet_values.sql @@ -1,7 +1,7 @@ select observation_datetime as datetime, value_as_real, unit, -value_as_text, +value_as_text,£. comment, vo.visit_observation_type_id , (select display_name diff --git a/sql_scripts/lab_results.sql b/sql_scripts/lab_results.sql new file mode 100644 index 0000000..9160ba6 --- /dev/null +++ b/sql_scripts/lab_results.sql @@ -0,0 +1,14 @@ +select +result_last_modified_datetime as date, +value_as_real as value, +units, +abnormal_flag, +comment + + +from star.lab_result as r +join star.lab_order as o +on r.lab_order_id = o.lab_order_id +where o.hospital_visit_id = 'xxx' +and r.result_status like 'FINAL' +and r.lab_test_definition_id in ('1001', '390793054', '390793057', '390793060', '722790196') From 8476351baada271db1b38a1545ce57096dcd5e90 Mon Sep 17 00:00:00 2001 From: Sarah Keating Date: Thu, 19 Feb 2026 14:54:35 +0000 Subject: [PATCH 3/4] adding more files --- sql_scripts/flow_sheet_values.sql | 22 ++++++++++++++++------ sql_scripts/get_hospital_visit_id.sql | 2 ++ sql_scripts/lab_results.sql | 12 +++++++++++- sql_scripts/lab_test_names.sql | 5 +++++ 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 sql_scripts/get_hospital_visit_id.sql create mode 100644 sql_scripts/lab_test_names.sql diff --git a/sql_scripts/flow_sheet_values.sql b/sql_scripts/flow_sheet_values.sql index bbfdfb9..2a6c9eb 100644 --- a/sql_scripts/flow_sheet_values.sql +++ b/sql_scripts/flow_sheet_values.sql @@ -1,13 +1,23 @@ +--get the flow sheet values for the particular visit +-- the flow sheet numbers are recorded as id_in_application in the visit_observation_type table +-- Temperature 6 +-- Noradrenalin 3040102622 +-- Metaraminol 12946 +-- Secretion amount 451120 +-- Sputum amount 302600 + + select observation_datetime as datetime, +(select display_name +from star.visit_observation_type as vt +where vt.visit_observation_type_id = vo.visit_observation_type_id ) as observation, value_as_real, unit, -value_as_text,£. +value_as_text, comment, -vo.visit_observation_type_id , -(select display_name -from star.visit_observation_type as vt -where vt.visit_observation_type_id = vo.visit_observation_type_id ) as observation +vo.visit_observation_type_id from star.visit_observation as vo left join star.visit_observation_type as vt on vo.visit_observation_type_id = vt.visit_observation_type_id -where hospital_visit_id = 'csn' \ No newline at end of file +where vt.id_in_application in ('6', '12946', '302600', '451120', '3040102622') +and hospital_visit_id = 'xx' \ No newline at end of file diff --git a/sql_scripts/get_hospital_visit_id.sql b/sql_scripts/get_hospital_visit_id.sql new file mode 100644 index 0000000..5a60221 --- /dev/null +++ b/sql_scripts/get_hospital_visit_id.sql @@ -0,0 +1,2 @@ +select hospital_visit_id from star.hospital_visit as hv +where hv.encounter = 'csn' \ No newline at end of file diff --git a/sql_scripts/lab_results.sql b/sql_scripts/lab_results.sql index 9160ba6..9309722 100644 --- a/sql_scripts/lab_results.sql +++ b/sql_scripts/lab_results.sql @@ -1,5 +1,14 @@ +-- This selects the values are units of lab tests +-- 1011 CRP +-- 722790196 CRP +-- 390793054 WCC +-- 390793057 WCC +-- 390793060 WCC + select result_last_modified_datetime as date, +(select name from star.lab_test_definition as ltd +where ltd.lab_test_definition_id = r.lab_test_definition_id) as name, value_as_real as value, units, abnormal_flag, @@ -9,6 +18,7 @@ comment from star.lab_result as r join star.lab_order as o on r.lab_order_id = o.lab_order_id -where o.hospital_visit_id = 'xxx' +where o.hospital_visit_id = 'xx' and r.result_status like 'FINAL' and r.lab_test_definition_id in ('1001', '390793054', '390793057', '390793060', '722790196') + diff --git a/sql_scripts/lab_test_names.sql b/sql_scripts/lab_test_names.sql new file mode 100644 index 0000000..85ce715 --- /dev/null +++ b/sql_scripts/lab_test_names.sql @@ -0,0 +1,5 @@ +select lab_test_definition_id as id, + name, + standardised_vocabulary as vocab +from star.lab_test_definition as ltd +where ltd.lab_test_definition_id in ('1001', '390793054', '390793057', '390793060', '722790196') \ No newline at end of file From 39611434440923c21c600b6c9b0f83dbe68c393f Mon Sep 17 00:00:00 2001 From: Sarah Keating Date: Sun, 9 Aug 2026 16:54:34 +0100 Subject: [PATCH 4/4] Add and refine SQL scripts for EHR data extraction Introduces new queries for airway and sputum/secretions data, and refactors the flow sheet query to consolidate multiple values into a wider format. A new README documents the overall data extraction goal and the current set of scripts. --- sql_scripts/README.md | 33 ++++++++++++++++++ sql_scripts/airway.sql | 20 +++++++++++ sql_scripts/flow_sheet_values.sql | 50 +++++++++++++++++---------- sql_scripts/get_hospital_visit_id.sql | 7 +++- sql_scripts/lab_results.sql | 10 +++--- sql_scripts/sputum_secretions.sql | 25 ++++++++++++++ 6 files changed, 120 insertions(+), 25 deletions(-) create mode 100644 sql_scripts/README.md create mode 100644 sql_scripts/airway.sql create mode 100644 sql_scripts/sputum_secretions.sql diff --git a/sql_scripts/README.md b/sql_scripts/README.md new file mode 100644 index 0000000..98ae5af --- /dev/null +++ b/sql_scripts/README.md @@ -0,0 +1,33 @@ +# Notes on putting together the EHR needed + +## Goal + +The ultimate aim is to have one csv per patient per day which looks roughly like + + | DateTimeRecorded | Temperature | noradrenaline | etc | Secretions | etc | Placementinstant | RemovalInstant | TubeSize | etc |Units | Comments | +| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | +| 08/08/2026 00:00:15 | 36.4 | | | | | | | | | | +| 08/08/2026 00:00:16 | | 1 | | | | | | | | mg/L | | +| 08/08/2026 00:00:17 | | | | None | | | | | | | +| 08/08/2026 00:02:18 | | | | | | 07/08/2026 | |8mm | | | +| 08/08/2026 00:00:15 | 38.5 | | | | | | | | | | Doctors alerted | + +*Note: The insertion date for a tube may well be earlier than the day on which it is recorded as these seem to get populated during the nightly update to caboodle.* + +## Current scripts + +| script | arguments | record | location of script in repo | database | +|- | --- | --- |- | --- | +| mrn_based_on_bed_and_datetime.sql | location string | csn |waveform-controller/src/sql | star | +| get_hospital_visit_id.sql| csn | hospital_visit_id | waveform-controller/sql_scripts| star | +| flow_sheet_values.sql| hospital_visit_id/today/yesterday | part of table above | waveform-controller/sql_scripts| star | +| airway.sql | csn/today/yesterday | part of the table above | waveform-controller/sql_scripts | caboodle | +| sputum_secretions.sql | csn/today/yesterday | part of the table above | waveform-controller/sql_scripts | caboodle | + +## Unfinished scripts + +lab_results.sql need dealing with in the same way as flow_sheet_values + +lab_test_names.sql forms part of the above query but is useful for exploring + +We need scripts for any of the items in the a tracker that have not yet been covered. diff --git a/sql_scripts/airway.sql b/sql_scripts/airway.sql new file mode 100644 index 0000000..3b0889e --- /dev/null +++ b/sql_scripts/airway.sql @@ -0,0 +1,20 @@ + +SELECT +lda._CreationInstant as DateTimeRecorded, +lda.PlacementInstant, +lda.RemovalInstant, +fvf.Value AS TubeSize + +FROM FilteredAccess.LdaFact lda +JOIN FilteredAccess.FlowsheetValueFact fvf ON fvf.LdaKey = lda.LdaKey +JOIN FilteredAccess.FlowsheetRowDim frd ON frd.FlowsheetRowKey = fvf.FlowsheetRowKey +JOIN FilteredAccess.EncounterFact enc ON enc.EncounterKey = lda.InitialEncounterKey + +WHERE +fvf.FlowsheetRowEpicId ='1120100079' +AND enc.Type != 'Anaesthesia' +AND frd.DisplayName like 'Single Lumen Tube Size' +--and enc.PatientDurableKey = '1782941' + +AND lda._CreationInstant BETWEEN %(yesterday)s AND %(today)s +AND enc.EncounterEpicCsn = %(csn) diff --git a/sql_scripts/flow_sheet_values.sql b/sql_scripts/flow_sheet_values.sql index 2a6c9eb..af9b76f 100644 --- a/sql_scripts/flow_sheet_values.sql +++ b/sql_scripts/flow_sheet_values.sql @@ -1,23 +1,35 @@ ---get the flow sheet values for the particular visit +--get the flow sheet values for the particular visit on a particular day -- the flow sheet numbers are recorded as id_in_application in the visit_observation_type table -- Temperature 6 -- Noradrenalin 3040102622 -- Metaraminol 12946 --- Secretion amount 451120 --- Sputum amount 302600 - - -select observation_datetime as datetime, -(select display_name -from star.visit_observation_type as vt -where vt.visit_observation_type_id = vo.visit_observation_type_id ) as observation, -value_as_real, -unit, -value_as_text, -comment, -vo.visit_observation_type_id -from star.visit_observation as vo -left join star.visit_observation_type as vt -on vo.visit_observation_type_id = vt.visit_observation_type_id -where vt.id_in_application in ('6', '12946', '302600', '451120', '3040102622') -and hospital_visit_id = 'xx' \ No newline at end of file + +SELECT + vo.observation_datetime AS DateTimeRecorded, + + (array_agg(vo.value_as_real) FILTER ( + WHERE vt.id_in_application = '6' + ))[1] AS "Temperature", + + (array_agg(vo.value_as_real) FILTER ( + WHERE vt.id_in_application = '3040102622' + ))[1] AS "Noradrenaline", + + (array_agg(vo.value_as_real) FILTER ( + WHERE vt.id_in_application = '12946' + ))[1] AS "Metaraminol", + + vo.unit AS Units, + vo.comment AS Comments + +FROM star.visit_observation AS vo + +LEFT JOIN star.visit_observation_type AS vt + ON vo.visit_observation_type_id = vt.visit_observation_type_id + +WHERE vt.id_in_application IN ('6', '3040102622', '12946') +AND vo.valid_from BETWEEN %(yesterday)s AND %(today)s +AND vo.hospital_visit_id = %(hospital_visit_id)s + +GROUP BY DateTimeRecorded, Units, vo.comment + diff --git a/sql_scripts/get_hospital_visit_id.sql b/sql_scripts/get_hospital_visit_id.sql index 5a60221..408447f 100644 --- a/sql_scripts/get_hospital_visit_id.sql +++ b/sql_scripts/get_hospital_visit_id.sql @@ -1,2 +1,7 @@ + + + select hospital_visit_id from star.hospital_visit as hv -where hv.encounter = 'csn' \ No newline at end of file +where hv.encounter = %(csn)s -- note the CSN must be in quotes + + diff --git a/sql_scripts/lab_results.sql b/sql_scripts/lab_results.sql index 9309722..aa05264 100644 --- a/sql_scripts/lab_results.sql +++ b/sql_scripts/lab_results.sql @@ -6,13 +6,13 @@ -- 390793060 WCC select -result_last_modified_datetime as date, +r.result_last_modified_datetime as date, (select name from star.lab_test_definition as ltd where ltd.lab_test_definition_id = r.lab_test_definition_id) as name, -value_as_real as value, -units, -abnormal_flag, -comment +r.value_as_real as value, +r.units, +r.abnormal_flag, +r.comment from star.lab_result as r diff --git a/sql_scripts/sputum_secretions.sql b/sql_scripts/sputum_secretions.sql new file mode 100644 index 0000000..8f27ab8 --- /dev/null +++ b/sql_scripts/sputum_secretions.sql @@ -0,0 +1,25 @@ +<- Retrieved the information about sputum and secretions > + +SELECT +fv.TakenInstant AS 'DateTimeRecorded', +CASE +WHEN fsd.FlowsheetRowEpicId = '451120' +THEN fv.Value +END +AS 'Secretions' , +CASE +WHEN fsd.FlowsheetRowEpicId = '302600' +THEN fv.Value +END +AS 'Sputum' , +fv.Comment AS Comments + +FROM FilteredAccess.FlowsheetValueFact fv +INNER JOIN FilteredAccess.FlowsheetRowDim fsd ON fv.FlowsheetRowKey = fsd.FlowsheetRowKey +INNER JOIN FilteredAccess.EncounterFact enc ON fv.EncounterKey = enc.EncounterKey + +WHERE +(fsd.FlowsheetRowEpicId = '451120' OR +fsd.FlowsheetRowEpicId ='302600') +AND fv.TakenInstant BETWEEN %(yesterday)s AND %(today)s +AND enc.EncounterEpicCsn = %(csn)