jeniferfleurant

OVC_SERV children (newly added)

Mar 26th, 2019
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 24.76 KB | None | 0 0
  1. USE caris_db;
  2. SET
  3.   @start_date = "2019-01-01";
  4. SET
  5.   @end_date = "2019-03-31";
  6. SELECT
  7.     patient_code,
  8.     COALESCE(ti.dob, tm.infant_dob, ts.date_of_birth) AS dob,
  9.     IF(TIMESTAMPDIFF(YEAR,
  10.             COALESCE(IF(tm.infant_dob = '0000-00-00',
  11.                         NULL,
  12.                         tm.infant_dob),
  13.                     IF(ts.date_of_birth = '0000-00-00',
  14.                         NULL,
  15.                         ts.date_of_birth),
  16.                     ti.dob),
  17.             @start_date) < 1
  18.             AND COALESCE(IF(tm.infant_dob = '0000-00-00',
  19.                     NULL,
  20.                     tm.infant_dob),
  21.                 IF(ts.date_of_birth = '0000-00-00',
  22.                     NULL,
  23.                     ts.date_of_birth),
  24.                 ti.dob) != '0000-00-00',
  25.         'under1',
  26.         IF(TIMESTAMPDIFF(YEAR,
  27.                 COALESCE(IF(tm.infant_dob = '0000-00-00',
  28.                             NULL,
  29.                             tm.infant_dob),
  30.                         IF(ts.date_of_birth = '0000-00-00',
  31.                             NULL,
  32.                             ts.date_of_birth),
  33.                         ti.dob),
  34.                 @start_date) >= 1
  35.                 AND TIMESTAMPDIFF(YEAR,
  36.                 COALESCE(IF(tm.infant_dob = '0000-00-00',
  37.                             NULL,
  38.                             tm.infant_dob),
  39.                         IF(ts.date_of_birth = '0000-00-00',
  40.                             NULL,
  41.                             ts.date_of_birth),
  42.                         ti.dob),
  43.                 @start_date) < 10
  44.                 AND COALESCE(IF(tm.infant_dob = '0000-00-00',
  45.                         NULL,
  46.                         tm.infant_dob),
  47.                     IF(ts.date_of_birth = '0000-00-00',
  48.                         NULL,
  49.                         ts.date_of_birth),
  50.                     ti.dob) != '0000-00-00',
  51.             'bt_1_9',
  52.             IF(TIMESTAMPDIFF(YEAR,
  53.                     COALESCE(IF(tm.infant_dob = '0000-00-00',
  54.                                 NULL,
  55.                                 tm.infant_dob),
  56.                             IF(ts.date_of_birth = '0000-00-00',
  57.                                 NULL,
  58.                                 ts.date_of_birth),
  59.                             ti.dob),
  60.                     @start_date) < 1
  61.                     AND COALESCE(IF(tm.infant_dob = '0000-00-00',
  62.                             NULL,
  63.                             tm.infant_dob),
  64.                         IF(ts.date_of_birth = '0000-00-00',
  65.                             NULL,
  66.                             ts.date_of_birth),
  67.                         ti.dob) != '0000-00-00'
  68.                     AND (ti.gender = 1 OR tm.infant_gender = 1),
  69.                 'm_under1',
  70.                 IF(TIMESTAMPDIFF(YEAR,
  71.                         COALESCE(IF(tm.infant_dob = '0000-00-00',
  72.                                     NULL,
  73.                                     tm.infant_dob),
  74.                                 IF(ts.date_of_birth = '0000-00-00',
  75.                                     NULL,
  76.                                     ts.date_of_birth),
  77.                                 ti.dob),
  78.                         @start_date) < 1
  79.                         AND COALESCE(IF(tm.infant_dob = '0000-00-00',
  80.                                 NULL,
  81.                                 tm.infant_dob),
  82.                             IF(ts.date_of_birth = '0000-00-00',
  83.                                 NULL,
  84.                                 ts.date_of_birth),
  85.                             ti.dob) != '0000-00-00'
  86.                         AND (ti.gender = 2 OR tm.infant_gender = 2),
  87.                     'f_under1',
  88.                     IF(TIMESTAMPDIFF(YEAR,
  89.                             COALESCE(IF(tm.infant_dob = '0000-00-00',
  90.                                         NULL,
  91.                                         tm.infant_dob),
  92.                                     IF(ts.date_of_birth = '0000-00-00',
  93.                                         NULL,
  94.                                         ts.date_of_birth),
  95.                                     ti.dob),
  96.                             @start_date) >= 1
  97.                             AND TIMESTAMPDIFF(YEAR,
  98.                             COALESCE(IF(tm.infant_dob = '0000-00-00',
  99.                                         NULL,
  100.                                         tm.infant_dob),
  101.                                     IF(ts.date_of_birth = '0000-00-00',
  102.                                         NULL,
  103.                                         ts.date_of_birth),
  104.                                     ti.dob),
  105.                             @start_date) < 10
  106.                             AND COALESCE(IF(tm.infant_dob = '0000-00-00',
  107.                                     NULL,
  108.                                     tm.infant_dob),
  109.                                 IF(ts.date_of_birth = '0000-00-00',
  110.                                     NULL,
  111.                                     ts.date_of_birth),
  112.                                 ti.dob) != '0000-00-00'
  113.                             AND (ti.gender = 1 OR tm.infant_gender = 1),
  114.                         'm_bt_1_9',
  115.                         IF(TIMESTAMPDIFF(YEAR,
  116.                                 COALESCE(IF(tm.infant_dob = '0000-00-00',
  117.                                             NULL,
  118.                                             tm.infant_dob),
  119.                                         IF(ts.date_of_birth = '0000-00-00',
  120.                                             NULL,
  121.                                             ts.date_of_birth),
  122.                                         ti.dob),
  123.                                 @start_date) >= 1
  124.                                 AND TIMESTAMPDIFF(YEAR,
  125.                                 COALESCE(IF(tm.infant_dob = '0000-00-00',
  126.                                             NULL,
  127.                                             tm.infant_dob),
  128.                                         IF(ts.date_of_birth = '0000-00-00',
  129.                                             NULL,
  130.                                             ts.date_of_birth),
  131.                                         ti.dob),
  132.                                 @start_date) < 10
  133.                                 AND COALESCE(IF(tm.infant_dob = '0000-00-00',
  134.                                         NULL,
  135.                                         tm.infant_dob),
  136.                                     IF(ts.date_of_birth = '0000-00-00',
  137.                                         NULL,
  138.                                         ts.date_of_birth),
  139.                                     ti.dob) != '0000-00-00'
  140.                                 AND (ti.gender = 2 OR tm.infant_gender = 2),
  141.                             'f_bt_1_9',
  142.                             IF(TIMESTAMPDIFF(YEAR,
  143.                                     COALESCE(IF(tm.infant_dob = '0000-00-00',
  144.                                                 NULL,
  145.                                                 tm.infant_dob),
  146.                                             IF(ts.date_of_birth = '0000-00-00',
  147.                                                 NULL,
  148.                                                 ts.date_of_birth),
  149.                                             ti.dob),
  150.                                     @start_date) >= 10
  151.                                     AND TIMESTAMPDIFF(YEAR,
  152.                                     COALESCE(IF(tm.infant_dob = '0000-00-00',
  153.                                                 NULL,
  154.                                                 tm.infant_dob),
  155.                                             IF(ts.date_of_birth = '0000-00-00',
  156.                                                 NULL,
  157.                                                 ts.date_of_birth),
  158.                                             ti.dob),
  159.                                     @start_date) < 15
  160.                                     AND COALESCE(IF(tm.infant_dob = '0000-00-00',
  161.                                             NULL,
  162.                                             tm.infant_dob),
  163.                                         IF(ts.date_of_birth = '0000-00-00',
  164.                                             NULL,
  165.                                             ts.date_of_birth),
  166.                                         ti.dob) != '0000-00-00'
  167.                                     AND (ti.gender = 1 OR tm.infant_gender = 1),
  168.                                 'm_bt_10_14',
  169.                                 IF(TIMESTAMPDIFF(YEAR,
  170.                                         COALESCE(IF(tm.infant_dob = '0000-00-00',
  171.                                                     NULL,
  172.                                                     tm.infant_dob),
  173.                                                 IF(ts.date_of_birth = '0000-00-00',
  174.                                                     NULL,
  175.                                                     ts.date_of_birth),
  176.                                                 ti.dob),
  177.                                         @start_date) >= 10
  178.                                         AND TIMESTAMPDIFF(YEAR,
  179.                                         COALESCE(IF(tm.infant_dob = '0000-00-00',
  180.                                                     NULL,
  181.                                                     tm.infant_dob),
  182.                                                 IF(ts.date_of_birth = '0000-00-00',
  183.                                                     NULL,
  184.                                                     ts.date_of_birth),
  185.                                                 ti.dob),
  186.                                         @start_date) < 15
  187.                                         AND COALESCE(IF(tm.infant_dob = '0000-00-00',
  188.                                                 NULL,
  189.                                                 tm.infant_dob),
  190.                                             IF(ts.date_of_birth = '0000-00-00',
  191.                                                 NULL,
  192.                                                 ts.date_of_birth),
  193.                                             ti.dob) != '0000-00-00'
  194.                                         AND (ti.gender = 2 OR tm.infant_gender = 2),
  195.                                     'f_bt_10_14',
  196.                                     IF(TIMESTAMPDIFF(YEAR,
  197.                                             COALESCE(IF(tm.infant_dob = '0000-00-00',
  198.                                                         NULL,
  199.                                                         tm.infant_dob),
  200.                                                     IF(ts.date_of_birth = '0000-00-00',
  201.                                                         NULL,
  202.                                                         ts.date_of_birth),
  203.                                                     ti.dob),
  204.                                             @start_date) >= 15
  205.                                             AND TIMESTAMPDIFF(YEAR,
  206.                                             COALESCE(IF(tm.infant_dob = '0000-00-00',
  207.                                                         NULL,
  208.                                                         tm.infant_dob),
  209.                                                     IF(ts.date_of_birth = '0000-00-00',
  210.                                                         NULL,
  211.                                                         ts.date_of_birth),
  212.                                                     ti.dob),
  213.                                             @start_date) < 18
  214.                                             AND COALESCE(IF(tm.infant_dob = '0000-00-00',
  215.                                                     NULL,
  216.                                                     tm.infant_dob),
  217.                                                 IF(ts.date_of_birth = '0000-00-00',
  218.                                                     NULL,
  219.                                                     ts.date_of_birth),
  220.                                                 ti.dob) != '0000-00-00'
  221.                                             AND (ti.gender = 1 OR tm.infant_gender = 1),
  222.                                         'm_bt_15_17',
  223.                                         IF(TIMESTAMPDIFF(YEAR,
  224.                                                 COALESCE(IF(tm.infant_dob = '0000-00-00',
  225.                                                             NULL,
  226.                                                             tm.infant_dob),
  227.                                                         IF(ts.date_of_birth = '0000-00-00',
  228.                                                             NULL,
  229.                                                             ts.date_of_birth),
  230.                                                         ti.dob),
  231.                                                 @start_date) >= 15
  232.                                                 AND TIMESTAMPDIFF(YEAR,
  233.                                                 COALESCE(IF(tm.infant_dob = '0000-00-00',
  234.                                                             NULL,
  235.                                                             tm.infant_dob),
  236.                                                         IF(ts.date_of_birth = '0000-00-00',
  237.                                                             NULL,
  238.                                                             ts.date_of_birth),
  239.                                                         ti.dob),
  240.                                                 @start_date) < 18
  241.                                                 AND COALESCE(IF(tm.infant_dob = '0000-00-00',
  242.                                                         NULL,
  243.                                                         tm.infant_dob),
  244.                                                     IF(ts.date_of_birth = '0000-00-00',
  245.                                                         NULL,
  246.                                                         ts.date_of_birth),
  247.                                                     ti.dob) != '0000-00-00'
  248.                                                 AND (ti.gender = 2 OR tm.infant_gender = 2),
  249.                                             'f_bt_15_17',
  250.                                             IF(TIMESTAMPDIFF(YEAR,
  251.                                                     COALESCE(IF(tm.infant_dob = '0000-00-00',
  252.                                                                 NULL,
  253.                                                                 tm.infant_dob),
  254.                                                             IF(ts.date_of_birth = '0000-00-00',
  255.                                                                 NULL,
  256.                                                                 ts.date_of_birth),
  257.                                                             ti.dob),
  258.                                                     @start_date) >= 18
  259.                                                     AND TIMESTAMPDIFF(YEAR,
  260.                                                     COALESCE(IF(tm.infant_dob = '0000-00-00',
  261.                                                                 NULL,
  262.                                                                 tm.infant_dob),
  263.                                                             IF(ts.date_of_birth = '0000-00-00',
  264.                                                                 NULL,
  265.                                                                 ts.date_of_birth),
  266.                                                             ti.dob),
  267.                                                     @start_date) < 25
  268.                                                     AND COALESCE(IF(tm.infant_dob = '0000-00-00',
  269.                                                             NULL,
  270.                                                             tm.infant_dob),
  271.                                                         IF(ts.date_of_birth = '0000-00-00',
  272.                                                             NULL,
  273.                                                             ts.date_of_birth),
  274.                                                         ti.dob) != '0000-00-00'
  275.                                                     AND (ti.gender = 1 OR tm.infant_gender = 1),
  276.                                                 'm_bt_18_24',
  277.                                                 IF(TIMESTAMPDIFF(YEAR,
  278.                                                         COALESCE(IF(tm.infant_dob = '0000-00-00',
  279.                                                                     NULL,
  280.                                                                     tm.infant_dob),
  281.                                                                 IF(ts.date_of_birth = '0000-00-00',
  282.                                                                     NULL,
  283.                                                                     ts.date_of_birth),
  284.                                                                 ti.dob),
  285.                                                         @start_date) >= 18
  286.                                                         AND TIMESTAMPDIFF(YEAR,
  287.                                                         COALESCE(IF(tm.infant_dob = '0000-00-00',
  288.                                                                     NULL,
  289.                                                                     tm.infant_dob),
  290.                                                                 IF(ts.date_of_birth = '0000-00-00',
  291.                                                                     NULL,
  292.                                                                     ts.date_of_birth),
  293.                                                                 ti.dob),
  294.                                                         @start_date) < 25
  295.                                                         AND COALESCE(IF(tm.infant_dob = '0000-00-00',
  296.                                                                 NULL,
  297.                                                                 tm.infant_dob),
  298.                                                             IF(ts.date_of_birth = '0000-00-00',
  299.                                                                 NULL,
  300.                                                                 ts.date_of_birth),
  301.                                                             ti.dob) != '0000-00-00'
  302.                                                         AND (ti.gender = 2 OR tm.infant_gender = 2),
  303.                                                     'f_bt_18_24',
  304.                                                     IF(TIMESTAMPDIFF(YEAR,
  305.                                                             COALESCE(IF(tm.infant_dob = '0000-00-00',
  306.                                                                         NULL,
  307.                                                                         tm.infant_dob),
  308.                                                                     IF(ts.date_of_birth = '0000-00-00',
  309.                                                                         NULL,
  310.                                                                         ts.date_of_birth),
  311.                                                                     ti.dob),
  312.                                                             @start_date) >= 25
  313.                                                             AND COALESCE(IF(tm.infant_dob = '0000-00-00',
  314.                                                                     NULL,
  315.                                                                     tm.infant_dob),
  316.                                                                 IF(ts.date_of_birth = '0000-00-00',
  317.                                                                     NULL,
  318.                                                                     ts.date_of_birth),
  319.                                                                 ti.dob) != '0000-00-00'
  320.                                                             AND (ti.gender = 1 OR tm.infant_gender = 1),
  321.                                                         'm_25',
  322.                                                         IF(TIMESTAMPDIFF(YEAR,
  323.                                                                 COALESCE(IF(tm.infant_dob = '0000-00-00',
  324.                                                                             NULL,
  325.                                                                             tm.infant_dob),
  326.                                                                         IF(ts.date_of_birth = '0000-00-00',
  327.                                                                             NULL,
  328.                                                                             ts.date_of_birth),
  329.                                                                         ti.dob),
  330.                                                                 @start_date) >= 25
  331.                                                                 AND COALESCE(IF(tm.infant_dob = '0000-00-00',
  332.                                                                         NULL,
  333.                                                                         tm.infant_dob),
  334.                                                                     IF(ts.date_of_birth = '0000-00-00',
  335.                                                                         NULL,
  336.                                                                         ts.date_of_birth),
  337.                                                                     ti.dob) != '0000-00-00'
  338.                                                                 AND (ti.gender = 2 OR tm.infant_gender = 2),
  339.                                                             'f_25',
  340.                                                             '')))))))))))))) AS tranche,
  341.     IF(regime.id_patient IS NOT NULL,
  342.         'yes',
  343.         'non') AS on_arv,
  344.     office,
  345.     IF(statut.id_patient IS NOT NULL,
  346.         'pos',
  347.         'neg') AS statut
  348. FROM
  349.     (SELECT
  350.         id_patient, MIN(date) AS date
  351.     FROM
  352.         (SELECT
  353.         id_patient, date
  354.     FROM
  355.         (SELECT
  356.         id_patient, date
  357.     FROM
  358.         questionnaire_child
  359.     LEFT JOIN patient ON patient.id = id_patient
  360.     WHERE
  361.         patient.created_at >= @start_date
  362.             AND date IS NOT NULL
  363.             AND date != '0000-00-00') ques UNION SELECT
  364.         id_patient, date
  365.     FROM
  366.         (SELECT
  367.         patient.id AS id_patient, date_of_visit AS date
  368.     FROM
  369.         patient
  370.     LEFT JOIN openfn.odk_child_visit od ON patient.patient_code = od.patient_code
  371.     WHERE
  372.         date_of_visit IS NOT NULL
  373.             AND date_of_visit != '0000-00-00'
  374.             AND patient.created_at >= @start_date) odk UNION SELECT
  375.         id_patient, date
  376.     FROM
  377.         (SELECT
  378.         id_patient, date
  379.     FROM
  380.         session
  381.     LEFT JOIN club_session ON club_session.id = session.id_club_session
  382.     LEFT JOIN club ON club.id = id_club
  383.     LEFT JOIN patient ON patient.id = id_patient
  384.     WHERE
  385.         is_present = 1 AND club_type != 1
  386.             AND patient.created_at >= @start_date) club UNION SELECT
  387.         id_patient, date
  388.     FROM
  389.         (SELECT
  390.         id_patient, date
  391.     FROM
  392.         (SELECT
  393.         id_patient, date
  394.     FROM
  395.         testing_mereenfant UNION SELECT
  396.         id_patient, date_blood_taken AS date
  397.     FROM
  398.         testing_specimen UNION SELECT
  399.         id_patient, blood_draw_date AS date
  400.     FROM
  401.         testing_result) pcr
  402.     LEFT JOIN patient ON patient.id = pcr.id_patient
  403.     WHERE
  404.         patient.created_at >= @start_date) p UNION SELECT
  405.         id_patient, date
  406.     FROM
  407.         (SELECT
  408.         id_patient, date
  409.     FROM
  410.         tracking_followup
  411.     LEFT JOIN patient ON patient.id = id_patient
  412.     WHERE
  413.         patient.created_at >= @start_date) o) last
  414.     GROUP BY id_patient) lastone
  415.         LEFT JOIN
  416.     patient ON patient.id = lastone.id_patient
  417.         LEFT JOIN
  418.     tracking_infant ti ON ti.id_patient = lastone.id_patient
  419.         LEFT JOIN
  420.     testing_mereenfant tm ON lastone.id_patient = tm.id_patient
  421.         LEFT JOIN
  422.     testing_specimen ts ON ts.id_patient = lastone.id_patient
  423.         LEFT JOIN
  424.     lookup_hospital ON CONCAT(lookup_hospital.city_code,
  425.             '/',
  426.             lookup_hospital.hospital_code) = LEFT(patient_code, 8)
  427.         LEFT JOIN
  428.     (SELECT DISTINCT
  429.         id_patient
  430.     FROM
  431.         tracking_regime
  432.     WHERE
  433.         category = 'regime_infant_treatment') regime ON regime.id_patient = lastone.id_patient
  434.         LEFT JOIN
  435.     (SELECT
  436.         id AS id_patient
  437.     FROM
  438.         view_patient_positive UNION SELECT
  439.         id_patient
  440.     FROM
  441.         tracking_regime
  442.     WHERE
  443.         category = 'regime_infant_treatment') statut ON statut.id_patient = lastone.id_patient
  444. WHERE
  445.     lastone.date BETWEEN @start_date AND @end_date
  446.         AND patient.created_at >= @start_date
  447.         AND lastone.id_patient IS NOT NULL
  448.         AND linked_to_id_patient = 0
  449. GROUP BY lastone.id_patient
Advertisement
Add Comment
Please, Sign In to add comment