Advertisement
jeniferfleurant

ovc serv with total

Apr 11th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. USE caris_db;
  2. SET @start_date= "2014-10-01";
  3. SET @end_date="2015-09-30";
  4.  
  5. select lookup_hospital.hospital_code, total,
  6. m_under_1, f_under_1, m_btwn_1_and_4, f_btwn_1_and_4, m_btwn_5_and_9, f_btwn_5_and_9, m_btwn_10_and_14, f_btwn_10_and_14 , m_btwn_15_and_17, f_btwn_15_and_17 , m_btwn_18_and_24, f_btwn_18_and_24, m_25plus, f_25plus
  7. from
  8. (SELECT
  9. CONCAT(city_code, '/', hospital_code) AS hospital_code,
  10. hospital_code AS hcode_nocity
  11. FROM
  12. lookup_hospital) lookup_hospital
  13. LEFT JOIN
  14.  
  15. (
  16. select CONCAT(city_code, '/', hospital_code) AS hospital_code , count(*) as 'total',
  17. #gender, dob ,
  18.  
  19. SUM(CASE
  20. WHEN
  21. dob between (@start_date - interval 364 day) and @end_date
  22. and dob != '0000-00-00'
  23. and gender = 1
  24. THEN
  25. 1
  26. ELSE 0
  27. END) AS m_under_1,
  28. SUM(CASE
  29. WHEN
  30. dob between (@start_date - interval 364 day) and @end_date
  31. and dob != '0000-00-00'
  32. and gender = 2
  33. THEN
  34. 1
  35. ELSE 0
  36. END) AS f_under_1,
  37. SUM(CASE
  38. WHEN
  39. dob between (@start_date - interval 4 year + interval 1 day) and @end_date - interval 1 year
  40. and dob != '0000-00-00' and timestampdiff(year, dob, @start_date)>=1
  41. and gender = 1
  42.  
  43. THEN
  44. 1
  45. ELSE 0
  46. END) AS m_btwn_1_and_4,
  47. SUM(CASE
  48. WHEN
  49. dob between (@start_date - interval 4 year + interval 1 day) and @end_date - interval 1 year
  50. and dob != '0000-00-00' and timestampdiff(year, dob, @start_date)>=1
  51. and gender = 2
  52. THEN
  53. 1
  54. ELSE 0
  55. END) AS f_btwn_1_and_4,
  56. SUM(CASE
  57. WHEN
  58. dob between (@start_date - interval 9 year + interval 1 day) and @end_date - interval 5 year
  59. and dob != '0000-00-00' and timestampdiff(year, dob, @start_date)>= 5
  60. and gender = 1
  61. THEN
  62. 1
  63. ELSE 0
  64. END) AS m_btwn_5_and_9,
  65. SUM(CASE
  66. WHEN
  67. dob between (@start_date - interval 9 year + interval 1 day) and @end_date - interval 5 year
  68. and dob != '0000-00-00' and timestampdiff(year, dob, @start_date)>= 5
  69. and gender = 2
  70. THEN
  71. 1
  72. ELSE 0
  73. END) AS f_btwn_5_and_9,
  74. SUM(CASE
  75. WHEN
  76. dob between (@start_date - interval 14 year + interval 1 day) and @end_date - interval 10 year
  77. and dob != '0000-00-00' and timestampdiff(year, dob, @start_date)>= 10
  78. and gender = 1
  79. THEN
  80. 1
  81. ELSE 0
  82. END) AS m_btwn_10_and_14,
  83. SUM(CASE
  84. WHEN
  85. dob between (@start_date - interval 14 year + interval 1 day) and @end_date - interval 10 year
  86. and dob != '0000-00-00' and timestampdiff(year, dob, @start_date)>= 10
  87. and gender = 2
  88. THEN
  89. 1
  90. ELSE 0
  91. END) AS f_btwn_10_and_14,
  92. SUM(CASE
  93. WHEN
  94. dob between (@start_date - interval 17 year + interval 1 day) and @end_date - interval 15 year
  95. and dob != '0000-00-00' and timestampdiff(year, dob, @start_date)>= 15
  96. and gender = 1
  97. THEN
  98. 1
  99. ELSE 0
  100. END) AS m_btwn_15_and_17,
  101. SUM(CASE
  102. WHEN
  103. dob between (@start_date - interval 17 year + interval 1 day) and @end_date - interval 15 year
  104. and dob != '0000-00-00' and timestampdiff(year, dob, @start_date)>= 15
  105. and gender = 2
  106. THEN
  107. 1
  108. ELSE 0
  109. END) AS f_btwn_15_and_17,
  110. SUM(CASE
  111. WHEN
  112. dob between (@start_date - interval 24 year + interval 1 day) and @end_date - interval 18 year
  113. and dob != '0000-00-00' and timestampdiff(year, dob, @start_date)>= 18
  114. and gender = 1
  115. THEN
  116. 1
  117. ELSE 0
  118. END) AS m_btwn_18_and_24,
  119. SUM(CASE
  120. WHEN
  121. dob between (@start_date - interval 24 year + interval 1 day) and @end_date - interval 18 year
  122. and dob != '0000-00-00' and timestampdiff(year, dob, @start_date)>= 18
  123. and gender = 2
  124. THEN
  125. 1
  126. ELSE 0
  127. END) AS f_btwn_18_and_24,
  128. SUM(CASE
  129. WHEN
  130. dob <= @end_date - interval 25 year and dob != '0000-00-00' and timestampdiff(year, dob, @start_date)>= 25
  131. and gender = 1
  132. THEN
  133. 1
  134. ELSE 0
  135. END) AS m_25plus,
  136. SUM(CASE
  137. WHEN
  138. dob <= @end_date - interval 25 year and dob != '0000-00-00' and timestampdiff(year, dob, @start_date)>= 25
  139. and gender = 2
  140. THEN
  141. 1
  142. ELSE 0
  143. END) AS f_25plus
  144. from
  145. /*
  146. (
  147. select id_patient, infant_dob as dob , infant_gender as gender
  148. from testing_mereenfant
  149. where date >="2013-10-01" and date<@start_date
  150. ) i , patient
  151. */
  152. (
  153. (
  154. select id_patient, infant_dob as dob , infant_gender as gender
  155. from testing_mereenfant
  156. where date >=@start_date and date<@end_date )
  157.  
  158. union
  159. (
  160. select id_patient, dob , gender
  161. from patient LEFT JOIN tracking_infant
  162. ON tracking_infant.id_patient = patient.id
  163. and
  164. (
  165. (
  166. ( kids_club_program =1
  167. or id_patient in (select id from tracking_followup where date>=@start_date AND date<@end_date )
  168. )
  169. AND id_patient not in (select id_patient from testing_mereenfant where date >=@start_date and date<@end_date )
  170. )
  171. OR
  172. (
  173. id_patient in (
  174. select id from patient where
  175.  
  176. (
  177. created_at >=@start_date and created_at<=@end_date
  178. and id not in
  179. (
  180. select id_patient from testing_mereenfant where date >=@start_date and date<@end_date
  181. )
  182. )
  183. or id in
  184. (
  185. select id_patient from testing_specimen where date_blood_taken >=@start_date and date_blood_taken <@end_date
  186. )
  187. or id in
  188. (
  189. select id_patient from tracking_regime where created_at >=@start_date and created_at<=@end_date
  190.  
  191. )
  192. or id in
  193. (
  194. select id_patient from tracking_followup where date >=@start_date and date<=@end_date
  195.  
  196. )
  197. or id in
  198. (
  199. select id_patient from session left join club_session on session.id_club_session = club_session.id
  200. where club_session.date >=@start_date and club_session.date<=@end_date
  201. )
  202.  
  203. )
  204. )
  205.  
  206. )
  207.  
  208. ) ) i , patient
  209. where
  210. #kids_club_program =1
  211. #and
  212.  
  213. patient.id = i.id_patient
  214. #and datediff(@start_date,dob) /365 <15
  215. group by hospital_code
  216. ) table_for_left_join_with_lookup_hospital
  217. ON table_for_left_join_with_lookup_hospital.hospital_code = lookup_hospital.hospital_code
  218. order by hcode_nocity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement