jeniferfleurant

OVC_SERV mother (helper)

Apr 24th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 8.46 KB | None | 0 0
  1. USE caris_db;
  2. SET  @start_date = '2019-07-01';
  3. SET @end_date = '2019-09-30';
  4. set @start_date_last = '2019-04-01';
  5. set @end_date_last = '2019-06-30';
  6. SELECT
  7.     bot.patient_code,
  8.     IF(club_q1 IS NOT NULL,
  9.         'yes',
  10.         IF(quest_q1 IS NOT NULL,
  11.             'yes',
  12.             IF(arv_1 IS NOT NULL,
  13.                 'yes',
  14.                 IF(odk_1 IS NOT NULL,
  15.                     'yes',
  16.                     IF(ptme_1 IS NOT NULL,
  17.                         'yes',
  18.                         IF(ptme1_1 IS NOT NULL,
  19.                             'yes',
  20.                             IF(mereenfant_1 IS NOT NULL,
  21.                                 'yes',
  22.                                 'no'))))))) AS q1,
  23.     IF(club_q2 IS NOT NULL,
  24.         'yes',
  25.         IF(quest_q2 IS NOT NULL,
  26.             'yes',
  27.             IF(arv_2 IS NOT NULL,
  28.                 'yes',
  29.                 IF(odk_2 IS NOT NULL,
  30.                     'yes',
  31.                     IF(ptme_2 IS NOT NULL,
  32.                         'yes',
  33.                         IF(ptme1_2 IS NOT NULL,
  34.                             'yes',
  35.                             IF(mereenfant_2 IS NOT NULL,
  36.                                 'yes',
  37.                                 'no'))))))) AS q2,
  38.     IF(club_q1 IS NOT NULL, 'yes', 'no') AS club_q1,
  39.     IF(club_q2 IS NOT NULL, 'yes', 'no') AS club_q2,
  40.     IF(quest_q1 IS NOT NULL, 'yes', 'no') AS quest_q1,
  41.     IF(quest_q2 IS NOT NULL, 'yes', 'no') AS quest_q2,
  42.     IF(arv_1 IS NOT NULL, 'yes', 'no') AS on_arv_q1,
  43.     IF(arv_2 IS NOT NULL, 'yes', 'no') AS on_arv_q2,
  44.     IF(odk_1 IS NOT NULL, 'yes', 'no') AS odk_q1,
  45.     IF(odk_2 IS NOT NULL, 'yes', 'no') AS odk_q2,
  46.     IF(COALESCE(ptme_1, ptme1_1) IS NOT NULL,
  47.         'yes',
  48.         'no') AS ptme_q1,
  49.     IF(COALESCE(ptme_2, ptme1_2) IS NOT NULL,
  50.         'yes',
  51.         'no') AS ptme_q2,
  52.     IF(mereenfant_1 IS NOT NULL,
  53.         'yes',
  54.         'no') AS mereenfant_q1,
  55.     IF(mereenfant_2 IS NOT NULL,
  56.         'yes',
  57.         'no') AS mereenfant_2,
  58.     office
  59. FROM
  60.     (SELECT
  61.         *
  62.     FROM
  63.         (SELECT
  64.         patient_code
  65.     FROM
  66.         tracking_motherbasicinfo
  67.     LEFT JOIN patient ON patient.id = tracking_motherbasicinfo.id_patient) o UNION SELECT
  68.         health_id AS patient_code
  69.     FROM
  70.         openfn.odk_pregnancy_visit
  71.     WHERE
  72.         date_of_visit BETWEEN @start_date_last AND @end_date) bot
  73.         LEFT JOIN
  74.     (SELECT DISTINCT
  75.         patient_code AS club_q2
  76.     FROM
  77.         session
  78.     LEFT JOIN club_session ON club_session.id = session.id_club_session
  79.     LEFT JOIN patient ON patient.id = session.id_patient
  80.     WHERE
  81.         is_present = 1
  82.             AND club_session.date BETWEEN @start_date AND @end_date) c2 ON club_q2 = bot.patient_code
  83.         LEFT JOIN
  84.     patient ON patient.patient_code = bot.patient_code
  85.         LEFT JOIN
  86.     (SELECT DISTINCT
  87.         patient_code AS club_q1
  88.     FROM
  89.         session
  90.     LEFT JOIN club_session ON club_session.id = session.id_club_session
  91.     LEFT JOIN patient ON patient.id = session.id_patient
  92.     WHERE
  93.         is_present = 1
  94.             AND club_session.date BETWEEN @start_date_last AND @end_date_last) c1 ON club_q1 = bot.patient_code
  95.         LEFT JOIN
  96.     (SELECT
  97.         patient_code AS quest_q1, date
  98.     FROM
  99.         (SELECT
  100.         id_patient, date
  101.     FROM
  102.         questionnaire_motherhivknowledge UNION SELECT
  103.         id_patient, date
  104.     FROM
  105.         questionnaire_mothersurvey UNION SELECT
  106.         id_patient, date
  107.     FROM
  108.         questionnaire_newmotherhivknowledge) x
  109.     LEFT JOIN patient ON patient.id = x.id_patient
  110.     WHERE
  111.         date BETWEEN @start_date_last AND @end_date_last) quest ON quest_q1 = bot.patient_code
  112.         LEFT JOIN
  113.     (SELECT
  114.         patient_code AS quest_q2
  115.     FROM
  116.         (SELECT
  117.         id_patient
  118.     FROM
  119.         questionnaire_motherhivknowledge
  120.     WHERE
  121.         date BETWEEN @start_date AND @end_date UNION SELECT
  122.         id_patient
  123.     FROM
  124.         questionnaire_mothersurvey
  125.     WHERE
  126.         date BETWEEN @start_date AND @end_date UNION SELECT
  127.         id_patient
  128.     FROM
  129.         questionnaire_newmotherhivknowledge
  130.     WHERE
  131.         date BETWEEN @start_date AND @end_date) y
  132.     LEFT JOIN patient ON patient.id = y.id_patient) ques ON quest_q2 = bot.patient_code
  133.         LEFT JOIN
  134.     lookup_hospital ON CONCAT(lookup_hospital.city_code,
  135.             '/',
  136.             lookup_hospital.hospital_code) = LEFT(patient.patient_code, 8)
  137.         LEFT JOIN
  138.     (SELECT DISTINCT
  139.         patient_code AS arv_1
  140.     FROM
  141.         tracking_regime
  142.     LEFT JOIN patient ON patient.id = id_patient
  143.     WHERE
  144.         (start_date BETWEEN @start_date_last AND @end_date_last
  145.             OR end_date BETWEEN @start_date_last AND @end_date_last)
  146.             AND category = 'regime_mother_treatment') ar ON arv_1 = bot.patient_code
  147.         LEFT JOIN
  148.     (SELECT DISTINCT
  149.         patient_code AS arv_2
  150.     FROM
  151.         tracking_regime
  152.     LEFT JOIN patient ON patient.id = id_patient
  153.     WHERE
  154.         (start_date BETWEEN @start_date AND @end_date
  155.             OR end_date BETWEEN @start_date AND @end_date)
  156.             AND category = 'regime_mother_treatment') arv ON arv_2 = bot.patient_code
  157.         LEFT JOIN
  158.     (SELECT DISTINCT
  159.         health_id AS odk_1
  160.     FROM
  161.         openfn.odk_pregnancy_visit
  162.     WHERE
  163.         date_of_visit BETWEEN @start_date_last AND @end_date_last) odk1 ON odk_1 = bot.patient_code
  164.         LEFT JOIN
  165.     (SELECT DISTINCT
  166.         health_id AS odk_2
  167.     FROM
  168.         openfn.odk_pregnancy_visit
  169.     WHERE
  170.         date_of_visit BETWEEN @start_date AND @end_date) odk2 ON odk_2 = bot.patient_code
  171.         LEFT JOIN
  172.     (SELECT DISTINCT
  173.         patient_code AS ptme_1
  174.     FROM
  175.         tracking_pregnancy
  176.     LEFT JOIN patient ON patient.id = id_patient_mother
  177.     WHERE
  178.         (ptme_enrollment_date BETWEEN @start_date_last AND @end_date_last)
  179.             OR (actual_delivery_date BETWEEN @start_date_last AND @end_date_last)) ptme1 ON ptme_1 = bot.patient_code
  180.         LEFT JOIN
  181.     (SELECT DISTINCT
  182.         patient_code AS ptme_2
  183.     FROM
  184.         tracking_pregnancy
  185.     LEFT JOIN patient ON patient.id = id_patient_mother
  186.     WHERE
  187.         (ptme_enrollment_date BETWEEN @start_date AND @end_date)
  188.             OR (actual_delivery_date BETWEEN @start_date AND @end_date)) ptme2 ON ptme_2 = bot.patient_code
  189.         LEFT JOIN
  190.     (SELECT DISTINCT
  191.         patient_code AS ptme1_1
  192.     FROM
  193.         tracking_motherbasicinfo
  194.     LEFT JOIN patient ON patient.id = id_patient
  195.     WHERE
  196.         PTME_date BETWEEN @start_date_last AND @end_date_last) pt1 ON ptme1_1 = bot.patient_code
  197.         LEFT JOIN
  198.     (SELECT DISTINCT
  199.         patient_code AS ptme1_2
  200.     FROM
  201.         tracking_motherbasicinfo
  202.     LEFT JOIN patient ON patient.id = id_patient
  203.     WHERE
  204.         PTME_date BETWEEN @start_date AND @end_date) pt2 ON ptme1_2 = bot.patient_code
  205.         LEFT JOIN
  206.     (SELECT
  207.         patient.patient_code AS mereenfant_1
  208.     FROM
  209.         tracking_motherbasicinfo
  210.     LEFT JOIN patient ON patient.id = tracking_motherbasicinfo.id_patient
  211.     LEFT JOIN testing_mereenfant ON CONCAT(testing_mereenfant.mother_city_code, '/', testing_mereenfant.mother_hospital_code, '/', testing_mereenfant.mother_code) = patient_code
  212.     WHERE
  213.         date BETWEEN @start_date_last AND @end_date_last
  214.             AND patient_code IS NOT NULL
  215.     GROUP BY patient_code) me1 ON mereenfant_1 = bot.patient_code
  216.         LEFT JOIN
  217.     (SELECT
  218.         patient.patient_code AS mereenfant_2
  219.     FROM
  220.         tracking_motherbasicinfo
  221.     LEFT JOIN patient ON patient.id = tracking_motherbasicinfo.id_patient
  222.     LEFT JOIN testing_mereenfant ON CONCAT(testing_mereenfant.mother_city_code, '/', testing_mereenfant.mother_hospital_code, '/', testing_mereenfant.mother_code) = patient_code
  223.     WHERE
  224.         date BETWEEN @start_date AND @end_date
  225.             AND patient_code IS NOT NULL
  226.     GROUP BY patient_code) me2 ON mereenfant_2 = bot.patient_code
  227. WHERE
  228.     (linked_to_id_patient = 0 or linked_to_id_patient is null)
  229.         AND (club_q1 IS NOT NULL
  230.         OR club_q2 IS NOT NULL
  231.         OR quest_q1 IS NOT NULL
  232.         OR quest_q2 IS NOT NULL
  233.         OR arv_1 IS NOT NULL
  234.         OR arv_2 IS NOT NULL
  235.         OR odk_1 IS NOT NULL
  236.         OR odk_2 IS NOT NULL
  237.         OR ptme_1 IS NOT NULL
  238.         OR ptme_2 IS NOT NULL
  239.         OR ptme1_1 IS NOT NULL
  240.         OR ptme1_2 IS NOT NULL
  241.         OR mereenfant_1 IS NOT NULL
  242.         OR mereenfant_2 IS NOT NULL)
  243. GROUP BY bot.patient_code
Add Comment
Please, Sign In to add comment