-
Notifications
You must be signed in to change notification settings - Fork 0
/
assosciation_errors.sql
267 lines (229 loc) · 13.1 KB
/
assosciation_errors.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
------------------------------------------------------------------- Encounter updates --------------------------------------------------------------------------------
set role apfodisha;
SELECT *
FROM public.program_encounter
WHERE observations::text NOT LIKE '%be0ab05f-b0f3-43ec-b598-fdde0679104a%'
AND encounter_type_id IN (1653, 1655, 1656)
AND encounter_date_time NOTNULL; --305 encounters
SELECT *
FROM encounter_type;
-- 1653,ANC
-- 1655,Delivery
-- 1656,Child followup
-- ANCs whose delivery is done
SELECT count(pe1.id)
FROM program_encounter pe1
LEFT JOIN program_encounter pe2 ON pe1.individual_id = pe2.individual_id
WHERE pe2.encounter_type_id = 1655
AND pe1.encounter_type_id = 1653 and pe2.encounter_date_time is not null;
-- 671
SELECT count(id)
FROM public.program_encounter
WHERE observations::text NOT LIKE '%be0ab05f-b0f3-43ec-b598-fdde0679104a%'
AND encounter_type_id IN (1653, 1655)
AND encounter_date_time IS NOT NULL;
-- 292
-- Query to update ANC & Delivery encounter:
-- If the delivery form is filled, then set "High risk" to "No".
-- If the delivery form is not filled and there is any data for the 'ANC' encounter related to the concept "High risk Conditions",
-- then set "High risk" to "Yes".
-- If neither of the above conditions are met, set "High risk" to "No".
WITH anc_whose_delivery_done AS (SELECT pe1.id
FROM program_encounter pe1
LEFT JOIN program_encounter pe2 ON pe1.individual_id = pe2.individual_id
WHERE pe2.encounter_type_id = 1655
AND pe1.encounter_type_id = 1653 and pe2.encounter_date_time is not null)
UPDATE public.program_encounter pe
SET observations = CASE
WHEN pe.id IN (SELECT id FROM anc_whose_delivery_done) THEN
observations ||
'{"be0ab05f-b0f3-43ec-b598-fdde0679104a":["a77bd700-1409-4d52-93bc-9fe32c0e169b"]}' -- high risk no
WHEN pe.observations -> '9a7f284b-251d-459b-97d9-929ed280b3d3' IS NOT NULL THEN
observations ||
'{"be0ab05f-b0f3-43ec-b598-fdde0679104a":["8ebbf088-f292-483e-9084-7de919ce67b7"]}' -- high risk yes
ELSE
observations ||
'{"be0ab05f-b0f3-43ec-b598-fdde0679104a":["aa77bd700-1409-4d52-93bc-9fe32c0e169b"]}' -- high risk no
END,
last_modified_date_time = now(),
last_modified_by_id = (select id from users where username ='nupoork@apfodisha'),
manual_update_history = 'Fixing missing high risk sync attributes'
WHERE pe.id IN (SELECT id
FROM public.program_encounter
WHERE observations::text NOT LIKE '%be0ab05f-b0f3-43ec-b598-fdde0679104a%'
AND encounter_type_id IN (1653, 1655)
AND encounter_date_time IS NOT NULL);
-- 292 rows affected
-- Query to update child follow-up encounter:
-- If a child's 'Weight for Height Status' is 'SAM' (OR) their 'Weight for Age Status' is 'Severely Underweight',
-- then record an observation for 'High risk' as 'Yes'. Otherwise, record 'High risk' as 'No'.
UPDATE public.program_encounter AS pe
SET observations = CASE
WHEN pe.observations -> 'efeb0a0b-aea4-4af1-9bc4-37d86bc865a1' =
'["4b855734-921c-4796-a752-39b3ede1c66c"]'
OR pe.observations -> '2a2027c1-cec9-4237-a53a-80d6b1047979' =
'["7d0229c1-e69e-4cac-8937-cdd48c8ed9dd"]'
THEN
observations ||
'{"be0ab05f-b0f3-43ec-b598-fdde0679104a":["8ebbf088-f292-483e-9084-7de919ce67b7"]}'
ELSE
observations ||
'{"be0ab05f-b0f3-43ec-b598-fdde0679104a":["a77bd700-1409-4d52-93bc-9fe32c0e169b"]}'
END,
last_modified_date_time = now(),
last_modified_by_id = (select id from users where username ='nupoork@apfodisha'),
manual_update_history = 'Fixing missing high risk sync attributes'
WHERE pe.id IN (SELECT id
FROM public.program_encounter
WHERE observations::text NOT LIKE '%be0ab05f-b0f3-43ec-b598-fdde0679104a%'
AND encounter_type_id = 1656
AND encounter_date_time IS NOT NULL);
-- 13
------------------------------------------------------------------- Encounter updates end--------------------------------------------------------------------------------
------------------------------------------------------------------- Enrolment updates --------------------------------------------------------------------------------
set role apfodisha;
-- Observations that don't have high risk status in enrolments.
select count(*)
from public.program_enrolment
where observations::text not like '%be0ab05f-b0f3-43ec-b598-fdde0679104a%'; -- 1665 enrolments
select * from program;
-- 489 Child
-- 488 Pregnancy
-- Query to update Pregnancy enrolment:
WITH LatestEncounter AS (SELECT enl.id,
coalesce(pe.observations -> 'be0ab05f-b0f3-43ec-b598-fdde0679104a',
null) AS high_risk_value,
ROW_NUMBER()
OVER (PARTITION BY enl.id ORDER BY coalesce(pe.encounter_date_time, enl.enrolment_date_time) DESC NULLS LAST) AS rn
FROM public.program_enrolment enl
left join program_encounter pe
on enl.id = pe.program_enrolment_id and pe.encounter_type_id = 1653
)
UPDATE public.program_enrolment enl
SET observations =
CASE
-- If there's a corresponding ANC encounter, use its High risk value
WHEN le.high_risk_value IS NOT NULL THEN
enl.observations ||
JSONB_SET(enl.observations, '{"be0ab05f-b0f3-43ec-b598-fdde0679104a"}', high_risk_value)
-- If there's no ANC but there's a Red Flag in enrolment, set High risk to what is there in Red Flag observation
WHEN enl.observations ->> '72f8785c-f064-4549-ab45-47defa40f5fb' IS NOT NULL
and enl.observations ->> '72f8785c-f064-4549-ab45-47defa40f5fb' = '"8ebbf088-f292-483e-9084-7de919ce67b7"'
then
enl.observations ||
'{"be0ab05f-b0f3-43ec-b598-fdde0679104a":["8ebbf088-f292-483e-9084-7de919ce67b7"]}'
-- Otherwise, set High risk to no
ELSE
enl.observations ||
'{"be0ab05f-b0f3-43ec-b598-fdde0679104a":["a77bd700-1409-4d52-93bc-9fe32c0e169b"]}'
END,
last_modified_date_time = now(),
last_modified_by_id = (select id from users where username = 'nupoork@apfodisha'),
manual_update_history = 'Fixing missing high risk sync attributes'
FROM LatestEncounter le
WHERE enl.id = le.id
AND le.rn = 1
AND enl.observations::text not like '%be0ab05f-b0f3-43ec-b598-fdde0679104a%'
and program_id =488;
-- 1,665 rows affected
-- Query to update Child enrolment:
WITH LatestEncounter AS (SELECT pe.program_enrolment_id,
pe.observations -> 'be0ab05f-b0f3-43ec-b598-fdde0679104a' AS high_risk_value,
ROW_NUMBER()
OVER (PARTITION BY pe.program_enrolment_id ORDER BY pe.encounter_date_time DESC NULLS LAST) AS rn
FROM public.program_encounter pe
WHERE pe.encounter_type_id = 1656)
UPDATE public.program_enrolment enl
SET observations =
CASE
-- If there's a corresponding child followup encounter, use its High risk value
WHEN le.high_risk_value IS NOT NULL THEN
enl.observations ||
JSONB_SET(enl.observations, '{"be0ab05f-b0f3-43ec-b598-fdde0679104a"}', high_risk_value)
-- Otherwise, set High risk to no
ELSE
enl.observations || '{"be0ab05f-b0f3-43ec-b598-fdde0679104a":["a77bd700-1409-4d52-93bc-9fe32c0e169b"]}'
END,
last_modified_date_time = now(),
last_modified_by_id = (select id from users where username ='nupoork@apfodisha'),
manual_update_history = 'Fixing missing high risk sync attributes'
FROM LatestEncounter le
WHERE enl.id = le.program_enrolment_id
AND le.rn = 1
AND enl.observations::text not like '%be0ab05f-b0f3-43ec-b598-fdde0679104a%'
and program_id =489;
------------------------------------------------------------------- Enrolment update end --------------------------------------------------------------------------------
------------------------------------------------------------------- Individual update --------------------------------------------------------------------------------
set role apfodisha;
-- Query to update individual table:
UPDATE public.individual AS ind
SET observations =
CASE
-- If there's a corresponding program enrolment, use its High risk value
WHEN pe.observations ->> 'be0ab05f-b0f3-43ec-b598-fdde0679104a' IS NOT NULL THEN
ind.observations || JSONB_SET(ind.observations, '{"be0ab05f-b0f3-43ec-b598-fdde0679104a"}',
pe.observations -> 'be0ab05f-b0f3-43ec-b598-fdde0679104a')
-- If there's no program enrolment, set High risk to no
ELSE
ind.observations || '{"be0ab05f-b0f3-43ec-b598-fdde0679104a":["a77bd700-1409-4d52-93bc-9fe32c0e169b"]}'
END,
last_modified_date_time = now(),
last_modified_by_id = (select id from users where username ='nupoork@apfodisha'),
manual_update_history = 'Fixing missing high risk sync attributes'
FROM public.program_enrolment pe
WHERE ind.id = pe.individual_id
AND subject_type_id = 672
AND ind.observations::text not like '%be0ab05f-b0f3-43ec-b598-fdde0679104a%';
------------------------------------------------------------------- Individual update end--------------------------------------------------------------------------------
------------------------------------------------------------------- Sync concpet1 value update --------------------------------------------------------------------------------
set role apfodisha;
-- Query to update sync_concept_1_value column in the individual table
UPDATE public.individual
SET sync_concept_1_value =
CASE
WHEN observations ->> 'be0ab05f-b0f3-43ec-b598-fdde0679104a' = 'a77bd700-1409-4d52-93bc-9fe32c0e169b'
THEN 'a77bd700-1409-4d52-93bc-9fe32c0e169b'
WHEN observations ->> 'be0ab05f-b0f3-43ec-b598-fdde0679104a' = '8ebbf088-f292-483e-9084-7de919ce67b7'
THEN '8ebbf088-f292-483e-9084-7de919ce67b7'
END,
last_modified_date_time = now(),
last_modified_by_id = (select id from users where username ='nupoork@apfodisha'),
manual_update_history = 'Fixing missing high risk sync attributes'
WHERE subject_type_id = 672
AND observations ->> 'be0ab05f-b0f3-43ec-b598-fdde0679104a' IS NOT NULL;
-- 4772
-- Query to update sync_concept_1_value column in the program enrolment table
UPDATE public.program_enrolment e
SET sync_concept_1_value =
CASE
WHEN i.observations ->> 'be0ab05f-b0f3-43ec-b598-fdde0679104a' = 'a77bd700-1409-4d52-93bc-9fe32c0e169b'
THEN 'a77bd700-1409-4d52-93bc-9fe32c0e169b'
WHEN i.observations ->> 'be0ab05f-b0f3-43ec-b598-fdde0679104a' = '8ebbf088-f292-483e-9084-7de919ce67b7'
THEN '8ebbf088-f292-483e-9084-7de919ce67b7'
END,
last_modified_date_time = now(),
last_modified_by_id = (select id from users where username ='nupoork@apfodisha'),
manual_update_history = 'Fixing missing high risk sync attributes'
FROM public.individual i
WHERE i.id = e.individual_id
and subject_type_id = 672
AND i.observations ->> 'be0ab05f-b0f3-43ec-b598-fdde0679104a' IS NOT NULL;
-- 2,400
-- Query to update sync_concept_1_value column in the program encounter table
UPDATE public.program_encounter enc
SET sync_concept_1_value =
CASE
WHEN i.observations ->> 'be0ab05f-b0f3-43ec-b598-fdde0679104a' = 'a77bd700-1409-4d52-93bc-9fe32c0e169b'
THEN 'a77bd700-1409-4d52-93bc-9fe32c0e169b'
WHEN i.observations ->> 'be0ab05f-b0f3-43ec-b598-fdde0679104a' = '8ebbf088-f292-483e-9084-7de919ce67b7'
THEN '8ebbf088-f292-483e-9084-7de919ce67b7'
END,
last_modified_date_time = now(),
last_modified_by_id = (select id from users where username ='nupoork@apfodisha'),
manual_update_history = 'Fixing missing high risk sync attributes'
FROM public.individual i
WHERE i.id = enc.individual_id
and subject_type_id = 672
AND i.observations ->> 'be0ab05f-b0f3-43ec-b598-fdde0679104a' IS NOT NULL;
-- 5526
------------------------------------------------------------------- Sync concpet1 value update end--------------------------------------------------------------------------------