jeniferfleurant

PMTCT_EID newest

Apr 8th, 2016
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.88 KB | None | 0 0
  1. USE caris_db;
  2. SET @start_date="2015-03-01";
  3. SET @end_date="2015-09-30";
  4. ###PMTCT_EID
  5. SELECT
  6. hospital_code,
  7. numb_positive_pregnant_women,
  8. virologic_HIV_test_within_12_months,
  9. virologic_test_within_2_months_of_birth,
  10. (virologic_HIV_test_within_12_months-virologic_test_within_2_months_of_birth) AS virologic_HIV_test_bet_2_and_12_months,
  11. positive_virologic_test_within_12_months,
  12. positive_virologic_test_within_2_months,
  13. (positive_virologic_test_within_12_months-positive_virologic_test_within_2_months) AS positive_virologic_test_bet_2_and_12_months
  14. FROM (
  15.  
  16. SELECT b.*
  17.   FROM(
  18.  
  19. SELECT DISTINCT(a.hospital_code), IF(abc00.numb_positive_pregnant_women IS NULL,0,abc00.numb_positive_pregnant_women) AS numb_positive_pregnant_women,
  20. 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.  
  25. FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code , hospital_code AS hcode_nocity FROM lookup_hospital) a LEFT JOIN
  26. (
  27.  
  28. SELECT COUNT(*) AS 'numb_positive_pregnant_women' , CONCAT(city_code, "/", hospital_code) AS hospital_code FROM patient
  29. WHERE
  30. id IN (
  31. SELECT id_patient_mother FROM tracking_pregnancy WHERE (dpa BETWEEN (@start_date) AND (@end_date + INTERVAL 9 MONTH)
  32. OR
  33. ddr BETWEEN (@start_date - INTERVAL 9 MONTH) AND (@end_date -  INTERVAL 2 week))
  34.  )
  35. GROUP BY hospital_code
  36. ) abc00 ON abc00.hospital_code=a.hospital_code
  37. ### INFANT VIROLOGY
  38. #WITHING 2 MONTHS
  39. LEFT JOIN (
  40. SELECT COUNT(*) AS `virologic_test_within_2_months_of_birth`, CONCAT(city_code, "/", hospital_code) AS hospital_code
  41.  FROM patient
  42. WHERE  
  43. id IN (SELECT id_patient FROM testing_specimen WHERE date_blood_taken >= @start_date
  44. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) <= 61
  45. AND DATEDIFF(date_blood_taken,date_of_birth) > 0 AND which_pcr = 1
  46. ) GROUP BY hospital_code
  47. ) abc01 ON abc01.hospital_code=a.hospital_code
  48. # BETWEEN 0 12
  49. LEFT JOIN (
  50. SELECT COUNT(*) AS "virologic_HIV_test_within_12_months", CONCAT(city_code, "/", hospital_code) AS hospital_code
  51.  FROM patient
  52. WHERE
  53. id IN (SELECT id_patient FROM testing_specimen WHERE date_blood_taken >= @start_date
  54. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) >= 0
  55. AND DATEDIFF(date_blood_taken,date_of_birth) <= 365 AND which_pcr = 1
  56. )
  57. GROUP BY hospital_code
  58. ) abc02 ON abc02.hospital_code=a.hospital_code
  59. #POSITIVE WITHIN 2 MONTHS
  60. LEFT JOIN (
  61. SELECT COUNT(*) AS "positive_virologic_test_within_2_months", CONCAT(city_code, "/", hospital_code) AS hospital_code
  62.  FROM patient WHERE id IN (SELECT id_patient FROM testing_specimen WHERE date_blood_taken >= @start_date
  63. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) <= 61
  64. AND DATEDIFF(date_blood_taken,date_of_birth) > 0 AND pcr_result=1 AND which_pcr = 1
  65. ) GROUP BY hospital_code
  66. ) abc03 ON abc03.hospital_code=a.hospital_code
  67.  
  68. # POSITIVE BETWEEN 0 12
  69. LEFT JOIN (
  70. SELECT COUNT(*) AS "positive_virologic_test_within_12_months", CONCAT(city_code, "/", hospital_code) AS hospital_code
  71.  FROM patient
  72. WHERE
  73. id IN (SELECT id_patient FROM testing_specimen WHERE date_blood_taken >= @start_date
  74. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) >= 0
  75. AND DATEDIFF(date_blood_taken,date_of_birth) <= 365 AND pcr_result = 1 AND which_pcr = 1
  76. )
  77. GROUP BY hospital_code
  78. ) abc04 ON abc04.hospital_code=a.hospital_code
  79.  
  80.  
  81. ) b
  82.  
  83. ) c
  84. ORDER BY c.hospital_code
Advertisement
Add Comment
Please, Sign In to add comment