Advertisement
jeniferfleurant

ovc_serv last

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