jeniferfleurant

OVC_SERV Mother/PTME 1quarter

Jul 25th, 2019
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.70 KB | None | 0 0
  1. USE caris_db;
  2. SET @start_date="2019-04-01";
  3. SET @end_date="2019-06-30";
  4. SELECT
  5.     a.patient_code,
  6.     dob,
  7.     IF(TIMESTAMPDIFF(YEAR, dob, @start_date) >= 25,
  8.         '25+',
  9.         IF(TIMESTAMPDIFF(YEAR, dob, @start_date) < 25
  10.                 AND TIMESTAMPDIFF(YEAR, dob, @start_date) >= 18,
  11.             '18-24',
  12.             IF(TIMESTAMPDIFF(YEAR, dob, @start_date) < 18
  13.                     AND TIMESTAMPDIFF(YEAR, dob, @start_date) >= 15,
  14.                 '15 17',
  15.                 IF(TIMESTAMPDIFF(YEAR, dob, @start_date) < 15
  16.                         AND TIMESTAMPDIFF(YEAR, dob, @start_date) >= 10,
  17.                     '10 15',
  18.                     IF(TIMESTAMPDIFF(YEAR, dob, @start_date) < 10
  19.                             AND TIMESTAMPDIFF(YEAR, dob, @start_date) >= 1,
  20.                         '1 9',
  21.                         ''))))) AS tranche
  22. FROM
  23.     (SELECT DISTINCT
  24.         patient_code
  25.     FROM
  26.         (SELECT
  27.         patient_code
  28.     FROM
  29.         tracking_motherbasicinfo
  30.     LEFT JOIN patient ON patient.id = tracking_motherbasicinfo.id_patient
  31.     LEFT JOIN (SELECT DISTINCT
  32.         id_patient
  33.     FROM
  34.         tracking_regime
  35.     WHERE
  36.         category = 'regime_mother_treatment'
  37.             AND (start_date BETWEEN @start_date AND @end_date
  38.             OR end_date BETWEEN @start_date AND @end_date)) z ON z.id_patient = tracking_motherbasicinfo.id_patient
  39.     LEFT JOIN (SELECT
  40.         id_patient
  41.     FROM
  42.         questionnaire_motherhivknowledge
  43.     WHERE
  44.         date BETWEEN @start_date AND @end_date UNION SELECT
  45.         id_patient
  46.     FROM
  47.         questionnaire_newmotherhivknowledge
  48.     WHERE
  49.         date BETWEEN @start_date AND @end_date UNION SELECT
  50.         id_patient
  51.     FROM
  52.         questionnaire_mothersurvey
  53.     WHERE
  54.         date BETWEEN @start_date AND @end_date) x ON x.id_patient = tracking_motherbasicinfo.id_patient
  55.     LEFT JOIN (SELECT DISTINCT
  56.         session.id_patient
  57.     FROM
  58.         session
  59.     LEFT JOIN club_session ON club_session.id = session.id_club_session
  60.     WHERE
  61.         is_present = 1
  62.             AND club_session.date BETWEEN @start_date AND @end_date) y ON y.id_patient = tracking_motherbasicinfo.id_patient
  63.     LEFT JOIN tracking_pregnancy on tracking_pregnancy.id_patient_mother = tracking_motherbasicinfo.id_patient
  64.     LEFT JOIN (SELECT
  65.         patient.id AS id_patient
  66.     FROM
  67.         tracking_motherbasicinfo
  68.     LEFT JOIN patient ON patient.id = tracking_motherbasicinfo.id_patient
  69.     LEFT JOIN testing_mereenfant ON CONCAT(testing_mereenfant.mother_city_code, '/', testing_mereenfant.mother_hospital_code, '/', testing_mereenfant.mother_code) = patient_code
  70.     WHERE
  71.         date BETWEEN @start_date AND @end_date
  72.             AND patient_code IS NOT NULL
  73.     GROUP BY patient_code) bb ON bb.id_patient = tracking_motherbasicinfo.id_patient
  74.     WHERE
  75.         patient.id IS NOT NULL
  76.             AND (x.id_patient IS NOT NULL
  77.             OR PTME_date BETWEEN @start_date AND @end_date
  78.             OR tracking_pregnancy.actual_delivery_date BETWEEN @start_date AND @end_date
  79.             or ptme_enrollment_date BETWEEN @start_date AND @end_date
  80.             OR y.id_patient IS NOT NULL
  81.             OR z.id_patient IS NOT NULL
  82.             OR bb.id_patient IS NOT NULL)) c UNION SELECT DISTINCT
  83.         health_id AS patient_code
  84.     FROM
  85.         openfn.odk_pregnancy_visit
  86.     WHERE
  87.         date_of_visit BETWEEN @start_date AND @end_date) a
  88.         LEFT JOIN
  89.     patient ON patient.patient_code = a.patient_code
  90.         LEFT JOIN
  91.     tracking_motherbasicinfo ON tracking_motherbasicinfo.id_patient = patient.id
  92. WHERE
  93.     patient.linked_to_id_patient IS NULL
  94.         OR patient.linked_to_id_patient = 0
  95. GROUP BY a.patient_code
Advertisement
Add Comment
Please, Sign In to add comment