1
1
-- ------------------------------
2
- -- VISIT_OCCURRENCE FROM hesop_appointment
2
+ -- VISIT_OCCURRENCE FROM hesop_appointment, hesop_clinical
3
3
-- ------------------------------
4
4
DROP SEQUENCE IF EXISTS {TARGET_SCHEMA}.sequence_vo;
5
5
CREATE SEQUENCE {TARGET_SCHEMA}.sequence_vo INCREMENT 1 ;
6
- SELECT setval(' {TARGET_SCHEMA}.sequence_vo' , (SELECT max_id from {TARGET_SCHEMA_TO_LINK}._max_ids WHERE lower (tbl_name) = ' visit_occurrence' ));
6
+ SELECT setval(' {TARGET_SCHEMA}.sequence_vo' ,
7
+ (SELECT max_id from {TARGET_SCHEMA_TO_LINK}._max_ids WHERE lower (tbl_name) = ' visit_occurrence' ));
7
8
8
9
with cte1 AS (
9
10
SELECT person_id
10
11
FROM {TARGET_SCHEMA}.person
11
12
),
12
13
cte2 AS (
14
+ select t1 .person_id , t2 .attendkey ,
15
+ CASE WHEN t2 .tretspef <> ' &' THEN t2 .tretspef ELSE CASE WHEN t2 .mainspef <> ' &' THEN t2 .mainspef ELSE Null END END as specialty
16
+ FROM cte1 as t1
17
+ INNER JOIN {SOURCE_SCHEMA}.hesop_clinical AS t2 on t1 .person_id = t2 .patid
18
+ ),
19
+ cte3 AS (
20
+ select t1 .person_id , t1 .attendkey , t3 .provider_id
21
+ FROM cte2 as t1
22
+ LEFT JOIN {VOCABULARY_SCHEMA}.source_to_concept_map as t2 on t1 .specialty = t2 .source_code
23
+ and t2 .source_vocabulary_id = ' HES_SPEC_STCM'
24
+ LEFT JOIN {TARGET_SCHEMA}.provider as t3 on t3 .specialty_source_value = t2 .source_code_description
25
+ ),
26
+ cte4 AS (
13
27
SELECT
14
28
NEXTVAL(' {TARGET_SCHEMA}.sequence_vo' ) AS visit_occurrence_id,
15
29
t1 .patid AS person_id,
@@ -19,7 +33,7 @@ cte2 AS (
19
33
t1 .apptdate AS visit_end_date,
20
34
t1 .apptdate AS visit_end_datetime,
21
35
32818 AS visit_type_concept_id,
22
- NULL :: bigint AS provider_id,
36
+ t2 . provider_id ,
23
37
NULL ::int AS care_site_id,
24
38
t1 .attendkey AS visit_source_value,
25
39
NULL ::int AS visit_source_concept_id,
@@ -28,13 +42,14 @@ cte2 AS (
28
42
NULL ::varchar AS discharge_to_source_value,
29
43
NULL ::int AS discharge_to_concept_id
30
44
FROM {SOURCE_SCHEMA}.hesop_appointment AS t1
31
- INNER JOIN cte1 as t2 ON t1 .patid = t2 .person_id
45
+ INNER JOIN cte3 as t2 ON t1 .patid = t2 .person_id AND t1 . attendkey = t2 . attendkey
32
46
WHERE t1 .attended = 5 -- 5 = (Seen, having attended on time or, if late, before the relevant care professional was ready to see the patient)
47
+ ORDER BY t1 .apptdate , t1 .patid , t1 .attendkey
33
48
),
34
- cte3 AS (
49
+ cte5 AS (
35
50
SELECT t1 .person_id , t1 .visit_occurrence_id , MAX (t2 .visit_occurrence_id ) AS preceding_visit_occurrence_id
36
- FROM cte2 AS t1
37
- INNER JOIN cte2 AS t2 ON t1 .person_id = t2 .person_id
51
+ FROM cte4 AS t1
52
+ INNER JOIN cte4 AS t2 ON t1 .person_id = t2 .person_id
38
53
WHERE t1 .visit_occurrence_id > t2 .visit_occurrence_id
39
54
GROUP BY t1 .person_id , t1 .visit_occurrence_id
40
55
)
@@ -60,8 +75,8 @@ INSERT INTO {TARGET_SCHEMA}.visit_occurrence (
60
75
SELECT
61
76
t1.* ,
62
77
t2 .preceding_visit_occurrence_id
63
- FROM cte2 AS t1
64
- LEFT JOIN cte3 AS t2 ON t1 .visit_occurrence_id = t2 .visit_occurrence_id ;
78
+ FROM cte4 AS t1
79
+ LEFT JOIN cte5 AS t2 ON t1 .visit_occurrence_id = t2 .visit_occurrence_id ;
65
80
66
81
DROP SEQUENCE IF EXISTS {TARGET_SCHEMA}.sequence_vo;
67
82
@@ -82,6 +97,19 @@ with cte1 AS (
82
97
SELECT person_id
83
98
FROM {TARGET_SCHEMA}.person
84
99
),
100
+ -- cte2 AS (
101
+ -- select t1.person_id, t2.attendkey,
102
+ -- CASE WHEN t2.tretspef <> '&' THEN t2.tretspef ELSE t2.mainspef END as specialty
103
+ -- FROM cte1 as t1
104
+ -- inner join {SOURCE_SCHEMA}.hesop_clinical AS t2 on t1.person_id = t2.patid
105
+ -- ),
106
+ -- cte3 AS (
107
+ -- select t1.person_id, t1.attendkey, t3.provider_id
108
+ -- FROM cte2 as t1
109
+ -- LEFT JOIN {VOCABULARY_SCHEMA}.source_to_concept_map as t2 on t1.specialty = t2.source_code
110
+ -- and t2.source_vocabulary_id = 'HES_SPEC_STCM'
111
+ -- INNER JOIN {TARGET_SCHEMA}.provider as t3 on t3.specialty_source_value = t2.source_code_description
112
+ -- ),
85
113
cte2 AS (
86
114
SELECT
87
115
NEXTVAL(' {TARGET_SCHEMA}.sequence_vd' ) AS visit_detail_id,
@@ -92,7 +120,6 @@ cte2 AS (
92
120
t1 .apptdate AS visit_detail_end_date,
93
121
t1 .apptdate AS visit_detail_end_datetime,
94
122
32818 AS visit_detail_type_concept_id,
95
- NULL ::int AS provider_id,
96
123
NULL ::int AS care_site_id,
97
124
t1 .attendkey AS visit_detail_source_value,
98
125
NULL ::int AS visit_detail_source_concept_id,
@@ -103,13 +130,15 @@ cte2 AS (
103
130
NULL ::int AS preceding_visit_detail_id,
104
131
NULL ::int AS visit_detail_parent_id
105
132
FROM {SOURCE_SCHEMA}.hesop_appointment AS t1
133
+ -- INNER JOIN cte3 as t2 ON t1.patid = t2.person_id AND t1.attendkey = t2.attendkey
106
134
INNER JOIN cte1 as t2 ON t1 .patid = t2 .person_id
107
- WHERE t1 .attended = 5 -- 5 = (Seen, having attended on time or, if late, before the relevant care professional was ready to see the patient)
135
+ WHERE t1 .attended = 5 -- 5 = (Seen, having attended on time or, if late, before the relevant care professional was ready to see the patient)
136
+ ORDER BY t1 .apptdate , t1 .patid , t1 .attendkey
108
137
),
109
138
cte3 AS (
110
139
SELECT
111
140
t1.* ,
112
- t2 .visit_occurrence_id
141
+ t2 .visit_occurrence_id , t2 . provider_id
113
142
FROM cte2 as t1
114
143
INNER JOIN {TARGET_SCHEMA}.visit_occurrence AS t2 ON t1 .person_id = t2 .person_id and t1 .visit_detail_source_value = t2 .visit_source_value ::bigint
115
144
),
0 commit comments