forked from seekme94/tuberculosis-predictive-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrepare Data.sql
More file actions
1128 lines (1092 loc) · 33.1 KB
/
Prepare Data.sql
File metadata and controls
1128 lines (1092 loc) · 33.1 KB
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
-- create schema tb_study;
-- ################## PHASE1 DETECTION
-- ##################################################################################################################################
-- Meta tables
drop table if exists gp;
create table gp (
GPID int not null auto_increment,
GPName varchar(45) not null,
Qualification varchar(45) null,
Speciality varchar(45) null,
primary key (GPID)
);
insert into gp
select 0,GPID,Qualification1,Speciality1 from tbreach_rpt.gp
union
select 0,GPID,Qualification,Specialization from tbreach2_rpt.gp;
drop table if exists users;
create table users (
UserID int not null auto_increment,
UserName varchar(45) null,
Role varchar(45) null,
primary key (UserID)
);
insert into users
select 0, UserName, Role from tbreach_rpt.users
union
select 0, UserName, Role from tbreach2_rpt.users;
-- ##################################################################################################################################
-- ##################################################################################################################################
-- Prepare patient table
drop table if exists _patient;
create table _patient select PatientID,
CHWID,
GPID,
Weight,
Height,
BloodGroup,
DateRegistered,
TreatmentCenter,
(TreatmentSupporter is not null) as HasTreatmentSupporter,
DiseaseConfirmed,
DiseaseCategory,
DiseaseSite,
Severity,
Regimen,
DoseCombination,
OtherDoseDescription,
TreatmentPhase,
PatientStatus,
PatientType,
DiseaseHistory,
TreatedPreviously,
CompletedPreviousTreatment from
tbreach_rpt.patient
union select
PatientID,
CHWID,
GPID,
Weight,
Height,
BloodGroup,
DateRegistered,
TreatmentCenter,
(TreatmentSupporter is not null) as HasTreatmentSupporter,
DiseaseConfirmed,
DiseaseCategory,
DiseaseSite,
Severity,
Regimen,
DoseCombination,
Streptomycin,
TreatmentPhase,
PatientStatus,
PatientType,
DiseaseHistory,
TreatedPreviously,
CompletedPreviousTreatment
from
tbreach2_rpt.patient;
drop table if exists _person;
create table _person select PID,
Salutation,
LastName,
Surname,
Gender,
DOB,
MaritalStatus,
Domicile,
Religion,
Caste,
RoleInSystem,
Alive from
tbreach_rpt.person
union select
PID,
Salutation,
LastName,
Surname,
Gender,
DOB,
MaritalStatus,
Domicile,
Religion,
Caste,
RoleInSystem,
Alive
from
tbreach2_rpt.person;
-- Merge Patient and Person tables, removing names
drop table if exists patient;
create table patient select PatientID,
CHWID,
GPID,
DOB,
Gender,
MaritalStatus,
Domicile,
Religion,
Caste,
Weight,
Height,
DateRegistered,
TreatmentCenter,
HasTreatmentSupporter,
DiseaseConfirmed,
DiseaseCategory,
DiseaseSite,
Severity,
Regimen,
DoseCombination,
OtherDoseDescription,
TreatmentPhase,
PatientStatus,
PatientType,
DiseaseHistory,
TreatedPreviously,
CompletedPreviousTreatment from
_patient as P
inner join
_person as R ON R.PID = P.PatientID
where
DiseaseConfirmed = 1
and PatientType is not null;
alter table patient add primary key (PatientID);
-- Normalize attributes
update patient
set
DiseaseCategory = 'CAT I'
where
DiseaseCategory = 'CATEGORY 1';
update patient
set
DiseaseCategory = 'CAT II'
where
DiseaseCategory = 'CATEGORY 2';
update patient
set
Regimen = 'RHZE'
where
Regimen = 'RHEZ';
-- Height
update patient
set
Height = null
where
Height >= 444;
update patient
set
Height = Height * 30.48
where
Height < 40;-- Into cm
update patient
set
Height = null
where
Height = 0;-- Nullify 0 data
-- Weight
update patient
set
Weight = null
where
Weight >= 444;
update patient
set
Weight = Weight * 0.4535
where
Weight > 140;-- Into Kg
update patient
set
Weight = null
where
Weight = 0; -- Nullify 0 data
-- Cleanup
drop table if exists _patient;
drop table if exists _person;
-- ##################################################################################################################################
-- ##################################################################################################################################
-- Create screening table
drop table if exists enc_screening;
create table enc_screening select EncounterID,
PID1,
PID2,
DateEncounterStart,
DateEncounterEnd,
EnteredDate,
ENTERED_DATE,
AGE,
FEVER,
COUGH,
COUGH_DURATION,
PRODUCTIVE_COUGH,
HAEMOPTYSIS,
NIGHT_SWEATS,
WEIGHT_LOSS,
TB_HISTORY,
TB_FAMILY_HISTORY from
tbreach_rpt.enc_suspect_id
where
PID1 in (select
PatientID
from
patient)
union select
EncounterID,
PID1,
PID2,
DateEncounterStart,
DateEncounterEnd,
EnteredDate,
ENTERED_DATE,
AGE,
FEVER,
COUGH,
COUGH_DURATION,
PRODUCTIVE_COUGH,
HAEMOPTYSIS,
NIGHT_SWEATS,
WEIGHT_LOSS,
TB_HISTORY,
TB_FAMILY_HISTORY
from
tbreach2_rpt.enc_suspect_id
where
PID1 in (select
PatientID
from
patient);
update enc_screening
set
COUGH_DURATION = 'LESS THAN 2 WEEKS'
where
COUGH_DURATION = '< 2 WEEKS';
update enc_screening
set
COUGH_DURATION = '2 TO 3 WEEKS'
where
COUGH_DURATION = '2-3 WEEKS';
update enc_screening
set
COUGH_DURATION = 'MORE THAN 3 WEEKS'
where
COUGH_DURATION = '> 3 WEEKS';
drop table if exists screening;
create table screening select EncounterID,
PID1 as PatientID,
PID2 as ScreenerID,
EnteredDate as ScreeningDate,
cast(AGE as unsigned) as ScreeningAge,
FEVER as Fever,
COUGH as Cough,
COUGH_DURATION as CoughDuration,
PRODUCTIVE_COUGH as ProductiveCough,
HAEMOPTYSIS as BloodInCough,
NIGHT_SWEATS as NightSweats,
WEIGHT_LOSS as WeightLoss,
TB_HISTORY as TBHistory,
TB_FAMILY_HISTORY as TBInFamily from
enc_screening;
delete from screening
where
PatientID = '03212052401'
and EncounterID = 1; -- Unexpected duplicate
alter table screening add primary key (PatientID);
-- Cleanup
drop table if exists enc_screening;
-- ##################################################################################################################################
-- ##################################################################################################################################
-- Prepare baseline test results table
drop table if exists _sputumresults;
create table _sputumresults select PatientID,
SputumTestID,
Month,
DateSubmitted,
DateTested,
SmearResult from
tbreach_rpt.sputumresults
where
Month = 0 and SmearResult is not null
and PatientID in (select
PatientID
from
patient)
union select
PatientID,
SputumLabID,
Month,
SmearOrderDate,
DateSmearTested,
SmearResult
from
tbreach2_rpt.sputumresults
where
Month = 0 and SmearResult is not null
and PatientID in (select
PatientID
from
patient);
update _sputumresults
set
SmearResult = '1-9AFB'
where
SmearResult = '1-9 AFB';
-- In case of multiple sputum results, keep the positive one, if both are positive or negative, keep any one
drop table if exists tmp;
create table tmp select PatientID from
_sputumresults
group by PatientID
having count(*) > 1;
delete from _sputumresults
where
SmearResult = 'NEGATIVE'
and PatientID in (select
PatientID
from
tmp);
drop table tmp;
drop table if exists _genexpertresults;
create table _genexpertresults select PatientID,
LaboratoryID,
DateTested,
Remarks,
GeneXpertResult,
DrugResistance from
tbreach_rpt.genexpertresults
where
DateTested is not null
and PatientID in (select
PatientID
from
patient)
union select
PatientID,
SputumLabID,
DateGeneXpertTested,
Details,
GeneXpertResult,
GeneXpertResistance
from
tbreach2_rpt.sputumresults
where
Month = 0
and DateGeneXpertTested is not null
and PatientID in (select
PatientID
from
patient);
delete from _genexpertresults
where
GeneXpertResult = ''
or GeneXpertResult = 'ERROR'
or GeneXpertResult = 'INVALID';-- Not valid GXP tests
update _genexpertresults
set
GeneXpertResult = 'POSITIVE'
where
GeneXpertResult like '%POSITIVE%';
update _genexpertresults
set
GeneXpertResult = 'NEGATIVE'
where
GeneXpertResult like '%NEGATIVE%';
update _genexpertresults
set
DrugResistance = 'INDETERMINATE'
where
DrugResistance like '%INDETERMINATE%';
update _genexpertresults
set
DrugResistance = 'RESISTANT'
where
DrugResistance like '%RESISTANT%';
update _genexpertresults
set
DrugResistance = 'SENSITIVE'
where
DrugResistance like '%SENSITIVE%';
-- In case of multiple sputum results, keep the positive one, if both are positive or negative, keep any one
drop table if exists tmp;
create table tmp select PatientID from
_genexpertresults
group by PatientID
having count(*) > 1;
delete from _genexpertresults
where
GeneXpertResult = 'NEGATIVE'
and PatientID in (select
PatientID
from
tmp);
drop table tmp;
drop table if exists _xrayresults;
create table _xrayresults select PatientID, XRayDate, DateReported, XRayResults, Remarks from
tbreach_rpt.xrayresults
where
XRayResults is not null
and PatientID in (select
PatientID
from
patient)
union select
PatientID, XRayDate, DateReported, XRayResult, Remarks
from
tbreach2_rpt.xrayresults
where
XRayResult is not null
and PatientID in (select
PatientID
from
patient);
delete from _xrayresults
where
XRayResults = 'IMAGE UNCLEAR'; -- Not a valid XRay
-- One way to distinguish baseline Xrays from others is to match Baseline treatment date
drop table if exists enc_baseline;
create table enc_baseline as select EncounterID,
PID1,
PID2,
DateEncounterStart,
DateEncounterEnd,
EnteredDate,
ENTERED_DATE,
FDC_TABLETS,
PATIENT_CATEGORY,
PATIENT_TYPE,
STREPOMYCIN as STREPTOMYCIN,
REGIMEN,
WEIGHT from
tbreach_rpt.enc_baseline
where
PID1 in (select
PatientID
from
patient)
union select
EncounterID,
PID1,
PID2,
DateEncounterStart,
DateEncounterEnd,
EnteredDate,
ENTERED_DATE,
FDC_TABLETS,
PATIENT_CATEGORY,
PATIENT_TYPE,
STREPTOMYCIN,
REGIMEN,
WEIGHT
from
tbreach2_rpt.enc_baseline
where
PID1 in (select
PatientID
from
patient);
drop table if exists tmp;
create table tmp select X . * from
_xrayresults as X
left outer join
enc_baseline as B ON B.PID1 = X.PatientID
and B.PID1 in (select
PatientID
from
patient)
where
X.XRayDate < B.EnteredDate;
drop table if exists _xrayresults;
create table _xrayresults as select * from
tmp;
drop table tmp;
drop table if exists baseline_results;
create table baseline_results select P.PatientID,
S.Month,
S.SputumTestID,
S.DateSubmitted as DateSputumSubmitted,
S.DateTested as DateSputumTested,
datediff(S.DateTested, S.DateSubmitted) as SputumResultDelay,
S.SmearResult,
X.XRayDate,
X.DateReported as DateXRayReported,
datediff(X.DateReported, X.XRayDate) as XRayResultDelay,
X.XRayResults,
X.Remarks as XRayRemarks,
G.DateTested as DateGXPTested,
G.GeneXpertResult,
G.DrugResistance,
G.Remarks as GXRemarks from
patient as P
left outer join
(select
*
from
_sputumresults
group by PatientID) as S USING (PatientID)
left outer join
(select
*
from
_xrayresults
group by PatientID) as X USING (PatientID)
left outer join
(select
*
from
_genexpertresults
group by PatientID) as G USING (PatientID);
alter table baseline_results add primary key (PatientID);
-- Clean up
drop table if exists _sputumresults;
drop table if exists _genexpertresults;
drop table if exists _xrayresults;
-- ##################################################################################################################################
-- ##################################################################################################################################
-- Create clinical diagnosis table
drop table if exists enc_cdf;
create table enc_cdf as select * from
tbreach_rpt.enc_cdf
where
PID1 in (select
PatientID
from
patient)
union select
*
from
tbreach2_rpt.enc_cdf
where
PID1 in (select
PatientID
from
patient);
-- Keep only the latest encounter in case of duplicates
drop table if exists _diagnosis;
create table _diagnosis select a . * from
enc_cdf as a
where
EnteredDate = (select
EnteredDate
from
enc_cdf
where
PID1 = a.PID1
order by EnteredDate desc
limit 1);
drop table if exists diagnosis;
create table diagnosis as select PID1 as PatientID,
PID2 as GPID,
EnteredDate as DiagnosisDate,
ANTIBIOTIC_TRIAL as DiagnosisAntibiotic,
CLINICAL_SYMP_TB as TBSymptomsDiagnosed,
CONTACT_HISTORY as TBContactDiagnosed,
DIAGNOSIS as Diagnosis,
LARGE_LYMPH_NODES as LargeLymphDiagnosed,
LYMPH_NODE_BIOPSY as LymphBiopsyDiagnosed,
MANTOUX as MantouxDiagnosed,
PAST_HISTORY as PastTBDiagnosed,
XRAY_SUGGESTIVE as XRaySuggestiveDiagnosed,
OTHER_THAN_TB_DETAIL as OtherTBDiagnosis,
concat(OTHER_DIAGNOSTIC_DETAIL, ' ', NOTES) as DiganosisNotes from
_diagnosis;
alter table diagnosis add primary key (PatientID);
-- Cleanup
drop table if exists enc_cdf;
drop table if exists _diagnosis;
-- ##################################################################################################################################
-- ##################################################################################################################################
-- Create Baseline Treatment table
drop table if exists enc_baseline;
create table enc_baseline as select EncounterID,
PID1,
PID2,
DateEncounterStart,
DateEncounterEnd,
EnteredDate,
ENTERED_DATE,
FDC_TABLETS,
PATIENT_CATEGORY,
PATIENT_TYPE,
STREPOMYCIN as STREPTOMYCIN,
REGIMEN,
WEIGHT from
tbreach_rpt.enc_baseline
where
PID1 in (select
PatientID
from
patient)
union select
EncounterID,
PID1,
PID2,
DateEncounterStart,
DateEncounterEnd,
EnteredDate,
ENTERED_DATE,
FDC_TABLETS,
PATIENT_CATEGORY,
PATIENT_TYPE,
STREPTOMYCIN,
REGIMEN,
WEIGHT
from
tbreach2_rpt.enc_baseline
where
PID1 in (select
PatientID
from
patient);
update enc_baseline
set
WEIGHT = null
where
WEIGHT > 200;
update enc_baseline
set
PATIENT_CATEGORY = 'CAT I'
where
PATIENT_CATEGORY = 'CATEGORY 1';
update enc_baseline
set
PATIENT_CATEGORY = 'CAT II'
where
PATIENT_CATEGORY = 'CATEGORY 2';
update enc_baseline
set
REGIMEN = 'RHZE'
where
REGIMEN = 'RHEZ';
update enc_baseline
set
REGIMEN = 'HE'
where
REGIMEN = 'EH';
drop table if exists baseline_treatment;
create table baseline_treatment select PID1 as PatientID,
PID2 as BaselineCHWID,
EnteredDate as BaselineDate,
WEIGHT as BaselineWeight,
PATIENT_CATEGORY as BaselinePatientCategory,
PATIENT_TYPE as BaselinePatientType,
REGIMEN as BaselineRegimen,
FDC_TABLETS as FDCTablets,
STREPTOMYCIN as Streptomycin from
enc_baseline;
alter table baseline_treatment add primary key (PatientID);
-- Cleanup
drop table if exists enc_baseline;
-- ##################################################################################################################################
-- ################## PHASE2 TREATMENT
-- ##################################################################################################################################
-- Create drugs history record
drop table if exists _drug_history;
create table _drug_history select PatientID,
DispensationNo,
DateDispensed,
PillsDelivered,
PillsQuotaDelivered,
StreptomycinDelivered,
StreptomycinQuotaDelivered,
Remarks from
tbreach_rpt.drughistory
union select
PatientID,
DispensalNo,
DateDispensed,
PillsDelivered,
PillsQuotaDelivered,
StreptomycinDelivered,
StreptomycinQuotaDelivered,
Remarks
from
tbreach2_rpt.drughistory;
-- Create combined table for drug history
drop table if exists drug_history;
create table drug_history select PatientID,
count(DispensationNo) as TotalDispersals,
sum(PillsDelivered) as TotalPils,
sum(PillsQuotaDelivered) as TotalPillsQuote,
sum(StreptomycinDelivered) as TotalStreptomycin,
sum(StreptomycinQuotaDelivered) as TotalStreptomycinQuote from
_drug_history
group by PatientID;
-- Create Follow-up table
drop table if exists enc_follow_up;
create table enc_follow_up as select EncounterID,
PID1,
PID2,
DateEncounterStart,
DateEncounterEnd,
EnteredDate,
ENTERED_DATE,
MONTH,
ifnull(FDC_TABLETS, FDC_TABLET) as FDC_TABLETS,
STREPOMYCIN as STREPTOMYCIN,
REGIMEN,
WEIGHT,
TREATMENT_PHASE from
tbreach_rpt.enc_follow_up
where
PID1 in (select
PatientID
from
patient)
union select
EncounterID,
PID1,
PID2,
DateEncounterStart,
DateEncounterEnd,
EnteredDate,
ENTERED_DATE,
MONTH,
FDC_TABLETS,
STREPOMYCIN as STREPTOMYCIN,
REGIMEN,
WEIGHT,
TREATMENT_PHASE
from
tbreach2_rpt.enc_follow_up
where
PID1 in (select
PatientID
from
patient);
-- Create Treatment refusal table
drop table if exists enc_refusal;
create table enc_refusal as select EncounterID,
PID1,
PID2,
DateEncounterStart,
DateEncounterEnd,
EnteredDate,
ENTERED_DATE,
REASON,
WHAT_REFUSED from
tbreach2_rpt.enc_refusal
where
PID1 in (select
PatientID
from
patient)
union select
EncounterID,
PID1,
PID2,
DateEncounterStart,
DateEncounterEnd,
EnteredDate,
ENTERED_DATE,
REASON,
WHAT_REFUSED
from
tbreach_rpt.enc_refusal
where
PID1 in (select
PatientID
from
patient);
drop table if exists refusal;
create table refusal select a . * from
enc_refusal as a
where
EncounterID = (select
max(EncounterID)
from
enc_refusal
where
PID1 = a.PID1);
-- ###################################################################################################################################
-- ################## PHASE3 OUTCOMES
-- ##################################################################################################################################
-- Create End of Follow-up table
drop table if exists enc_end_fol;
create table enc_end_fol as select EncounterID,
PID1,
PID2,
DateEncounterStart,
DateEncounterEnd,
EnteredDate,
ENTERED_DATE,
REASON,
OTHER_REASON from
tbreach_rpt.enc_end_fol
where
PID1 in (select
PatientID
from
patient)
union select
EncounterID,
PID1,
PID2,
DateEncounterStart,
DateEncounterEnd,
EnteredDate,
ENTERED_DATE,
REASON,
OTHER_REASON
from
tbreach2_rpt.enc_end_fol
where
PID1 in (select
PatientID
from
patient);
drop table if exists end_follow_up;
create table end_follow_up select a . * from
enc_end_fol as a
where
concat(EncounterID, EnteredDate) = (select
concat(EncounterID, EnteredDate)
from
enc_end_fol
where
PID1 = a.PID1
order by EnteredDate desc , EncounterID desc
limit 1);
update end_follow_up
set
REASON = 'TREATMENT FAILURE'
where
REASON = 'TX FAILURE'
or REASON = 'FAILURE';
update end_follow_up
set
REASON = 'TREATMENT COMPLETED'
where
REASON = 'TX COMPLETED'
or REASON = 'TREAMTENT COMPLETED';
update end_follow_up
set
REASON = 'TRANSFERRED'
where
REASON like '%TRANSFER%';
update end_follow_up
set
REASON = 'MOVED'
where
REASON = 'DEAFAULT'
or REASON = 'DEFAULTED'
or REASON = 'DEFAULT/MOVED OUT OF AREA';
update end_follow_up
set
REASON = 'MDR PATIENT'
where
REASON like '%MDR%'
or REASON like '%RESISTANT%';
drop table if exists treatment_outcome;
create table treatment_outcome select PID1 as PatientID,
EnteredDate as TreatmentEndDate,
REASON as TreatmentOutcome,
OTHER_REASON as TreatmentOutcomeDetails from
end_follow_up;
alter table treatment_outcome add primary key (PatientID);
-- Cleanup
drop table if exists enc_end_fol;
drop table if exists end_follow_up;
-- ##################################################################################################################################
-- ################## PHASE4 MERGING DATA
-- ##################################################################################################################################
-- Create a table to hide actual IDs
drop table if exists ids;
create table ids (
ID int not null auto_increment,
OriginalID varchar(45) not null,
primary key (ID)
);
insert into ids
select 0, PatientID from patient where PatientID is not null;
-- Create unified table till baseline for each patient
drop table if exists tb_data;
create table tb_data (
PatientID varchar(50) NOT NULL DEFAULT '',
GP varchar(12) DEFAULT '',
GPQualification varchar(45) DEFAULT '',
GPSpeciality varchar(45) DEFAULT '',
Screener varchar(12) NOT NULL DEFAULT '',
DOB varchar(50) DEFAULT '',
BirthYear varchar(50) DEFAULT '',
ScreeningAge varchar(50) DEFAULT '',
Gender char(1) NOT NULL DEFAULT '',
MaritalStatus varchar(20) DEFAULT '',
Religion varchar(20) DEFAULT '',
Caste varchar(20) DEFAULT '',
Weight varchar(50) DEFAULT '',
Height varchar(50) DEFAULT '',
ScreeningDate varchar(50) DEFAULT '',
ScreeningYear varchar(50) DEFAULT '',
DateRegistered varchar(50) DEFAULT '',
RegistrationYear varchar(50) DEFAULT '',
HasTreatmentSupporter bit(1) NOT NULL DEFAULT '',
DiseaseConfirmed bit(1) DEFAULT '',
DiseaseCategory varchar(12) DEFAULT '',
DiseaseSite varchar(50) DEFAULT '',
LastRegimen varchar(12) DEFAULT '',
DoseCombination varchar(50) DEFAULT '',
OtherDoseDescription varchar(255) DEFAULT '',
TreatmentPhase varchar(12) DEFAULT '',
PatientStatus varchar(12) DEFAULT '',
PatientType varchar(50) DEFAULT '',
TreatedPreviously bit(1) DEFAULT '',
CompletedPreviousTreatment bit(1) DEFAULT '',
Fever longtext,
Cough longtext,
CoughDuration longtext,
ProductiveCough longtext,
BloodInCough longtext,
NightSweats longtext,
WeightLoss longtext,
TBHistory longtext,
TBInFamily longtext,
SmearTested varchar(3)CHARACTER SET utf8 NOT NULL DEFAULT '',
DateSputumSubmitted varchar(50) DEFAULT '',
DateSputumTested varchar(50) DEFAULT '',
SputumResultDelay varchar(50) DEFAULT '',
SmearResult varchar(12) DEFAULT '',
SmearPositive varchar(3)CHARACTER SET utf8 NOT NULL DEFAULT '',
XRayDone varchar(3)CHARACTER SET utf8 NOT NULL DEFAULT '',
XRayDate varchar(50) DEFAULT '',
DateXRayReported varchar(50) DEFAULT '',
XRayResultDelay varchar(50) DEFAULT '',
XRayResults varchar(255) DEFAULT '',
XRayRemarks varchar(255) DEFAULT '',
XRayIndicative varchar(3)CHARACTER SET utf8 NOT NULL DEFAULT '',
GeneXpertTested varchar(3)CHARACTER SET utf8 NOT NULL DEFAULT '',
DateGXPTested varchar(50) DEFAULT '',
GXPDelay varchar(50) DEFAULT '',
GeneXpertResult varchar(50) DEFAULT '',
DrugResistance varchar(50) DEFAULT '',
GXRemarks varchar(255) DEFAULT '',
GXPPositive varchar(3)CHARACTER SET utf8 NOT NULL DEFAULT '',
DiagnosedBy varchar(12) DEFAULT '',
DiagnosisDate varchar(50) DEFAULT '',
DiagnosisAntibiotic longtext,
TBSymptomsDiagnosed longtext,
TBContactDiagnosed longtext,
Diagnosis longtext,
LargeLymphDiagnosed longtext,
LymphBiopsyDiagnosed longtext,
MantouxDiagnosed longtext,
PastTBDiagnosed longtext,
XRaySuggestiveDiagnosed longtext,
OtherTBDiagnosis longtext,
DiagnosisNotes longtext,
BaselineCHWID varchar(12) DEFAULT '',
BaselineDate varchar(50) DEFAULT '',
BaselineWeight longtext,