generated from databricks-industry-solutions/industry-solutions-blueprints
-
Notifications
You must be signed in to change notification settings - Fork 12
/
4-omop531-etl-synthea.sql
1225 lines (1107 loc) · 34.6 KB
/
4-omop531-etl-synthea.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Databricks notebook source
-- MAGIC %md
-- MAGIC You may find this series of notebooks at https://github.com/databricks-industry-solutions/omop-cdm. For more information about this solution accelerator, visit https://www.databricks.com/blog/2021/07/19/unlocking-the-power-of-health-data-with-a-modern-data-lakehouse.html.
-- COMMAND ----------
-- MAGIC %md
-- MAGIC # ETL: Synthea to OMOP531
-- MAGIC Now that all tables and schemas are defined, we need to ETL the data from the source to OMOP model. Note that ETL process is heavily dependent on the source data model which varies from institution to institution. Arguably writing the ETL is the most time consuming part of the process.
-- MAGIC In this notebook, we use an example ETL, based on [OHDSI/ETL-Synthea](https://github.com/OHDSI/ETL-Synthea) repository, for mapping from synthea model to OMOP to populate a subset of OMOP tables as a point of reference for benchmarking. Also, we provide example notebooks for data analysis on the resulting omop tables using SQL, python and R.
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ## Load synthea tables from Bronze layer
-- COMMAND ----------
-- MAGIC %python
-- MAGIC import json
-- MAGIC
-- MAGIC project_name='omop-cdm-100K'
-- MAGIC omop_version="OMOP531"
-- MAGIC
-- MAGIC with open(f'/tmp/{project_name}_configs.json','r') as f:
-- MAGIC paths = json.load(f)
-- MAGIC base_path = paths['base_path']
-- MAGIC delta_path = paths['delta_path']
-- MAGIC
-- MAGIC delta_bronze_path = f'{delta_path}/bronze'
-- MAGIC delta_silver_path = f'{delta_path}/silver/{omop_version}'
-- COMMAND ----------
-- MAGIC %python
-- MAGIC table_names =['allergies','careplans','conditions','devices','encounters','imaging_studies',\
-- MAGIC 'immunizations','medications','observations','organizations','patients',\
-- MAGIC 'payer_transitions','payers','procedures','providers','supplies']
-- MAGIC for table_name in table_names:
-- MAGIC sql(f"""
-- MAGIC CREATE OR REPLACE TEMPORARY VIEW {table_name} AS (
-- MAGIC SELECT * FROM delta.`{delta_bronze_path}/{table_name}`
-- MAGIC )
-- MAGIC """
-- MAGIC )
-- COMMAND ----------
-- MAGIC %py
-- MAGIC sql(f"USE {omop_version};")
-- MAGIC print(f"Using OMOP version {omop_version}")
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ## Create temporary views
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ### IP_VISITS
-- COMMAND ----------
CREATE OR REPLACE TEMPORARY VIEW IP_VISITS AS (
WITH CTE_END_DATES AS (
SELECT
patient,
encounterclass,
DATE_ADD(EVENT_DATE, -1) AS END_DATE
FROM
(
SELECT
patient,
encounterclass,
EVENT_DATE,
EVENT_TYPE,
MAX(START_ORDINAL) OVER (
PARTITION BY patient,
encounterclass
ORDER BY
EVENT_DATE,
EVENT_TYPE ROWS UNBOUNDED PRECEDING
) AS START_ORDINAL,
ROW_NUMBER() OVER (
PARTITION BY patient,
encounterclass
ORDER BY
EVENT_DATE,
EVENT_TYPE
) AS OVERALL_ORD
FROM
(
SELECT
patient,
encounterclass,
start AS EVENT_DATE,
-1 AS EVENT_TYPE,
ROW_NUMBER () OVER (
PARTITION BY patient,
encounterclass
ORDER BY
start,
stop
) AS START_ORDINAL
FROM
encounters
WHERE
encounterclass = 'inpatient'
UNION ALL
SELECT
patient,
encounterclass,
DATE_ADD(stop, 1),
1 AS EVENT_TYPE,
NULL
FROM
encounters
WHERE
encounterclass = 'inpatient'
) RAWDATA
) E
WHERE
(2 * E.START_ORDINAL - E.OVERALL_ORD = 0)
),
CTE_VISIT_ENDS AS (
SELECT
MIN(V.id) AS encounter_id,
V.patient,
V.encounterclass,
V.start AS VISIT_START_DATE,
MIN(E.END_DATE) AS VISIT_END_DATE
FROM
encounters V
INNER JOIN CTE_END_DATES E ON V.patient = E.patient
AND V.encounterclass = E.encounterclass
AND E.END_DATE >= V.start
GROUP BY
V.patient,
V.encounterclass,
V.start
)
SELECT
T2.encounter_id,
T2.patient,
T2.encounterclass,
T2.VISIT_START_DATE,
T2.VISIT_END_DATE
FROM
(
SELECT
encounter_id,
patient,
encounterclass,
MIN(VISIT_START_DATE) AS VISIT_START_DATE,
VISIT_END_DATE
FROM
CTE_VISIT_ENDS
GROUP BY
encounter_id,
patient,
encounterclass,
VISIT_END_DATE
) T2
);
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ### ER_VISITS
-- COMMAND ----------
CREATE OR REPLACE TEMPORARY VIEW ER_VISITS AS
SELECT
T2.encounter_id,
T2.patient,
T2.encounterclass,
T2.VISIT_START_DATE,
T2.VISIT_END_DATE
FROM
(
SELECT
MIN(encounter_id) AS encounter_id,
patient,
encounterclass,
VISIT_START_DATE,
MAX(VISIT_END_DATE) AS VISIT_END_DATE
FROM
(
SELECT
CL1.id AS encounter_id,
CL1.patient,
CL1.encounterclass,
CL1.start AS VISIT_START_DATE,
CL2.stop AS VISIT_END_DATE
FROM
encounters CL1
INNER JOIN encounters CL2 ON CL1.patient = CL2.patient
AND CL1.start = CL2.start
AND CL1.encounterclass = CL2.encounterclass
WHERE
CL1.encounterclass in ('emergency', 'urgent')
) T1
GROUP BY
patient,
encounterclass,
VISIT_START_DATE
) T2;
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ### OP_VISITS
-- COMMAND ----------
CREATE
OR REPLACE TEMPORARY VIEW OP_VISITS AS WITH CTE_VISITS_DISTINCT AS (
SELECT
MIN(id) AS encounter_id,
patient,
encounterclass,
start AS VISIT_START_DATE,
stop AS VISIT_END_DATE
FROM
encounters
WHERE
encounterclass in ('ambulatory', 'wellness', 'outpatient')
GROUP BY
patient,
encounterclass,
start,
stop
)
SELECT
MIN(encounter_id) AS encounter_id,
patient,
encounterclass,
VISIT_START_DATE,
MAX(VISIT_END_DATE) AS VISIT_END_DATE
FROM
CTE_VISITS_DISTINCT
GROUP BY
patient,
encounterclass,
VISIT_START_DATE;
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ## Create tables
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ### all_visits
-- COMMAND ----------
DROP TABLE IF EXISTS all_visits;
CREATE TABLE all_visits
SELECT
*,
ROW_NUMBER() OVER(
ORDER BY
patient
) as visit_occurrence_id
FROM
(
SELECT
*
FROM
IP_VISITS
UNION ALL
SELECT
*
FROM
ER_VISITS
UNION ALL
SELECT
*
FROM
OP_VISITS
) T1;
-- COMMAND ----------
SELECT
*
FROM
all_visits
LIMIT
100;
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ### assign_all_visit_ids
-- COMMAND ----------
DROP TABLE IF EXISTS assign_all_visit_ids;
CREATE TABLE assign_all_visit_ids
SELECT
E.id AS encounter_id,
E.patient as person_source_value,
E.start AS date_service,
E.stop AS date_service_end,
E.encounterclass,
AV.encounterclass AS VISIT_TYPE,
AV.VISIT_START_DATE,
AV.VISIT_END_DATE,
AV.VISIT_OCCURRENCE_ID,
CASE
WHEN E.encounterclass = 'inpatient'
and AV.encounterclass = 'inpatient' THEN VISIT_OCCURRENCE_ID
WHEN E.encounterclass in ('emergency', 'urgent') THEN (
CASE
WHEN AV.encounterclass = 'inpatient'
AND E.start > AV.VISIT_START_DATE THEN VISIT_OCCURRENCE_ID
WHEN AV.encounterclass in ('emergency', 'urgent')
AND E.start = AV.VISIT_START_DATE THEN VISIT_OCCURRENCE_ID
ELSE NULL
END
)
WHEN E.encounterclass in ('ambulatory', 'wellness', 'outpatient') THEN (
CASE
WHEN AV.encounterclass = 'inpatient'
AND E.start >= AV.VISIT_START_DATE THEN VISIT_OCCURRENCE_ID
WHEN AV.encounterclass in ('ambulatory', 'wellness', 'outpatient') THEN VISIT_OCCURRENCE_ID
ELSE NULL
END
)
ELSE NULL
END AS VISIT_OCCURRENCE_ID_NEW
FROM
ENCOUNTERS E
INNER JOIN ALL_VISITS AV ON E.patient = AV.patient
AND E.start >= AV.VISIT_START_DATE
AND E.start <= AV.VISIT_END_DATE;
-- COMMAND ----------
SELECT * FROM assign_all_visit_ids LIMIT 100;
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ### final_visit_ids
-- COMMAND ----------
DROP TABLE IF EXISTS final_visit_ids;
CREATE TABLE final_visit_ids
SELECT encounter_id, VISIT_OCCURRENCE_ID_NEW
FROM(
SELECT *, ROW_NUMBER () OVER (PARTITION BY encounter_id ORDER BY PRIORITY) AS RN
FROM (
SELECT *,
CASE
WHEN encounterclass in ('emergency','urgent')
THEN (
CASE
WHEN VISIT_TYPE = 'inpatient' AND VISIT_OCCURRENCE_ID_NEW IS NOT NULL
THEN 1
WHEN VISIT_TYPE in ('emergency','urgent') AND VISIT_OCCURRENCE_ID_NEW IS NOT NULL
THEN 2
ELSE 99
END
)
WHEN encounterclass in ('ambulatory', 'wellness', 'outpatient')
THEN (
CASE
WHEN VISIT_TYPE = 'inpatient' AND VISIT_OCCURRENCE_ID_NEW IS NOT NULL
THEN 1
WHEN VISIT_TYPE in ('ambulatory', 'wellness', 'outpatient') AND VISIT_OCCURRENCE_ID_NEW IS NOT NULL
THEN 2
ELSE 99
END
)
WHEN encounterclass = 'inpatient' AND VISIT_TYPE = 'inpatient' AND VISIT_OCCURRENCE_ID_NEW IS NOT NULL
THEN 1
ELSE 99
END AS PRIORITY
FROM ASSIGN_ALL_VISIT_IDS
) T1
) T2
WHERE RN=1;
-- COMMAND ----------
SELECT * FROM final_visit_ids LIMIT 100;
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ### person
-- COMMAND ----------
INSERT
OVERWRITE person
SELECT
ROW_NUMBER() OVER(
ORDER BY
p.id
),
case
upper(p.gender)
when 'M' then 8507
when 'F' then 8532
end,
YEAR(p.birthdate),
MONTH(p.birthdate),
DAY(p.birthdate),
p.birthdate,
case
upper(p.race)
when 'WHITE' then 8527
when 'BLACK' then 8516
when 'ASIAN' then 8515
else 0
end,
case
when upper(p.race) = 'HISPANIC' then 38003563
else 0
end,
NULL,
0,
NULL,
p.id,
p.gender,
0,
p.race,
0,
p.ethnicity,
0
from
patients p
where
p.gender is not null;
-- COMMAND ----------
SELECT
*
FROM
person
LIMIT
100;
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ### observation_period
-- COMMAND ----------
INSERT
OVERWRITE observation_period
SELECT
ROW_NUMBER() OVER(
ORDER BY
person_id
),
person_id,
start_date,
end_date,
44814724 AS period_type_concept_id
FROM
(
SELECT
p.person_id,
MIN(e.start) AS start_date,
MAX(e.stop) AS end_date
FROM
person p
INNER JOIN encounters e ON p.person_source_value = e.patient
GROUP BY
p.person_id
) tmp;
-- COMMAND ----------
SELECT
*
FROM
observation_period
LIMIT
100;
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ### visit_occurrence
-- COMMAND ----------
INSERT
OVERWRITE visit_occurrence
select
av.visit_occurrence_id,
p.person_id,
case
lower(av.encounterclass)
when 'ambulatory' then 9202
when 'emergency' then 9203
when 'inpatient' then 9201
when 'wellness' then 9202
when 'urgentcare' then 9203
when 'outpatient' then 9202
else 0
end,
av.visit_start_date,
av.visit_start_date,
av.visit_end_date,
av.visit_end_date,
44818517,
0,
null,
av.encounter_id,
0,
0,
NULL,
0,
NULL,
lag(visit_occurrence_id) over(
partition by p.person_id
order by
av.visit_start_date
)
from
all_visits av
join person p on av.patient = p.person_source_value
where
av.visit_occurrence_id in (
select
distinct visit_occurrence_id_new
from
final_visit_ids
);
-- COMMAND ----------
SELECT
*
FROM
visit_occurrence
LIMIT
100;
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ### condition_occurrence
-- COMMAND ----------
INSERT OVERWRITE condition_occurrence
select
row_number()over(order by p.person_id),
p.person_id,
coalesce(srctostdvm.target_concept_id,0) AS target_concept_id,
c.start,
c.start,
c.stop,
c.stop,
32020,
null,
0,
fv.visit_occurrence_id_new AS visit_occurrence_id,
0,
c.code,
coalesce(srctosrcvm.source_concept_id,0),
NULL,
0
from conditions c
inner join source_to_standard_vocab_map srctostdvm
on srctostdvm.source_code = c.code
and srctostdvm.target_domain_id = 'Condition'
and srctostdvm.source_vocabulary_id = 'SNOMED'
and srctostdvm.target_standard_concept = 'S'
and (srctostdvm.target_invalid_reason IS NULL OR srctostdvm.target_invalid_reason = '')
left join source_to_source_vocab_map srctosrcvm
on srctosrcvm.source_code = c.code
and srctosrcvm.source_vocabulary_id = 'SNOMED'
left join final_visit_ids fv
on fv.encounter_id = c.encounter
inner join person p
on c.patient = p.person_source_value;
-- COMMAND ----------
SELECT
*
FROM
condition_occurrence
LIMIT
100;
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ### measurement
-- COMMAND ----------
INSERT OVERWRITE measurement
SELECT row_number()over(order by person_id) AS measurement_id,
person_id,
measurement_concept_id,
measurement_date,
measurement_datetime,
measurement_time,
measurement_type_concept_id,
operator_concept_id,
value_as_number,
value_as_concept_id,
unit_concept_id,
range_low,
range_high,
provider_id,
visit_occurrence_id,
visit_detail_id,
measurement_source_value,
measurement_source_concept_id,
unit_source_value,
value_source_value
from (
select
p.person_id,
coalesce(srctostdvm.target_concept_id,0) AS measurement_concept_id,
pr.start AS measurement_date,
pr.start AS measurement_datetime,
pr.start AS measurement_time,
5001 AS measurement_type_concept_id,
0 as operator_concept_id,
null as value_as_number,
0 as value_as_concept_id,
0 as unit_concept_id,
null as range_low,
null as range_high,
0 as provider_id,
fv.visit_occurrence_id_new AS visit_occurrence_id,
0 as visit_detail_id,
pr.code AS measurement_source_value,
coalesce(srctosrcvm.source_concept_id,0) AS measurement_source_concept_id,
null as unit_source_value,
null as value_source_value
from procedures pr
inner join source_to_standard_vocab_map srctostdvm
on srctostdvm.source_code = pr.code
and srctostdvm.target_domain_id = 'Measurement'
and srctostdvm.source_vocabulary_id = 'SNOMED'
and srctostdvm.target_standard_concept = 'S'
and (srctostdvm.target_invalid_reason IS NULL or srctostdvm.target_invalid_reason = '')
left join source_to_source_vocab_map srctosrcvm
on srctosrcvm.source_code = pr.code
and srctosrcvm.source_vocabulary_id = 'SNOMED'
left join final_visit_ids fv
on fv.encounter_id = pr.encounter
join person p
on p.person_source_value = pr.patient
union all
select
p.person_id,
coalesce(srctostdvm.target_concept_id,0) AS target_concept_id,
o.date,
o.date,
o.date,
5001,
0,
nanvl(cast(o.value as float), null) as value_as_number,
coalesce(srcmap2.target_concept_id,0) AS value_as_concept_id,
coalesce(srcmap1.target_concept_id,0) AS unit_concept_id,
null ,
null ,
0 as provider_id,
fv.visit_occurrence_id_new AS visit_occurrence_id,
0 as visit_detail_id,
o.code AS measurement_source_value,
coalesce(srctosrcvm.source_concept_id,0) AS measurement_source_concept_id,
o.units AS unit_source_value,
o.value AS value_source_value
from observations o
inner join source_to_standard_vocab_map srctostdvm
on srctostdvm.source_code = o.code
and srctostdvm.target_domain_id = 'Measurement'
and srctostdvm.source_vocabulary_id = 'LOINC'
and srctostdvm.target_standard_concept = 'S'
and (srctostdvm.target_invalid_reason IS NULL OR srctostdvm.target_invalid_reason = '')
left join source_to_standard_vocab_map srcmap1
on srcmap1.source_code = o.units
and srcmap1.target_vocabulary_id = 'UCUM'
and srcmap1.target_standard_concept = 'S'
and (srcmap1.target_invalid_reason IS NULL OR srcmap1.target_invalid_reason = '')
left join source_to_standard_vocab_map srcmap2
on srcmap2.source_code = o.value
and srcmap2.target_domain_id = 'Meas value'
and srcmap2.target_standard_concept = 'S'
and (srcmap2.target_invalid_reason IS NULL OR srcmap2.target_invalid_reason = '')
left join source_to_source_vocab_map srctosrcvm
on srctosrcvm.source_code = o.code
and srctosrcvm.source_vocabulary_id = 'LOINC'
left join final_visit_ids fv
on fv.encounter_id = o.encounter
inner join person p
on p.person_source_value = o.patient
) tmp;
-- COMMAND ----------
SELECT
*
FROM
measurement
LIMIT
100;
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ### procedure_occurrence
-- COMMAND ----------
INSERT OVERWRITE procedure_occurrence
SELECT
row_number()over(order by p.person_id),
p.person_id,
coalesce(srctostdvm.target_concept_id, 0) as target_concept_id,
pr.start,
pr.start,
38000275,
0,
null,
0,
fv.visit_occurrence_id_new AS visit_occurrence_id,
0,
pr.code,
coalesce(srctosrcvm.source_concept_id,0),
NULL
from procedures pr
inner join source_to_standard_vocab_map srctostdvm
on srctostdvm.source_code = pr.code
and srctostdvm.target_domain_id = 'Procedure'
and srctostdvm.source_vocabulary_id = 'SNOMED'
and srctostdvm.target_standard_concept = 'S'
and (srctostdvm.target_invalid_reason IS NULL OR srctostdvm.target_invalid_reason = '')
left join source_to_source_vocab_map srctosrcvm
on srctosrcvm.source_code = pr.code
and srctosrcvm.source_vocabulary_id = 'SNOMED'
left join final_visit_ids fv
on fv.encounter_id = pr.encounter
inner join person p
on p.person_source_value = pr.patient;
-- COMMAND ----------
SELECT
*
FROM
procedure_occurrence
LIMIT
100;
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ### drug_exposure
-- COMMAND ----------
INSERT OVERWRITE drug_exposure
SELECT row_number()over(order by person_id) AS drug_exposure_id,
person_id,
drug_concept_id,
drug_exposure_start_date,
drug_exposure_start_datetime,
drug_exposure_end_date,
drug_exposure_end_datetime,
verbatim_end_date,
drug_type_concept_id,
stop_reason,
refills,
quantity,
days_supply,
sig,
route_concept_id,
lot_number,
provider_id,
visit_occurrence_id,
visit_detail_id,
drug_source_value,
drug_source_concept_id,
route_source_value,
dose_unit_source_value
from (
select
p.person_id,
coalesce(srctostdvm.target_concept_id,0) as drug_concept_id,
c.start AS drug_exposure_start_date,
c.start AS drug_exposure_start_datetime,
coalesce(c.stop,c.start) AS drug_exposure_end_date,
coalesce(c.stop,c.start) AS drug_exposure_end_datetime,
c.stop AS verbatim_end_date,
581452 as drug_type_concept_id,
null as stop_reason,
0 as refills,
0 as quantity,
coalesce(datediff(c.stop,c.start),0) as days_supply,
null as sig,
0 as route_concept_id,
0 as lot_number,
0 as provider_id,
fv.visit_occurrence_id_new AS visit_occurrence_id,
0 as visit_detail_id,
c.code AS drug_source_value,
coalesce(srctosrcvm.source_concept_id,0) AS drug_source_concept_id,
null as route_source_value,
null as dose_unit_source_value
from conditions c
join source_to_standard_vocab_map srctostdvm
on srctostdvm.source_code = c.code
and srctostdvm.target_domain_id = 'Drug'
and srctostdvm.source_vocabulary_id = 'RxNorm'
and srctostdvm.target_standard_concept = 'S'
and (srctostdvm.target_invalid_reason IS NULL OR srctostdvm.target_invalid_reason = '')
left join source_to_source_vocab_map srctosrcvm
on srctosrcvm.source_code = c.code
and srctosrcvm.source_vocabulary_id = 'RxNorm'
left join final_visit_ids fv
on fv.encounter_id = c.encounter
join person p
on p.person_source_value = c.patient
union all
select
p.person_id,
coalesce(srctostdvm.target_concept_id,0) as drug_concept_id,
m.start,
m.start,
coalesce(m.stop,m.start),
coalesce(m.stop,m.start),
m.stop,
38000177,
null,
0,
0,
coalesce(datediff(m.stop,m.start),0),
null,
0,
0,
0,
fv.visit_occurrence_id_new AS visit_occurrence_id,
0,
m.code,
coalesce(srctosrcvm.source_concept_id,0),
null,
null
from medications m
join source_to_standard_vocab_map srctostdvm
on srctostdvm.source_code = m.code
and srctostdvm.target_domain_id = 'Drug'
and srctostdvm.source_vocabulary_id = 'RxNorm'
and srctostdvm.target_standard_concept = 'S'
and (srctostdvm.target_invalid_reason IS NULL OR srctostdvm.target_invalid_reason = '')
left join source_to_source_vocab_map srctosrcvm
on srctosrcvm.source_code = m.code
and srctosrcvm.source_vocabulary_id = 'RxNorm'
left join final_visit_ids fv
on fv.encounter_id = m.encounter
join person p
on p.person_source_value = m.patient
union all
select
p.person_id,
coalesce(srctostdvm.target_concept_id,0) as drug_concept_id,
i.date,
i.date,
i.date,
i.date,
i.date,
581452,
null,
0,
0,
0,
null,
0,
0,
0,
fv.visit_occurrence_id_new AS visit_occurrence_id,
0,
i.code,
coalesce(srctosrcvm.source_concept_id,0),
null,
null
from immunizations i
left join source_to_standard_vocab_map srctostdvm
on srctostdvm.source_code = i.code
and srctostdvm.target_domain_id = 'Drug'
and srctostdvm.source_vocabulary_id = 'CVX'
and srctostdvm.target_standard_concept = 'S'
and (srctostdvm.target_invalid_reason IS NULL OR srctostdvm.target_invalid_reason = '')
left join source_to_source_vocab_map srctosrcvm
on srctosrcvm.source_code = i.code
and srctosrcvm.source_vocabulary_id = 'CVX'
left join final_visit_ids fv
on fv.encounter_id = i.encounter
join person p
on p.person_source_value = i.patient
) tmp;
-- COMMAND ----------
SELECT
*
FROM
drug_exposure
LIMIT
100;
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ### condition_era
-- COMMAND ----------
INSERT OVERWRITE condition_era (
WITH cteConditionTarget (condition_occurrence_id, person_id, condition_concept_id, condition_start_datetime, condition_end_datetime) AS
(
SELECT
co.condition_occurrence_id
, co.person_id
, co.condition_concept_id
, co.condition_start_datetime
, COALESCE(co.condition_end_datetime, date_add(condition_start_datetime,1)) AS condition_end_datetime
FROM condition_occurrence co
/* Depending on the needs of your data, you can put more filters on to your code. We assign 0 to our unmapped condition_concept_id's,
* and since we don't want different conditions put in the same era, we put in the filter below.
*/
---WHERE condition_concept_id != 0
),
--------------------------------------------------------------------------------------------------------------
cteEndDates (person_id, condition_concept_id, end_date) AS -- the magic
(
SELECT
person_id
, condition_concept_id
, date_add(event_date,-30) AS end_date -- unpad the end date
FROM
(
SELECT
person_id
, condition_concept_id
, event_date
, event_type
, MAX(start_ordinal) OVER (PARTITION BY person_id, condition_concept_id ORDER BY event_date, event_type ROWS UNBOUNDED PRECEDING) AS start_ordinal -- this pulls the current START down from the prior rows so that the NULLs from the END DATES will contain a value we can compare with
, ROW_NUMBER() OVER (PARTITION BY person_id, condition_concept_id ORDER BY event_date, event_type) AS overall_ord -- this re-numbers the inner UNION so all rows are numbered ordered by the event date
FROM
(
-- select the start dates, assigning a row number to each
SELECT
person_id
, condition_concept_id
, condition_start_datetime AS event_date
, -1 AS event_type
, ROW_NUMBER() OVER (PARTITION BY person_id, condition_concept_id ORDER BY condition_start_datetime) AS start_ordinal
FROM cteConditionTarget
UNION ALL
-- pad the end dates by 30 to allow a grace period for overlapping ranges.
SELECT
person_id
, condition_concept_id
, date_add(condition_end_datetime,30)
, 1 AS event_type
, NULL
FROM cteConditionTarget
) RAWDATA
) e
WHERE (2 * e.start_ordinal) - e.overall_ord = 0
),
--------------------------------------------------------------------------------------------------------------
cteConditionEnds (person_id, condition_concept_id, condition_start_datetime, era_end_datetime) AS
(
SELECT
c.person_id
, c.condition_concept_id
, c.condition_start_datetime
, MIN(e.end_date) AS era_end_datetime