Skip to content

Commit 3e16a2f

Browse files
committed
Update 4e_hesop_map_tbl_visit.sql
Bug sorted: provider_id was never mapped in visit_occurrence/visit_detail
1 parent ba993cd commit 3e16a2f

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

sql_scripts/4e_hesop_map_tbl_visit.sql

+41-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
--------------------------------
2-
-- VISIT_OCCURRENCE FROM hesop_appointment
2+
-- VISIT_OCCURRENCE FROM hesop_appointment, hesop_clinical
33
--------------------------------
44
DROP SEQUENCE IF EXISTS {TARGET_SCHEMA}.sequence_vo;
55
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'));
78

89
with cte1 AS (
910
SELECT person_id
1011
FROM {TARGET_SCHEMA}.person
1112
),
1213
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 (
1327
SELECT
1428
NEXTVAL('{TARGET_SCHEMA}.sequence_vo') AS visit_occurrence_id,
1529
t1.patid AS person_id,
@@ -19,7 +33,7 @@ cte2 AS (
1933
t1.apptdate AS visit_end_date,
2034
t1.apptdate AS visit_end_datetime,
2135
32818 AS visit_type_concept_id,
22-
NULL::bigint AS provider_id,
36+
t2.provider_id,
2337
NULL::int AS care_site_id,
2438
t1.attendkey AS visit_source_value,
2539
NULL::int AS visit_source_concept_id,
@@ -28,13 +42,14 @@ cte2 AS (
2842
NULL::varchar AS discharge_to_source_value,
2943
NULL::int AS discharge_to_concept_id
3044
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
3246
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
3348
),
34-
cte3 AS (
49+
cte5 AS (
3550
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
3853
WHERE t1.visit_occurrence_id > t2.visit_occurrence_id
3954
GROUP BY t1.person_id, t1.visit_occurrence_id
4055
)
@@ -60,8 +75,8 @@ INSERT INTO {TARGET_SCHEMA}.visit_occurrence (
6075
SELECT
6176
t1.*,
6277
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;
6580

6681
DROP SEQUENCE IF EXISTS {TARGET_SCHEMA}.sequence_vo;
6782

@@ -82,6 +97,19 @@ with cte1 AS (
8297
SELECT person_id
8398
FROM {TARGET_SCHEMA}.person
8499
),
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+
--),
85113
cte2 AS (
86114
SELECT
87115
NEXTVAL('{TARGET_SCHEMA}.sequence_vd') AS visit_detail_id,
@@ -92,7 +120,6 @@ cte2 AS (
92120
t1.apptdate AS visit_detail_end_date,
93121
t1.apptdate AS visit_detail_end_datetime,
94122
32818 AS visit_detail_type_concept_id,
95-
NULL::int AS provider_id,
96123
NULL::int AS care_site_id,
97124
t1.attendkey AS visit_detail_source_value,
98125
NULL::int AS visit_detail_source_concept_id,
@@ -103,13 +130,15 @@ cte2 AS (
103130
NULL::int AS preceding_visit_detail_id,
104131
NULL::int AS visit_detail_parent_id
105132
FROM {SOURCE_SCHEMA}.hesop_appointment AS t1
133+
-- INNER JOIN cte3 as t2 ON t1.patid = t2.person_id AND t1.attendkey = t2.attendkey
106134
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
108137
),
109138
cte3 AS (
110139
SELECT
111140
t1.*,
112-
t2.visit_occurrence_id
141+
t2.visit_occurrence_id, t2.provider_id
113142
FROM cte2 as t1
114143
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
115144
),

0 commit comments

Comments
 (0)