Skip to content
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ wheels/

# IDEs
.idea/
.vscode/

# settings files (should not be in the source tree anyway, but just in case)
*.env
Expand Down
33 changes: 33 additions & 0 deletions sql_scripts/README.md
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 20 additions & 0 deletions sql_scripts/airway.sql
Original file line number Diff line number Diff line change
@@ -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)
35 changes: 35 additions & 0 deletions sql_scripts/flow_sheet_values.sql
Original file line number Diff line number Diff line change
@@ -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

7 changes: 7 additions & 0 deletions sql_scripts/get_hospital_visit_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- Retrieve the hospital_visit_id associated with the csn value applied to this function -->


select hospital_visit_id from star.hospital_visit as hv
where hv.encounter = %(csn)s -- note the CSN must be in quotes


24 changes: 24 additions & 0 deletions sql_scripts/lab_results.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- This selects the values are units of lab tests
-- 1011 CRP
-- 722790196 CRP
-- 390793054 WCC
-- 390793057 WCC
-- 390793060 WCC

select
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,
r.value_as_real as value,
r.units,
r.abnormal_flag,
r.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 = 'xx'
and r.result_status like 'FINAL'
and r.lab_test_definition_id in ('1001', '390793054', '390793057', '390793060', '722790196')

5 changes: 5 additions & 0 deletions sql_scripts/lab_test_names.sql
Original file line number Diff line number Diff line change
@@ -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')
25 changes: 25 additions & 0 deletions sql_scripts/sputum_secretions.sql
Original file line number Diff line number Diff line change
@@ -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)
Loading