-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhole_script.sql
2036 lines (1821 loc) · 73.2 KB
/
whole_script.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
Add trashholds for extreme values
for non-standard units
/***** TRANSPLANT
excluded:
Transplant from live donor
Transplant from cadaver
Resection of Right Ureter, Open Approach
Resection of Right Ureter, Percutaneous Endoscopic Approach
Resection of Right Ureter, Via Natural or Artificial Opening
Resection of Right Ureter, Via Natural or Artificial Opening Endoscopic
Nephroureterectomy ;
Ureteroneocystostomy
Nephroureterectomy
Venous catheterization for renal dialysis
bypasses
********/
IF OBJECT_ID('#ckd_codes') IS NOT NULL
DROP TABLE @target_database_schema.#ckd_codes;
create table @target_database_schema.#ckd_codes
(category nvarchar(400),
concept_id int,
concept_name nvarchar(400),
concept_code nvarchar(400),
vocabulary_id nvarchar(400),
domain_id nvarchar(400),
);
insert into @target_database_schema.#ckd_codes (category,concept_id,concept_name,concept_code,vocabulary_id,domain_id )
select c.category,
c.concept_id,
c.concept_name,
c.concept_code,
c.vocabulary_id,
c.domain_id
from
(
select
'height' as category,
c.*
from @cdm_database_schema.concept c
where concept_id in (4030731, 3014149, 3008989, 3015514, 3019171, 3013842, 3023357, 3023540, 3035463, 3036277)
union all
select
'creatinine',
c.*
from @cdm_database_schema.concept c
where concept_id in (3045558,4150621,3042112,3050975,3026387,3026726,3004239,3024742,3013280,3053284,3014654,3040071,3022243,40760461,42868736,3040510,
21491015,21492443,3033837,40762044,3021126,4324383,3006701,3010663,3037459,3037441,40762091,3008392,3012506,3013296,3028031,3013539,
3026275,3032932,3043954,3004171,3016647,3011002,3016723,3051825,3025065,3003447,3022016,43055681,3012179,40762887,3045443,4276116,
3041339,3049517,3018968,3005717,3027322,40760837,3014699,3022673,3045262,3019491,3048925,3052562,4013964,3015872,3007795,3041045,
3001349,3016662,3027111,40757505,3038830,3007760,3024275,3007196,3040209,3041716,40757506,40758722,3035090,40768326,43055236,3026249,
3025645,3021074,3007081,3025715,3042571,3041735,3036094,3038385,21491016,3007349,3041197,3017250,3044547,4155367,46235076,3019397,
3010529,3014724,42868738,4230629,3045369,3045571,3048276,3040006,3006181,3020564,3027362,40762895,40760463,3025813,3037052,40760462,
3009508,3023157,3020847,3040495)
union all
select
'albumin',
c.* -- mass/time
from @cdm_database_schema.concept c
where concept_id in
(46236963, 3033268, 3050449, 3018097, 3027035, 043771, 40766204, 3005577, 3040290, 3043179, 40759673, 3049506, 40761549)
union all
select
'albumin',
c.* -- mass/volume
from @cdm_database_schema.concept c
where concept_id in
(3008512, 3012516, 46236875, 3039775, 3018104, 3030511, 3008960, 37393656, 4193719, 40760483, 3005031, 3039436, 3046828, 3000034)
union all
select
'albumin',
c.* -- albumin general codes
from @cdm_database_schema.concept c
where concept_id in (4017498, 2212188, 2212189, 4152996)
union all
select
'protein',
c.* -- general
from @cdm_database_schema.concept c
where concept_id in (4152995, 4064934,3001237,3005897,3011705,3014051,3017756,3017817,3019077,3020876,3028250,3029872,3033812,3035511,
3037121,3037185,3038906,3039271,3040443,3040816,3044927,4025832,4041881,4064934,4152995,4154500,
4211845,4220762,4251338,21491095,40760845,40762085,46235791)
union all
select
'alb/creat_ratio',
c.*
from @cdm_database_schema.concept c
where
concept_id in (3000819, 3034485, 3002812, 3000837, 46235897, 3020682, 3043209, 3002827, 3001802, 40762252,
46235435, 3022826, 46235434, 3023556, 4154347)
union all
select
'egfr',
c.*
from @cdm_database_schema.concept c
where concept_id in
(3029829, 3029859, 3030104, 3045262, 36304157, 36306178, 40478895, 40478963, 40483219, 40485075,
40490315, 40764999, 40771922, 42869913, 4478827544790183, 44806420, 46236952, 46236975, 3049187,
3053283, 36303797)
union all
select
'gravity',
c.*
from @cdm_database_schema.concept c
where concept_id in
(2212165, 2212166, 2212577, 3000330, 3019150, 3029991, 3032448, 3033543, 3034076, 3039919, 3043812, 4147583)
) c
;
INSERT INTO @target_database_schema.#ckd_codes
(
category,
concept_id,
concept_name,
concept_code,
vocabulary_id,
domain_id
)
SELECT 'transplant' AS category,
c.concept_id,
c.concept_name,
c.concept_code,
c.vocabulary_id,
c.domain_id
FROM @cdm_database_schema.concept_ancestor
JOIN @cdm_database_schema.concept_relationship cr ON cr.concept_id_2 = descendant_concept_id
JOIN @cdm_database_schema.concept c
ON concept_id_1 = c.concept_id
AND cr.invalid_reason IS NULL
AND relationship_id IN ('Maps to', 'Maps to value', 'Has asso proc')
AND c.invalid_reason IS NULL
WHERE ancestor_concept_id IN (
--------procedures------
4163566,-- SNOMED Renal replacement
4322471,-- transplant of kidney
4146256,-- transplant nephrectomy
2877118,-- ICD10 0TY0
2833286,-- ICD10 0TY1
4082531,--US scan of transpl
4180454,--Examination of live donor after kidney transplant
42690461,-- Fluoroscopy guided removal of nephrostomy tube from transplanted kidney
------ conditions ----
42539502,-- transplanted kidney present
4324887--Disorder related to renal transplantation
)
AND c.vocabulary_id NOT IN ('MeSH','PPI','SUS');
INSERT INTO @target_database_schema.#ckd_codes
(
category,
concept_id,
concept_name,
concept_code,
vocabulary_id,
domain_id
)
SELECT 'dialysis' AS category,
c.concept_id,
c.concept_name,
c.concept_code,
c.vocabulary_id,
c.domain_id
FROM @cdm_database_schema.concept_ancestor
JOIN @cdm_database_schema.concept_relationship cr ON cr.concept_id_2 = descendant_concept_id
JOIN @cdm_database_schema.concept c
ON concept_id_1 = c.concept_id
AND cr.invalid_reason IS NULL
AND relationship_id IN ('Maps to', 'Maps to value', 'Has asso proc', 'Followed by', 'Has due to')
AND c.invalid_reason IS NULL
WHERE ancestor_concept_id IN (
4019967,-- Dependence on renal dialysis
4059475,-- H/O: renal dialysis
4300837,-- Dialysis care assessment
4300838,-- Dialysis care education
4146536,-- Renal dialysis
438624,-- Complication of renal dialysis
4026915,-- Revision of arteriovenous shunt for renal dialysis
4289454,-- Venous catheterization for renal dialysis
4272012,--Insertion of cannula for hemodialysis
4300839,--Dialysis care management
45887996--End-Stage Renal Disease Services
)
AND c.vocabulary_id NOT IN ('MeSH','PPI','SUS');
INSERT INTO @target_database_schema.#ckd_codes
(category, concept_id, concept_name, concept_code, vocabulary_id, domain_id)
SELECT
'AKD' AS category,
c.concept_id,
c.concept_name,
c.concept_code,
c.vocabulary_id,
c.domain_id
FROM @cdm_database_schema.concept_ancestor
JOIN @cdm_database_schema.concept_relationship cr ON cr.concept_id_2 = descendant_concept_id
JOIN @cdm_database_schema.concept c
ON concept_id_1 = c.concept_id
AND cr.invalid_reason IS NULL
AND relationship_id IN ('Maps to', 'Maps to value', 'Has asso proc', 'Followed by', 'Has due to')
AND c.invalid_reason IS NULL
WHERE ancestor_concept_id IN (
197320, --Acute renal failure syndrome
444044, --Acute tubular necrosis
761083, --Acute injury of kidney -- coundn't find a common parent w/o nephritis
37116430, --Acute kidney failure stage 1
37116431, --Acute kidney failure stage 2
37116432, --Acute kidney failure stage 3
4228827, --Acute milk alkali syndrome
4111399, --Acute pericarditis secondary to uremia
4232873, --Acute postoperative renal failure
40481064, --Acute renal cortical necrosis
4126305, --Acute renal impairment
37399017, --Hemorrhagic fever with renal syndrome
37116834, --Postpartum acute renal failure
42536547--Ischemia of kidney (in SNOMED only acute)
)
AND c.vocabulary_id NOT IN ('MeSH', 'PPI', 'SUS')
;
INSERT INTO @target_database_schema.#ckd_codes
(
category,
concept_id,
concept_name,
concept_code,
vocabulary_id,
domain_id
)
SELECT
'CKD' AS category,
c.concept_id,
c.concept_name,
c.concept_code,
c.vocabulary_id,
c.domain_id
FROM @cdm_database_schema.concept_ancestor
JOIN @cdm_database_schema.concept_relationship cr ON cr.concept_id_2 = descendant_concept_id
JOIN @cdm_database_schema.concept c
ON concept_id_1 = c.concept_id
AND cr.invalid_reason IS NULL
AND relationship_id IN ('Maps to', 'Maps to value', 'Has asso proc', 'Followed by', 'Has due to')
AND c.invalid_reason IS NULL
WHERE ancestor_concept_id IN (
193782, -- End-stage renal disease
46271022, -- Chronic kidney disease
196991, --Chronic renal impairment
40769275 -- Estimated or measured glomerular filtration rate less than 50 percent [Reported]
)
AND c.vocabulary_id NOT IN ('MeSH', 'PPI', 'SUS');
INSERT INTO @target_database_schema.#ckd_codes
(
category,
concept_id,
concept_name,
concept_code,
vocabulary_id,
domain_id
)
SELECT
'other_acute' AS category,
c.concept_id,
c.concept_name,
c.concept_code,
c.vocabulary_id,
c.domain_id
FROM @cdm_database_schema.concept_ancestor
JOIN @cdm_database_schema.concept_relationship cr ON cr.concept_id_2 = descendant_concept_id
JOIN @cdm_database_schema.concept c
ON concept_id_1 = c.concept_id
AND cr.invalid_reason IS NULL
AND relationship_id IN ('Maps to', 'Maps to value', 'Has asso proc', 'Followed by', 'Has due to')
AND c.invalid_reason IS NULL
WHERE ancestor_concept_id IN (
132797, -- Sepsis
436375, -- Hypovolemia
201965-- Shock
)
AND c.vocabulary_id NOT IN ('MeSH', 'PPI', 'SUS');
-- didn't get additional codes
INSERT INTO @target_database_schema.#ckd_codes
(
category,
concept_id,
concept_name,
concept_code,
vocabulary_id,
domain_id
)
SELECT
'CKD' AS category,
c.concept_id,
c.concept_name,
c.concept_code,
c.vocabulary_id,
c.domain_id
FROM @cdm_database_schema.concept_ancestor
JOIN @cdm_database_schema.concept_relationship cr ON cr.concept_id_2 = descendant_concept_id
JOIN @cdm_database_schema.concept c
ON concept_id_1 = c.concept_id
AND cr.invalid_reason IS NULL
AND relationship_id IN ('Maps to', 'Maps to value', 'Has asso proc', 'Followed by', 'Has due to')
AND c.invalid_reason IS NULL
WHERE ancestor_concept_id IN (
193782, -- End-stage renal disease
46271022, -- Chronic kidney disease
196991 --Chronic renal impairment
)
AND c.vocabulary_id NOT IN ('MeSH', 'PPI', 'SUS')
;
INSERT INTO @target_database_schema.#ckd_codes
(
category,
concept_id,
concept_name,
concept_code,
vocabulary_id,
domain_id
)
SELECT
'other_KD' AS category,
c.concept_id,
c.concept_name,
c.concept_code,
c.vocabulary_id,
c.domain_id
FROM @cdm_database_schema.concept_ancestor
JOIN @cdm_database_schema.concept_relationship cr ON cr.concept_id_2 = descendant_concept_id
JOIN @cdm_database_schema.concept c
ON concept_id_1 = c.concept_id
AND cr.invalid_reason IS NULL
AND relationship_id IN ('Maps to', 'Maps to value', 'Has asso proc', 'Followed by', 'Has due to')
AND c.invalid_reason IS NULL
WHERE ancestor_concept_id IN
(198124, --kidney disease
75650, --proteinuria
193955, --Tuberculosis of kidney
201313--Hypertensive renal disease
)
AND c.vocabulary_id NOT IN ('MeSH', 'PPI', 'SUS');IF OBJECT_ID('#creatinine') IS NOT NULL
DROP TABLE @target_database_schema.#creatinine;
CREATE TABLE @target_database_schema.#creatinine (
person_id INT,
gender_concept_id INT,
race_concept_id INT,
measurement_date DATETIME2(6),
measurement_concept_id INT,
ageAtMeasYear INT,
year_of_birth INT,
crVal VARCHAR(100),
value_as_concept_id INT,
genderFactor FLOAT,
raceFactor FLOAT,
kappaFactor FLOAT,
alphaFactor FLOAT,
minCrK FLOAT,
maxCrK FLOAT
);
INSERT INTO @target_database_schema.#creatinine
SELECT cr.*,
CASE WHEN value_as_number/kappaFactor < 1 THEN value_as_number/kappaFactor ELSE 1 END AS minCrK,
CASE WHEN value_as_number/kappaFactor > 1 THEN value_as_number/kappaFactor ELSE 1 END AS maxCrK
FROM (
SELECT DISTINCT m.person_id, gender_concept_id, race_concept_id,
measurement_date,
measurement_concept_id,
year(measurement_date) - year_of_birth AS ageAtMeasYear,
year_of_birth,
case
when unit_concept_id = 8840 then value_as_number-- mg/dl
when unit_concept_id = 8842 then value_as_number*0.0001 -- ng/ml
when unit_concept_id = 8749 then value_as_number*0.0113 -- mcmol/l
when unit_concept_id = 9586 then value_as_number*11300 -- mol/l
when unit_concept_id = 8753 then value_as_number*11.3 -- mmol/l
when unit_concept_id = 8636 then value_as_number*1000-- g/l
else null end as crVal,
value_as_concept_id,
CASE
WHEN gender_concept_id = 8532 -- female
THEN 1.018
ELSE 1 END AS genderFactor,
CASE
WHEN race_concept_id in (8516,38003598) THEN 1.159 --black
ELSE 1 END AS raceFactor,
CASE
WHEN gender_concept_id = 8532 THEN 0.7 -- female
ELSE 0.9 END AS kappaFactor,
CASE
WHEN gender_concept_id = 8532 THEN -0.329 -- female
ELSE -0.411 END AS alphaFactor
FROM @cdm_database_schema.person p
JOIN @cdm_database_schema.MEASUREMENT m on p.person_id = m.person_id
JOIN @target_database_schema.#CKD_codes on measurement_concept_id = concept_id and category = 'creatinine'
WHERE m.value_as_number IS NOT NULL and m.value_as_number>0
) CR
;IF OBJECT_ID('#height') IS NOT NULL
DROP TABLE @target_database_schema.#height;
CREATE TABLE @target_database_schema.#height (
person_id INT,
measurement_date DATETIME2(6),
measurement_concept_id INT,
ht VARCHAR(100),
value_as_concept_id INT
);
INSERT INTO @target_database_schema.#height
SELECT DISTINCT
person_id,
m.measurement_date,
m.measurement_concept_id,
case when m.unit_concept_id = 8533
then m.value_as_number * 2.54 -- Inches
when m.unit_concept_id = 8547
then m.value_as_number * 100 -- m
when m.unit_concept_id = 8582
then m.value_as_number --cm
else null end AS ht,
m.value_as_concept_id
FROM @cdm_database_schema.MEASUREMENT m
JOIN @target_database_schema.#creatinine c using (person_id)
WHERE m.measurement_concept_id IN (select concept_id
from @target_database_schema.#ckd_codes
where category = 'height')
AND DATEADD(year, 1, c.measurement_date) >= m.measurement_date
AND DATEADD(year, -1, c.measurement_date) <= m.measurement_date
AND m.value_as_number IS NOT NULL;
with primary_events (event_id, person_id, start_date, end_date, op_start_date, op_end_date, visit_occurrence_id) as
(
-- Begin Primary Events
select P.ordinal as event_id, P.person_id, P.start_date, P.end_date, op_start_date, op_end_date, cast(P.visit_occurrence_id as bigint) as visit_occurrence_id
FROM
(
select E.person_id, E.start_date, E.end_date, row_number() OVER (PARTITION BY E.person_id ORDER BY E.start_date ASC) ordinal, OP.observation_period_start_date as op_start_date, OP.observation_period_end_date as op_end_date, cast(E.visit_occurrence_id as bigint) as visit_occurrence_id
FROM
(
-- Begin CDM tables Criteria
select
pe.person_id as person_id,
pe.procedure_occurrence_id as event_id,
pe.procedure_date as start_date,
pe.procedure_date as end_date,
pe.procedure_concept_id as TARGET_CONCEPT_ID,
pe.visit_occurrence_id as visit_occurrence_id
FROM @target_database_schema.#CKD_codes ckd
JOIN @cdm_database_schema.PROCEDURE_OCCURRENCE pe
on (pe.procedure_concept_id = ckd.concept_id and ckd.category = 'transplant')
union all
select
co.person_id,
co.condition_occurrence_id as event_id,
co.condition_start_date as start_date,
co.condition_end_date as end_date,
co.condition_concept_id as TARGET_CONCEPT_ID,
co.visit_occurrence_id
FROM @target_database_schema.#CKD_codes ckd
JOIN @cdm_database_schema.CONDITION_OCCURRENCE co
on (co.condition_concept_id = ckd.concept_id and ckd.category = 'transplant')
) E
JOIN @cdm_database_schema.observation_period OP on E.person_id = OP.person_id and E.start_date >= OP.observation_period_start_date and E.start_date <= op.observation_period_end_date
WHERE DATEADD(day,0,OP.OBSERVATION_PERIOD_START_DATE) <= E.START_DATE AND DATEADD(day,0,E.START_DATE) <= OP.OBSERVATION_PERIOD_END_DATE
) P
WHERE P.ordinal = 1
-- End Primary Events
)
SELECT event_id, person_id, start_date, end_date, op_start_date, op_end_date, visit_occurrence_id
INTO #qualified_events
FROM
(
select pe.event_id, pe.person_id, pe.start_date, pe.end_date, pe.op_start_date, pe.op_end_date, row_number() over (partition by pe.person_id order by pe.start_date ASC) as ordinal, cast(pe.visit_occurrence_id as bigint) as visit_occurrence_id
FROM primary_events pe
) QE
;
--- Inclusion Rule Inserts
create table #inclusion_events (inclusion_rule_id bigint,
person_id bigint,
event_id bigint
);
with cteIncludedEvents(event_id, person_id, start_date, end_date, op_start_date, op_end_date, ordinal) as
(
SELECT event_id, person_id, start_date, end_date, op_start_date, op_end_date, row_number() over (partition by person_id order by start_date ASC) as ordinal
from
(
select Q.event_id, Q.person_id, Q.start_date, Q.end_date, Q.op_start_date, Q.op_end_date, SUM(coalesce(POWER(cast(2 as bigint), I.inclusion_rule_id), 0)) as inclusion_rule_mask
from #qualified_events Q
LEFT JOIN #inclusion_events I on I.person_id = Q.person_id and I.event_id = Q.event_id
GROUP BY Q.event_id, Q.person_id, Q.start_date, Q.end_date, Q.op_start_date, Q.op_end_date
) MG -- matching groups
)
select event_id, person_id, start_date, end_date, op_start_date, op_end_date
into #included_events
FROM cteIncludedEvents Results
WHERE Results.ordinal = 1
;
-- generate cohort periods into #final_cohort
with cohort_ends (event_id, person_id, end_date) as
(
-- cohort exit dates
-- By default, cohort exit at the event's op end date
select event_id, person_id, op_end_date as end_date from #included_events
),
first_ends (person_id, start_date, end_date) as
(
select F.person_id, F.start_date, F.end_date
FROM (
select I.event_id, I.person_id, I.start_date, E.end_date, row_number() over (partition by I.person_id, I.event_id order by E.end_date) as ordinal
from #included_events I
join cohort_ends E on I.event_id = E.event_id and I.person_id = E.person_id and E.end_date >= I.start_date
) F
WHERE F.ordinal = 1
)
select person_id, start_date, end_date
INTO #cohort_rows
from first_ends;
with cteEndDates (person_id, end_date) AS -- the magic
(
SELECT
person_id
, DATEADD(day,-1 * 0, event_date) as end_date
FROM
(
SELECT
person_id
, event_date
, event_type
, MAX(start_ordinal) OVER (PARTITION BY person_id ORDER BY event_date, event_type ROWS UNBOUNDED PRECEDING) AS start_ordinal
, ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY event_date, event_type) AS overall_ord
FROM
(
SELECT
person_id
, start_date AS event_date
, -1 AS event_type
, ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY start_date) AS start_ordinal
FROM #cohort_rows
UNION ALL
SELECT
person_id
, DATEADD(day,0,end_date) as end_date
, 1 AS event_type
, NULL
FROM #cohort_rows
) RAWDATA
) e
WHERE (2 * e.start_ordinal) - e.overall_ord = 0
),
cteEnds (person_id, start_date, end_date) AS
(
SELECT
c.person_id
, c.start_date
, MIN(e.end_date) AS era_end_date
FROM #cohort_rows c
JOIN cteEndDates e ON c.person_id = e.person_id AND e.end_date >= c.start_date
GROUP BY c.person_id, c.start_date
)
select person_id, min(start_date) as start_date, end_date
into #final_cohort
from cteEnds
group by person_id, end_date
;
DELETE FROM @target_database_schema.@target_cohort_table where cohort_definition_id = 1000;
INSERT INTO @target_database_schema.@target_cohort_table (cohort_definition_id, subject_id, cohort_start_date, cohort_end_date)
select 1000 as cohort_definition_id, person_id, start_date, end_date
FROM #final_cohort CO
;
TRUNCATE TABLE #cohort_rows;
DROP TABLE #cohort_rows;
TRUNCATE TABLE #final_cohort;
DROP TABLE #final_cohort;
TRUNCATE TABLE #inclusion_events;
DROP TABLE #inclusion_events;
TRUNCATE TABLE #qualified_events;
DROP TABLE #qualified_events;
TRUNCATE TABLE #included_events;
DROP TABLE #included_events;
with primary_events (event_id, person_id, start_date, end_date, op_start_date, op_end_date, visit_occurrence_id) as
(
-- Begin Primary Events
select P.ordinal as event_id, P.person_id, P.start_date, P.end_date, op_start_date, op_end_date, cast(P.visit_occurrence_id as bigint) as visit_occurrence_id
FROM
(
select E.person_id, E.start_date, E.end_date, row_number() OVER (PARTITION BY E.person_id ORDER BY E.start_date ASC) ordinal, OP.observation_period_start_date as op_start_date, OP.observation_period_end_date as op_end_date, cast(E.visit_occurrence_id as bigint) as visit_occurrence_id
FROM
(
-- Begin CDM tables Criteria
select
pe.person_id as person_id,
pe.procedure_occurrence_id as event_id,
pe.procedure_date as start_date,
pe.procedure_date as end_date,
pe.procedure_concept_id as TARGET_CONCEPT_ID,
pe.visit_occurrence_id as visit_occurrence_id
FROM @target_database_schema.CKD_codes ckd
JOIN @cdm_database_schema.PROCEDURE_OCCURRENCE pe
on (pe.procedure_concept_id = ckd.concept_id and ckd.category = 'dialysis')
union all
select
co.person_id,
co.condition_occurrence_id as event_id,
co.condition_start_date as start_date,
co.condition_end_date as end_date,
co.condition_concept_id as TARGET_CONCEPT_ID,
co.visit_occurrence_id
FROM @target_database_schema.#CKD_codes ckd
JOIN @cdm_database_schema.CONDITION_OCCURRENCE co
on (co.condition_concept_id = ckd.concept_id and ckd.category = 'dialysis')
union all
select
o.person_id,
o.observation_id as event_id,
o.observation_date as start_date,
o.observation_date as end_date,
o.observation_concept_id as TARGET_CONCEPT_ID,
o.visit_occurrence_id
FROM @target_database_schema.#CKD_codes ckd
JOIN @cdm_database_schema.OBSERVATION o
on (o.observation_concept_id = ckd.concept_id and ckd.category = 'dialysis')
) E
JOIN @cdm_database_schema.observation_period OP on E.person_id = OP.person_id and E.start_date >= OP.observation_period_start_date and E.start_date <= op.observation_period_end_date
WHERE DATEADD(day,0,OP.OBSERVATION_PERIOD_START_DATE) <= E.START_DATE AND DATEADD(day,0,E.START_DATE) <= OP.OBSERVATION_PERIOD_END_DATE
) P
WHERE P.ordinal = 1
-- End Primary Events
)
SELECT event_id, person_id, start_date, end_date, op_start_date, op_end_date, visit_occurrence_id
INTO #qualified_events
FROM
(
select pe.event_id, pe.person_id, pe.start_date, pe.end_date, pe.op_start_date, pe.op_end_date, row_number() over (partition by pe.person_id order by pe.start_date ASC) as ordinal, cast(pe.visit_occurrence_id as bigint) as visit_occurrence_id
FROM primary_events pe
) QE
;
--- Inclusion Rule Inserts
create table #inclusion_events (inclusion_rule_id bigint,
person_id bigint,
event_id bigint
);
with cteIncludedEvents(event_id, person_id, start_date, end_date, op_start_date, op_end_date, ordinal) as
(
SELECT event_id, person_id, start_date, end_date, op_start_date, op_end_date, row_number() over (partition by person_id order by start_date ASC) as ordinal
from
(
select Q.event_id, Q.person_id, Q.start_date, Q.end_date, Q.op_start_date, Q.op_end_date, SUM(coalesce(POWER(cast(2 as bigint), I.inclusion_rule_id), 0)) as inclusion_rule_mask
from #qualified_events Q
LEFT JOIN #inclusion_events I on I.person_id = Q.person_id and I.event_id = Q.event_id
GROUP BY Q.event_id, Q.person_id, Q.start_date, Q.end_date, Q.op_start_date, Q.op_end_date
) MG -- matching groups
)
select event_id, person_id, start_date, end_date, op_start_date, op_end_date
into #included_events
FROM cteIncludedEvents Results
WHERE Results.ordinal = 1
;
-- generate cohort periods into #final_cohort
with cohort_ends (event_id, person_id, end_date) as
(
-- cohort exit dates
-- By default, cohort exit at the event's op end date
select event_id, person_id, op_end_date as end_date from #included_events
),
first_ends (person_id, start_date, end_date) as
(
select F.person_id, F.start_date, F.end_date
FROM (
select I.event_id, I.person_id, I.start_date, E.end_date, row_number() over (partition by I.person_id, I.event_id order by E.end_date) as ordinal
from #included_events I
join cohort_ends E on I.event_id = E.event_id and I.person_id = E.person_id and E.end_date >= I.start_date
) F
WHERE F.ordinal = 1
)
select person_id, start_date, end_date
INTO #cohort_rows
from first_ends;
with cteEndDates (person_id, end_date) AS -- the magic
(
SELECT
person_id
, DATEADD(day,-1 * 0, event_date) as end_date
FROM
(
SELECT
person_id
, event_date
, event_type
, MAX(start_ordinal) OVER (PARTITION BY person_id ORDER BY event_date, event_type ROWS UNBOUNDED PRECEDING) AS start_ordinal
, ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY event_date, event_type) AS overall_ord
FROM
(
SELECT
person_id
, start_date AS event_date
, -1 AS event_type
, ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY start_date) AS start_ordinal
FROM #cohort_rows
UNION ALL
SELECT
person_id
, DATEADD(day,0,end_date) as end_date
, 1 AS event_type
, NULL
FROM #cohort_rows
) RAWDATA
) e
WHERE (2 * e.start_ordinal) - e.overall_ord = 0
),
cteEnds (person_id, start_date, end_date) AS
(
SELECT
c.person_id
, c.start_date
, MIN(e.end_date) AS era_end_date
FROM #cohort_rows c
JOIN cteEndDates e ON c.person_id = e.person_id AND e.end_date >= c.start_date
GROUP BY c.person_id, c.start_date
)
select person_id, min(start_date) as start_date, end_date
into #final_cohort
from cteEnds
group by person_id, end_date
;
DELETE FROM @target_database_schema.@target_cohort_table where cohort_definition_id = 1001;
INSERT INTO @target_database_schema.@target_cohort_table (cohort_definition_id, subject_id, cohort_start_date, cohort_end_date)
select 1001 as cohort_definition_id, person_id, start_date, end_date
FROM #final_cohort CO
;
TRUNCATE TABLE #cohort_rows;
DROP TABLE #cohort_rows;
TRUNCATE TABLE #final_cohort;
DROP TABLE #final_cohort;
TRUNCATE TABLE #inclusion_events;
DROP TABLE #inclusion_events;
TRUNCATE TABLE #qualified_events;
DROP TABLE #qualified_events;
TRUNCATE TABLE #included_events;
DROP TABLE #included_events;
with primary_events (event_id, person_id, start_date, end_date, op_start_date, op_end_date, visit_occurrence_id) as
(
-- Begin Primary Events
select P.ordinal as event_id, P.person_id, P.start_date, P.end_date, op_start_date, op_end_date, cast(P.visit_occurrence_id as bigint) as visit_occurrence_id
FROM
(
select E.person_id, E.start_date, E.end_date, row_number() OVER (PARTITION BY E.person_id ORDER BY E.start_date ASC) ordinal, OP.observation_period_start_date as op_start_date, OP.observation_period_end_date as op_end_date, cast(E.visit_occurrence_id as bigint) as visit_occurrence_id
FROM
(
-- Begin CDM tables Criteria
select
co.person_id,
co.condition_occurrence_id as event_id,
co.condition_start_date as start_date,
co.condition_end_date as end_date,
co.condition_concept_id as TARGET_CONCEPT_ID,
co.visit_occurrence_id
FROM @target_database_schema.#CKD_codes ckd
JOIN @cdm_database_schema.CONDITION_OCCURRENCE co
on (co.condition_concept_id = ckd.concept_id and ckd.category = 'other_acute')
) E
JOIN @cdm_database_schema.observation_period OP on E.person_id = OP.person_id and E.start_date >= OP.observation_period_start_date and E.start_date <= op.observation_period_end_date
WHERE DATEADD(day,0,OP.OBSERVATION_PERIOD_START_DATE) <= E.START_DATE AND DATEADD(day,0,E.START_DATE) <= OP.OBSERVATION_PERIOD_END_DATE
) P
WHERE P.ordinal = 1
-- End Primary Events
)
SELECT event_id, person_id, start_date, end_date, op_start_date, op_end_date, visit_occurrence_id
INTO #qualified_events
FROM
(
select pe.event_id, pe.person_id, pe.start_date, pe.end_date, pe.op_start_date, pe.op_end_date, row_number() over (partition by pe.person_id order by pe.start_date ASC) as ordinal, cast(pe.visit_occurrence_id as bigint) as visit_occurrence_id
FROM primary_events pe
) QE
;
--- Inclusion Rule Inserts
create table #inclusion_events (inclusion_rule_id bigint,
person_id bigint,
event_id bigint
);
with cteIncludedEvents(event_id, person_id, start_date, end_date, op_start_date, op_end_date, ordinal) as
(
SELECT event_id, person_id, start_date, end_date, op_start_date, op_end_date, row_number() over (partition by person_id order by start_date ASC) as ordinal
from
(
select Q.event_id, Q.person_id, Q.start_date, Q.end_date, Q.op_start_date, Q.op_end_date, SUM(coalesce(POWER(cast(2 as bigint), I.inclusion_rule_id), 0)) as inclusion_rule_mask
from #qualified_events Q
LEFT JOIN #inclusion_events I on I.person_id = Q.person_id and I.event_id = Q.event_id
GROUP BY Q.event_id, Q.person_id, Q.start_date, Q.end_date, Q.op_start_date, Q.op_end_date
) MG -- matching groups
)
select event_id, person_id, start_date, end_date, op_start_date, op_end_date
into #included_events
FROM cteIncludedEvents Results
WHERE Results.ordinal = 1
;
-- generate cohort periods into #final_cohort
with cohort_ends (event_id, person_id, end_date) as
(
-- cohort exit dates
-- By default, cohort exit at the event's op end date
select event_id, person_id, op_end_date as end_date from #included_events
),
first_ends (person_id, start_date, end_date) as
(
select F.person_id, F.start_date, F.end_date
FROM (
select I.event_id, I.person_id, I.start_date, E.end_date, row_number() over (partition by I.person_id, I.event_id order by E.end_date) as ordinal
from #included_events I
join cohort_ends E on I.event_id = E.event_id and I.person_id = E.person_id and E.end_date >= I.start_date
) F
WHERE F.ordinal = 1
)
select person_id, start_date, end_date
INTO #cohort_rows
from first_ends;
with cteEndDates (person_id, end_date) AS -- the magic
(
SELECT
person_id
, DATEADD(day,-1 * 0, event_date) as end_date
FROM
(
SELECT
person_id
, event_date
, event_type
, MAX(start_ordinal) OVER (PARTITION BY person_id ORDER BY event_date, event_type ROWS UNBOUNDED PRECEDING) AS start_ordinal
, ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY event_date, event_type) AS overall_ord
FROM
(
SELECT
person_id
, start_date AS event_date
, -1 AS event_type
, ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY start_date) AS start_ordinal
FROM #cohort_rows
UNION ALL
SELECT
person_id
, DATEADD(day,0,end_date) as end_date
, 1 AS event_type
, NULL
FROM #cohort_rows
) RAWDATA
) e
WHERE (2 * e.start_ordinal) - e.overall_ord = 0
),
cteEnds (person_id, start_date, end_date) AS
(
SELECT
c.person_id
, c.start_date
, MIN(e.end_date) AS era_end_date
FROM #cohort_rows c
JOIN cteEndDates e ON c.person_id = e.person_id AND e.end_date >= c.start_date
GROUP BY c.person_id, c.start_date
)
select person_id, min(start_date) as start_date, end_date
into #final_cohort
from cteEnds
group by person_id, end_date
;
DELETE FROM @target_database_schema.@target_cohort_table where cohort_definition_id = 1002;
INSERT INTO @target_database_schema.@target_cohort_table (cohort_definition_id, subject_id, cohort_start_date, cohort_end_date)
select 1002 as cohort_definition_id, person_id, start_date, end_date
FROM #final_cohort CO
;
TRUNCATE TABLE #cohort_rows;
DROP TABLE #cohort_rows;
TRUNCATE TABLE #final_cohort;
DROP TABLE #final_cohort;
TRUNCATE TABLE #inclusion_events;
DROP TABLE #inclusion_events;
TRUNCATE TABLE #qualified_events;
DROP TABLE #qualified_events;
TRUNCATE TABLE #included_events;
DROP TABLE #included_events;with primary_events (event_id, person_id, start_date, end_date, op_start_date, op_end_date, visit_occurrence_id) as
(
-- Begin Primary Events
select P.ordinal as event_id, P.person_id, P.start_date, P.end_date, op_start_date, op_end_date, cast(P.visit_occurrence_id as bigint) as visit_occurrence_id
FROM
(
select E.person_id, E.start_date, E.end_date, row_number() OVER (PARTITION BY E.person_id ORDER BY E.start_date ASC) ordinal, OP.observation_period_start_date as op_start_date, OP.observation_period_end_date as op_end_date, cast(E.visit_occurrence_id as bigint) as visit_occurrence_id
FROM
(
-- Begin CDM tables Criteria
select
co.person_id,
co.condition_occurrence_id as event_id,
co.condition_start_date as start_date,
co.condition_end_date as end_date,
co.condition_concept_id as TARGET_CONCEPT_ID,
co.visit_occurrence_id
FROM @target_database_schema.#CKD_codes ckd
JOIN @cdm_database_schema.CONDITION_OCCURRENCE co
on (co.condition_concept_id = ckd.concept_id and ckd.category = 'AKD')
) E
JOIN @cdm_database_schema.observation_period OP on E.person_id = OP.person_id and E.start_date >= OP.observation_period_start_date and E.start_date <= op.observation_period_end_date
WHERE DATEADD(day,0,OP.OBSERVATION_PERIOD_START_DATE) <= E.START_DATE AND DATEADD(day,0,E.START_DATE) <= OP.OBSERVATION_PERIOD_END_DATE
) P
WHERE P.ordinal = 1
-- End Primary Events
)
SELECT event_id, person_id, start_date, end_date, op_start_date, op_end_date, visit_occurrence_id
INTO #qualified_events
FROM
(
select pe.event_id, pe.person_id, pe.start_date, pe.end_date, pe.op_start_date, pe.op_end_date, row_number() over (partition by pe.person_id order by pe.start_date ASC) as ordinal, cast(pe.visit_occurrence_id as bigint) as visit_occurrence_id
FROM primary_events pe
) QE
;