diff --git a/.gitignore b/.gitignore index 4cf281c..a137605 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/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 new file mode 100644 index 0000000..af9b76f --- /dev/null +++ b/sql_scripts/flow_sheet_values.sql @@ -0,0 +1,35 @@ +--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 + +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 new file mode 100644 index 0000000..408447f --- /dev/null +++ b/sql_scripts/get_hospital_visit_id.sql @@ -0,0 +1,7 @@ + + + +select hospital_visit_id from star.hospital_visit as hv +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 new file mode 100644 index 0000000..21b8b5d --- /dev/null +++ b/sql_scripts/lab_results.sql @@ -0,0 +1,35 @@ +-- This selects the values of lab tests +-- 1011 CRP +-- 722790196 CRP +-- 390793054 WCC +-- 390793057 WCC +-- 390793060 WCC + +SELECT + r.result_last_modified_datetime AS DateTimeRecorded, + + MAX(r.value_as_real) FILTER (WHERE r.lab_test_definition_id = '1001') AS "C-reactive protein", + MAX(r.value_as_real) FILTER (WHERE r.lab_test_definition_id = '390793054') AS "CSF WCC TUBE 1", + MAX(r.value_as_real) FILTER (WHERE r.lab_test_definition_id = '390793057') AS "CSF WCC TUBE 2", + MAX(r.value_as_real) FILTER (WHERE r.lab_test_definition_id = '390793060') AS "CSF WCC TUBE 3", + MAX(r.value_as_real) FILTER (WHERE r.lab_test_definition_id = '722790196') AS "C-reactive protein" + + r.units AS Units, + r.abnormal_flag AS Abnormal_result, + r.comment AS Comments + +FROM star.lab_result AS r +LEFT JOIN star.lab_order AS o + ON r.lab_order_id = o.lab_order_id + +WHERE r.result_status like 'FINAL' +AND +r.lab_test_definition_id IN ('1001', + '390793054', + '390793057', + '390793060', + '722790196') +AND vo.valid_from BETWEEN %(yesterday)s AND %(today)s +AND o.hospital_visit_id = %(hospital_visit_id)s + +GROUP BY DateTimeRecorded, Units, Abnormal_result, Comments 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 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)