Advertisement
jeniferfleurant

PMTCT_EID

Oct 29th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.08 KB | None | 0 0
  1. USE caris_db;
  2. SET @start_date="2015-03-01";
  3. SET @end_date="2015-09-30";
  4.  
  5. SELECT DISTINCT(a.hospital_code), count_patient_whose_pcr_done, count_patient_virological_test_2_month, infant_virological_test_btw_2and12 , positive_pcr_within12, positive_pcr_within_2, positive_pcr_btw_2_12, positive_pregnant_women
  6. FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code , hospital_code AS hcode_nocity FROM lookup_hospital) a LEFT JOIN
  7. (
  8. ###PMTCT_EID
  9.  
  10. ##NUMERATOR: NUMBER OF CHILDREN WE TESTED AT EACH SITE SINCE 10/1/13
  11.  
  12. #SELECT * FROM testing_specimen
  13. #SELECT count(*) FROM testing_specimen
  14. #SELECT * FROM testing_result
  15. #SELECT count(*) FROM testing_result
  16.  
  17. #total number of tests performed
  18. #select id_patient from testing_specimen where date_blood_taken >= "2013-10-01"
  19. #and date_blood_taken <= "2014-09-31" limit 500000 #returns 3459 rows
  20.  
  21. SELECT abc12.hospital_code AS "hospital_code", infant_virological_test_btw_2and12 , positive_pcr_within12, positive_pcr_within_2, positive_pcr_btw_2_12, count_patient_whose_pcr_done, count_patient_virological_test_2_month, positive_pregnant_women FROM
  22. (
  23.  
  24.  
  25.  
  26. SELECT abc1.hospital_code AS "hospital_code", count_patient_whose_pcr_done, count_patient_virological_test_2_month FROM (
  27. #by site
  28.  
  29. SELECT lookup_hospital.hospital_code, count_patient_whose_pcr_done
  30. FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code FROM lookup_hospital) lookup_hospital LEFT JOIN (
  31. SELECT count(*) AS "count_patient_whose_pcr_done" , hospital_code
  32. FROM (
  33. SELECT * FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code, id FROM patient) patient
  34. WHERE
  35. id IN (SELECT id_patient FROM testing_specimen WHERE date_blood_taken >= @start_date
  36. AND date_blood_taken <= @end_date)
  37. ) a
  38. GROUP BY hospital_code
  39. ORDER BY hospital_code
  40. )
  41. x1
  42. ON x1.hospital_code = lookup_hospital.hospital_code
  43.  
  44. ) abc1, (
  45.  
  46. ##DENOMINATOR: NUMBER OF WOMEN WHO ARE POSITIVE AND PREGNANT AT EACH SITE
  47.  
  48. #see PMP code-2 for clarification (iSante and MESI)
  49.  
  50. ##DISAGGREGATION
  51.  
  52. ##infants who received a virologic test within 2 months of birth
  53. SELECT lookup_hospital.hospital_code, count_patient_virological_test_2_month
  54. FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code FROM lookup_hospital)
  55. lookup_hospital LEFT JOIN (
  56. SELECT count(*) AS "count_patient_virological_test_2_month", hospital_code
  57. FROM (
  58. SELECT * FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code, id FROM patient) patient
  59. WHERE
  60. id IN (SELECT id_patient FROM testing_specimen WHERE date_blood_taken >= @start_date
  61. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) <= 61
  62. AND DATEDIFF(date_blood_taken,date_of_birth) > 0 #and which_pcr = 1
  63. ) #and which_pcr = 2
  64.  
  65. ) d
  66. GROUP BY hospital_code
  67. ORDER BY hospital_code
  68. )
  69. x2
  70. ON x2.hospital_code = lookup_hospital.hospital_code
  71.  
  72.  
  73.  
  74. ) abc2
  75. WHERE abc1.hospital_code=abc2.hospital_code
  76.  
  77.  
  78. ) abc12
  79. ,
  80. (
  81.  
  82. SELECT abc3.hospital_code AS "hospital_code" , infant_virological_test_btw_2and12 , positive_pcr_within12, positive_pcr_within_2, positive_pcr_btw_2_12, positive_pregnant_women FROM (
  83. ##infants who received a virologic test between 2 months and 12 months of birth, DATEDIFF
  84. #this should be the difference the numerator and the first disaggregation described above
  85.  
  86. SELECT lookup_hospital.hospital_code, infant_virological_test_btw_2and12
  87. FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code FROM lookup_hospital)
  88. lookup_hospital LEFT JOIN (
  89. SELECT count(*) AS "infant_virological_test_btw_2and12", hospital_code
  90. FROM (
  91. SELECT * FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code, id FROM patient) patient
  92. WHERE
  93. id IN (SELECT id_patient FROM testing_specimen WHERE date_blood_taken >= @start_date
  94. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) >= 62
  95. AND DATEDIFF(date_blood_taken,date_of_birth) <= 365 #and which_pcr = 1
  96. ) #and which_pcr = 2
  97. AND # make sure patient ids counted in less than 2 months is also counted in 2 and 12 (in case they had 2nd PCR later
  98. id NOT IN (SELECT id_patient FROM testing_specimen WHERE date_blood_taken >= @start_date
  99. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) <= 61
  100. AND DATEDIFF(date_blood_taken,date_of_birth) > 0 #and which_pcr = 1
  101. ) #and which_pcr = 2
  102.  
  103.  
  104. ) bbb
  105. GROUP BY hospital_code
  106. ORDER BY hospital_code
  107. ) x3
  108. ON x3.hospital_code = lookup_hospital.hospital_code
  109. ) abc3 ,
  110.  
  111. ##infants with a positive pcr within 12 months of birth
  112. (
  113. SELECT lookup_hospital.hospital_code, positive_pcr_within12
  114. FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code FROM lookup_hospital)
  115. lookup_hospital LEFT JOIN (
  116. SELECT count(*) AS "positive_pcr_within12" , hospital_code
  117. FROM (
  118. SELECT * FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code, id FROM patient) patient
  119. WHERE
  120. id IN (
  121. SELECT id_patient FROM testing_specimen WHERE date_blood_taken >= @start_date
  122. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) >= 1
  123. AND DATEDIFF(date_blood_taken,date_of_birth) <= 365
  124. AND pcr_result = 1 #and which_pcr = 1
  125. ) #and which_pcr = 2
  126. ) jj #returns 5 rows
  127. GROUP BY hospital_code
  128. ORDER BY hospital_code
  129. )x4
  130. ON x4.hospital_code = lookup_hospital.hospital_code
  131.  
  132. )abc4
  133. ,
  134. (
  135. SELECT lookup_hospital.hospital_code, positive_pregnant_women
  136. FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code FROM lookup_hospital)
  137. lookup_hospital
  138. LEFT JOIN (
  139. SELECT count(*) AS "positive_pregnant_women" , hospital_code
  140. FROM (
  141. SELECT * FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code, id FROM patient) patient
  142. WHERE
  143. id IN (
  144. SELECT id_patient_mother FROM tracking_pregnancy where dpa between (@start_date) and (@end_date + interval 9 month)
  145. or
  146. ddr between (@start_date - interval 9 month) and (@end_date - interval 2 week)
  147. )
  148. ) jj5
  149. GROUP BY hospital_code
  150. ORDER BY hospital_code
  151. )x5
  152. ON x5.hospital_code = lookup_hospital.hospital_code
  153.  
  154. )abc5
  155. ,
  156.  
  157. (
  158. SELECT lookup_hospital.hospital_code, positive_pcr_within_2
  159. FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code FROM lookup_hospital)
  160. lookup_hospital LEFT JOIN (
  161. SELECT count(*) AS "positive_pcr_within_2" , hospital_code
  162. FROM (
  163. SELECT * FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code, id FROM patient) patient
  164. WHERE
  165. id IN (
  166. SELECT id_patient FROM testing_specimen WHERE date_blood_taken >= @start_date
  167. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) >= 1
  168. AND DATEDIFF(date_blood_taken,date_of_birth) <= 60
  169. AND pcr_result = 1 #and which_pcr = 1
  170. ) #and which_pcr = 2
  171. ) jj0 #returns 5 rows
  172. GROUP BY hospital_code
  173. ORDER BY hospital_code
  174. )x41
  175. ON x41.hospital_code = lookup_hospital.hospital_code
  176.  
  177. )abc6
  178. ,
  179. (
  180. SELECT lookup_hospital.hospital_code, positive_pcr_btw_2_12
  181. FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code FROM lookup_hospital)
  182. lookup_hospital LEFT JOIN (
  183. SELECT count(*) AS "positive_pcr_btw_2_12" , hospital_code
  184. FROM (
  185. SELECT * FROM (SELECT CONCAT(city_code, "/", hospital_code) AS hospital_code, id FROM patient) patient
  186. WHERE
  187. id IN (
  188. SELECT id_patient FROM testing_specimen WHERE date_blood_taken >= @start_date
  189. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) > 60
  190. AND DATEDIFF(date_blood_taken,date_of_birth) <= 365
  191. AND pcr_result = 1 #and which_pcr = 1
  192. ) #and which_pcr = 2
  193. AND
  194. id not in
  195. (
  196. SELECT id_patient FROM testing_specimen WHERE date_blood_taken >= @start_date
  197. AND date_blood_taken <= @end_date AND DATEDIFF(date_blood_taken,date_of_birth) >= 1
  198. AND DATEDIFF(date_blood_taken,date_of_birth) <= 60
  199. AND pcr_result = 1 #and which_pcr = 1
  200. )
  201.  
  202. ) jj1 #returns 5 rows
  203. GROUP BY hospital_code
  204. ORDER BY hospital_code
  205. )x42
  206. ON x42.hospital_code = lookup_hospital.hospital_code
  207.  
  208. )abc7
  209.  
  210. WHERE abc3.hospital_code=abc4.hospital_code and abc4.hospital_code = abc5.hospital_code and abc4.hospital_code = abc6.hospital_code and abc7.hospital_code = abc6.hospital_code
  211.  
  212.  
  213. ) abc34
  214.  
  215. WHERE abc34.hospital_code = abc12.hospital_code
  216. ) b
  217. ON a.hospital_code = b.hospital_code
  218.  
  219. ORDER BY hcode_nocity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement