-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathetl.sql
75 lines (75 loc) · 1.92 KB
/
etl.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
WITH
"noteevents" AS (
SELECT
mimic_id as note_id
, cgid
, subject_id
, hadm_id
, chartdate as note_date
, charttime as note_datetime
, description as note_title
, text as note_text
, category as note_source_value
FROM noteevents
WHERE iserror IS NULL
),
"gcpt_note_category_to_concept" AS (
SELECT category as note_source_value, concept_id as note_type_concept_id FROM gcpt_note_category_to_concept),
"admissions" as (SELECT hadm_id, mimic_id as visit_occurrence_id FROM admissions),
"patients" as (SELECT subject_id, mimic_id as person_id FROM patients),
"caregivers" AS (SELECT mimic_id AS provider_id, cgid FROM caregivers),
"row_to_insert" AS (
SELECT
note_id
, person_id
, note_date
, note_datetime as note_datetime
, coalesce(gcpt_note_category_to_concept.note_type_concept_id,0) AS note_type_concept_id
, 0 AS note_class_concept_id -- TODO/ not yet mapped to CDO
, note_title
, note_text
, 0 AS encoding_concept_id
, 40639385 as language_concept_id -- English (from metadata, maybe not the best)
, provider_id
, visit_occurrence_id
, noteevents.note_source_value AS note_source_value
, null::integer visit_detail_id
FROM noteevents
LEFT JOIN gcpt_note_category_to_concept ON trim(noteevents.note_source_value) = trim(gcpt_note_category_to_concept.note_source_value)
LEFT JOIN patients USING (subject_id)
LEFT JOIN admissions USING (hadm_id)
LEFT JOIN caregivers USING (cgid)
)
INSERT INTO :OMOP_SCHEMA.NOTE
(
note_id
, person_id
, note_date
, note_datetime
, note_type_concept_id
, note_class_concept_id
, note_title
, note_text
, encoding_concept_id
, language_concept_id
, provider_id
, visit_occurrence_id
, note_source_value
, visit_detail_id
)
SELECT
note_id
, person_id
, note_date
, note_datetime
, note_type_concept_id
, note_class_concept_id
, note_title
, note_text
, encoding_concept_id
, language_concept_id
, provider_id
, visit_occurrence_id
, note_source_value
, visit_detail_id
FROM row_to_insert;