jeniferfleurant

ALL first PCR

Mar 29th, 2019
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.27 KB | None | 0 0
  1. SELECT
  2.     xcx.*,
  3.     is_abandoned,
  4.     abandoned_date,
  5.     is_dead,
  6.     tracking_infant.death_date,
  7.     IF(tracking_regime.id_patient IS NOT NULL,
  8.         'yes',
  9.         IF(opq.id_patient IS NOT NULL,
  10.             'yes',
  11.             'non')) AS on_arv
  12. FROM
  13.     (SELECT
  14.         *
  15.     FROM
  16.         (SELECT
  17.         x.id_patient, date_blood_taken, patient_code, name AS result
  18.     FROM
  19.         (SELECT
  20.         id_patient,
  21.             date_blood_taken,
  22.             pcr_result AS result,
  23.             created_at
  24.     FROM
  25.         testing_specimen t1
  26.     WHERE
  27.         date_blood_taken = (SELECT
  28.                 MIN(date_blood_taken)
  29.             FROM
  30.                 testing_specimen t2
  31.             WHERE
  32.                 t1.id_patient = t2.id_patient) UNION SELECT
  33.         id_patient,
  34.             blood_draw_date AS date_blood_taken,
  35.             result,
  36.             created_at
  37.     FROM
  38.         testing_result t1
  39.     WHERE
  40.         #blood_draw_date != '0000-00-00'
  41.             #AND
  42.             blood_draw_date = (SELECT
  43.                 MIN(blood_draw_date)
  44.             FROM
  45.                 testing_result t2
  46.             WHERE
  47.                 t1.id_patient = t2.id_patient)) x
  48.     LEFT JOIN patient ON patient.id = x.id_patient
  49.     LEFT JOIN lookup_testing_lab_result ON lookup_testing_lab_result.id = x.result
  50.     WHERE
  51.         linked_to_id_patient = 0) op1
  52.     WHERE
  53.         date_blood_taken = (SELECT
  54.                 MIN(date_blood_taken)
  55.             FROM
  56.                 (SELECT
  57.                 x.id_patient, date_blood_taken, patient_code, name AS result
  58.             FROM
  59.                 (SELECT
  60.                 id_patient,
  61.                     date_blood_taken,
  62.                     pcr_result AS result,
  63.                     created_at
  64.             FROM
  65.                 testing_specimen t1
  66.             WHERE
  67.                 date_blood_taken = (SELECT
  68.                         MIN(date_blood_taken)
  69.                     FROM
  70.                         testing_specimen t2
  71.                     WHERE
  72.                         t1.id_patient = t2.id_patient) UNION SELECT
  73.                 id_patient,
  74.                     blood_draw_date AS date_blood_taken,
  75.                     result,
  76.                     created_at
  77.             FROM
  78.                 testing_result t1
  79.             WHERE
  80.                 #blood_draw_date != '0000-00-00'
  81.                    # AND
  82.                     blood_draw_date = (SELECT
  83.                         MIN(blood_draw_date)
  84.                     FROM
  85.                         testing_result t2
  86.                     WHERE
  87.                         t1.id_patient = t2.id_patient)) x
  88.             LEFT JOIN patient ON patient.id = x.id_patient
  89.             LEFT JOIN lookup_testing_lab_result ON lookup_testing_lab_result.id = x.result
  90.             WHERE
  91.                 linked_to_id_patient = 0) op2
  92.             WHERE
  93.                 op1.id_patient = op2.id_patient)
  94.     GROUP BY id_patient) xcx
  95.         LEFT JOIN
  96.     tracking_infant ON tracking_infant.id_patient = xcx.id_patient
  97.         LEFT JOIN
  98.     tracking_regime ON tracking_regime.id_patient = xcx.id_patient
  99.         LEFT JOIN
  100.     (SELECT
  101.         id_patient
  102.     FROM
  103.         tracking_followup
  104.     WHERE
  105.         on_arv = 1) opq ON opq.id_patient = xcx.id_patient
  106. GROUP BY id_patient
Advertisement
Add Comment
Please, Sign In to add comment