jeniferfleurant

OVC_SERV children detailed (2019)

Mar 25th, 2019
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 4.90 KB | None | 0 0
  1. USE caris_db;
  2. SET #start_date for Q1
  3.   @start_date = "2019-04-01";
  4. SET #end_date for Q1
  5.   @end_date = "2019-06-30";
  6. SET #start_date for Q2
  7.     @start_date_curr = "2019-07-01";
  8. SET #end_date for Q2
  9.     @end_date_curr = "2019-09-30";
  10. select patient_code, id, mereenfant_1, mereenfant_2, specimen_1, specimen_2, followup_1, followup_2, arv_1, arv_2, quest_1, quest_2, odk_1, odk_2, club_1, club_2
  11. from patient p
  12. left join
  13. (select id_patient, mereenfant_1, mereenfant_2  from testing_mereenfant tm
  14. left join (select id_patient as mereenfant_1 from testing_mereenfant where (date between @start_date and @end_date))x1 on tm.id_patient = mereenfant_1
  15. left join (select id_patient as mereenfant_2 from testing_mereenfant where (date between @start_date_curr and @end_date_curr))x2 on tm.id_patient = mereenfant_2
  16. )a on p.id = a.id_patient
  17. left join
  18. (
  19. select id_patient, specimen_1, specimen_2 from testing_specimen ts
  20. left join (select distinct id_patient as specimen_1 from testing_specimen where (date_blood_taken between @start_date and @end_date))y1 on ts.id_patient = specimen_1
  21. left join (select distinct id_patient as specimen_2 from testing_specimen where (date_blood_taken between @start_date_curr and @end_date_curr))y2 on ts.id_patient = specimen_2
  22.  
  23. )b on p.id = b.id_patient
  24. left join
  25. (
  26. select distinct id_patient, followup_1, followup_2 from tracking_followup tf
  27. left join (select distinct id_patient as followup_1 from tracking_followup where (date between @start_date and @end_date))z1 on tf.id_patient = followup_1
  28. left join (select distinct id_patient as followup_2 from tracking_followup where (date between @start_date_curr and @end_date_curr))z2 on tf.id_patient = followup_2
  29.  
  30. )c on p.id = c.id_patient
  31. left join
  32. (
  33. select distinct id_patient, arv_1, arv_2 from tracking_regime tr
  34. left join (select distinct id_patient as arv_1 from tracking_regime where category = 'regime_infant_treatment' and (start_date between @start_date and @end_date or end_date between @start_date and @end_date))zz1 on tr.id_patient = arv_1
  35. left join (select distinct id_patient as arv_2 from tracking_regime where category = 'regime_infant_treatment' and (start_date between @start_date_curr and @end_date_curr or end_date between @start_date_curr and @end_date_curr))zz2 on tr.id_patient = arv_2
  36. where category = 'regime_infant_treatment')d on p.id = d.id_patient
  37. left join
  38. (
  39. select distinct id_patient, quest_1, quest_2 from questionnaire_child qc
  40. left join (select distinct id_patient as quest_1 from questionnaire_child where (date between @start_date and @end_date))zq1 on qc.id_patient = quest_1
  41. left join (select distinct id_patient as quest_2 from questionnaire_child where (date between @start_date_curr and @end_date_curr))zq2 on qc.id_patient = quest_2
  42. )e on p.id = e.id_patient
  43. left join
  44. (
  45. select distinct patient.id as id_patient, odk_1, odk_2 from openfn.odk_child_visit odk
  46. left join patient on patient.patient_code = odk.patient_code
  47. left join (select distinct patient.id as odk_1 from openfn.odk_child_visit odk left join patient on patient.patient_code = odk.patient_code where date_of_visit between @start_date and @end_date AND is_available_at_time_visit = 1
  48.             )za1 on patient.id = odk_1
  49. left join (select distinct patient.id as odk_2 from openfn.odk_child_visit odk left join patient on patient.patient_code = odk.patient_code where date_of_visit between @start_date_curr and @end_date_curr and is_available_at_time_visit = 1
  50.             )za2 on patient.id = odk_2
  51. where patient.id is not null AND linked_to_id_patient = 0
  52. )f on p.id = f.id_patient
  53. left join
  54. (
  55. select distinct id_patient, club_1, club_2 from session s
  56. left join club_session on club_session.id = id_club_session
  57. left join club on club_session.id_club=club.id
  58. left join
  59. (SELECT DISTINCT
  60.         id_patient AS club_1
  61.     FROM
  62.         session
  63.     LEFT JOIN club_session ON club_session.id = id_club_session
  64.     LEFT JOIN club ON club_session.id_club = club.id
  65.     WHERE
  66.         is_present = 1
  67.             AND date BETWEEN @start_date AND @end_date
  68.             AND club_type != 1)zf1 on club_1 = s.id_patient
  69.             left join
  70. (SELECT DISTINCT
  71.         id_patient AS club_2
  72.     FROM
  73.         session
  74.     LEFT JOIN club_session ON club_session.id = id_club_session
  75.     LEFT JOIN club ON club_session.id_club = club.id
  76.     WHERE
  77.         is_present = 1
  78.             AND date BETWEEN @start_date_curr AND @end_date_curr
  79.             AND club_type != 1)zf2 on club_2 = s.id_patient
  80. where #date between @start_date and @end_date_curr and
  81. club_type !=1 and is_present =1
  82. )g on p.id = g.id_patient
  83.  
  84. where (mereenfant_1 is not null
  85. or
  86. specimen_1 is not null or followup_1 is not null or arv_1 is not null
  87. or quest_1 is not null or odk_1 is not null or club_1 is not null
  88. )
  89. and (mereenfant_2 is not null
  90. or
  91. specimen_2 is not null or followup_2 is not null or arv_2 is not null
  92. or quest_2 is not null or odk_2 is not null or club_2
  93. )
  94.  
  95. group by p.id
Advertisement
Add Comment
Please, Sign In to add comment