Repository navigation
Expand file tree
/
Copy pathsqlScripts.html
More file actions
1562 lines (1519 loc) · 118 KB
/
Copy pathsqlScripts.html
File metadata and controls
1562 lines (1519 loc) · 118 KB
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.10.18">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>SQL Scripts – OMOP Common Data Model</title>
<style>
/* Default styles provided by pandoc.
** See https://pandoc.org/MANUAL.html#variables-for-html for config info.
*/
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: 1.5em;}
div.column{flex: auto;}
@media screen {
div.columns{gap: min(4vw, 1.5em);}
div.column{overflow-x: auto;}
}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
html { -webkit-text-size-adjust: 100%; }
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="site_libs/quarto-nav/quarto-nav.js"></script>
<script src="site_libs/quarto-nav/headroom.min.js"></script>
<script src="site_libs/clipboard/clipboard.min.js"></script>
<script src="site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="site_libs/quarto-search/fuse.min.js"></script>
<script src="site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="./">
<link href="./man/favicon.ico" rel="icon">
<script src="site_libs/quarto-html/quarto.js" type="module"></script>
<script src="site_libs/quarto-html/tabsets/tabsets.js" type="module"></script>
<script src="site_libs/quarto-html/popper.min.js"></script>
<script src="site_libs/quarto-html/tippy.umd.min.js"></script>
<script src="site_libs/quarto-html/anchor.min.js"></script>
<link href="site_libs/quarto-html/tippy.css" rel="stylesheet">
<link href="site_libs/quarto-html/quarto-syntax-highlighting-64a204ab8560d761af7679ceaba22944.css" rel="stylesheet" class="quarto-color-scheme" id="quarto-text-highlighting-styles">
<link href="site_libs/quarto-html/quarto-syntax-highlighting-dark-b85bc7c33db8c58e0ec93c3465666fed.css" rel="stylesheet" class="quarto-color-scheme quarto-color-alternate" id="quarto-text-highlighting-styles">
<link href="site_libs/quarto-html/quarto-syntax-highlighting-64a204ab8560d761af7679ceaba22944.css" rel="stylesheet" class="quarto-color-scheme-extra" id="quarto-text-highlighting-styles">
<script src="site_libs/bootstrap/bootstrap.min.js"></script>
<link href="site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="site_libs/bootstrap/bootstrap-172ba59bd5e0b52282274ba0c0cc16fc.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme" id="quarto-bootstrap" data-mode="light">
<link href="site_libs/bootstrap/bootstrap-dark-8b348f4b4b74a528bf910da1e8c5139e.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme quarto-color-alternate" id="quarto-bootstrap" data-mode="dark">
<link href="site_libs/bootstrap/bootstrap-172ba59bd5e0b52282274ba0c0cc16fc.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme-extra" id="quarto-bootstrap" data-mode="light">
<script id="quarto-search-options" type="application/json">{
"location": "navbar",
"copy-button": false,
"collapse-after": 3,
"panel-placement": "end",
"type": "overlay",
"limit": 50,
"keyboard-shortcut": [
"f",
"/",
"s"
],
"show-item-context": false,
"language": {
"search-no-results-text": "No results",
"search-matching-documents-text": "matching documents",
"search-copy-link-title": "Copy link to search",
"search-hide-matches-text": "Hide additional matches",
"search-more-match-text": "more match in this document",
"search-more-matches-text": "more matches in this document",
"search-clear-button-title": "Clear",
"search-text-placeholder": "",
"search-detached-cancel-button-title": "Cancel",
"search-submit-button-title": "Submit",
"search-label": "Search"
}
}</script>
<!-- Google tag (gtag.js) -->
<script async="" src="https://www.googletagmanager.com/gtag/js?id=G-37JJNBGH0H"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-37JJNBGH0H');
</script>
<link rel="shortcut icon" type="image/x-icon" href="man/favicon.ico">
<link rel="stylesheet" href="man/style.css">
</head>
<body class="floating nav-fixed quarto-light"><script id="quarto-html-before-body" type="application/javascript">
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap:not([rel=disabled-stylesheet])");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
const setColorSchemeToggle = (alternate) => {
const toggles = window.document.querySelectorAll('.quarto-color-scheme-toggle');
for (let i=0; i < toggles.length; i++) {
const toggle = toggles[i];
if (toggle) {
if (alternate) {
toggle.classList.add("alternate");
} else {
toggle.classList.remove("alternate");
}
}
}
};
const toggleColorMode = (alternate) => {
// Switch the stylesheets
const primaryStylesheets = window.document.querySelectorAll('link.quarto-color-scheme:not(.quarto-color-alternate)');
const alternateStylesheets = window.document.querySelectorAll('link.quarto-color-scheme.quarto-color-alternate');
manageTransitions('#quarto-margin-sidebar .nav-link', false);
if (alternate) {
// note: dark is layered on light, we don't disable primary!
enableStylesheet(alternateStylesheets);
for (const sheetNode of alternateStylesheets) {
if (sheetNode.id === "quarto-bootstrap") {
toggleBodyColorMode(sheetNode);
}
}
} else {
disableStylesheet(alternateStylesheets);
enableStylesheet(primaryStylesheets)
toggleBodyColorPrimary();
}
manageTransitions('#quarto-margin-sidebar .nav-link', true);
// Switch the toggles
setColorSchemeToggle(alternate)
// Hack to workaround the fact that safari doesn't
// properly recolor the scrollbar when toggling (#1455)
if (navigator.userAgent.indexOf('Safari') > 0 && navigator.userAgent.indexOf('Chrome') == -1) {
manageTransitions("body", false);
window.scrollTo(0, 1);
setTimeout(() => {
window.scrollTo(0, 0);
manageTransitions("body", true);
}, 40);
}
}
const disableStylesheet = (stylesheets) => {
for (let i=0; i < stylesheets.length; i++) {
const stylesheet = stylesheets[i];
stylesheet.rel = 'disabled-stylesheet';
}
}
const enableStylesheet = (stylesheets) => {
for (let i=0; i < stylesheets.length; i++) {
const stylesheet = stylesheets[i];
if(stylesheet.rel !== 'stylesheet') { // for Chrome, which will still FOUC without this check
stylesheet.rel = 'stylesheet';
}
}
}
const manageTransitions = (selector, allowTransitions) => {
const els = window.document.querySelectorAll(selector);
for (let i=0; i < els.length; i++) {
const el = els[i];
if (allowTransitions) {
el.classList.remove('notransition');
} else {
el.classList.add('notransition');
}
}
}
const isFileUrl = () => {
return window.location.protocol === 'file:';
}
const hasAlternateSentinel = () => {
let styleSentinel = getColorSchemeSentinel();
if (styleSentinel !== null) {
return styleSentinel === "alternate";
} else {
return false;
}
}
const setStyleSentinel = (alternate) => {
const value = alternate ? "alternate" : "default";
if (!isFileUrl()) {
window.localStorage.setItem("quarto-color-scheme", value);
} else {
localAlternateSentinel = value;
}
}
const getColorSchemeSentinel = () => {
if (!isFileUrl()) {
const storageValue = window.localStorage.getItem("quarto-color-scheme");
return storageValue != null ? storageValue : localAlternateSentinel;
} else {
return localAlternateSentinel;
}
}
const toggleGiscusIfUsed = (isAlternate, darkModeDefault) => {
const baseTheme = document.querySelector('#giscus-base-theme')?.value ?? 'light';
const alternateTheme = document.querySelector('#giscus-alt-theme')?.value ?? 'dark';
let newTheme = '';
if(authorPrefersDark) {
newTheme = isAlternate ? baseTheme : alternateTheme;
} else {
newTheme = isAlternate ? alternateTheme : baseTheme;
}
const changeGiscusTheme = () => {
// From: https://github.com/giscus/giscus/issues/336
const sendMessage = (message) => {
const iframe = document.querySelector('iframe.giscus-frame');
if (!iframe) return;
iframe.contentWindow.postMessage({ giscus: message }, 'https://giscus.app');
}
sendMessage({
setConfig: {
theme: newTheme
}
});
}
const isGiscussLoaded = window.document.querySelector('iframe.giscus-frame') !== null;
if (isGiscussLoaded) {
changeGiscusTheme();
}
};
const authorPrefersDark = false;
const darkModeDefault = authorPrefersDark;
document.querySelector('link#quarto-text-highlighting-styles.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
document.querySelector('link#quarto-bootstrap.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
let localAlternateSentinel = darkModeDefault ? 'alternate' : 'default';
// Dark / light mode switch
window.quartoToggleColorScheme = () => {
// Read the current dark / light value
let toAlternate = !hasAlternateSentinel();
toggleColorMode(toAlternate);
setStyleSentinel(toAlternate);
toggleGiscusIfUsed(toAlternate, darkModeDefault);
window.dispatchEvent(new Event('resize'));
};
// Switch to dark mode if need be
if (hasAlternateSentinel()) {
toggleColorMode(true);
} else {
toggleColorMode(false);
}
</script>
<div id="quarto-search-results"></div>
<header id="quarto-header" class="headroom fixed-top">
<nav class="navbar navbar-expand-lg " data-bs-theme="dark">
<div class="navbar-container container-fluid">
<div class="navbar-brand-container mx-auto">
<a href="./index.html" class="navbar-brand navbar-brand-logo">
<img src="./man/ohdsi16x16.png" alt="" class="navbar-logo light-content">
<img src="./man/ohdsi16x16.png" alt="" class="navbar-logo dark-content">
</a>
<a class="navbar-brand" href="./index.html">
<span class="navbar-title">OMOP Common Data Model</span>
</a>
</div>
<div id="quarto-search" class="" title="Search"></div>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" role="menu" aria-expanded="false" aria-label="Toggle navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav navbar-nav-scroll me-auto">
<li class="nav-item">
<a class="nav-link" href="./index.html"> <i class="bi bi-house" role="img">
</i>
<span class="menu-text"></span></a>
</li>
<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle" href="#" id="nav-menu-background" role="link" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-bank" role="img">
</i>
<span class="menu-text">Background</span>
</a>
<ul class="dropdown-menu" aria-labelledby="nav-menu-background">
<li>
<a class="dropdown-item" href="./background.html">
<span class="dropdown-text">Model Background</span></a>
</li>
<li>
<a class="dropdown-item" href="./cdmRefreshProcess.html">
<span class="dropdown-text">CDM Refresh Process</span></a>
</li>
<li>
<a class="dropdown-item" href="./vocabulary.html">
<span class="dropdown-text">How the Vocabulary is Built</span></a>
</li>
</ul>
</li>
<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle" href="#" id="nav-menu-conventions" role="link" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-card-list" role="img">
</i>
<span class="menu-text">Conventions</span>
</a>
<ul class="dropdown-menu" aria-labelledby="nav-menu-conventions">
<li>
<a class="dropdown-item" href="https://ohdsi.github.io/Themis"><i class="bi bi-box-arrow-up-right" role="img">
</i>
<span class="dropdown-text">THEMIS Convention Library</span></a>
</li>
<li>
<a class="dropdown-item" href="./dataModelConventions.html">
<span class="dropdown-text">General Conventions</span></a>
</li>
<li>
<a class="dropdown-item" href="./cdmPrivacy.html">
<span class="dropdown-text">Patient Privacy and OMOP</span></a>
</li>
<li>
<a class="dropdown-item" href="./customConcepts.html">
<span class="dropdown-text">Custom Concepts</span></a>
</li>
<li>
<a class="dropdown-item" href="./drug_dose.html">
<span class="dropdown-text">How to Calculate Drug Dose</span></a>
</li>
<li>
<a class="dropdown-item" href="./ehrObsPeriods.html">
<span class="dropdown-text">Observation Period Considerations for EHR Data</span></a>
</li>
</ul>
</li>
<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle" href="#" id="nav-menu-cdm-versions" role="link" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-clock-history" role="img">
</i>
<span class="menu-text">CDM Versions</span>
</a>
<ul class="dropdown-menu" aria-labelledby="nav-menu-cdm-versions">
<li>
<a class="dropdown-item" href="./cdm30.html">
<span class="dropdown-text">CDM v3.0</span></a>
</li>
<li>
<a class="dropdown-item" href="./cdm53.html">
<span class="dropdown-text">CDM v5.3</span></a>
</li>
<li>
<a class="dropdown-item" href="./cdm54.html">
<span class="dropdown-text">CDM v5.4</span></a>
</li>
<li>
<a class="dropdown-item" href="./cdm54Changes.html">
<span class="dropdown-text">- Changes from CDM v5.3</span></a>
</li>
<li>
<a class="dropdown-item" href="./cdm54erd.html">
<span class="dropdown-text">- CDM v5.4 Entity Relationships</span></a>
</li>
<li>
<a class="dropdown-item" href="./cdm54ToolingSupport.html">
<span class="dropdown-text">- CDM v5.4 Tooling Support</span></a>
</li>
<li>
<a class="dropdown-item" href="./cdm55.html">
<span class="dropdown-text">CDM v5.5</span></a>
</li>
<li>
<a class="dropdown-item" href="./cdm55Changes.html">
<span class="dropdown-text">- Changes from CDM v5.4</span></a>
</li>
</ul>
</li>
<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle" href="#" id="nav-menu-cdm-additions" role="link" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-plus-square" role="img">
</i>
<span class="menu-text">CDM Additions</span>
</a>
<ul class="dropdown-menu" aria-labelledby="nav-menu-cdm-additions">
<li>
<a class="dropdown-item" href="./typesOfAdditions.html">
<span class="dropdown-text">Types of CDM Additions</span></a>
</li>
<li>
<a class="dropdown-item" href="./cdmRequestProcess.html">
<span class="dropdown-text">How to Propose Changes to the CDM</span></a>
</li>
<li>
<a class="dropdown-item" href="https://github.com/OHDSI/CommonDataModel/tree/develop">
<span class="dropdown-text">Accepted Changes</span></a>
</li>
</ul>
</li>
<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle" href="#" id="nav-menu-how-to" role="link" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-question-circle" role="img">
</i>
<span class="menu-text">How to</span>
</a>
<ul class="dropdown-menu" aria-labelledby="nav-menu-how-to">
<li>
<a class="dropdown-item" href="./download.html">
<span class="dropdown-text">Download the DDL</span></a>
</li>
<li>
<a class="dropdown-item" href="./cdmRPackage.html">
<span class="dropdown-text">Use the CDM R Package</span></a>
</li>
<li>
<a class="dropdown-item" href="./drug_dose.html">
<span class="dropdown-text">Calculate Drug Dose</span></a>
</li>
</ul>
</li>
<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle" href="#" id="nav-menu-support" role="link" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-life-preserver" role="img">
</i>
<span class="menu-text">Support</span>
</a>
<ul class="dropdown-menu" aria-labelledby="nav-menu-support">
<li>
<a class="dropdown-item" href="./cdmDecisionTree/cdmDecisionTree.html">
<span class="dropdown-text">Help! My Data Doesn’t Fit!</span></a>
</li>
<li>
<a class="dropdown-item" href="./faq.html">
<span class="dropdown-text">FAQ</span></a>
</li>
<li>
<a class="dropdown-item" href="./sqlScripts.html">
<span class="dropdown-text">SQL Scripts</span></a>
</li>
<li>
<a class="dropdown-item" href="./contribute.html">
<span class="dropdown-text">Ask a Question</span></a>
</li>
</ul>
</li>
</ul>
<ul class="navbar-nav navbar-nav-scroll ms-auto">
<li class="nav-item compact">
<a class="nav-link" href="https://github.com/OHDSI/CommonDataModel"> <i class="bi bi-github" role="img">
</i>
<span class="menu-text"></span></a>
</li>
</ul>
</div> <!-- /navcollapse -->
<div class="quarto-navbar-tools">
<a href="" class="quarto-color-scheme-toggle quarto-navigation-tool px-1" onclick="window.quartoToggleColorScheme(); return false;" title="Toggle dark mode"><i class="bi"></i></a>
</div>
</div> <!-- /container-fluid -->
</nav>
</header>
<!-- content -->
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article page-navbar">
<!-- sidebar -->
<nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal quarto-sidebar-collapse-item sidebar-navigation floating overflow-auto">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">On this page</h2>
<ul>
<li><a href="#vocabulary-mapping" id="toc-vocabulary-mapping" class="nav-link active" data-scroll-target="#vocabulary-mapping">Vocabulary Mapping</a>
<ul class="collapse">
<li><a href="#source-to-standard" id="toc-source-to-standard" class="nav-link" data-scroll-target="#source-to-standard">Source to Standard</a></li>
<li><a href="#source-to-source" id="toc-source-to-source" class="nav-link" data-scroll-target="#source-to-source">Source to Source</a></li>
</ul></li>
<li><a href="#era-tables" id="toc-era-tables" class="nav-link" data-scroll-target="#era-tables">Era Tables</a>
<ul class="collapse">
<li><a href="#condition-eras" id="toc-condition-eras" class="nav-link" data-scroll-target="#condition-eras">Condition Eras</a></li>
<li><a href="#drug-eras" id="toc-drug-eras" class="nav-link" data-scroll-target="#drug-eras">Drug Eras</a></li>
</ul></li>
<li><a href="#example-etl-script" id="toc-example-etl-script" class="nav-link" data-scroll-target="#example-etl-script">Example ETL Script</a>
<ul class="collapse">
<li><a href="#cdm_source-table" id="toc-cdm_source-table" class="nav-link" data-scroll-target="#cdm_source-table">CDM_SOURCE Table</a></li>
</ul></li>
<li><a href="#bonus-queries" id="toc-bonus-queries" class="nav-link" data-scroll-target="#bonus-queries">Bonus Queries</a>
<ul class="collapse">
<li><a href="#visit-concept-roll-up" id="toc-visit-concept-roll-up" class="nav-link" data-scroll-target="#visit-concept-roll-up">Visit Concept Roll-up</a></li>
</ul></li>
</ul>
</nav>
</nav>
<div id="quarto-sidebar-glass" class="quarto-sidebar-collapse-item" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item"></div>
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar zindex-bottom">
</div>
<!-- main -->
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">SQL Scripts</h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<p>These SQL scripts have been developed by the community to facilitate the Extract, Transform, and Load (ETL) process from source data to the OMOP CDM.</p>
<section id="vocabulary-mapping" class="level2">
<h2 class="anchored" data-anchor-id="vocabulary-mapping">Vocabulary Mapping</h2>
<section id="source-to-standard" class="level3">
<h3 class="anchored" data-anchor-id="source-to-standard">Source to Standard</h3>
<p>This script will create a large table containing all source codes and the Standard Concepts they map to. By creating this table ahead of time and referencing it during ETL, you can save processing time than if you were to map source codes on the fly. It is important to note that this script will show ALL mappings, not just those to Standard, Valid Concepts. Be sure to add a filter that will only grab the correct Standard, Valid codes.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">SELECT</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a> c.concept_code <span class="kw">AS</span> source_code,</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a> c.concept_id <span class="kw">AS</span> source_concept_id,</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> c.concept_name <span class="kw">AS</span> source_code_description,</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a> c.vocabulary_id <span class="kw">AS</span> source_vocabulary_id,</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a> c.domain_id <span class="kw">AS</span> source_domain_id,</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a> c.concept_class_id <span class="kw">AS</span> source_concept_class_id,</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a> c.valid_start_date <span class="kw">AS</span> source_valid_start_date,</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a> c.valid_end_date <span class="kw">AS</span> source_valid_end_date,</span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a> c.invalid_reason <span class="kw">AS</span> source_invalid_reason,</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a> c1.concept_id <span class="kw">AS</span> target_concept_id,</span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a> c1.concept_name <span class="kw">AS</span> target_concept_name,</span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a> c1.vocabulary_id <span class="kw">AS</span> target_vocabulary_id,</span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a> c1.domain_id <span class="kw">AS</span> target_domain_id,</span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a> c1.concept_class_id <span class="kw">AS</span> target_concept_class_id,</span>
<span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a> c1.invalid_reason <span class="kw">AS</span> target_invalid_reason,</span>
<span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a> c1.standard_concept <span class="kw">AS</span> target_standard_concept</span>
<span id="cb1-18"><a href="#cb1-18" aria-hidden="true" tabindex="-1"></a><span class="kw">FROM</span> concept c</span>
<span id="cb1-19"><a href="#cb1-19" aria-hidden="true" tabindex="-1"></a><span class="kw">JOIN</span> concept_relationship cr</span>
<span id="cb1-20"><a href="#cb1-20" aria-hidden="true" tabindex="-1"></a> <span class="kw">ON</span> c.concept_id <span class="op">=</span> cr.concept_id_1</span>
<span id="cb1-21"><a href="#cb1-21" aria-hidden="true" tabindex="-1"></a> <span class="kw">AND</span> cr.invalid_reason <span class="kw">IS</span> <span class="kw">NULL</span></span>
<span id="cb1-22"><a href="#cb1-22" aria-hidden="true" tabindex="-1"></a> <span class="kw">AND</span> cr.relationship_id <span class="op">=</span> <span class="st">'Maps to'</span></span>
<span id="cb1-23"><a href="#cb1-23" aria-hidden="true" tabindex="-1"></a><span class="kw">JOIN</span> concept c1</span>
<span id="cb1-24"><a href="#cb1-24" aria-hidden="true" tabindex="-1"></a> <span class="kw">ON</span> cr.concept_id_2 <span class="op">=</span> c1.concept_id</span>
<span id="cb1-25"><a href="#cb1-25" aria-hidden="true" tabindex="-1"></a> <span class="kw">AND</span> c1.invalid_reason <span class="kw">IS</span> <span class="kw">NULL</span></span>
<span id="cb1-26"><a href="#cb1-26" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-27"><a href="#cb1-27" aria-hidden="true" tabindex="-1"></a><span class="kw">UNION</span></span>
<span id="cb1-28"><a href="#cb1-28" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-29"><a href="#cb1-29" aria-hidden="true" tabindex="-1"></a><span class="kw">SELECT</span></span>
<span id="cb1-30"><a href="#cb1-30" aria-hidden="true" tabindex="-1"></a> stcm.source_code,</span>
<span id="cb1-31"><a href="#cb1-31" aria-hidden="true" tabindex="-1"></a> stcm.source_concept_id,</span>
<span id="cb1-32"><a href="#cb1-32" aria-hidden="true" tabindex="-1"></a> stcm.source_code_description,</span>
<span id="cb1-33"><a href="#cb1-33" aria-hidden="true" tabindex="-1"></a> stcm.source_vocabulary_id,</span>
<span id="cb1-34"><a href="#cb1-34" aria-hidden="true" tabindex="-1"></a> c1.domain_id <span class="kw">AS</span> source_domain_id,</span>
<span id="cb1-35"><a href="#cb1-35" aria-hidden="true" tabindex="-1"></a> c1.concept_class_id <span class="kw">AS</span> source_concept_class_id,</span>
<span id="cb1-36"><a href="#cb1-36" aria-hidden="true" tabindex="-1"></a> c1.valid_start_date <span class="kw">AS</span> source_valid_start_date,</span>
<span id="cb1-37"><a href="#cb1-37" aria-hidden="true" tabindex="-1"></a> c1.valid_end_date <span class="kw">AS</span> source_valid_end_date,</span>
<span id="cb1-38"><a href="#cb1-38" aria-hidden="true" tabindex="-1"></a> stcm.invalid_reason <span class="kw">AS</span> source_invalid_reason,</span>
<span id="cb1-39"><a href="#cb1-39" aria-hidden="true" tabindex="-1"></a> stcm.target_concept_id,</span>
<span id="cb1-40"><a href="#cb1-40" aria-hidden="true" tabindex="-1"></a> c2.concept_name <span class="kw">AS</span> target_concept_name,</span>
<span id="cb1-41"><a href="#cb1-41" aria-hidden="true" tabindex="-1"></a> stcm.target_vocabulary_id,</span>
<span id="cb1-42"><a href="#cb1-42" aria-hidden="true" tabindex="-1"></a> c2.domain_id <span class="kw">AS</span> target_domain_id,</span>
<span id="cb1-43"><a href="#cb1-43" aria-hidden="true" tabindex="-1"></a> c2.concept_class_id <span class="kw">AS</span> target_concept_class_id,</span>
<span id="cb1-44"><a href="#cb1-44" aria-hidden="true" tabindex="-1"></a> c2.invalid_reason <span class="kw">AS</span> target_invalid_reason,</span>
<span id="cb1-45"><a href="#cb1-45" aria-hidden="true" tabindex="-1"></a> c2.standard_concept <span class="kw">AS</span> target_standard_concept</span>
<span id="cb1-46"><a href="#cb1-46" aria-hidden="true" tabindex="-1"></a><span class="kw">FROM</span> source_to_concept_map stcm</span>
<span id="cb1-47"><a href="#cb1-47" aria-hidden="true" tabindex="-1"></a><span class="kw">LEFT</span> <span class="kw">JOIN</span> concept c1</span>
<span id="cb1-48"><a href="#cb1-48" aria-hidden="true" tabindex="-1"></a> <span class="kw">ON</span> c1.concept_id <span class="op">=</span> stcm.source_concept_id</span>
<span id="cb1-49"><a href="#cb1-49" aria-hidden="true" tabindex="-1"></a><span class="kw">LEFT</span> <span class="kw">JOIN</span> concept c2</span>
<span id="cb1-50"><a href="#cb1-50" aria-hidden="true" tabindex="-1"></a> <span class="kw">ON</span> c2.concept_id <span class="op">=</span> stcm.target_concept_id</span>
<span id="cb1-51"><a href="#cb1-51" aria-hidden="true" tabindex="-1"></a><span class="kw">WHERE</span> stcm.invalid_reason <span class="kw">IS</span> <span class="kw">NULL</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
</section>
<section id="source-to-source" class="level3">
<h3 class="anchored" data-anchor-id="source-to-source">Source to Source</h3>
<p>This script will create a large table containing all source codes and their referent Concepts. While you may be tempted to use the Source to Standard query to find these, it is important to use this one instead to populate *_SOURCE_CONCEPT_ID fields. Some source codes map to multiple Standard Concepts though they have only one source Concept. By using the Source to Standard query you may inadvertently create duplicate records in your CDM instance.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">SELECT</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> c.concept_code <span class="kw">AS</span> source_code,</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> c.concept_id <span class="kw">AS</span> source_concept_id,</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a> c.concept_name <span class="kw">AS</span> source_code_description,</span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a> c.vocabulary_id <span class="kw">AS</span> source_vocabulary_id,</span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a> c.domain_id <span class="kw">AS</span> source_domain_id,</span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a> c.concept_class_id <span class="kw">AS</span> source_concept_class_id,</span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a> c.valid_start_date <span class="kw">AS</span> source_valid_start_date,</span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a> c.valid_end_date <span class="kw">AS</span> source_valid_end_date,</span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a> c.invalid_reason <span class="kw">AS</span> source_invalid_reason,</span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a> c.concept_id <span class="kw">AS</span> target_concept_id,</span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a> c.concept_name <span class="kw">AS</span> target_concept_name,</span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a> c.vocabulary_id <span class="kw">AS</span> target_vocabulary_id,</span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a> c.domain_id <span class="kw">AS</span> target_domain_id,</span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a> c.concept_class_id <span class="kw">AS</span> target_concept_class_id,</span>
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a> c.invalid_reason <span class="kw">AS</span> target_invalid_reason,</span>
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a> c.standard_concept <span class="kw">AS</span> target_standard_concept</span>
<span id="cb2-18"><a href="#cb2-18" aria-hidden="true" tabindex="-1"></a><span class="kw">FROM</span> concept c</span>
<span id="cb2-19"><a href="#cb2-19" aria-hidden="true" tabindex="-1"></a><span class="kw">UNION</span></span>
<span id="cb2-20"><a href="#cb2-20" aria-hidden="true" tabindex="-1"></a><span class="kw">SELECT</span></span>
<span id="cb2-21"><a href="#cb2-21" aria-hidden="true" tabindex="-1"></a> source_code,</span>
<span id="cb2-22"><a href="#cb2-22" aria-hidden="true" tabindex="-1"></a> source_concept_id,</span>
<span id="cb2-23"><a href="#cb2-23" aria-hidden="true" tabindex="-1"></a> source_code_description,</span>
<span id="cb2-24"><a href="#cb2-24" aria-hidden="true" tabindex="-1"></a> source_vocabulary_id,</span>
<span id="cb2-25"><a href="#cb2-25" aria-hidden="true" tabindex="-1"></a> c1.domain_id <span class="kw">AS</span> source_domain_id,</span>
<span id="cb2-26"><a href="#cb2-26" aria-hidden="true" tabindex="-1"></a> c1.concept_class_id <span class="kw">AS</span> source_concept_class_id,</span>
<span id="cb2-27"><a href="#cb2-27" aria-hidden="true" tabindex="-1"></a> c1.valid_start_date <span class="kw">AS</span> source_valid_start_date,</span>
<span id="cb2-28"><a href="#cb2-28" aria-hidden="true" tabindex="-1"></a> c1.valid_end_date <span class="kw">AS</span> source_valid_end_date,</span>
<span id="cb2-29"><a href="#cb2-29" aria-hidden="true" tabindex="-1"></a> stcm.invalid_reason <span class="kw">AS</span> source_invalid_reason,</span>
<span id="cb2-30"><a href="#cb2-30" aria-hidden="true" tabindex="-1"></a> stcm.target_concept_id,</span>
<span id="cb2-31"><a href="#cb2-31" aria-hidden="true" tabindex="-1"></a> c2.concept_name <span class="kw">AS</span> target_concept_name,</span>
<span id="cb2-32"><a href="#cb2-32" aria-hidden="true" tabindex="-1"></a> stcm.target_vocabulary_id <span class="kw">AS</span> target_vocabulary_id,</span>
<span id="cb2-33"><a href="#cb2-33" aria-hidden="true" tabindex="-1"></a> c2.domain_id <span class="kw">AS</span> target_domain_id,</span>
<span id="cb2-34"><a href="#cb2-34" aria-hidden="true" tabindex="-1"></a> c2.concept_class_id <span class="kw">AS</span> target_concept_class_id,</span>
<span id="cb2-35"><a href="#cb2-35" aria-hidden="true" tabindex="-1"></a> c2.invalid_reason <span class="kw">AS</span> target_invalid_reason,</span>
<span id="cb2-36"><a href="#cb2-36" aria-hidden="true" tabindex="-1"></a> c2.standard_concept <span class="kw">AS</span> target_standard_concept</span>
<span id="cb2-37"><a href="#cb2-37" aria-hidden="true" tabindex="-1"></a><span class="kw">FROM</span> source_to_concept_map stcm</span>
<span id="cb2-38"><a href="#cb2-38" aria-hidden="true" tabindex="-1"></a><span class="kw">LEFT</span> <span class="kw">OUTER</span> <span class="kw">JOIN</span> concept c1</span>
<span id="cb2-39"><a href="#cb2-39" aria-hidden="true" tabindex="-1"></a> <span class="kw">ON</span> c1.concept_id <span class="op">=</span> stcm.source_concept_id</span>
<span id="cb2-40"><a href="#cb2-40" aria-hidden="true" tabindex="-1"></a><span class="kw">LEFT</span> <span class="kw">OUTER</span> <span class="kw">JOIN</span> concept c2</span>
<span id="cb2-41"><a href="#cb2-41" aria-hidden="true" tabindex="-1"></a> <span class="kw">ON</span> c2.concept_id <span class="op">=</span> stcm.target_concept_id</span>
<span id="cb2-42"><a href="#cb2-42" aria-hidden="true" tabindex="-1"></a><span class="kw">WHERE</span> stcm.invalid_reason <span class="kw">IS</span> <span class="kw">NULL</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
</section>
</section>
<section id="era-tables" class="level2">
<h2 class="anchored" data-anchor-id="era-tables">Era Tables</h2>
<section id="condition-eras" class="level3">
<h3 class="anchored" data-anchor-id="condition-eras">Condition Eras</h3>
<p>This script will insert values into the CONDITION_ERA table given that the CONDITION_OCCURRENCE table is populated. It will string together condition records that have <= 30 days between them into eras during which the Person is assumed to have the given condition. <strong>NOTE</strong> This query only works with 5.3 and below.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co">/****************************************************</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="co">OHDSI-SQL File Instructions</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="co">-----------------------------</span></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="co"> 1. Set parameter name of schema that contains CDMv4 instance</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="co"> (@SOURCE_CDMV4, @SOURCE_CDMV4_SCHEMA)</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="co"> 2. Set parameter name of schema that contains CDMv5 instance</span></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="co"> (@TARGET_CDMV5, @TARGET_CDMV5_SCHEMA)</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a><span class="co"> 3. Run this script through SqlRender to produce a script that will work in your</span></span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a><span class="co"> source dialect. SqlRender can be found here: https://github.com/OHDSI/SqlRender</span></span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a><span class="co"> 4. Run the script produced by SQL Render on your target RDBDMS.</span></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a><span class="co"><RDBMS> File Instructions</span></span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a><span class="co">-------------------------</span></span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a><span class="co"> 1. This script will hold a number of placeholders for your CDM V4 and CDMV5</span></span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a><span class="co"> database/schema. In order to make this file work in your environment, you</span></span>
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a><span class="co"> should plan to do a global "FIND AND REPLACE" on this file to fill in the</span></span>
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a><span class="co"> file with values that pertain to your environment. The following are the</span></span>
<span id="cb3-17"><a href="#cb3-17" aria-hidden="true" tabindex="-1"></a><span class="co"> tokens you should use when doing your "FIND AND REPLACE" operation:</span></span>
<span id="cb3-18"><a href="#cb3-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-19"><a href="#cb3-19" aria-hidden="true" tabindex="-1"></a><span class="co"> [CDM]</span></span>
<span id="cb3-20"><a href="#cb3-20" aria-hidden="true" tabindex="-1"></a><span class="co"> [CDM].[CDMSCHEMA]</span></span>
<span id="cb3-21"><a href="#cb3-21" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-22"><a href="#cb3-22" aria-hidden="true" tabindex="-1"></a><span class="co">*********************************************************************************/</span></span>
<span id="cb3-23"><a href="#cb3-23" aria-hidden="true" tabindex="-1"></a><span class="co">/* SCRIPT PARAMETERS */</span></span>
<span id="cb3-24"><a href="#cb3-24" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-25"><a href="#cb3-25" aria-hidden="true" tabindex="-1"></a> {<span class="kw">DEFAULT</span> @TARGET_CDMV5 <span class="op">=</span> <span class="st">'[CDM]'</span> } <span class="co">-- The target CDMv5 database name</span></span>
<span id="cb3-26"><a href="#cb3-26" aria-hidden="true" tabindex="-1"></a> {<span class="kw">DEFAULT</span> @TARGET_CDMV5_SCHEMA <span class="op">=</span> <span class="st">'[CDM].[CDMSCHEMA]'</span> } <span class="co">-- the target CDMv5 database plus schema</span></span>
<span id="cb3-27"><a href="#cb3-27" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-28"><a href="#cb3-28" aria-hidden="true" tabindex="-1"></a><span class="kw">USE</span> @TARGET_CDMV5;</span>
<span id="cb3-29"><a href="#cb3-29" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-30"><a href="#cb3-30" aria-hidden="true" tabindex="-1"></a><span class="co">/****</span></span>
<span id="cb3-31"><a href="#cb3-31" aria-hidden="true" tabindex="-1"></a><span class="co">CONDITION ERA</span></span>
<span id="cb3-32"><a href="#cb3-32" aria-hidden="true" tabindex="-1"></a><span class="co">Note: Eras derived from CONDITION_OCCURRENCE table, using 30d gap</span></span>
<span id="cb3-33"><a href="#cb3-33" aria-hidden="true" tabindex="-1"></a><span class="co"> ****/</span></span>
<span id="cb3-34"><a href="#cb3-34" aria-hidden="true" tabindex="-1"></a><span class="cf">IF</span> OBJECT_ID(<span class="st">'tempdb..#condition_era_phase_1'</span>, <span class="st">'U'</span>) <span class="kw">IS</span> <span class="kw">NOT</span> <span class="kw">NULL</span></span>
<span id="cb3-35"><a href="#cb3-35" aria-hidden="true" tabindex="-1"></a> <span class="kw">DROP</span> <span class="kw">TABLE</span> #condition_era_phase_1;</span>
<span id="cb3-36"><a href="#cb3-36" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-37"><a href="#cb3-37" aria-hidden="true" tabindex="-1"></a><span class="co">/* / */</span></span>
<span id="cb3-38"><a href="#cb3-38" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-39"><a href="#cb3-39" aria-hidden="true" tabindex="-1"></a><span class="cf">IF</span> OBJECT_ID(<span class="st">'tempdb..#cteConditionTarget'</span>, <span class="st">'U'</span>) <span class="kw">IS</span> <span class="kw">NOT</span> <span class="kw">NULL</span></span>
<span id="cb3-40"><a href="#cb3-40" aria-hidden="true" tabindex="-1"></a> <span class="kw">DROP</span> <span class="kw">TABLE</span> #cteConditionTarget;</span>
<span id="cb3-41"><a href="#cb3-41" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-42"><a href="#cb3-42" aria-hidden="true" tabindex="-1"></a><span class="co">/* / */</span></span>
<span id="cb3-43"><a href="#cb3-43" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-44"><a href="#cb3-44" aria-hidden="true" tabindex="-1"></a><span class="co">-- create base eras from the concepts found in condition_occurrence</span></span>
<span id="cb3-45"><a href="#cb3-45" aria-hidden="true" tabindex="-1"></a><span class="kw">SELECT</span></span>
<span id="cb3-46"><a href="#cb3-46" aria-hidden="true" tabindex="-1"></a> co.person_id,</span>
<span id="cb3-47"><a href="#cb3-47" aria-hidden="true" tabindex="-1"></a> co.condition_concept_id,</span>
<span id="cb3-48"><a href="#cb3-48" aria-hidden="true" tabindex="-1"></a> co.condition_start_date,</span>
<span id="cb3-49"><a href="#cb3-49" aria-hidden="true" tabindex="-1"></a> <span class="fu">COALESCE</span>(co.condition_end_date, DATEADD(<span class="dt">day</span>, <span class="dv">1</span>, condition_start_date)) <span class="kw">AS</span> condition_end_date</span>
<span id="cb3-50"><a href="#cb3-50" aria-hidden="true" tabindex="-1"></a><span class="kw">INTO</span> #cteConditionTarget</span>
<span id="cb3-51"><a href="#cb3-51" aria-hidden="true" tabindex="-1"></a><span class="kw">FROM</span> @TARGET_CDMV5_SCHEMA.CONDITION_OCCURRENCE co;</span>
<span id="cb3-52"><a href="#cb3-52" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-53"><a href="#cb3-53" aria-hidden="true" tabindex="-1"></a><span class="co">/* / */</span></span>
<span id="cb3-54"><a href="#cb3-54" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-55"><a href="#cb3-55" aria-hidden="true" tabindex="-1"></a><span class="cf">IF</span> OBJECT_ID(<span class="st">'tempdb..#cteCondEndDates'</span>, <span class="st">'U'</span>) <span class="kw">IS</span> <span class="kw">NOT</span> <span class="kw">NULL</span></span>
<span id="cb3-56"><a href="#cb3-56" aria-hidden="true" tabindex="-1"></a> <span class="kw">DROP</span> <span class="kw">TABLE</span> #cteCondEndDates;</span>
<span id="cb3-57"><a href="#cb3-57" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-58"><a href="#cb3-58" aria-hidden="true" tabindex="-1"></a><span class="co">/* / */</span></span>
<span id="cb3-59"><a href="#cb3-59" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-60"><a href="#cb3-60" aria-hidden="true" tabindex="-1"></a><span class="kw">SELECT</span></span>
<span id="cb3-61"><a href="#cb3-61" aria-hidden="true" tabindex="-1"></a> person_id,</span>
<span id="cb3-62"><a href="#cb3-62" aria-hidden="true" tabindex="-1"></a> condition_concept_id,</span>
<span id="cb3-63"><a href="#cb3-63" aria-hidden="true" tabindex="-1"></a> DATEADD(<span class="dt">day</span>, <span class="op">-</span> <span class="dv">30</span>, event_date) <span class="kw">AS</span> end_date <span class="co">-- unpad the end date</span></span>
<span id="cb3-64"><a href="#cb3-64" aria-hidden="true" tabindex="-1"></a><span class="kw">INTO</span> #cteCondEndDates</span>
<span id="cb3-65"><a href="#cb3-65" aria-hidden="true" tabindex="-1"></a><span class="kw">FROM</span> (</span>
<span id="cb3-66"><a href="#cb3-66" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span></span>
<span id="cb3-67"><a href="#cb3-67" aria-hidden="true" tabindex="-1"></a> e1.person_id,</span>
<span id="cb3-68"><a href="#cb3-68" aria-hidden="true" tabindex="-1"></a> e1.condition_concept_id,</span>
<span id="cb3-69"><a href="#cb3-69" aria-hidden="true" tabindex="-1"></a> e1.event_date,</span>
<span id="cb3-70"><a href="#cb3-70" aria-hidden="true" tabindex="-1"></a> <span class="fu">COALESCE</span>(e1.start_ordinal, <span class="fu">MAX</span>(e2.start_ordinal)) start_ordinal,</span>
<span id="cb3-71"><a href="#cb3-71" aria-hidden="true" tabindex="-1"></a> e1.overall_ord</span>
<span id="cb3-72"><a href="#cb3-72" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span> (</span>
<span id="cb3-73"><a href="#cb3-73" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span></span>
<span id="cb3-74"><a href="#cb3-74" aria-hidden="true" tabindex="-1"></a> person_id,</span>
<span id="cb3-75"><a href="#cb3-75" aria-hidden="true" tabindex="-1"></a> condition_concept_id,</span>
<span id="cb3-76"><a href="#cb3-76" aria-hidden="true" tabindex="-1"></a> event_date,</span>
<span id="cb3-77"><a href="#cb3-77" aria-hidden="true" tabindex="-1"></a> event_type,</span>
<span id="cb3-78"><a href="#cb3-78" aria-hidden="true" tabindex="-1"></a> start_ordinal,</span>
<span id="cb3-79"><a href="#cb3-79" aria-hidden="true" tabindex="-1"></a> <span class="fu">ROW_NUMBER</span>() <span class="kw">OVER</span> (</span>
<span id="cb3-80"><a href="#cb3-80" aria-hidden="true" tabindex="-1"></a> <span class="kw">PARTITION</span> <span class="kw">BY</span> person_id</span>
<span id="cb3-81"><a href="#cb3-81" aria-hidden="true" tabindex="-1"></a> ,condition_concept_id <span class="kw">ORDER</span> <span class="kw">BY</span> event_date</span>
<span id="cb3-82"><a href="#cb3-82" aria-hidden="true" tabindex="-1"></a> ,event_type</span>
<span id="cb3-83"><a href="#cb3-83" aria-hidden="true" tabindex="-1"></a> ) <span class="kw">AS</span> overall_ord <span class="co">-- this re-numbers the inner UNION so all rows are numbered ordered by the event date</span></span>
<span id="cb3-84"><a href="#cb3-84" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span> (</span>
<span id="cb3-85"><a href="#cb3-85" aria-hidden="true" tabindex="-1"></a> <span class="co">-- select the start dates, assigning a row number to each</span></span>
<span id="cb3-86"><a href="#cb3-86" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span></span>
<span id="cb3-87"><a href="#cb3-87" aria-hidden="true" tabindex="-1"></a> person_id,</span>
<span id="cb3-88"><a href="#cb3-88" aria-hidden="true" tabindex="-1"></a> condition_concept_id,</span>
<span id="cb3-89"><a href="#cb3-89" aria-hidden="true" tabindex="-1"></a> condition_start_date <span class="kw">AS</span> event_date,</span>
<span id="cb3-90"><a href="#cb3-90" aria-hidden="true" tabindex="-1"></a> <span class="op">-</span> <span class="dv">1</span> <span class="kw">AS</span> event_type,</span>
<span id="cb3-91"><a href="#cb3-91" aria-hidden="true" tabindex="-1"></a> <span class="fu">ROW_NUMBER</span>() <span class="kw">OVER</span> (</span>
<span id="cb3-92"><a href="#cb3-92" aria-hidden="true" tabindex="-1"></a> <span class="kw">PARTITION</span> <span class="kw">BY</span> person_id</span>
<span id="cb3-93"><a href="#cb3-93" aria-hidden="true" tabindex="-1"></a> ,condition_concept_id <span class="kw">ORDER</span> <span class="kw">BY</span> condition_start_date</span>
<span id="cb3-94"><a href="#cb3-94" aria-hidden="true" tabindex="-1"></a> ) <span class="kw">AS</span> start_ordinal</span>
<span id="cb3-95"><a href="#cb3-95" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span> #cteConditionTarget</span>
<span id="cb3-96"><a href="#cb3-96" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-97"><a href="#cb3-97" aria-hidden="true" tabindex="-1"></a> <span class="kw">UNION</span> <span class="kw">ALL</span></span>
<span id="cb3-98"><a href="#cb3-98" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-99"><a href="#cb3-99" aria-hidden="true" tabindex="-1"></a> <span class="co">-- pad the end dates by 30 to allow a grace period for overlapping ranges.</span></span>
<span id="cb3-100"><a href="#cb3-100" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span></span>
<span id="cb3-101"><a href="#cb3-101" aria-hidden="true" tabindex="-1"></a> person_id,</span>
<span id="cb3-102"><a href="#cb3-102" aria-hidden="true" tabindex="-1"></a> condition_concept_id,</span>
<span id="cb3-103"><a href="#cb3-103" aria-hidden="true" tabindex="-1"></a> DATEADD(<span class="dt">day</span>, <span class="dv">30</span>, condition_end_date),</span>
<span id="cb3-104"><a href="#cb3-104" aria-hidden="true" tabindex="-1"></a> <span class="dv">1</span> <span class="kw">AS</span> event_type,</span>
<span id="cb3-105"><a href="#cb3-105" aria-hidden="true" tabindex="-1"></a> <span class="kw">NULL</span></span>
<span id="cb3-106"><a href="#cb3-106" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span> #cteConditionTarget</span>
<span id="cb3-107"><a href="#cb3-107" aria-hidden="true" tabindex="-1"></a> ) RAWDATA</span>
<span id="cb3-108"><a href="#cb3-108" aria-hidden="true" tabindex="-1"></a> ) e1</span>
<span id="cb3-109"><a href="#cb3-109" aria-hidden="true" tabindex="-1"></a> <span class="kw">INNER</span> <span class="kw">JOIN</span> (</span>
<span id="cb3-110"><a href="#cb3-110" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span></span>
<span id="cb3-111"><a href="#cb3-111" aria-hidden="true" tabindex="-1"></a> person_id,</span>
<span id="cb3-112"><a href="#cb3-112" aria-hidden="true" tabindex="-1"></a> condition_concept_id,</span>
<span id="cb3-113"><a href="#cb3-113" aria-hidden="true" tabindex="-1"></a> condition_start_date <span class="kw">AS</span> event_date,</span>
<span id="cb3-114"><a href="#cb3-114" aria-hidden="true" tabindex="-1"></a> <span class="fu">ROW_NUMBER</span>() <span class="kw">OVER</span> (</span>
<span id="cb3-115"><a href="#cb3-115" aria-hidden="true" tabindex="-1"></a> <span class="kw">PARTITION</span> <span class="kw">BY</span> person_id</span>
<span id="cb3-116"><a href="#cb3-116" aria-hidden="true" tabindex="-1"></a> ,condition_concept_id <span class="kw">ORDER</span> <span class="kw">BY</span> condition_start_date</span>
<span id="cb3-117"><a href="#cb3-117" aria-hidden="true" tabindex="-1"></a> ) <span class="kw">AS</span> start_ordinal</span>
<span id="cb3-118"><a href="#cb3-118" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span> #cteConditionTarget</span>
<span id="cb3-119"><a href="#cb3-119" aria-hidden="true" tabindex="-1"></a> ) e2 <span class="kw">ON</span> e1.person_id <span class="op">=</span> e2.person_id</span>
<span id="cb3-120"><a href="#cb3-120" aria-hidden="true" tabindex="-1"></a> <span class="kw">AND</span> e1.condition_concept_id <span class="op">=</span> e2.condition_concept_id</span>
<span id="cb3-121"><a href="#cb3-121" aria-hidden="true" tabindex="-1"></a> <span class="kw">AND</span> e2.event_date <span class="op"><=</span> e1.event_date</span>
<span id="cb3-122"><a href="#cb3-122" aria-hidden="true" tabindex="-1"></a> <span class="kw">GROUP</span> <span class="kw">BY</span> e1.person_id</span>
<span id="cb3-123"><a href="#cb3-123" aria-hidden="true" tabindex="-1"></a> ,e1.condition_concept_id</span>
<span id="cb3-124"><a href="#cb3-124" aria-hidden="true" tabindex="-1"></a> ,e1.event_date</span>
<span id="cb3-125"><a href="#cb3-125" aria-hidden="true" tabindex="-1"></a> ,e1.start_ordinal</span>
<span id="cb3-126"><a href="#cb3-126" aria-hidden="true" tabindex="-1"></a> ,e1.overall_ord</span>
<span id="cb3-127"><a href="#cb3-127" aria-hidden="true" tabindex="-1"></a>) e</span>
<span id="cb3-128"><a href="#cb3-128" aria-hidden="true" tabindex="-1"></a><span class="kw">WHERE</span> (<span class="dv">2</span> <span class="op">*</span> e.start_ordinal) <span class="op">-</span> e.overall_ord <span class="op">=</span> <span class="dv">0</span>;</span>
<span id="cb3-129"><a href="#cb3-129" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-130"><a href="#cb3-130" aria-hidden="true" tabindex="-1"></a><span class="co">/* / */</span></span>
<span id="cb3-131"><a href="#cb3-131" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-132"><a href="#cb3-132" aria-hidden="true" tabindex="-1"></a><span class="cf">IF</span> OBJECT_ID(<span class="st">'tempdb..#cteConditionEnds'</span>, <span class="st">'U'</span>) <span class="kw">IS</span> <span class="kw">NOT</span> <span class="kw">NULL</span></span>
<span id="cb3-133"><a href="#cb3-133" aria-hidden="true" tabindex="-1"></a> <span class="kw">DROP</span> <span class="kw">TABLE</span> #cteConditionEnds;</span>
<span id="cb3-134"><a href="#cb3-134" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-135"><a href="#cb3-135" aria-hidden="true" tabindex="-1"></a><span class="co">/* / */</span></span>
<span id="cb3-136"><a href="#cb3-136" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-137"><a href="#cb3-137" aria-hidden="true" tabindex="-1"></a><span class="kw">SELECT</span></span>
<span id="cb3-138"><a href="#cb3-138" aria-hidden="true" tabindex="-1"></a> c.person_id,</span>
<span id="cb3-139"><a href="#cb3-139" aria-hidden="true" tabindex="-1"></a> c.condition_concept_id,</span>
<span id="cb3-140"><a href="#cb3-140" aria-hidden="true" tabindex="-1"></a> c.condition_start_date,</span>
<span id="cb3-141"><a href="#cb3-141" aria-hidden="true" tabindex="-1"></a> <span class="fu">MIN</span>(e.end_date) <span class="kw">AS</span> era_end_date</span>
<span id="cb3-142"><a href="#cb3-142" aria-hidden="true" tabindex="-1"></a><span class="kw">INTO</span> #cteConditionEnds</span>
<span id="cb3-143"><a href="#cb3-143" aria-hidden="true" tabindex="-1"></a><span class="kw">FROM</span> #cteConditionTarget c</span>
<span id="cb3-144"><a href="#cb3-144" aria-hidden="true" tabindex="-1"></a><span class="kw">INNER</span> <span class="kw">JOIN</span> #cteCondEndDates e <span class="kw">ON</span> c.person_id <span class="op">=</span> e.person_id</span>
<span id="cb3-145"><a href="#cb3-145" aria-hidden="true" tabindex="-1"></a> <span class="kw">AND</span> c.condition_concept_id <span class="op">=</span> e.condition_concept_id</span>
<span id="cb3-146"><a href="#cb3-146" aria-hidden="true" tabindex="-1"></a> <span class="kw">AND</span> e.end_date <span class="op">>=</span> c.condition_start_date</span>
<span id="cb3-147"><a href="#cb3-147" aria-hidden="true" tabindex="-1"></a><span class="kw">GROUP</span> <span class="kw">BY</span> c.person_id</span>
<span id="cb3-148"><a href="#cb3-148" aria-hidden="true" tabindex="-1"></a> ,c.condition_concept_id</span>
<span id="cb3-149"><a href="#cb3-149" aria-hidden="true" tabindex="-1"></a> ,c.condition_start_date;</span>
<span id="cb3-150"><a href="#cb3-150" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-151"><a href="#cb3-151" aria-hidden="true" tabindex="-1"></a><span class="co">/* / */</span></span>
<span id="cb3-152"><a href="#cb3-152" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-153"><a href="#cb3-153" aria-hidden="true" tabindex="-1"></a><span class="kw">INSERT</span> <span class="kw">INTO</span> @TARGET_CDMV5_SCHEMA.condition_era (</span>
<span id="cb3-154"><a href="#cb3-154" aria-hidden="true" tabindex="-1"></a> condition_era_id</span>
<span id="cb3-155"><a href="#cb3-155" aria-hidden="true" tabindex="-1"></a> ,person_id</span>
<span id="cb3-156"><a href="#cb3-156" aria-hidden="true" tabindex="-1"></a> ,condition_concept_id</span>
<span id="cb3-157"><a href="#cb3-157" aria-hidden="true" tabindex="-1"></a> ,condition_era_start_date</span>
<span id="cb3-158"><a href="#cb3-158" aria-hidden="true" tabindex="-1"></a> ,condition_era_end_date</span>
<span id="cb3-159"><a href="#cb3-159" aria-hidden="true" tabindex="-1"></a> ,condition_occurrence_count</span>
<span id="cb3-160"><a href="#cb3-160" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb3-161"><a href="#cb3-161" aria-hidden="true" tabindex="-1"></a><span class="kw">SELECT</span></span>
<span id="cb3-162"><a href="#cb3-162" aria-hidden="true" tabindex="-1"></a> <span class="fu">row_number</span>() <span class="kw">OVER</span> (</span>
<span id="cb3-163"><a href="#cb3-163" aria-hidden="true" tabindex="-1"></a> <span class="kw">ORDER</span> <span class="kw">BY</span> person_id</span>
<span id="cb3-164"><a href="#cb3-164" aria-hidden="true" tabindex="-1"></a> ) <span class="kw">AS</span> condition_era_id,</span>
<span id="cb3-165"><a href="#cb3-165" aria-hidden="true" tabindex="-1"></a> person_id,</span>
<span id="cb3-166"><a href="#cb3-166" aria-hidden="true" tabindex="-1"></a> condition_concept_id,</span>
<span id="cb3-167"><a href="#cb3-167" aria-hidden="true" tabindex="-1"></a> <span class="fu">min</span>(condition_start_date) <span class="kw">AS</span> condition_era_start_date,</span>
<span id="cb3-168"><a href="#cb3-168" aria-hidden="true" tabindex="-1"></a> era_end_date <span class="kw">AS</span> condition_era_end_date,</span>
<span id="cb3-169"><a href="#cb3-169" aria-hidden="true" tabindex="-1"></a> <span class="fu">COUNT</span>(<span class="op">*</span>) <span class="kw">AS</span> condition_occurrence_count</span>
<span id="cb3-170"><a href="#cb3-170" aria-hidden="true" tabindex="-1"></a><span class="kw">FROM</span> #cteConditionEnds</span>
<span id="cb3-171"><a href="#cb3-171" aria-hidden="true" tabindex="-1"></a><span class="kw">GROUP</span> <span class="kw">BY</span> person_id</span>
<span id="cb3-172"><a href="#cb3-172" aria-hidden="true" tabindex="-1"></a> ,condition_concept_id</span>
<span id="cb3-173"><a href="#cb3-173" aria-hidden="true" tabindex="-1"></a> ,era_end_date;</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
</section>
<section id="drug-eras" class="level3">
<h3 class="anchored" data-anchor-id="drug-eras">Drug Eras</h3>
<p>If the DRUG_EXPOSURE table is populated this script will string together periods of time that a person is exposed to an active drug ingredient, allowing for 30 gaps in between. It will then insert the resulting era records into the DRUG_ERA table. <strong>NOTE</strong> This query only works with the <strong>version 5</strong> series and below.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="co">-- Code taken from:</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="co">-- https://github.com/OHDSI/ETL-CMS/blob/master/SQL/create_CDMv5_drug_era_non_stockpile.sql</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> object_id(<span class="st">'tempdb..#tmp_de'</span>, <span class="st">'U'</span>) <span class="kw">is</span> <span class="kw">not</span> <span class="kw">null</span> <span class="kw">drop</span> <span class="kw">table</span> #tmp_de;</span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a><span class="kw">WITH</span></span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a>ctePreDrugTarget(drug_exposure_id, person_id, ingredient_concept_id, drug_exposure_start_date, days_supply, drug_exposure_end_date) <span class="kw">AS</span></span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a>(<span class="co">-- Normalize DRUG_EXPOSURE_END_DATE to either the existing drug exposure end date, or add days supply, or add 1 day to the start date</span></span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span></span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a> d.drug_exposure_id,</span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a> d.person_id,</span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a> c.concept_id <span class="kw">AS</span> ingredient_concept_id,</span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a> d.drug_exposure_start_date <span class="kw">AS</span> drug_exposure_start_date,</span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a> d.days_supply <span class="kw">AS</span> days_supply,</span>
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">COALESCE</span>(</span>
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true" tabindex="-1"></a> <span class="co">---NULLIF returns NULL if both values are the same, otherwise it returns the first parameter</span></span>
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">NULLIF</span>(drug_exposure_end_date, <span class="kw">NULL</span>),</span>
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true" tabindex="-1"></a> <span class="co">---If drug_exposure_end_date != NULL, return drug_exposure_end_date, otherwise go to next case</span></span>
<span id="cb4-20"><a href="#cb4-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">NULLIF</span>(dateadd(<span class="dt">day</span>,days_supply,drug_exposure_start_date), drug_exposure_start_date),</span>
<span id="cb4-21"><a href="#cb4-21" aria-hidden="true" tabindex="-1"></a> <span class="co">---If days_supply != NULL or 0, return drug_exposure_start_date + days_supply, otherwise go to next case</span></span>
<span id="cb4-22"><a href="#cb4-22" aria-hidden="true" tabindex="-1"></a> dateadd(<span class="dt">day</span>,<span class="dv">1</span>,drug_exposure_start_date)</span>
<span id="cb4-23"><a href="#cb4-23" aria-hidden="true" tabindex="-1"></a> <span class="co">---Add 1 day to the drug_exposure_start_date since there is no end_date or INTERVAL for the days_supply</span></span>
<span id="cb4-24"><a href="#cb4-24" aria-hidden="true" tabindex="-1"></a> ) <span class="kw">AS</span> drug_exposure_end_date</span>
<span id="cb4-25"><a href="#cb4-25" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span> @cdm_schema.drug_exposure d</span>
<span id="cb4-26"><a href="#cb4-26" aria-hidden="true" tabindex="-1"></a> <span class="kw">JOIN</span> @cdm_schema.concept_ancestor ca <span class="kw">ON</span> ca.descendant_concept_id <span class="op">=</span> d.drug_concept_id</span>
<span id="cb4-27"><a href="#cb4-27" aria-hidden="true" tabindex="-1"></a> <span class="kw">JOIN</span> @cdm_schema.concept c <span class="kw">ON</span> ca.ancestor_concept_id <span class="op">=</span> c.concept_id</span>
<span id="cb4-28"><a href="#cb4-28" aria-hidden="true" tabindex="-1"></a> <span class="kw">WHERE</span> c.vocabulary_id <span class="op">=</span> <span class="st">'RxNorm'</span> <span class="co">---8 selects RxNorm from the vocabulary_id</span></span>
<span id="cb4-29"><a href="#cb4-29" aria-hidden="true" tabindex="-1"></a> <span class="kw">AND</span> c.concept_class_id <span class="op">=</span> <span class="st">'Ingredient'</span></span>
<span id="cb4-30"><a href="#cb4-30" aria-hidden="true" tabindex="-1"></a> <span class="kw">AND</span> d.drug_concept_id <span class="op">!=</span> <span class="dv">0</span> <span class="co">---Our unmapped drug_concept_id's are set to 0, so we don't want different drugs wrapped up in the same era</span></span>
<span id="cb4-31"><a href="#cb4-31" aria-hidden="true" tabindex="-1"></a> <span class="kw">AND</span> <span class="fu">coalesce</span>(d.days_supply,<span class="dv">0</span>) <span class="op">>=</span> <span class="dv">0</span> <span class="co">---We have cases where days_supply is negative, and this can set the end_date before the start_date, which we don't want. So we're just looking over those rows. This is a data-quality issue.</span></span>
<span id="cb4-32"><a href="#cb4-32" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb4-33"><a href="#cb4-33" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-34"><a href="#cb4-34" aria-hidden="true" tabindex="-1"></a>, cteSubExposureEndDates (person_id, ingredient_concept_id, end_date) <span class="kw">AS</span> <span class="co">--- A preliminary sorting that groups all of the overlapping exposures into one exposure so that we don't double-count non-gap-days</span></span>
<span id="cb4-35"><a href="#cb4-35" aria-hidden="true" tabindex="-1"></a>(</span>
<span id="cb4-36"><a href="#cb4-36" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span></span>
<span id="cb4-37"><a href="#cb4-37" aria-hidden="true" tabindex="-1"></a> person_id,</span>
<span id="cb4-38"><a href="#cb4-38" aria-hidden="true" tabindex="-1"></a> ingredient_concept_id,</span>
<span id="cb4-39"><a href="#cb4-39" aria-hidden="true" tabindex="-1"></a> event_date <span class="kw">AS</span> end_date</span>
<span id="cb4-40"><a href="#cb4-40" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span></span>
<span id="cb4-41"><a href="#cb4-41" aria-hidden="true" tabindex="-1"></a> (</span>
<span id="cb4-42"><a href="#cb4-42" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span></span>
<span id="cb4-43"><a href="#cb4-43" aria-hidden="true" tabindex="-1"></a> person_id,</span>
<span id="cb4-44"><a href="#cb4-44" aria-hidden="true" tabindex="-1"></a> ingredient_concept_id,</span>
<span id="cb4-45"><a href="#cb4-45" aria-hidden="true" tabindex="-1"></a> event_date,</span>
<span id="cb4-46"><a href="#cb4-46" aria-hidden="true" tabindex="-1"></a> event_type,</span>
<span id="cb4-47"><a href="#cb4-47" aria-hidden="true" tabindex="-1"></a> <span class="fu">MAX</span>(start_ordinal) <span class="kw">OVER</span> (<span class="kw">PARTITION</span> <span class="kw">BY</span> person_id, ingredient_concept_id</span>
<span id="cb4-48"><a href="#cb4-48" aria-hidden="true" tabindex="-1"></a> <span class="kw">ORDER</span> <span class="kw">BY</span> event_date, event_type <span class="kw">ROWS</span> <span class="kw">unbounded</span> <span class="kw">preceding</span>) <span class="kw">AS</span> start_ordinal,</span>
<span id="cb4-49"><a href="#cb4-49" aria-hidden="true" tabindex="-1"></a> <span class="co">-- this pulls the current START down from the prior rows so that the NULLs</span></span>
<span id="cb4-50"><a href="#cb4-50" aria-hidden="true" tabindex="-1"></a> <span class="co">-- from the </span><span class="re">END</span><span class="co"> DATES will contain a value we can compare with</span></span>
<span id="cb4-51"><a href="#cb4-51" aria-hidden="true" tabindex="-1"></a> <span class="fu">ROW_NUMBER</span>() <span class="kw">OVER</span> (<span class="kw">PARTITION</span> <span class="kw">BY</span> person_id, ingredient_concept_id</span>
<span id="cb4-52"><a href="#cb4-52" aria-hidden="true" tabindex="-1"></a> <span class="kw">ORDER</span> <span class="kw">BY</span> event_date, event_type) <span class="kw">AS</span> overall_ord</span>
<span id="cb4-53"><a href="#cb4-53" aria-hidden="true" tabindex="-1"></a> <span class="co">-- this re-numbers the inner UNION so all rows are numbered ordered by the event date</span></span>
<span id="cb4-54"><a href="#cb4-54" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span> (</span>
<span id="cb4-55"><a href="#cb4-55" aria-hidden="true" tabindex="-1"></a> <span class="co">-- select the start dates, assigning a row number to each</span></span>
<span id="cb4-56"><a href="#cb4-56" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span></span>
<span id="cb4-57"><a href="#cb4-57" aria-hidden="true" tabindex="-1"></a> person_id,</span>
<span id="cb4-58"><a href="#cb4-58" aria-hidden="true" tabindex="-1"></a> ingredient_concept_id,</span>
<span id="cb4-59"><a href="#cb4-59" aria-hidden="true" tabindex="-1"></a> drug_exposure_start_date <span class="kw">AS</span> event_date,</span>
<span id="cb4-60"><a href="#cb4-60" aria-hidden="true" tabindex="-1"></a> <span class="op">-</span><span class="dv">1</span> <span class="kw">AS</span> event_type,</span>
<span id="cb4-61"><a href="#cb4-61" aria-hidden="true" tabindex="-1"></a> <span class="fu">ROW_NUMBER</span>() <span class="kw">OVER</span> (<span class="kw">PARTITION</span> <span class="kw">BY</span> person_id, ingredient_concept_id</span>
<span id="cb4-62"><a href="#cb4-62" aria-hidden="true" tabindex="-1"></a> <span class="kw">ORDER</span> <span class="kw">BY</span> drug_exposure_start_date) <span class="kw">AS</span> start_ordinal</span>
<span id="cb4-63"><a href="#cb4-63" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span> ctePreDrugTarget</span>
<span id="cb4-64"><a href="#cb4-64" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-65"><a href="#cb4-65" aria-hidden="true" tabindex="-1"></a> <span class="kw">UNION</span> <span class="kw">ALL</span></span>
<span id="cb4-66"><a href="#cb4-66" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-67"><a href="#cb4-67" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span></span>
<span id="cb4-68"><a href="#cb4-68" aria-hidden="true" tabindex="-1"></a> person_id,</span>
<span id="cb4-69"><a href="#cb4-69" aria-hidden="true" tabindex="-1"></a> ingredient_concept_id,</span>
<span id="cb4-70"><a href="#cb4-70" aria-hidden="true" tabindex="-1"></a> drug_exposure_end_date,</span>
<span id="cb4-71"><a href="#cb4-71" aria-hidden="true" tabindex="-1"></a> <span class="dv">1</span> <span class="kw">AS</span> event_type,</span>
<span id="cb4-72"><a href="#cb4-72" aria-hidden="true" tabindex="-1"></a> <span class="kw">NULL</span></span>
<span id="cb4-73"><a href="#cb4-73" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span> ctePreDrugTarget</span>
<span id="cb4-74"><a href="#cb4-74" aria-hidden="true" tabindex="-1"></a> ) RAWDATA</span>
<span id="cb4-75"><a href="#cb4-75" aria-hidden="true" tabindex="-1"></a> ) e</span>
<span id="cb4-76"><a href="#cb4-76" aria-hidden="true" tabindex="-1"></a> <span class="kw">WHERE</span> (<span class="dv">2</span> <span class="op">*</span> e.start_ordinal) <span class="op">-</span> e.overall_ord <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb4-77"><a href="#cb4-77" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb4-78"><a href="#cb4-78" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-79"><a href="#cb4-79" aria-hidden="true" tabindex="-1"></a>, cteDrugExposureEnds (person_id, drug_concept_id, drug_exposure_start_date, drug_sub_exposure_end_date) <span class="kw">AS</span></span>
<span id="cb4-80"><a href="#cb4-80" aria-hidden="true" tabindex="-1"></a>(</span>
<span id="cb4-81"><a href="#cb4-81" aria-hidden="true" tabindex="-1"></a><span class="kw">SELECT</span></span>
<span id="cb4-82"><a href="#cb4-82" aria-hidden="true" tabindex="-1"></a> dt.person_id,</span>
<span id="cb4-83"><a href="#cb4-83" aria-hidden="true" tabindex="-1"></a> dt.ingredient_concept_id,</span>
<span id="cb4-84"><a href="#cb4-84" aria-hidden="true" tabindex="-1"></a> dt.drug_exposure_start_date,</span>
<span id="cb4-85"><a href="#cb4-85" aria-hidden="true" tabindex="-1"></a> <span class="fu">MIN</span>(e.end_date) <span class="kw">AS</span> drug_sub_exposure_end_date</span>
<span id="cb4-86"><a href="#cb4-86" aria-hidden="true" tabindex="-1"></a><span class="kw">FROM</span> ctePreDrugTarget dt</span>
<span id="cb4-87"><a href="#cb4-87" aria-hidden="true" tabindex="-1"></a><span class="kw">JOIN</span> cteSubExposureEndDates e <span class="kw">ON</span> dt.person_id <span class="op">=</span> e.person_id <span class="kw">AND</span> dt.ingredient_concept_id <span class="op">=</span> e.ingredient_concept_id <span class="kw">AND</span> e.end_date <span class="op">>=</span> dt.drug_exposure_start_date</span>
<span id="cb4-88"><a href="#cb4-88" aria-hidden="true" tabindex="-1"></a><span class="kw">GROUP</span> <span class="kw">BY</span></span>
<span id="cb4-89"><a href="#cb4-89" aria-hidden="true" tabindex="-1"></a> dt.drug_exposure_id</span>
<span id="cb4-90"><a href="#cb4-90" aria-hidden="true" tabindex="-1"></a> , dt.person_id</span>
<span id="cb4-91"><a href="#cb4-91" aria-hidden="true" tabindex="-1"></a> , dt.ingredient_concept_id</span>
<span id="cb4-92"><a href="#cb4-92" aria-hidden="true" tabindex="-1"></a> , dt.drug_exposure_start_date</span>
<span id="cb4-93"><a href="#cb4-93" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb4-94"><a href="#cb4-94" aria-hidden="true" tabindex="-1"></a><span class="co">--------------------------------------------------------------------------------------------------------------</span></span>
<span id="cb4-95"><a href="#cb4-95" aria-hidden="true" tabindex="-1"></a>, cteSubExposures(<span class="fu">row_number</span>, person_id, drug_concept_id, drug_sub_exposure_start_date, drug_sub_exposure_end_date, drug_exposure_count) <span class="kw">AS</span></span>
<span id="cb4-96"><a href="#cb4-96" aria-hidden="true" tabindex="-1"></a>(</span>
<span id="cb4-97"><a href="#cb4-97" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span></span>
<span id="cb4-98"><a href="#cb4-98" aria-hidden="true" tabindex="-1"></a> <span class="fu">ROW_NUMBER</span>() <span class="kw">OVER</span> (<span class="kw">PARTITION</span> <span class="kw">BY</span> person_id, drug_concept_id, drug_sub_exposure_end_date <span class="kw">ORDER</span> <span class="kw">BY</span> person_id),</span>
<span id="cb4-99"><a href="#cb4-99" aria-hidden="true" tabindex="-1"></a> person_id,</span>
<span id="cb4-100"><a href="#cb4-100" aria-hidden="true" tabindex="-1"></a> drug_concept_id,</span>
<span id="cb4-101"><a href="#cb4-101" aria-hidden="true" tabindex="-1"></a> <span class="fu">MIN</span>(drug_exposure_start_date) <span class="kw">AS</span> drug_sub_exposure_start_date,</span>
<span id="cb4-102"><a href="#cb4-102" aria-hidden="true" tabindex="-1"></a> drug_sub_exposure_end_date,</span>
<span id="cb4-103"><a href="#cb4-103" aria-hidden="true" tabindex="-1"></a> <span class="fu">COUNT</span>(<span class="op">*</span>) <span class="kw">AS</span> drug_exposure_count</span>
<span id="cb4-104"><a href="#cb4-104" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span> cteDrugExposureEnds</span>
<span id="cb4-105"><a href="#cb4-105" aria-hidden="true" tabindex="-1"></a> <span class="kw">GROUP</span> <span class="kw">BY</span> person_id, drug_concept_id, drug_sub_exposure_end_date</span>
<span id="cb4-106"><a href="#cb4-106" aria-hidden="true" tabindex="-1"></a> <span class="co">--ORDER BY person_id, drug_concept_id</span></span>
<span id="cb4-107"><a href="#cb4-107" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb4-108"><a href="#cb4-108" aria-hidden="true" tabindex="-1"></a><span class="co">--------------------------------------------------------------------------------------------------------------</span></span>
<span id="cb4-109"><a href="#cb4-109" aria-hidden="true" tabindex="-1"></a><span class="co">/*Everything above grouped exposures into sub_exposures if there was overlap between exposures.</span></span>
<span id="cb4-110"><a href="#cb4-110" aria-hidden="true" tabindex="-1"></a><span class="co"> *So there was no persistence window. Now we can add the persistence window to calculate eras.</span></span>
<span id="cb4-111"><a href="#cb4-111" aria-hidden="true" tabindex="-1"></a><span class="co"> */</span></span>
<span id="cb4-112"><a href="#cb4-112" aria-hidden="true" tabindex="-1"></a><span class="co">--------------------------------------------------------------------------------------------------------------</span></span>
<span id="cb4-113"><a href="#cb4-113" aria-hidden="true" tabindex="-1"></a>, cteFinalTarget(<span class="fu">row_number</span>, person_id, ingredient_concept_id, drug_sub_exposure_start_date, drug_sub_exposure_end_date, drug_exposure_count, days_exposed) <span class="kw">AS</span></span>
<span id="cb4-114"><a href="#cb4-114" aria-hidden="true" tabindex="-1"></a>(</span>
<span id="cb4-115"><a href="#cb4-115" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span></span>
<span id="cb4-116"><a href="#cb4-116" aria-hidden="true" tabindex="-1"></a> <span class="fu">row_number</span>,</span>
<span id="cb4-117"><a href="#cb4-117" aria-hidden="true" tabindex="-1"></a> person_id,</span>
<span id="cb4-118"><a href="#cb4-118" aria-hidden="true" tabindex="-1"></a> drug_concept_id,</span>
<span id="cb4-119"><a href="#cb4-119" aria-hidden="true" tabindex="-1"></a> drug_sub_exposure_start_date,</span>
<span id="cb4-120"><a href="#cb4-120" aria-hidden="true" tabindex="-1"></a> drug_sub_exposure_end_date,</span>
<span id="cb4-121"><a href="#cb4-121" aria-hidden="true" tabindex="-1"></a> drug_exposure_count,</span>
<span id="cb4-122"><a href="#cb4-122" aria-hidden="true" tabindex="-1"></a> datediff(<span class="dt">day</span>,drug_sub_exposure_start_date,drug_sub_exposure_end_date) <span class="kw">AS</span> days_exposed</span>
<span id="cb4-123"><a href="#cb4-123" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span> cteSubExposures</span>
<span id="cb4-124"><a href="#cb4-124" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb4-125"><a href="#cb4-125" aria-hidden="true" tabindex="-1"></a><span class="co">--------------------------------------------------------------------------------------------------------------</span></span>
<span id="cb4-126"><a href="#cb4-126" aria-hidden="true" tabindex="-1"></a>, cteEndDates (person_id, ingredient_concept_id, end_date) <span class="kw">AS</span> <span class="co">-- the magic</span></span>
<span id="cb4-127"><a href="#cb4-127" aria-hidden="true" tabindex="-1"></a>(</span>
<span id="cb4-128"><a href="#cb4-128" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span></span>
<span id="cb4-129"><a href="#cb4-129" aria-hidden="true" tabindex="-1"></a> person_id,</span>
<span id="cb4-130"><a href="#cb4-130" aria-hidden="true" tabindex="-1"></a> ingredient_concept_id,</span>
<span id="cb4-131"><a href="#cb4-131" aria-hidden="true" tabindex="-1"></a> dateadd(<span class="dt">day</span>,<span class="op">-</span><span class="dv">30</span>,event_date) <span class="kw">AS</span> end_date <span class="co">-- unpad the end date</span></span>
<span id="cb4-132"><a href="#cb4-132" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span></span>
<span id="cb4-133"><a href="#cb4-133" aria-hidden="true" tabindex="-1"></a> (</span>
<span id="cb4-134"><a href="#cb4-134" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span></span>
<span id="cb4-135"><a href="#cb4-135" aria-hidden="true" tabindex="-1"></a> person_id,</span>
<span id="cb4-136"><a href="#cb4-136" aria-hidden="true" tabindex="-1"></a> ingredient_concept_id,</span>
<span id="cb4-137"><a href="#cb4-137" aria-hidden="true" tabindex="-1"></a> event_date,</span>
<span id="cb4-138"><a href="#cb4-138" aria-hidden="true" tabindex="-1"></a> event_type,</span>
<span id="cb4-139"><a href="#cb4-139" aria-hidden="true" tabindex="-1"></a> <span class="fu">MAX</span>(start_ordinal) <span class="kw">OVER</span> (<span class="kw">PARTITION</span> <span class="kw">BY</span> person_id, ingredient_concept_id</span>
<span id="cb4-140"><a href="#cb4-140" aria-hidden="true" tabindex="-1"></a> <span class="kw">ORDER</span> <span class="kw">BY</span> event_date, event_type <span class="kw">ROWS</span> <span class="kw">UNBOUNDED</span> <span class="kw">PRECEDING</span>) <span class="kw">AS</span> start_ordinal,</span>
<span id="cb4-141"><a href="#cb4-141" aria-hidden="true" tabindex="-1"></a> <span class="co">-- this pulls the current START down from the prior rows so that the NULLs</span></span>
<span id="cb4-142"><a href="#cb4-142" aria-hidden="true" tabindex="-1"></a> <span class="co">-- from the </span><span class="re">END</span><span class="co"> DATES will contain a value we can compare with</span></span>
<span id="cb4-143"><a href="#cb4-143" aria-hidden="true" tabindex="-1"></a> <span class="fu">ROW_NUMBER</span>() <span class="kw">OVER</span> (<span class="kw">PARTITION</span> <span class="kw">BY</span> person_id, ingredient_concept_id</span>
<span id="cb4-144"><a href="#cb4-144" aria-hidden="true" tabindex="-1"></a> <span class="kw">ORDER</span> <span class="kw">BY</span> event_date, event_type) <span class="kw">AS</span> overall_ord</span>
<span id="cb4-145"><a href="#cb4-145" aria-hidden="true" tabindex="-1"></a> <span class="co">-- this re-numbers the inner UNION so all rows are numbered ordered by the event date</span></span>
<span id="cb4-146"><a href="#cb4-146" aria-hidden="true" tabindex="-1"></a> <span class="kw">FROM</span> (</span>
<span id="cb4-147"><a href="#cb4-147" aria-hidden="true" tabindex="-1"></a> <span class="co">-- select the start dates, assigning a row number to each</span></span>
<span id="cb4-148"><a href="#cb4-148" aria-hidden="true" tabindex="-1"></a> <span class="kw">SELECT</span></span>
<span id="cb4-149"><a href="#cb4-149" aria-hidden="true" tabindex="-1"></a> person_id,</span>
<span id="cb4-150"><a href="#cb4-150" aria-hidden="true" tabindex="-1"></a> ingredient_concept_id,</span>
<span id="cb4-151"><a href="#cb4-151" aria-hidden="true" tabindex="-1"></a> drug_sub_exposure_start_date <span class="kw">AS</span> event_date,</span>