-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpediatric_visit_Migration.sql
2111 lines (1872 loc) · 127 KB
/
pediatric_visit_Migration.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
drop procedure if exists pediatric_visit_Migration;
DELIMITER $$
CREATE PROCEDURE pediatric_visit_Migration()
BEGIN
DECLARE done INT DEFAULT FALSE;
-- DECLARE a CHAR(16);
DECLARE vstatus boolean;
DECLARE vvisit_type_id INT;
DECLARE obs_datetime_,vobs_datetime,vdate_created,vencounter_datetime datetime;
DECLARE vobs_id,vperson_id,vconcept_id,vencounter_id,vlocation_id INT;
DECLARE uuid_encounter CURSOR for SELECT DISTINCT e.patientID,e.encounter_id FROM itech.encounter e;
/* L'enfant est-il au courant de son statut VIH?*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163524,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedChildAware=1 THEN 1065
WHEN v.pedChildAware=2 THEN 1066
WHEN v.pedChildAware=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedChildAware in (1,2,4);
select 1 as stVih;
/* Le parent/substitut est-il au courant du statut VIH?*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163525,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedParentAware=1 THEN 1065
WHEN v.pedParentAware=2 THEN 1066
WHEN v.pedParentAware=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d')= concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedParentAware in (1,2,4);
select 2 as stVih;
/* ANTÉCÉDENTS MÉDICAUX DE LA MÈRE BIOLOGIQUE */
/* Le parent/substitut est-il au courant du statut VIH?*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,1856,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedParentAware=1 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedMotherHistUnk=1;
select 3 as stVih;
/*concept group */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,160593,e.encounter_id,e.encounter_datetime,e.location_id,1,e.date_created,UUID()
FROM itech.encounter c, encounter e,itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
(FindNumericValue(pedMotherHistDobYy)>0 or
FindNumericValue(pedMotherHistRecentTb)>0 or
FindNumericValue(pedMotherHistActiveTb)>0 or
FindNumericValue(pedMotherHistTreatTb)>0 or
FindNumericValue(pedMotherHistTreatTbYy)>0);
select 5 as trt1;
delete from itech.obs_concept_group where 1;
INSERT INTO itech.obs_concept_group (obs_id,person_id,concept_id,encounter_id)
SELECT MAX(openmrs.obs.obs_id) as obs_id,openmrs.obs.person_id,openmrs.obs.concept_id,openmrs.obs.encounter_id
FROM openmrs.obs
WHERE openmrs.obs.concept_id=160593
GROUP BY openmrs.obs.person_id,encounter_id;
/*migration for Mere*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,1560,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,970,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d')= concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedMotherHistDobYy)>0;
select 5 as trt2;
/* Date de naissance de la mère*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_datetime,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,160751,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
formatDate(v.pedMotherHistDobYy,v.pedMotherHistDobMm,pedMotherHistDobDd),1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedMotherHistDobYy)>0;
select 5 as trt3;
/* migration TB récente*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,160592,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN v.pedMotherHistRecentTb>0 THEN 42
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedMotherHistRecentTb)>0;
select 5 as trt4;
/* migration TB active (avec crachats positifs)*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,160592,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN v.pedMotherHistRecentTb>0 THEN 113489
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedMotherHistActiveTb)>0;
select 5 as trt5;
/* migration Si TB active, traitement TB en cours*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,160592,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN v.pedMotherHistTreatTb=1 THEN 1065
WHEN v.pedMotherHistTreatTb=2 THEN 1066
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedMotherHistTreatTb)>0;
select 5 as trt6;
/* migration Date de début de traitment*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_datetime,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163526,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
formatDate(v.pedMotherHistTreatTbYy,v.pedMotherHistTreatTbMm,v.pedMotherHistTreatTbDd),1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedMotherHistTreatTbYy)>0;
select 5 as trt;
/*migration de grossesse */
/*concept group */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163770,e.encounter_id,e.encounter_datetime,e.location_id,1,e.date_created,UUID()
FROM itech.encounter c, encounter e,itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
(FindNumericValue(pedMotherHistGrosGrav)>0 or
FindNumericValue(pedMotherHistGrosPara)>0 or
FindNumericValue(pedMotherHistGrosAbor)>0 or
FindNumericValue(pedMotherHistGrosViva)>0);
delete from itech.obs_concept_group where 1;
INSERT INTO itech.obs_concept_group (obs_id,person_id,concept_id,encounter_id)
SELECT MAX(openmrs.obs.obs_id) as obs_id,openmrs.obs.person_id,openmrs.obs.concept_id,openmrs.obs.encounter_id
FROM openmrs.obs
WHERE openmrs.obs.concept_id=163770
GROUP BY openmrs.obs.person_id,encounter_id;
/* gravida*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_numeric,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,5624,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN FindNumericValue(v.pedMotherHistGrosGrav)>=0 THEN FindNumericValue(v.pedMotherHistGrosGrav)
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedMotherHistGrosGrav)>0;
/* Para*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_numeric,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,1053,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN FindNumericValue(v.pedMotherHistGrosPara)>0 THEN FindNumericValue(v.pedMotherHistGrosPara)
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedMotherHistGrosPara)>0;
/* Aborta*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_numeric,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,1823,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN FindNumericValue(v.pedMotherHistGrosAbor)>0 THEN FindNumericValue(v.pedMotherHistGrosAbor)
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedMotherHistGrosAbor)>0;
/* Enfant Vivant*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_numeric,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,1825,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN FindNumericValue(v.pedMotherHistGrosViva)>0 THEN FindNumericValue(v.pedMotherHistGrosViva)
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedMotherHistGrosViva)>0;
select 1 as antecedent;
/* END of ANTÉCÉDENTS MÉDICAUX DE LA MÈRE BIOLOGIQUE */
/* migration mort infantiles */
/*concept group */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163528,e.encounter_id,e.encounter_datetime,e.location_id,1,e.date_created,UUID()
FROM itech.encounter c, encounter e,itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
(digits(v.pedMotherHistGrosDeadAge1)>0 or digits(v.pedMotherHistGrosDeadCause1)>0 or digits(v.pedMotherHistGrosDeadUnk1)>0 or
digits(v.pedMotherHistGrosDeadAge2)>0 or digits(v.pedMotherHistGrosDeadCause2)>0 or digits(v.pedMotherHistGrosDeadUnk2)>0 or
digits(v.pedMotherHistGrosDeadAge3)>0 or digits(v.pedMotherHistGrosDeadCause3)>0 or digits(v.pedMotherHistGrosDeadUnk3)>0
);
delete from itech.obs_concept_group where 1;
INSERT INTO itech.obs_concept_group (obs_id,person_id,concept_id,encounter_id)
SELECT MAX(openmrs.obs.obs_id) as obs_id,openmrs.obs.person_id,openmrs.obs.concept_id,openmrs.obs.encounter_id
FROM openmrs.obs
WHERE openmrs.obs.concept_id=163528
GROUP BY openmrs.obs.person_id,encounter_id;
/* age du deces*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_numeric,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163527,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN FindNumericValue(v.pedMotherHistGrosDeadAge1)>0 THEN FindNumericValue(v.pedMotherHistGrosDeadAge1)
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedMotherHistGrosDeadAge1)>0;
/*----------------------------------------------------------------*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_numeric,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163527,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN FindNumericValue(v.pedMotherHistGrosDeadAge2)>0 THEN FindNumericValue(v.pedMotherHistGrosDeadAge2)
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedMotherHistGrosDeadAge2)>0;
/*----------------------------------------------------------------*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_numeric,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163527,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN FindNumericValue(v.pedMotherHistGrosDeadAge3)>0 THEN FindNumericValue(v.pedMotherHistGrosDeadAge3)
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedMotherHistGrosDeadAge3)>0;
select 1 as Age;
/* cause du deces*/
/*INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_text,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,159482,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN v.pedMotherHistGrosDeadCause1>0 THEN v.pedMotherHistGrosDeadCause1
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date(e.encounter_datetime) = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
(v.pedMotherHistGrosDeadCause1>0);
*/
select 1 as Cause1;
/*----------------------------------------------------------------*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_text,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,159482,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN v.pedMotherHistGrosDeadCause2<>'' THEN v.pedMotherHistGrosDeadCause2
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedMotherHistGrosDeadCause2<>'';
select 1 as Cause2;
/*----------------------------------------------------------------*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_text,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,159482,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN v.pedMotherHistGrosDeadCause3<>'' THEN v.pedMotherHistGrosDeadCause3
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
(v.pedMotherHistGrosDeadCause3)<>'';
select 1 as Cause3;
/* Histoire d'IST pendant la grossesse?*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,159482,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedMotherHistGrosIst=1 THEN 1065
WHEN v.pedMotherHistGrosIst=2 THEN 1066
WHEN v.pedMotherHistGrosIst=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
(v.pedMotherHistGrosIst>0);
/* Si Oui, préciser le trimestre */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163772,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedMotherHistGrosIstTri=1 THEN 1721
WHEN v.pedMotherHistGrosIstTri=2 THEN 1722
WHEN v.pedMotherHistGrosIstTri=4 THEN 1723
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
(v.pedMotherHistGrosIst>0 and pedMotherHistGrosIstTri>0);
/* soins prenatal */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163773,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedMotherHistGrosPreCare=1 THEN 1065
WHEN v.pedMotherHistGrosPreCare=2 THEN 1066
WHEN v.pedMotherHistGrosPreCare=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
(v.pedMotherHistGrosPreCare>0);
/* Si Oui, préciser lieu des soins */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_text,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163529,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedMotherHistGrosPreCareSite<>'' THEN v.pedMotherHistGrosPreCareSite
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
(v.pedMotherHistGrosPreCareSite<>'');
/* No. du dossier médical de la mère */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_text,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163530,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedMotherHistGrosPreCareNum<>'' THEN v.pedMotherHistGrosPreCareNum
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d')= concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
(v.pedMotherHistGrosPreCareNum<>'');
select 1 as method;
/* Méthode d'accouchement */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,5630,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedMotherHistGrosMeth=1 THEN 1170
WHEN v.pedMotherHistGrosMeth=2 THEN 1171
WHEN v.pedMotherHistGrosMeth=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
(v.pedMotherHistGrosMeth in (1,2,4));
/* Lieu d'accouchement */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163774,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedMotherHistGrosWhere=1 THEN 1536
WHEN v.pedMotherHistGrosWhere=2 THEN 1589
WHEN v.pedMotherHistGrosWhere=4 THEN 5622
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
(v.pedMotherHistGrosWhere in (1,2,4));
/* Precisez Lieu d'accouchement */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_text,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163531,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedMotherHistGrosWhereOther<>'' THEN v.pedMotherHistGrosWhereOther
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
(v.pedMotherHistGrosWhereOther<>'');
/* Statut VIH */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,1396,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedMotherHistHivStat=1 THEN 663
WHEN v.pedMotherHistHivStat=2 THEN 703
WHEN v.pedMotherHistHivStat=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedMotherHistHivStat in (1,2,4);
/*Mère décédée du SIDA/probablement du SIDA*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,5590,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedMotherHistHivStat=8 THEN 1065
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedMotherHistHivStat=8;
/*Fait partie d'un programme PTME*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163776,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedMotherHistHivPmtct=1 THEN 1065
WHEN v.pedMotherHistHivPmtct=2 THEN 1066
WHEN v.pedMotherHistHivPmtct=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedMotherHistHivPmtct in (1,2,4);
/*Fait partie d'un programme PTME si oui precisez */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_text,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163779,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedMotherHistHivPmtctWhere<>'' THEN v.pedMotherHistHivPmtctWhere
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedMotherHistHivPmtctWhere<>'';
/* Prophylaxie ARV pendant la grossesse */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163780,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedMotherHistHivArvPreg=1 THEN 1065
WHEN v.pedMotherHistHivArvPreg=2 THEN 1066
WHEN v.pedMotherHistHivArvPreg=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedMotherHistHivArvPreg in (1,2,4);
/* Debut Prophylaxie ARV pendant la grossesse */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_datetime,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163781,e.encounter_id,e.encounter_datetime,e.location_id,
formatDate(v.pedMotherHistHivArvPregStartYy,v.pedMotherHistHivArvPregStartMm,v.pedMotherHistHivArvPregStartDd),1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedMotherHistHivArvPreg=1 and FindNumericValue(v.pedMotherHistHivArvPregStartYy)>0;
/* Fin Prophylaxie ARV pendant la grossesse */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_datetime,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163782,e.encounter_id,e.encounter_datetime,e.location_id,
formatDate(v.pedMotherHistHivArvPregStopYy,v.pedMotherHistHivArvPregStopMm,v.pedMotherHistHivArvPregStopDd),1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedMotherHistHivArvPreg=1 and FindNumericValue(v.pedMotherHistHivArvPregStopYy)>0;
/* Trithérapie pendant la grossesse*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163783,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedMotherHistHivHaartPreg=1 THEN 1065
WHEN v.pedMotherHistHivHaartPreg=2 THEN 1066
WHEN v.pedMotherHistHivHaartPreg=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedMotherHistHivHaartPreg in (1,2,4);
/* Debut Trithérapie pendant la grossesse */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_datetime,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163784,e.encounter_id,e.encounter_datetime,e.location_id,
formatDate(v.pedMotherHistHivHaartPregStYy,v.pedMotherHistHivHaartPregStMm,v.pedMotherHistHivHaartPregStDd),1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedMotherHistHivArvPreg=1 and FindNumericValue(v.pedMotherHistHivHaartPregStYy)>0;
/* Fin Trithérapie pendant la grossesse */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_datetime,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163785,e.encounter_id,e.encounter_datetime,e.location_id,
formatDate(v.pedMotherHistHivHaartPregSpYy,v.pedMotherHistHivHaartPregSpMm,pedMotherHistHivHaartPregSpDd),1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedMotherHistHivArvPreg=1 and FindNumericValue(v.pedMotherHistHivHaartPregSpYy)>0;
/* Prophylaxie ARV pendant l'accouchement */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,159595,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedMotherHistHivArvDel=1 THEN 1065
WHEN v.pedMotherHistHivArvDel=2 THEN 1066
WHEN v.pedMotherHistHivArvDel=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedMotherHistHivArvDel in (1,2,4);
/* préciser régime ARV */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,comments,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163786,e.encounter_id,e.encounter_datetime,e.location_id,5622,v.pedMotherHistHivReg,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedMotherHistHivReg<>'';
select 1 as grossesse;
/* ANTÉCÉDENTS MÉDICAUX DE LA FRATRIE */
/*Inconnus, passer à la section Prophylaxie du Nouveau Né et de l'Enfant.*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163533,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedFratHistUnk=1 THEN 1065
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d')= concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedFratHistUnk=1;
select 1 as antecedentMed;
/* statut TB*/
/* concept group*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,160593,e.encounter_id,e.encounter_datetime,e.location_id,1,e.date_created,UUID()
FROM itech.encounter c, encounter e,itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
(v.pedFratHistRecentTb>0 or pedFratHistActiveTb>0 or pedFratHistTreatTb>0 or pedFratHistTreatTbYy>0);
delete from itech.obs_concept_group where 1;
INSERT INTO itech.obs_concept_group (obs_id,person_id,concept_id,encounter_id)
SELECT MAX(openmrs.obs.obs_id) as obs_id,openmrs.obs.person_id,openmrs.obs.concept_id,openmrs.obs.encounter_id
FROM openmrs.obs
WHERE openmrs.obs.concept_id=160593
GROUP BY openmrs.obs.person_id,encounter_id;
select 1 as statuttb;
/* TB récente */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,160592,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN v.pedFratHistRecentTb=1 THEN 42
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedFratHistRecentTb=1;
select 1 as tbRecent1;
/* ----------------------------------------------------------------------------------------------------------------------*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,1560,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,972,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedFratHistRecentTb=1;
select 1 as tbRecent;
/* Si TB active, traitement TB en cours */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,160749,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN v.pedFratHistActiveTb=1 THEN 1065
WHEN v.pedFratHistActiveTb=2 THEN 1066
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedFratHistActiveTb in (1,2);
/* TB active (avec crachats positifs) */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,obs_group_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,160592,e.encounter_id,e.encounter_datetime,e.location_id,og.obs_id,
CASE WHEN v.pedFratHistTreatTb=1 THEN 113489
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v ,itech.obs_concept_group og
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
og.person_id=e.patient_id and e.encounter_id=og.encounter_id and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedFratHistTreatTb=1;
/* Date de début de traitment */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_datetime,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163526,e.encounter_id,e.encounter_datetime,e.location_id,
formatDate(v.pedFratHistTreatTbYy,v.pedFratHistTreatTbMm,v.pedFratHistTreatTbDd),1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedFratHistTreatTb=1 and FindNumericValue(v.pedFratHistTreatTbYy)>0;
select 1 as statut;
/* VIH /SIDA*/
/* Status du père */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,1397,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedFratHistHivStat=1 THEN 664
WHEN v.pedFratHistHivStat=2 THEN 703
WHEN v.pedFratHistHivStat=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedFratHistHivStat in (1,2,4);
/* Père décédé du SIDA/probablement du SIDA */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,5591,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedFratHistHivStat=8 THEN 1065
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedFratHistHivStat=8;
/* Status des Soeurs et freres */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,5587,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedFratHistHivStatFrat=1 THEN 1065
WHEN v.pedFratHistHivStatFrat=2 THEN 1066
WHEN v.pedFratHistHivStatFrat=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedFratHistHivStatFrat in (1,2,4);
/* Soeurs et freres décédé du SIDA/probablement du SIDA */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163534,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedFratHistHivStatFrat=8 THEN 1065
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedFratHistHivStatFrat=8;
/* Nombre de cas par statut VIH (fratrie)*/
/* VIH NEGATIF */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_numeric,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163535,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN FindNumericValue(v.pedFratHistHivStatNumNeg)>0 THEN FindNumericValue(v.pedFratHistHivStatNumNeg)
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedFratHistHivStatNumNeg)>0;
/* VIH POSITIF */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_numeric,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,159906,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN FindNumericValue(v.pedFratHistHivStatNumPos)>0 THEN FindNumericValue(v.pedFratHistHivStatNumPos)
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedFratHistHivStatNumPos)>0;
/* VIH INCONU */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_numeric,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163536,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN FindNumericValue(v.pedFratHistHivStatNumUnk)>0 THEN FindNumericValue(v.pedFratHistHivStatNumUnk)
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedFratHistHivStatNumUnk)>0;
/* Frères ou soeurs décédés du SIDA */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_numeric,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163537,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN FindNumericValue(v.pedFratHistHivStatNumDead)>0 THEN FindNumericValue(v.pedFratHistHivStatNumDead)
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedFratHistHivStatNumDead)>0;
select 1 as vih;
/* PROPHYLAXIE DU NOUVEAU NÉ ET DE L'ENFANT */
/*A reçu prophylaxie ARV dans les 72 heures suivant la naissance*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,5665,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedInfProArv=1 THEN 1065
WHEN v.pedInfProArv=2 THEN 1066
WHEN v.pedInfProArv=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d')= concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedInfProArv in (1,2,4);
/* A reçu prophylaxie contre PCP */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163538,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedInfProPcp=1 THEN 1065
WHEN v.pedInfProPcp=2 THEN 1066
WHEN v.pedInfProPcp=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedInfProPcp in (1,2,4);
/* A reçu prophylaxie contre MAC */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163539,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedInfProMac=1 THEN 1065
WHEN v.pedInfProMac=2 THEN 1066
WHEN v.pedInfProMac=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.pedHistory v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedInfProMac in (1,2,4);
/* */
/*COMPTE CD4/VIRÉMIE*/
/* Dernier compte CD4 */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_numeric,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163542,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN FindNumericValue(v.pedCd4CntPerc)>0 THEN FindNumericValue(v.pedCd4CntPerc)
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedCd4CntPerc)>0;
/* Date Dernier compte CD4 */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_datetime,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163526,e.encounter_id,e.encounter_datetime,e.location_id,
formatDate(v.lowestCd4CntYy,v.lowestCd4CntMm,v.lowestCd4CntDd),1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedCd4CntPerc>0 and FindNumericValue(v.lowestCd4CntYy)>0;
/*Non effectué,Inconn */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163544,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.lowestCd4CntNotDone=1 THEN 1066
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.lowestCd4CntNotDone)>0;
/* Virémie la plus récente*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_numeric,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163545,e.encounter_id,formatDate(v.firstViralLoadYy,v.firstViralLoadMm,firstViralLoadDd),e.location_id,
CASE WHEN FindNumericValue(v.firstViralLoad)>0 THEN FindNumericValue(v.firstViralLoad)
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.firstViralLoad)>0 and FindNumericValue(v.firstViralLoadYy)>0;
/*Non effectué,Inconn */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163546,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.firstViralLoadNotDone=1 THEN 1066
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.firstViralLoadNotDone)>0;
/* ÉVALUATION GYNÉCOLOGIQUE (ADOLESCENTE EN ÂGE DE PROCRÉER) */
/*Menstruations*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,160600,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedReproHealthMenses=1 THEN 1065
WHEN v.pedReproHealthMenses=2 THEN 1066
WHEN v.pedReproHealthMenses=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedReproHealthMenses in (1,2,4);
/*Dernières règles*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_datetime,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,1427,e.encounter_id,e.encounter_datetime,e.location_id,
formatDate(v.pregnantLmpYy,v.pregnantLmpMm,v.pregnantLmpDd),1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pregnantLmpYy)>0;
/*Grossesse*/
/*
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,5272,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pregnant=1 THEN 1065
WHEN v.pregnant=2 THEN 1066
WHEN v.pregnant=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d')= concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pregnant in (1,2,4);
*/
/*Si Oui, suivie en clinique prénatale?*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,1622,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pregnantPrenatal=1 THEN 1065
WHEN v.pregnantPrenatal=2 THEN 1066
WHEN v.pregnantPrenatal=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d')= concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pregnantPrenatal in (1,2,4);
/*Date de première visite de suivi prénatale*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_datetime,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163547,e.encounter_id,e.encounter_datetime,e.location_id,
formatDate(v.pregnantPrenatalFirstYy,v.pregnantPrenatalFirstMm,v.pregnantPrenatalFirstDd),1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pregnantPrenatalFirstYy)>0;
/*Date de dernière visite de suivi prénatale*/
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_datetime,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,159590,e.encounter_id,e.encounter_datetime,e.location_id,
formatDate(v.pregnantPrenatalLastYy,v.pregnantPrenatalLastMm,v.pregnantPrenatalLastDd),1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pregnantPrenatalLastYy)>0;
/* Pap Test */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163952,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.papTest=1 THEN 1267
WHEN v.papTest=2 THEN 1118
WHEN v.papTest=4 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d')= concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.papTest in (1,2,4);
/* Si Oui, résultat */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,885,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedPapTestRes=1 THEN 1115
WHEN v.pedPapTestRes=2 THEN 1116
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedPapTestRes in (1,2);
/* Si Oui, date du dernier test */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_datetime,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163267,e.encounter_id,e.encounter_datetime,e.location_id,
formatDate(v.papTestYy,v.papTestMm,v.papTestDd),1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d')= concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.papTestYy)>0;
/* ALIMENTATION DE L'ENFANT */
/* Allaitement exclusif */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,5526,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedFeedBreast=1 THEN 163720
WHEN v.pedFeedBreast=2 THEN 163721
WHEN v.pedFeedBreast=4 THEN 1090
WHEN v.pedFeedBreast=8 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedFeedBreast in (1,2,4,8);
/* Âge au sevrage (en mois) */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_numeric,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163548,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN FindNumericValue(v.pedFeedBreastAge)>0 THEN FindNumericValue(v.pedFeedBreastAge)
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
FindNumericValue(v.pedFeedBreastAge)>0;
/* Lait artificiel */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_coded,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,5254,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN v.pedFeedFormula=1 THEN 163720
WHEN v.pedFeedFormula=2 THEN 163721
WHEN v.pedFeedFormula=4 THEN 1090
WHEN v.pedFeedFormula=8 THEN 1067
ELSE NULL
END,1,e.date_created,UUID()
FROM itech.encounter c, encounter e, itech.vitals v
WHERE e.uuid = c.encGuid and c.patientID = v.patientID and c.seqNum = v.seqNum and
c.sitecode = v.sitecode and date_format(date(e.encounter_datetime),'%y-%m-%d') = concat(v.visitDateYy,'-',v.visitDateMm,'-',v.visitDateDd) AND
v.pedFeedFormula in (1,2,4,8);
/* Âge au sevrage (en mois) */
INSERT INTO obs(person_id,concept_id,encounter_id,obs_datetime,location_id,value_numeric,creator,date_created,uuid)
SELECT DISTINCT e.patient_id,163549,e.encounter_id,e.encounter_datetime,e.location_id,
CASE WHEN FindNumericValue(v.pedFeedFormulaAge)>0 THEN FindNumericValue(v.pedFeedFormulaAge)
ELSE NULL
END,1,e.date_created,UUID()