forked from rohankhera/mimic-omop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etl.sql
55 lines (55 loc) · 1.96 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
WITH
"wardid" as (
select distinct coalesce(curr_careunit,'UNKNOWN') as curr_careunit, curr_wardid
from transfers
),
"gcpt_care_site" AS (
SELECT
nextval('mimic_id_seq') as care_site_id
, CASE
WHEN wardid.curr_careunit IS NOT NULL THEN format_ward(care_site_name, curr_wardid)
ELSE care_site_name end as care_site_name
, place_of_service_concept_id as place_of_service_concept_id
, care_site_name as care_site_source_value
, place_of_service_source_value
FROM gcpt_care_site
left join wardid on care_site_name = curr_careunit
),
"insert_relationship_itself" AS (
INSERT INTO :OMOP_SCHEMA.fact_relationship
(domain_concept_id_1, fact_id_1, domain_concept_id_2, fact_id_2, relationship_concept_id)
SELECT
57 AS domain_concept_id_1 -- 57 Care site
, care_site_id AS fact_id_1
, 57 AS domain_concept_id_2 -- 57 Care site
, care_site_id AS fact_id_2
, 46233688 as relationship_concept_id -- care site has part of care site (any level is part of himself)
FROM gcpt_care_site
),
"insert_relationship_ward_hospit" AS ( --link the wards to BIDMC hospital
INSERT INTO :OMOP_SCHEMA.fact_relationship
(domain_concept_id_1, fact_id_1, domain_concept_id_2, fact_id_2, relationship_concept_id)
SELECT
57 AS domain_concept_id_1 -- 57 Care site
, gc1.care_site_id AS fact_id_1
, 57 AS domain_concept_id_2 -- 57 Care site
, gc2.care_site_id AS fact_id_2
, 46233688 as relationship_concept_id -- care site has part of care site (any level is part of himself)
FROM gcpt_care_site gc1
JOIN gcpt_care_site gc2 ON gc2.care_site_name = 'BIDMC'
WHERE gc1.care_site_name ~ ' ward '
)
INSERT INTO :OMOP_SCHEMA.CARE_SITE
(
care_site_id
, care_site_name
, place_of_service_concept_id
, care_site_source_value
, place_of_service_source_value
)
SELECT gcpt_care_site.care_site_id
, gcpt_care_site.care_site_name
, gcpt_care_site.place_of_service_concept_id
, care_site_source_value
, place_of_service_source_value
FROM gcpt_care_site;