jeniferfleurant

OVC_SERV children (helper)

Apr 11th, 2019
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 8.69 KB | None | 0 0
  1. USE caris_db;
  2. SET  @start_date = '2019-07-01';
  3. SET @end_date = '2019-09-30';
  4. set
  5. @start_date_last = '2019-04-01';
  6. set
  7. @end_date_last = '2019-06-30';
  8. SELECT
  9.     patient_code,
  10.     IF(arv.id_patient IS NOT NULL,
  11.         'yes',
  12.         'no') AS on_arv,
  13.     IF(statut.id_patient IS NOT NULL,
  14.         'pos',
  15.         'neg') AS statut,
  16.     IF(followup_q1 IS NOT NULL
  17.             OR odk_q1 IS NOT NULL
  18.             OR arv_q1 IS NOT NULL
  19.             OR ques_q1 IS NOT NULL
  20.             OR club_q1 IS NOT NULL
  21.             OR mereenfant_q1 is not null
  22.             OR specimen_q1 is not null,
  23.         'yes',
  24.         'no') AS q1,
  25.     IF(followup_q1 IS NOT NULL, 'yes', 'no') AS followup_q1,
  26.     IF(odk_q1 IS NOT NULL, 'yes', 'no') AS odk_q1,
  27.     IF(arv_q1 IS NOT NULL, 'yes', 'no') AS arv_q1,
  28.     IF(ques_q1 IS NOT NULL, 'yes', 'no') AS ques_q1,
  29.     IF(club_q1 IS NOT NULL, 'yes', 'no') AS club_q1,
  30.     if(mereenfant_q1 is not null, 'yes','no') as mereenfant_q1,
  31.     if(specimen_q1 is not null, 'yes','no') as specimen_q1,
  32.     IF(followup_q2 IS NOT NULL
  33.             OR odk_q2 IS NOT NULL
  34.             OR arv_q2 IS NOT NULL
  35.             OR ques_q2 IS NOT NULL
  36.             OR club_q2 IS NOT NULL
  37.             or mereenfant_q2 is not null
  38.             or specimen_q2 is not null,
  39.         'yes',
  40.         'no') AS q2,
  41.     IF(followup_q2 IS NOT NULL, 'yes', 'no') AS followup_q2,
  42.     IF(odk_q2 IS NOT NULL, 'yes', 'no') AS odk_q2,
  43.     IF(arv_q2 IS NOT NULL, 'yes', 'no') AS arv_q2,
  44.     IF(ques_q2 IS NOT NULL, 'yes', 'no') AS ques_q2,
  45.     IF(club_q2 IS NOT NULL, 'yes', 'no') AS club_q2,
  46.     if(mereenfant_q2 is not null, 'yes','no') as mereenfant_q2,
  47.     if(specimen_q2 is not null, 'yes','no') as specimen_q2,
  48.     office
  49. FROM
  50.     (SELECT
  51.         *
  52.     FROM
  53.         (SELECT
  54.         id_patient
  55.     FROM
  56.         tracking_followup
  57.     WHERE
  58.         date BETWEEN @start_date_last AND @end_date UNION (SELECT DISTINCT
  59.         patient.id AS id_patient
  60.     FROM
  61.         openfn.odk_child_visit
  62.     LEFT JOIN patient ON patient.patient_code = odk_child_visit.patient_code
  63.     WHERE
  64.         date_of_visit BETWEEN @start_date_last AND @end_date
  65.             AND patient.id IS NOT NULL
  66.             AND is_available_at_time_visit = 1
  67.             AND linked_to_id_patient = 0) UNION SELECT
  68.         id_patient
  69.     FROM
  70.         tracking_regime
  71.     WHERE
  72.         (start_date BETWEEN @start_date_last AND @end_date or end_date BETWEEN @start_date_last AND @end_date)
  73.             AND category = 'regime_infant_treatment' UNION SELECT
  74.         id_patient
  75.     FROM
  76.         questionnaire_child
  77.     WHERE
  78.         date BETWEEN @start_date_last AND @end_date UNION SELECT
  79.         id_patient
  80.     FROM
  81.         testing_mereenfant
  82.     WHERE
  83.         date BETWEEN @start_date_last AND @end_date UNION SELECT
  84.         id_patient
  85.     FROM
  86.         testing_specimen
  87.     WHERE
  88.         date_blood_taken BETWEEN @start_date_last AND @end_date  UNION SELECT
  89.         id_patient
  90.     FROM
  91.         questionnaire_proftraining18
  92.     WHERE
  93.         date BETWEEN @start_date_last AND @end_date UNION ((SELECT
  94.         id_patient
  95.     FROM
  96.         session
  97.     LEFT JOIN club_session ON club_session.id = id_club_session
  98.     LEFT JOIN club ON club_session.id_club = club.id
  99.     WHERE
  100.         is_present = 1
  101.             AND (date BETWEEN @start_date AND @end_date
  102.             OR date BETWEEN @start_date_last AND @end_date_last)
  103.             AND club_type != 1))) o) bot
  104.         LEFT JOIN
  105.     (SELECT DISTINCT
  106.         id_patient AS followup_q1
  107.     FROM
  108.         tracking_followup
  109.     WHERE
  110.         date BETWEEN @start_date_last AND @end_date_last) x ON followup_q1 = bot.id_patient
  111.         left join
  112. (select distinct id_patient as specimen_q1 from testing_specimen WHERE
  113.         date_blood_taken BETWEEN @start_date_last AND @end_date_last) x2 ON specimen_q1 = bot.id_patient
  114.                 left join
  115. (select distinct id_patient as specimen_q2 from testing_specimen WHERE
  116.         date_blood_taken BETWEEN @start_date AND @end_date) x3 ON specimen_q2 = bot.id_patient
  117.         LEFT JOIN
  118.     (SELECT DISTINCT
  119.         id_patient AS followup_q2
  120.     FROM
  121.         tracking_followup
  122.     WHERE
  123.         date BETWEEN @start_date AND @end_date) y ON followup_q2 = bot.id_patient
  124.         LEFT JOIN
  125.     patient ON patient.id = bot.id_patient
  126.         LEFT JOIN
  127.     (SELECT DISTINCT
  128.         id_patient AS mereenfant_q1
  129.     FROM
  130.         testing_mereenfant
  131.     WHERE
  132.         date BETWEEN @start_date_last AND @end_date_last) me ON mereenfant_q1 = bot.id_patient
  133.         LEFT JOIN
  134.     (SELECT DISTINCT
  135.         id_patient AS mereenfant_q2
  136.     FROM
  137.         testing_mereenfant
  138.     WHERE
  139.         date BETWEEN @start_date AND @end_date) me2 ON mereenfant_q2 = bot.id_patient
  140.         LEFT JOIN
  141.     (SELECT DISTINCT
  142.         patient.id AS odk_q1
  143.     FROM
  144.         openfn.odk_child_visit
  145.     LEFT JOIN patient ON patient.patient_code = odk_child_visit.patient_code
  146.     WHERE
  147.         date_of_visit BETWEEN @start_date_last AND @end_date_last
  148.             AND patient.id IS NOT NULL
  149.             AND is_available_at_time_visit = 1
  150.             AND linked_to_id_patient = 0) z ON odk_q1 = bot.id_patient
  151.         LEFT JOIN
  152.     (SELECT DISTINCT
  153.         patient.id AS odk_q2
  154.     FROM
  155.         openfn.odk_child_visit
  156.     LEFT JOIN patient ON patient.patient_code = odk_child_visit.patient_code
  157.     WHERE
  158.         date_of_visit BETWEEN @start_date AND @end_date
  159.             AND patient.id IS NOT NULL
  160.             AND is_available_at_time_visit = 1
  161.             AND linked_to_id_patient = 0) zz ON odk_q2 = bot.id_patient
  162.         LEFT JOIN
  163.     (SELECT DISTINCT
  164.         id_patient AS arv_q2
  165.     FROM
  166.         tracking_regime
  167.     WHERE
  168.         start_date BETWEEN @start_date AND @end_date or end_date BETWEEN @start_date AND @end_date) a2 ON arv_q2 = bot.id_patient
  169.         LEFT JOIN
  170.     (SELECT DISTINCT
  171.         id_patient AS arv_q1
  172.     FROM
  173.         tracking_regime
  174.     WHERE
  175.         start_date BETWEEN @start_date_last AND @end_date_last or end_date BETWEEN @start_date_last AND @end_date_last) a1 ON arv_q1 = bot.id_patient
  176.         LEFT JOIN
  177.     (SELECT
  178.         id_patient AS ques_q1
  179.     FROM
  180.         questionnaire_child
  181.     WHERE
  182.         date BETWEEN @start_date_last AND @end_date_last UNION SELECT
  183.         id_patient AS ques_q1
  184.     FROM
  185.         questionnaire_proftraining18
  186.     WHERE
  187.         date BETWEEN @start_date_last AND @end_date_last) q1 ON ques_q1 = bot.id_patient
  188.         LEFT JOIN
  189.     (SELECT
  190.         id_patient AS ques_q2
  191.     FROM
  192.         questionnaire_child
  193.     WHERE
  194.         date BETWEEN @start_date AND @end_date UNION SELECT
  195.         id_patient AS ques_q2
  196.     FROM
  197.         questionnaire_proftraining18
  198.     WHERE
  199.         date BETWEEN @start_date AND @end_date) q2 ON ques_q2 = bot.id_patient
  200.         LEFT JOIN
  201.     (SELECT DISTINCT
  202.         id_patient AS club_q1
  203.     FROM
  204.         session
  205.     LEFT JOIN club_session ON club_session.id = id_club_session
  206.     LEFT JOIN club ON club_session.id_club = club.id
  207.     WHERE
  208.         is_present = 1
  209.             AND date BETWEEN @start_date_last AND @end_date_last
  210.             AND club_type != 1) c1 ON club_q1 = bot.id_patient
  211.         LEFT JOIN
  212.     (SELECT DISTINCT
  213.         id_patient AS club_q2
  214.     FROM
  215.         session
  216.     LEFT JOIN club_session ON club_session.id = id_club_session
  217.     LEFT JOIN club ON club_session.id_club = club.id
  218.     WHERE
  219.         is_present = 1
  220.             AND date BETWEEN @start_date AND @end_date
  221.             AND club_type != 1) c2 ON club_q2 = bot.id_patient
  222.         LEFT JOIN
  223.     lookup_hospital ON CONCAT(lookup_hospital.city_code,
  224.             '/',
  225.             lookup_hospital.hospital_code) = LEFT(patient_code, 8)
  226.         LEFT JOIN
  227.     (SELECT DISTINCT
  228.         id_patient
  229.     FROM
  230.         tracking_regime
  231.     WHERE
  232.             category = 'regime_infant_treatment') arv ON arv.id_patient = bot.id_patient
  233.         LEFT JOIN
  234.     (SELECT
  235.         id_patient
  236.     FROM
  237.         tracking_followup UNION SELECT
  238.         id_patient
  239.     FROM
  240.         tracking_regime
  241.     WHERE
  242.         category = 'regime_infant_treatment' UNION SELECT
  243.         id_patient
  244.     FROM
  245.         club_patient UNION SELECT
  246.         id_patient
  247.     FROM
  248.         questionnaire_child UNION SELECT
  249.         id_patient
  250.     FROM
  251.         questionnaire_proftraining18 UNION SELECT
  252.         id_patient
  253.     FROM
  254.         testing_specimen
  255.     WHERE
  256.         pcr_result = 1 UNION SELECT
  257.         id_patient
  258.     FROM
  259.         testing_result
  260.     WHERE
  261.         result = 1 UNION SELECT
  262.         id_patient
  263.     FROM
  264.         tracking_infant
  265.     WHERE
  266.         positive_pcr_1 != '0000-00-00'
  267.             OR positive_pcr_2 != '0000-00-00') statut ON statut.id_patient = bot.id_patient
  268. WHERE
  269.     linked_to_id_patient = 0
Advertisement
Add Comment
Please, Sign In to add comment