Advertisement
jeniferfleurant

OVC_SERV mother/ptme (2019)

Mar 26th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.66 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.         tracking_motherbasicinfo.id_patient
  66.     FROM
  67.         testing_mereenfant
  68.     LEFT JOIN patient ON patient.patient_code = CONCAT(mother_city_code, '/', mother_hospital_code, '/', mother_code)
  69.     LEFT JOIN tracking_motherbasicinfo ON tracking_motherbasicinfo.id_patient = patient.id
  70.     WHERE
  71.         patient.id IS NOT NULL and linked_to_id_patient = 0
  72.             AND date BETWEEN @start_date AND @end_date) abc ON abc.id_patient = tracking_motherbasicinfo.id_patient
  73.     WHERE
  74.         patient.id IS NOT NULL
  75.             AND (x.id_patient IS NOT NULL
  76.             OR PTME_date BETWEEN @start_date AND @end_date
  77.             OR tracking_pregnancy.actual_delivery_date BETWEEN @start_date AND @end_date
  78.             OR tracking_pregnancy.ptme_enrollment_date BETWEEN @start_date AND @end_date
  79.             OR y.id_patient IS NOT NULL
  80.             OR z.id_patient IS NOT NULL
  81.             OR abc.id_patient IS NOT NULL)) c UNION SELECT DISTINCT
  82.         health_id AS patient_code
  83.     FROM
  84.         openfn.odk_pregnancy_visit
  85.     WHERE
  86.         date_of_visit BETWEEN @start_date AND @end_date) a
  87.         LEFT JOIN
  88.     patient ON patient.patient_code = a.patient_code
  89.         LEFT JOIN
  90.     tracking_motherbasicinfo ON tracking_motherbasicinfo.id_patient = patient.id
  91.     where linked_to_id_patient = 0 or linked_to_id_patient is null
  92. GROUP BY a.patient_code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement