Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- USE caris_db;
- SET @start_date= "2015-10-01";
- SET @end_date="2016-09-30";
- SELECT b.patient_surveyed,a.malnourished,
- a.malnourished_btwn_6_11_mnths,a.malnourished_btwn_12_60_mnths,a.malnourished_btwn_6_11_mnths_from_mereenfant,a.malnourished_btwn_12_60_mnths_from_mereenfant,nutrition_status_from_mereenfant, male, female
- FROM(
- SELECT
- patient_code,
- CASE
- WHEN (nutrition_status = 'underweight' OR nutrition_status = 'surunderweight') THEN "Yes"
- ELSE "no"
- END AS malnourished,
- CASE
- WHEN
- age_in_months >= 6
- AND age_in_months <= 11
- AND (nutrition_status = 'underweight' OR nutrition_status = 'surunderweight')
- THEN "Yes"
- ELSE "no"
- END AS malnourished_btwn_6_11_mnths,
- CASE
- WHEN
- age_in_months >= 12
- AND age_in_months <= 60
- AND (nutrition_status = 'underweight' OR nutrition_status = 'surunderweight')
- THEN "Yes"
- ELSE "no"
- END AS malnourished_btwn_12_60_mnths,
- # mereenfant
- CASE
- WHEN (nutrition_status_from_mereenfant = 'underweight' OR nutrition_status_from_mereenfant = 'surunderweight') THEN "Yes"
- ELSE "no"
- END AS malnourished_from_mereenfant,
- CASE
- WHEN
- age_in_month_me >= 6
- AND age_in_month_me <= 11
- AND (nutrition_status_from_mereenfant = 'underweight' OR nutrition_status_from_mereenfant = 'surunderweight')
- THEN "Yes"
- ELSE "no"
- END AS malnourished_btwn_6_11_mnths_from_mereenfant,
- CASE
- WHEN
- age_in_month_me >= 12
- AND age_in_month_me <= 60
- AND (nutrition_status_from_mereenfant = 'underweight' OR nutrition_status_from_mereenfant = 'surunderweight')
- THEN "Yes"
- ELSE "no"
- END AS malnourished_btwn_12_60_mnths_from_mereenfant,
- nutrition_status_from_mereenfant
- FROM(
- SELECT * FROM (
- SELECT a.*,
- CASE
- WHEN age_in_months = 0 THEN IF(infant_weight < 2, 'surunderweight', IF(infant_weight < 2.4, 'underweight', ''))
- WHEN age_in_months > 0 AND age_in_months <= 6 THEN
- IF((infant_weight - 2) / (age_in_months - 0) < ((4.6 - 2) / (6 - 0)), 'surunderweight',
- IF((infant_weight - 2.4) / (age_in_months - 0) < ((5.5 - 2.4) / (6 - 0)), 'underweight', ''))
- WHEN
- age_in_months > 6
- AND age_in_months <= 12
- THEN
- IF((infant_weight - 4.6) / (age_in_months - 6) < ((6.5- 4.6 ) / (12 - 6)), 'surunderweight',
- IF((infant_weight - 5.5) / (age_in_months - 6) < ((7.8 - 5.5) / (12 - 6)), 'underweight', ''))
- WHEN
- age_in_months > 12
- AND age_in_months <= 18
- THEN
- IF((infant_weight - 6.5) / (age_in_months - 12) < ((7.3- 6.5) / (18 - 12)), 'surunderweight',
- IF((infant_weight - 7.8) / (age_in_months - 12) < ((8.7 - 7.8) / (18 - 12)), 'underweight', ''))
- WHEN
- age_in_months > 18
- AND age_in_months <= 24
- THEN
- IF((infant_weight - 7.3) / (age_in_months - 18) < ((8.1 - 7.3) / (24 - 18)), 'surunderweight',
- IF((infant_weight - 8.7) / (age_in_months - 18) < ((9.6 - 8.7) / (24 - 18)), 'underweight', ''))
- WHEN
- age_in_months > 24
- AND age_in_months <= 30
- THEN
- IF((infant_weight - 8.1) / (age_in_months - 24) < ((9 - 8.1 ) / (30 - 24)), 'surunderweight',
- IF((infant_weight - 9.6) / (age_in_months - 24) < ((10.5 - 9.6) / (30 - 24)), 'underweight', ''))
- WHEN
- age_in_months > 30
- AND age_in_months <= 36
- THEN
- IF((infant_weight - 9) / (age_in_months - 30) < ((9.8 - 9) / (36 - 30)), 'surunderweight',
- IF((infant_weight - 10.5) / (age_in_months - 30) < ((11.4 - 10.5) / (36 - 30)), 'underweight', ''))
- WHEN
- age_in_months > 36
- AND age_in_months <= 42
- THEN
- IF((infant_weight - 9.8) / (age_in_months - 36) < ((10.3 - 9.8) / (42 - 36)), 'surunderweight',
- IF((infant_weight - 11.4) / (age_in_months - 36) < ((12 - 11.4) / (42 - 36)), 'underweight', ''))
- WHEN
- age_in_months > 42
- AND age_in_months <= 48
- THEN
- IF((infant_weight - 10.3) / (age_in_months - 42) < ((10.9 - 10.3) / (48 - 42)), 'surunderweight',
- IF((infant_weight - 12) / (age_in_months - 42) < ((12.8 - 12) / (48 - 42)), 'underweight', ''))
- WHEN
- age_in_months > 48
- AND age_in_months <= 54
- THEN
- IF((infant_weight - 10.9) / (age_in_months - 48) < ((11.5 - 10.9 ) / (54 - 48)), 'surunderweight',
- IF((infant_weight - 12.8) / (age_in_months - 48) < ((13.5 - 12.8) / (54 - 48)), 'underweight', ''))
- WHEN
- age_in_months > 54
- AND age_in_months <= 60
- THEN
- IF((infant_weight - 11.5) / (age_in_months - 54) < ((12 - 11.5 ) / (60 - 54)), 'surunderweight',
- IF((infant_weight - 13.5) / (age_in_months - 54) < ((14.2 - 13.5) / (60 - 54)), 'underweight', ''))
- END AS nutrition_status,
- CASE
- WHEN age_in_month_me = 0 THEN IF(weight_me < 2, 'surunderweight', IF(weight_me < 2.4, 'underweight', ''))
- WHEN age_in_month_me > 0 AND age_in_month_me <= 6 THEN
- IF((weight_me - 2) / (age_in_month_me - 0) < ((4.6 - 2) / (6 - 0)), 'surunderweight',
- IF((weight_me - 2.4) / (age_in_month_me - 0) < ((5.5 - 2.4) / (6 - 0)), 'underweight', ''))
- WHEN
- age_in_month_me > 6
- AND age_in_month_me <= 12
- THEN
- IF((weight_me - 4.6) / (age_in_month_me - 6) < ((6.5- 4.6 ) / (12 - 6)), 'surunderweight',
- IF((weight_me - 5.5) / (age_in_month_me - 6) < ((7.8 - 5.5) / (12 - 6)), 'underweight', ''))
- WHEN
- age_in_month_me > 12
- AND age_in_month_me <= 18
- THEN
- IF((weight_me - 6.5) / (age_in_month_me - 12) < ((7.3- 6.5) / (18 - 12)), 'surunderweight',
- IF((weight_me - 7.8) / (age_in_month_me - 12) < ((8.7 - 7.8) / (18 - 12)), 'underweight', ''))
- WHEN
- age_in_month_me > 18
- AND age_in_month_me <= 24
- THEN
- IF((weight_me - 7.3) / (age_in_month_me - 18) < ((8.1 - 7.3) / (24 - 18)), 'surunderweight',
- IF((weight_me - 8.7) / (age_in_month_me - 18) < ((9.6 - 8.7) / (24 - 18)), 'underweight', ''))
- WHEN
- age_in_month_me > 24
- AND age_in_month_me <= 30
- THEN
- IF((weight_me - 8.1) / (age_in_month_me - 24) < ((9 - 8.1 ) / (30 - 24)), 'surunderweight',
- IF((weight_me - 9.6) / (age_in_month_me - 24) < ((10.5 - 9.6) / (30 - 24)), 'underweight', ''))
- WHEN
- age_in_month_me > 30
- AND age_in_month_me <= 36
- THEN
- IF((weight_me - 9) / (age_in_month_me - 30) < ((9.8 - 9) / (36 - 30)), 'surunderweight',
- IF((weight_me - 10.5) / (age_in_month_me - 30) < ((11.4 - 10.5) / (36 - 30)), 'underweight', ''))
- WHEN
- age_in_month_me > 36
- AND age_in_month_me <= 42
- THEN
- IF((weight_me - 9.8) / (age_in_month_me - 36) < ((10.3 - 9.8) / (42 - 36)), 'surunderweight',
- IF((weight_me - 11.4) / (age_in_month_me - 36) < ((12 - 11.4) / (42 - 36)), 'underweight', ''))
- WHEN
- age_in_month_me > 42
- AND age_in_month_me <= 48
- THEN
- IF((weight_me - 10.3) / (age_in_month_me - 42) < ((10.9 - 10.3) / (48 - 42)), 'surunderweight',
- IF((weight_me - 12) / (age_in_month_me - 42) < ((12.8 - 12) / (48 - 42)), 'underweight', ''))
- WHEN
- age_in_month_me > 48
- AND age_in_month_me <= 54
- THEN
- IF((weight_me - 10.9) / (age_in_month_me - 48) < ((11.5 - 10.9 ) / (54 - 48)), 'surunderweight',
- IF((weight_me - 12.8) / (age_in_month_me - 48) < ((13.5 - 12.8) / (54 - 48)), 'underweight', ''))
- WHEN
- age_in_month_me > 54
- AND age_in_month_me <= 60
- THEN
- IF((weight_me - 11.5) / (age_in_month_me - 54) < ((12 - 11.5 ) / (60 - 54)), 'surunderweight',
- IF((weight_me - 13.5) / (age_in_month_me - 54) < ((14.2 - 13.5) / (60 - 54)), 'underweight', ''))
- END AS nutrition_status_from_mereenfant
- FROM (
- #all surveyed infant
- select patient.id,patient.city_code,patient.hospital_code,patient.patient_code ,patient.linked_to_id_patient,
- b12345.gender,b12345.dob,b12345.age_in_months,b12345.weight as infant_weight,b12345.date,c12345.*,IF(b12345.gender is not null,IF(b12345.gender=1,"YES","NO"),IF(c12345.gender_me=1,"YES","NO")) as male,
- IF(b12345.gender is not null,IF(b12345.gender=2,"YES","NO"),IF(c12345.gender_me=2,"YES","NO")) as female from
- (
- select id_patient from tracking_followup f where f.date>=@start_date and f.date<=@end_date
- union select id_patient from testing_mereenfant m where m.date>=@start_date and m.date<=@end_date
- ) a12345
- LEFT JOIN
- (
- select id_patient,gender,dob,c.age_in_months,c.weight,date
- from(
- select i.dob,i.gender,TIMESTAMPDIFF(MONTH,i.dob,f.date) as age_in_months,
- f.id_patient,f.weight,f.date from tracking_followup f left join tracking_infant i on i.id_patient=f.id_patient
- where (TIMESTAMPDIFF(MONTH,i.dob,f.date) between 6 and 60) && weight>0
- and f.date between @start_date and @end_date ) c
- ) b12345 on a12345.id_patient=b12345.id_patient
- LEFT JOIN (
- select * from(
- select id_patient,infant_gender as gender_me,infant_weight as weight_me,
- IF(age_in_month_gen<0,age_in_month,age_in_month_gen) as age_in_month_me,date_me from (
- select id, id_patient ,infant_gender, date as date_me,infant_dob,timestampdiff(month,infant_dob,date) as age_in_month_gen,
- IF(infant_age_months!=0,infant_age_months,infant_age_weeks*12/52) as age_in_month,infant_age_months ,infant_weight,created_at from testing_mereenfant
- ) a123
- ) b123 where (b123.age_in_month_me BETWEEN 6 AND 60) and b123.date_me>=@start_date and b123.date_me<=@end_date and weight_me>0
- ) c12345 on a12345.id_patient=c12345.id_patient
- LEFT JOIN patient on patient.id=a12345.id_patient
- where ((b12345.id_patient is not null) or (c12345.id_patient is not null))
- ) a
- WHERE( (a.age_in_months BETWEEN 6 AND 60) or (a.age_in_month_me BETWEEN 6 AND 60)) )b
- where(nutrition_status='underweight' or nutrition_status='surunderweight' or nutrition_status_from_mereenfant='underweight' or nutrition_status_from_mereenfant='surunderweight' ) group by id_patient) malnou ) a
- RIGHT JOIN (
- select patient.id,patient.city_code,patient.hospital_code,patient.patient_code as patient_surveyed,patient.linked_to_id_patient,
- b1234.gender,b1234.dob,c1234.*,IF(b1234.gender is not null,IF(b1234.gender=1,"YES","NO"),IF(c1234.gender_me=1,"YES","NO")) as male,
- IF(b1234.gender is not null,IF(b1234.gender=2,"YES","NO"),IF(c1234.gender_me=2,"YES","NO")) as female from
- (
- select id_patient from tracking_followup f where f.date>=@start_date and f.date<=@end_date
- union select id_patient from testing_mereenfant m where m.date>=@start_date and m.date<=@end_date
- ) a1234
- LEFT JOIN
- (
- select id_patient,gender,dob
- from(
- select i.dob,i.gender,TIMESTAMPDIFF(MONTH,i.dob,f.date) as age_in_month,
- f.* from tracking_followup f left join tracking_infant i on i.id_patient=f.id_patient
- where (TIMESTAMPDIFF(MONTH,i.dob,f.date) between 6 and 60) && weight<>0
- and f.date between @start_date and @end_date group by f.id_patient) c
- ) b1234 on a1234.id_patient=b1234.id_patient
- LEFT JOIN (
- select * from(
- select id_patient,infant_gender as gender_me,infant_weight as weight_me,
- IF(age_in_month_gen<0,age_in_month,age_in_month_gen) as age_in_month_me,date_me from (
- select id, id_patient ,infant_gender, date as date_me,infant_dob,timestampdiff(month,infant_dob,date) as age_in_month_gen,
- IF(infant_age_months!=0,infant_age_months,infant_age_weeks*12/52) as age_in_month,infant_age_months ,infant_weight,created_at from testing_mereenfant
- ) a123
- ) b123 where (b123.age_in_month_me BETWEEN 6 AND 60) and b123.date_me>=@start_date and b123.date_me<=@end_date and weight_me>0
- ) c1234 on a1234.id_patient=c1234.id_patient
- LEFT JOIN patient on patient.id=a1234.id_patient
- where ((b1234.id_patient is not null) or (c1234.id_patient is not null)) group by a1234.id_patient
- ) b on a.patient_code=b.patient_surveyed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement