You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
when value_drug is not null then concat(@boundary,o.concept_id,'_drug','=',value_drug,@boundary,@question_separator,@boundary,o.concept_id,'=',value_coded,@boundary)
120
+
when value_coded is not null then concat(@boundary,o.concept_id,'=',value_coded,@boundary)
121
+
when value_numeric is not null then concat(@boundary,o.concept_id,'=',value_numeric,@boundary)
122
+
when value_datetime is not null then concat(@boundary,o.concept_id,'=',date(value_datetime),@boundary)
123
+
-- when value_boolean is not null then concat(@boundary,o.concept_id,'=',value_boolean,@boundary)
124
+
when value_text is not null then concat(@boundary,o.concept_id,'=',value_text,@boundary)
125
+
when value_modifier is not null then concat(@boundary,o.concept_id,'=',value_modifier,@boundary)
126
+
end
127
+
order byo.concept_id,value_coded
128
+
separator ' ## '
129
+
) as obs,
130
+
131
+
group_concat(
132
+
case
133
+
when value_coded is not nullor value_numeric is not nullor value_datetime is not nullor value_text is not nullor value_drug is not nullor value_modifier is not null
134
+
then concat(@boundary,o.concept_id,'=',date(o.obs_datetime),@boundary)
135
+
end
136
+
order byo.concept_id,value_coded
137
+
separator ' ## '
138
+
) as obs_datetimes,
139
+
max(o.date_created) as max_date_created
140
+
141
+
from voided_obs v
142
+
joinamrs.obs o using (encounter_id)
143
+
joinamrs.encounter e using (encounter_id)
144
+
where
145
+
o.encounter_id>1ando.voided=0
146
+
group by encounter_id
147
+
);
148
+
149
+
150
+
# Add back obs sets without encounter_ids with voided obs removed
151
+
replace into flat_obs
152
+
(select
153
+
o.person_id,
154
+
@fake_visit_id :=@fake_visit_id +1,
155
+
min(o.obs_id) +100000000as encounter_id,
156
+
o.obs_datetime,
157
+
99999as encounter_type,
158
+
nullas location_id,
159
+
group_concat(
160
+
case
161
+
when value_drug is not null then concat(@boundary,o.concept_id,'_drug','=',value_drug,@boundary,@question_separator,@boundary,o.concept_id,'=',value_coded,@boundary)
162
+
when value_coded is not null then concat(@boundary,o.concept_id,'=',value_coded,@boundary)
163
+
when value_numeric is not null then concat(@boundary,o.concept_id,'=',value_numeric,@boundary)
164
+
when value_datetime is not null then concat(@boundary,o.concept_id,'=',date(value_datetime),@boundary)
165
+
-- when value_boolean is not null then concat(@boundary,o.concept_id,'=',value_boolean,@boundary)
166
+
when value_text is not null then concat(@boundary,o.concept_id,'=',value_text,@boundary)
167
+
when value_modifier is not null then concat(@boundary,o.concept_id,'=',value_modifier,@boundary)
168
+
end
169
+
order byo.concept_id,value_coded
170
+
separator ' ## '
171
+
) as obs,
172
+
173
+
group_concat(
174
+
case
175
+
when value_coded is not nullor value_numeric is not nullor value_datetime is not nullor value_text is not nullor value_drug is not nullor value_modifier is not null
176
+
then concat(@boundary,o.concept_id,'=',date(o.obs_datetime),@boundary)
177
+
end
178
+
order byo.concept_id,value_coded
179
+
separator ' ## '
180
+
) as obs_datetimes,
181
+
max(o.date_created) as max_date_created
182
+
183
+
from voided_obs v
184
+
joinamrs.obs o using (person_id, obs_datetime)
185
+
where
186
+
o.encounter_id is nulland voided=0
187
+
group by person_id, o.obs_datetime
188
+
);
189
+
190
+
191
+
192
+
# find all encounters which have new obs after @last_update
193
+
droptable if exists encounters_with_new_obs;
194
+
create temporary table encounters_with_new_obs
195
+
(select
196
+
distinct encounter_id
197
+
fromamrs.obs o
198
+
whereo.encounter_id>0
199
+
ando.voided=0
200
+
ando.date_created> @last_update
201
+
);
202
+
203
+
# Insert newly created obs with encounter_ids
204
+
replace into flat_obs
205
+
(select
206
+
o.person_id,
207
+
case
208
+
when e.visit_idis not null then e.visit_id else @fake_visit_id :=@fake_visit_id +1
209
+
end as visit_id,
210
+
o.encounter_id,
211
+
encounter_datetime,
212
+
encounter_type,
213
+
e.location_id,
214
+
group_concat(
215
+
case
216
+
when value_drug is not null then concat(@boundary,o.concept_id,'_drug','=',value_drug,@boundary,@question_separator,@boundary,o.concept_id,'=',value_coded,@boundary)
217
+
when value_coded is not null then concat(@boundary,o.concept_id,'=',value_coded,@boundary)
218
+
when value_numeric is not null then concat(@boundary,o.concept_id,'=',value_numeric,@boundary)
219
+
when value_datetime is not null then concat(@boundary,o.concept_id,'=',date(value_datetime),@boundary)
220
+
-- when value_boolean is not null then concat(@boundary,o.concept_id,'=',value_boolean,@boundary)
221
+
when value_text is not null then concat(@boundary,o.concept_id,'=',value_text,@boundary)
222
+
when value_modifier is not null then concat(@boundary,o.concept_id,'=',value_modifier,@boundary)
223
+
end
224
+
order by concept_id,value_coded
225
+
separator ' ## '
226
+
) as obs,
227
+
228
+
group_concat(
229
+
case
230
+
when value_coded is not nullor value_numeric is not nullor value_datetime is not nullor value_text is not nullor value_drug is not nullor value_modifier is not null
231
+
then concat(@boundary,o.concept_id,'=',date(o.obs_datetime),@boundary)
232
+
end
233
+
order byo.concept_id,value_coded
234
+
separator ' ## '
235
+
) as obs_datetimes,
236
+
max(o.date_created) as max_date_created
237
+
238
+
fromamrs.obs o
239
+
join encounters_with_new_obs e1 using (encounter_id)
240
+
joinamrs.encounter e using (encounter_id)
241
+
whereo.voided=0
242
+
group byo.encounter_id
243
+
);
244
+
245
+
# Insert newly creatred obs without encounter_ids
246
+
replace into flat_obs
247
+
(select
248
+
o.person_id,
249
+
@fake_visit_id :=@fake_visit_id +1as visit_id,
250
+
min(o.obs_id) +100000000as encounter_id,
251
+
o.obs_datetime,
252
+
99999as encounter_type,
253
+
nullas location_id,
254
+
group_concat(
255
+
case
256
+
when value_drug is not null then concat(@boundary,o.concept_id,'_drug','=',value_drug,@boundary,@question_separator,@boundary,o.concept_id,'=',value_coded,@boundary)
257
+
when value_coded is not null then concat(@boundary,o.concept_id,'=',value_coded,@boundary)
258
+
when value_numeric is not null then concat(@boundary,o.concept_id,'=',value_numeric,@boundary)
259
+
when value_datetime is not null then concat(@boundary,o.concept_id,'=',date(value_datetime),@boundary)
260
+
-- when value_boolean is not null then concat(@boundary,o.concept_id,'=',value_boolean,@boundary)
261
+
when value_text is not null then concat(@boundary,o.concept_id,'=',value_text,@boundary)
262
+
when value_modifier is not null then concat(@boundary,o.concept_id,'=',value_modifier,@boundary)
263
+
end
264
+
order by concept_id,value_coded
265
+
separator ' ## '
266
+
) as obs,
267
+
268
+
group_concat(
269
+
case
270
+
when value_coded is not nullor value_numeric is not nullor value_datetime is not nullor value_text is not nullor value_drug is not nullor value_modifier is not null
271
+
then concat(@boundary,o.concept_id,'=',date(o.obs_datetime),@boundary)
272
+
end
273
+
order byo.concept_id,value_coded
274
+
separator ' ## '
275
+
) as obs_datetimes,
276
+
max(o.date_created) as max_date_created
277
+
278
+
fromamrs.obs o use index (date_created)
279
+
where
280
+
o.encounter_id is null
281
+
and voided=0ando.date_created> @last_update
282
+
group by person_id, o.obs_datetime
283
+
);
284
+
285
+
286
+
# Insert obs whose encounters changed after last flat_obs update
287
+
replace into flat_obs
288
+
(select
289
+
o.person_id,
290
+
case
291
+
when e.visit_idis not null then e.visit_id else @fake_visit_id :=@fake_visit_id +1
292
+
end as visit_id,
293
+
o.encounter_id,
294
+
encounter_datetime,
295
+
encounter_type,
296
+
e.location_id,
297
+
group_concat(
298
+
case
299
+
when value_drug is not null then concat(@boundary,o.concept_id,'_drug','=',value_drug,@boundary,@question_separator,@boundary,o.concept_id,'=',value_coded,@boundary)
300
+
when value_coded is not null then concat(@boundary,o.concept_id,'=',value_coded,@boundary)
301
+
when value_numeric is not null then concat(@boundary,o.concept_id,'=',value_numeric,@boundary)
302
+
when value_datetime is not null then concat(@boundary,o.concept_id,'=',date(value_datetime),@boundary)
303
+
-- when value_boolean is not null then concat(@boundary,o.concept_id,'=',value_boolean,@boundary)
304
+
when value_text is not null then concat(@boundary,o.concept_id,'=',value_text,@boundary)
305
+
when value_modifier is not null then concat(@boundary,o.concept_id,'=',value_modifier,@boundary)
306
+
end
307
+
order by concept_id,value_coded
308
+
separator ' ## '
309
+
) as obs,
310
+
311
+
group_concat(
312
+
case
313
+
when value_coded is not nullor value_numeric is not nullor value_datetime is not nullor value_text is not nullor value_drug is not nullor value_modifier is not null
314
+
then concat(@boundary,o.concept_id,'=',date(o.obs_datetime),@boundary)
315
+
end
316
+
order byo.concept_id,value_coded
317
+
separator ' ## '
318
+
) as obs_datetimes,
319
+
max(o.date_created) as max_date_created
320
+
321
+
fromamrs.obs o
322
+
join encounters_with_updated_data e1 using (encounter_id)
323
+
joinamrs.encounter e using (encounter_id)
324
+
whereo.voided=0
325
+
group byo.encounter_id
326
+
);
327
+
328
+
329
+
# remove voided patients
330
+
delete t1
331
+
from flat_obs t1
332
+
joinamrs.person t2 using (person_id)
333
+
wheret2.voided=1;
334
+
335
+
droptable voided_obs;
336
+
337
+
select @end := now();
338
+
insert into flat_log values (@start,@last_date_created,@table_version,timestampdiff(second,@start,@end));
339
+
select concat(@table_version," : Time to complete: ",timestampdiff(minute, @start, @end)," minutes");
0 commit comments