jeniferfleurant

nut 2017 numerator (muac/weigh)

Oct 27th, 2017
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 8.88 KB | None | 0 0
  1. USE caris_db;
  2. SET @start_date= '2016-10-01';
  3. SET @end_date='2017-09-30';
  4.  
  5.  
  6. select left(patient_code, 8) as hospital, sum(gender=1) as total_male, sum(gender=2) as total_female, sum(gender!=1 and gender!=2) as total_unknown, sum(age_in_month between 6 and 11) as '6-11', sum(age_in_month between 12 and 59) as '12-59' from
  7. (
  8. select patient_code, age_in_month, CASE
  9.                         WHEN age_in_month = 0 THEN IF(weight < 2, 'surunderweight', IF(weight < 2.4, 'underweight', ''))
  10.                         WHEN age_in_month > 0 AND age_in_month <= 6 THEN
  11.                             IF((weight - 2) / (age_in_month - 0) < ((4.6 - 2) / (6 - 0)), 'surunderweight',
  12.                             IF((weight - 2.4) / (age_in_month - 0) < ((5.5 - 2.4) / (6 - 0)), 'underweight', ''))
  13.                                 WHEN
  14.                                     age_in_month > 6
  15.                                         AND age_in_month <= 12
  16.                                 THEN
  17.                                     IF((weight - 4.6) / (age_in_month - 6) < ((6.5- 4.6 ) / (12 - 6)), 'surunderweight',
  18.                                         IF((weight - 5.5) / (age_in_month - 6) < ((7.8 - 5.5) / (12 - 6)), 'underweight', ''))
  19.                                 WHEN
  20.                                     age_in_month > 12
  21.                                         AND age_in_month <= 18
  22.                                 THEN
  23.                                     IF((weight - 6.5) / (age_in_month - 12) < ((7.3- 6.5) / (18 - 12)), 'surunderweight',
  24.                                         IF((weight - 7.8) / (age_in_month - 12) < ((8.7 - 7.8) / (18 - 12)), 'underweight', ''))
  25.                                 WHEN
  26.                                     age_in_month > 18
  27.                                         AND age_in_month <= 24
  28.                                 THEN
  29.                                     IF((weight - 7.3) / (age_in_month - 18) < ((8.1 - 7.3) / (24 - 18)), 'surunderweight',
  30.                                         IF((weight - 8.7) / (age_in_month - 18) < ((9.6 - 8.7) / (24 - 18)), 'underweight', ''))
  31.                                 WHEN
  32.                                     age_in_month > 24
  33.                                         AND age_in_month <= 30
  34.                                 THEN
  35.                                     IF((weight - 8.1) / (age_in_month - 24) < ((9 - 8.1 ) / (30 - 24)), 'surunderweight',
  36.                                         IF((weight - 9.6) / (age_in_month - 24) < ((10.5 - 9.6) / (30 - 24)), 'underweight', ''))
  37.                                 WHEN
  38.                                     age_in_month > 30
  39.                                         AND age_in_month <= 36
  40.                                 THEN
  41.                                     IF((weight - 9) / (age_in_month - 30) < ((9.8 - 9) / (36 - 30)), 'surunderweight',
  42.                                         IF((weight - 10.5) / (age_in_month - 30) < ((11.4 - 10.5) / (36 - 30)), 'underweight', ''))
  43.                                 WHEN
  44.                                     age_in_month > 36
  45.                                         AND age_in_month <= 42
  46.                                 THEN
  47.                                     IF((weight - 9.8) / (age_in_month - 36) < ((10.3 - 9.8) / (42 - 36)), 'surunderweight',
  48.                                         IF((weight - 11.4) / (age_in_month - 36) < ((12 - 11.4) / (42 - 36)), 'underweight', ''))
  49.                                 WHEN
  50.                                     age_in_month > 42
  51.                                         AND age_in_month <= 48
  52.                                 THEN
  53.                                     IF((weight - 10.3) / (age_in_month - 42) < ((10.9 - 10.3) / (48 - 42)), 'surunderweight',
  54.                                         IF((weight - 12) / (age_in_month - 42) < ((12.8 - 12) / (48 - 42)), 'underweight', ''))
  55.                                 WHEN
  56.                                     age_in_month > 48
  57.                                         AND age_in_month <= 54
  58.                                 THEN
  59.                                     IF((weight - 10.9) / (age_in_month - 48) < ((11.5 - 10.9 ) / (54 - 48)), 'surunderweight',
  60.                                         IF((weight - 12.8) / (age_in_month - 48) < ((13.5 - 12.8) / (54 - 48)), 'underweight', ''))
  61.                                 WHEN
  62.                                     age_in_month > 54
  63.                                         AND age_in_month <= 60
  64.                                 THEN
  65.                                     IF((weight - 11.5) / (age_in_month - 54) < ((12 - 11.5 ) / (60 - 54)), 'surunderweight',
  66.                                         IF((weight - 13.5) / (age_in_month - 54) < ((14.2 - 13.5) / (60 - 54)), 'underweight', ''))
  67.                             END AS nutrition_status, gender
  68. from
  69. (
  70. select patient_code, weight, age_in_month, gender from(
  71. select patient_code, infant_weight as weight, timestampdiff(month,infant_dob,date) as age_in_month,infant_gender as gender from testing_mereenfant
  72. left join testing_specimen on testing_specimen.id_patient=testing_mereenfant.id_patient
  73. left join patient on patient.id=testing_mereenfant.id_patient
  74. where pcr_result=1 and infant_weight>0 and date between @start_date and @end_date and timestampdiff(month,infant_dob,date)<60 and timestampdiff(month,infant_dob,date)>=6
  75. union
  76. select distinct patient_code, weight, timestampdiff(month,dob,date) as age_in_month, gender from tracking_followup f1 left join patient on patient.id=f1.id_patient
  77. left join tracking_infant on patient.id=tracking_infant.id_patient
  78.  where date between @start_date and @end_date and weight>0 and timestampdiff(month,dob,date)<60 and timestampdiff(month,dob,date)>=6
  79.  and weight = (select min(weight) from tracking_followup f2 where f1.id_patient=f2.id_patient)
  80. union
  81. select distinct o1.patient_code, weight_kg as weight, timestampdiff(month,dob,date_of_visit) as age_in_month, gender from openfn.odk_child_visit o1
  82. left join patient on patient.patient_code=o1.patient_code
  83. left join tracking_infant on patient.id=id_patient
  84. where
  85. date_of_visit between @start_date and @end_date and weight_kg>0 and timestampdiff(month,dob,date_of_visit)<60 and timestampdiff(month,dob,date_of_visit)>=6
  86. and weight_kg = (select min(weight_kg) from openfn.odk_child_visit o2 where o1.patient_code=o2.patient_code)
  87. )code1
  88.     where code1.weight=(
  89.     select min(weight) from(
  90.     select patient_code, infant_weight as weight, infant_gender, timestampdiff(month,infant_dob,date) as age_in_month from testing_mereenfant
  91.     left join testing_specimen on testing_specimen.id_patient=testing_mereenfant.id_patient
  92.     left join patient on patient.id=testing_mereenfant.id_patient
  93.     where pcr_result=1 and infant_weight>0 and date between @start_date and @end_date and timestampdiff(month,infant_dob,date)<60 and timestampdiff(month,infant_dob,date)>=6
  94.     union
  95.     select distinct patient_code, weight, gender, timestampdiff(month,dob,date) as age_in_month from tracking_followup f1 left join patient on patient.id=f1.id_patient
  96.     left join tracking_infant on patient.id=tracking_infant.id_patient
  97.      where date between @start_date and @end_date and weight>0 and timestampdiff(month,dob,date)<60 and timestampdiff(month,dob,date)>=6
  98.      and weight = (select min(weight) from tracking_followup f2 where f1.id_patient=f2.id_patient)
  99.     union
  100.     select distinct o1.patient_code, weight_kg as weight, gender, timestampdiff(month,dob,date_of_visit) as age_in_month from openfn.odk_child_visit o1
  101.     left join patient on patient.patient_code=o1.patient_code
  102.     left join tracking_infant on patient.id=id_patient
  103.     where
  104.     date_of_visit between @start_date and @end_date and weight_kg>0 and timestampdiff(month,dob,date_of_visit)<60 and timestampdiff(month,dob,date_of_visit)>=6
  105.     and weight_kg = (select min(weight_kg) from openfn.odk_child_visit o2 where o1.patient_code=o2.patient_code)
  106.     )code2
  107.     where code2.patient_code=code1.patient_code
  108.     )
  109. )as end_of_weight
  110. union
  111. select patient_code, age_in_month, 'surunderweight' as nutrition_status, gender from
  112. (select distinct patient_code, timestampdiff(month,dob,date) as age_in_month, gender from tracking_followup left join patient on patient.id=tracking_followup.id_patient
  113. left join tracking_infant on patient.id=tracking_infant.id_patient
  114.  where date between '2016-10-01' and '2017-09-30' and mid_arm_circumference>3 and mid_arm_circumference<=12.5
  115.  and timestampdiff(month,dob,date)<60 and timestampdiff(month,dob,date)>=6
  116. union
  117. select distinct odk_child_visit.patient_code, timestampdiff(month,dob,date_of_visit) as age_in_month, gender from openfn.odk_child_visit
  118. left join patient on patient.patient_code=odk_child_visit.patient_code
  119. left join tracking_infant on patient.id=id_patient
  120. where
  121. date_of_visit between '2016-10-01' and '2017-09-30' and (muac>3) and muac<=12.5
  122. and timestampdiff(month,dob,date_of_visit)<60 and timestampdiff(month,dob,date_of_visit)>=6
  123. )a
  124. )as end
  125. where nutrition_status='surunderweight'
  126.  
  127. group by hospital
Advertisement
Add Comment
Please, Sign In to add comment