Advertisement
jeniferfleurant

PMTCT EID 2017

Mar 2nd, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 5.33 KB | None | 0 0
  1. USE caris_db;
  2. SET @start_date="2016-07-01";
  3. SET @end_date="2016-09-31";
  4. ###PMTCT_EID
  5. SELECT
  6. hc,
  7. virologic_HIV_test_within_12_months,
  8. positive_virologic_test_within_2_months,
  9. (positive_virologic_test_within_12_months-positive_virologic_test_within_2_months) as positive_virologic_test_bet_2_and_12_months,
  10. negative_virologic_test_within_2_months,
  11. (negative_virologic_test_within_12_months-negative_virologic_test_within_2_months) as negative_virologic_test_bet_2_and_12_months,
  12. (virologic_test_within_2_months_of_birth-positive_virologic_test_within_2_months-negative_virologic_test_within_2_months)as no_result_within_2,
  13. (virologic_HIV_test_within_12_months-virologic_test_within_2_months_of_birth-positive_virologic_test_within_12_months+positive_virologic_test_within_2_months-negative_virologic_test_within_12_months+negative_virologic_test_within_2_months) as no_result_2_12
  14.  
  15. FROM (
  16.  
  17. SELECT b.*
  18.   FROM(
  19.  
  20. SELECT DISTINCT(a.hc), IF(abc01.virologic_test_within_2_months_of_birth IS NULL,0,abc01.virologic_test_within_2_months_of_birth) as virologic_test_within_2_months_of_birth,
  21. IF(abc02.virologic_HIV_test_within_12_months IS NULL,0,abc02.virologic_HIV_test_within_12_months) as virologic_HIV_test_within_12_months,
  22. IF(abc03.positive_virologic_test_within_2_months IS NULL,0,abc03.positive_virologic_test_within_2_months) as positive_virologic_test_within_2_months,
  23. IF(abc04.positive_virologic_test_within_12_months IS NULL,0,abc04.positive_virologic_test_within_12_months) as positive_virologic_test_within_12_months,
  24. IF(abc05.negative_virologic_test_within_2_months IS NULL,0,abc05.negative_virologic_test_within_2_months) as negative_virologic_test_within_2_months,
  25. IF(abc06.negative_virologic_test_within_12_months IS NULL,0,abc06.negative_virologic_test_within_12_months) as negative_virologic_test_within_12_months
  26.  
  27. FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hc , hospital_code AS hcode_nocity FROM lookup_hospital) a
  28. ### INFANT VIROLOGY
  29. #WITHING 2 MONTHS
  30. LEFT JOIN (
  31. SELECT count(*) AS `virologic_test_within_2_months_of_birth`, CONCAT(city_code, "/", hospital_code) AS hc
  32.  from patient
  33. WHERE  
  34. id IN (SELECT id_patient FROM testing_specimen t1 WHERE date_blood_taken >= @start_date
  35. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) <= 61
  36. AND DATEDIFF(date_blood_taken,date_of_birth) > 0 AND date_blood_taken=(select min(date_blood_taken) from testing_specimen t2 where t1.id_patient=t2.id_patient) #which_pcr = 1
  37. ) GROUP BY hc
  38. ) abc01 on abc01.hc=a.hc
  39. # BETWEEN 0 12
  40. LEFT JOIN (
  41. SELECT count(*) AS "virologic_HIV_test_within_12_months", CONCAT(city_code, "/", hospital_code) AS hc
  42.  from patient
  43. WHERE
  44. id IN (SELECT id_patient FROM testing_specimen t1 WHERE date_blood_taken >= @start_date
  45. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) >= 0
  46. AND DATEDIFF(date_blood_taken,date_of_birth) <= 365 AND date_blood_taken=(select min(date_blood_taken) from testing_specimen t2 where t1.id_patient=t2.id_patient) #which_pcr = 1
  47. )
  48. GROUP BY hc
  49. ) abc02 on abc02.hc=a.hc
  50. #POSITIVE WITHIN 2 MONTHS
  51. LEFT JOIN (
  52. SELECT count(*) AS "positive_virologic_test_within_2_months", CONCAT(city_code, "/", hospital_code) AS hc
  53.  from patient WHERE id IN (SELECT id_patient FROM testing_specimen t1 WHERE date_blood_taken >= @start_date
  54. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) <= 61
  55. AND DATEDIFF(date_blood_taken,date_of_birth) > 0 AND pcr_result=1 AND date_blood_taken=(select min(date_blood_taken) from testing_specimen t2 where t1.id_patient=t2.id_patient) #which_pcr = 1
  56. ) GROUP BY hc
  57. ) abc03 on abc03.hc=a.hc
  58.  
  59. # POSITIVE BETWEEN 0 12
  60. LEFT JOIN (
  61. SELECT count(*) AS "positive_virologic_test_within_12_months", CONCAT(city_code, "/", hospital_code) AS hc
  62.  from patient
  63. WHERE
  64. id IN (SELECT id_patient FROM testing_specimen t1 WHERE date_blood_taken >= @start_date
  65. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) >= 0
  66. AND DATEDIFF(date_blood_taken,date_of_birth) <= 365 and pcr_result = 1 AND date_blood_taken=(select min(date_blood_taken) from testing_specimen t2 where t1.id_patient=t2.id_patient) #which_pcr = 1
  67. )
  68. GROUP BY hc
  69. ) abc04 on abc04.hc=a.hc
  70.  
  71.  
  72.  
  73. #NEGATIVE WITHIN 2 MONTHS
  74. LEFT JOIN (
  75. SELECT count(*) AS "negative_virologic_test_within_2_months", CONCAT(city_code, "/", hospital_code) AS hc
  76.  from patient WHERE id IN (SELECT id_patient FROM testing_specimen t1 WHERE date_blood_taken >= @start_date
  77. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) <= 61
  78. AND DATEDIFF(date_blood_taken,date_of_birth) > 0 AND pcr_result=2 AND date_blood_taken=(select min(date_blood_taken) from testing_specimen t2 where t1.id_patient=t2.id_patient) #which_pcr = 1
  79. ) GROUP BY hc
  80. ) abc05 on abc05.hc=a.hc
  81.  
  82. # NEGATIVE BETWEEN 0 12
  83. LEFT JOIN (
  84. SELECT count(*) AS "negative_virologic_test_within_12_months", CONCAT(city_code, "/", hospital_code) AS hc
  85.  from patient
  86. WHERE
  87. id IN (SELECT id_patient FROM testing_specimen t1 WHERE date_blood_taken >= @start_date
  88. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) >= 0
  89. AND DATEDIFF(date_blood_taken,date_of_birth) <= 365 and pcr_result = 2 AND date_blood_taken=(select min(date_blood_taken) from testing_specimen t2 where t1.id_patient=t2.id_patient) #which_pcr = 1
  90. )
  91. GROUP BY hc
  92. ) abc06 on abc06.hc=a.hc
  93.  
  94.  
  95.  
  96. ) b
  97.  
  98. ) c
  99. ORDER BY c.hc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement