jeniferfleurant

OVC_SERV with all disagg

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