Advertisement
jeniferfleurant

OVC_SERV children detailed

Oct 18th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 13.60 KB | None | 0 0
  1. USE caris_db;
  2. SET
  3.   @start_date = "2018-07-01";
  4. SET
  5.   @end_date = "2018-09-30";
  6. SELECT
  7.   patient.id,
  8.   left(patient_code, 8) AS hospital_code,
  9.   patient_code,
  10.   if(
  11.     timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)< 1
  12.     and COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob) != '0000-00-00',
  13.     'under1',
  14.     if(
  15.       timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)>= 1
  16.       and timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)< 10
  17.       and COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob) != '0000-00-00',
  18.       'bt_1_9',
  19.       if(
  20.         timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)< 1
  21.         and COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob) != '0000-00-00'
  22.         and (
  23.           ti.gender = 1
  24.           or e.gender = 1
  25.           or tm.infant_gender = 1
  26.         ),
  27.         'm_under1',
  28.         if(
  29.           timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)< 1
  30.           and COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob) != '0000-00-00'
  31.           and (
  32.             ti.gender = 2
  33.             or e.gender = 2
  34.             or tm.infant_gender = 2
  35.           ),
  36.           'f_under1',
  37.           if(
  38.             timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)>= 1
  39.             and timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)< 10
  40.             and COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob) != '0000-00-00'
  41.             and (
  42.               ti.gender = 1
  43.               or e.gender = 1
  44.               or tm.infant_gender = 1
  45.             ),
  46.             'm_bt_1_9',
  47.             if(
  48.               timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)>= 1
  49.               and timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)< 10
  50.               and COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob) != '0000-00-00'
  51.               and (
  52.                 ti.gender = 2
  53.                 or e.gender = 2
  54.                 or tm.infant_gender = 2
  55.               ),
  56.               'f_bt_1_9',
  57.               if(
  58.                 timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)>= 10
  59.                 and timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)< 15
  60.                 and COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob) != '0000-00-00'
  61.                 and (
  62.                   ti.gender = 1
  63.                   or e.gender = 1
  64.                   or tm.infant_gender = 1
  65.                 ),
  66.                 'm_bt_10_14',
  67.                 if(
  68.                   timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)>= 10
  69.                   and timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)< 15
  70.                   and COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob) != '0000-00-00'
  71.                   and (
  72.                     ti.gender = 2
  73.                     or e.gender = 2
  74.                     or tm.infant_gender = 2
  75.                   ),
  76.                   'f_bt_10_14',
  77.                   if(
  78.                     timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)>= 15
  79.                     and timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)< 18
  80.                     and COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob) != '0000-00-00'
  81.                     and (
  82.                       ti.gender = 1
  83.                       or e.gender = 1
  84.                       or tm.infant_gender = 1
  85.                     ),
  86.                     'm_bt_15_17',
  87.                     if(
  88.                       timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)>= 15
  89.                       and timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)< 18
  90.                       and COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob) != '0000-00-00'
  91.                       and (
  92.                         ti.gender = 2
  93.                         or e.gender = 2
  94.                         or tm.infant_gender = 2
  95.                       ),
  96.                       'f_bt_15_17',
  97.                       if(
  98.                         timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)>= 18
  99.                         and timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)< 25
  100.                         and COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob) != '0000-00-00'
  101.                         and (
  102.                           ti.gender = 1
  103.                           or e.gender = 1
  104.                           or tm.infant_gender = 1
  105.                         ),
  106.                         'm_bt_18_24',
  107.                         if(
  108.                           timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)>= 18
  109.                           and timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)< 25
  110.                           and COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob) != '0000-00-00'
  111.                           and (
  112.                             ti.gender = 2
  113.                             or e.gender = 2
  114.                             or tm.infant_gender = 2
  115.                           ),
  116.                           'f_bt_18_24',
  117.                           if(
  118.                             timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)>= 25
  119.                             and COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob) != '0000-00-00'
  120.                             and (
  121.                               ti.gender = 1
  122.                               or e.gender = 1
  123.                               or tm.infant_gender = 1
  124.                             ),
  125.                             'm_25',
  126.                             if(
  127.                               timestampdiff(year, COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob), @start_date)>= 25
  128.                               and COALESCE(if(tm.infant_dob='0000-00-00',null,tm.infant_dob),if(ts.date_of_birth='0000-00-00',null,ts.date_of_birth),ti.dob) != '0000-00-00'
  129.                               and (
  130.                                 ti.gender = 2
  131.                                 or e.gender = 2
  132.                                 or tm.infant_gender = 2
  133.                               ),
  134.                               'f_25',
  135.                               ''
  136.                             )
  137.                           )
  138.                         )
  139.                       )
  140.                     )
  141.                   )
  142.                 )
  143.               )
  144.             )
  145.           )
  146.         )
  147.       )
  148.     )
  149.   ) as tranche_age,
  150.   if(pos.id_patient is null,'negative','positive') as status
  151. from
  152.   (
  153.     select
  154.       *
  155.     from
  156.       (
  157.         select
  158.           testing_mereenfant.id_patient,
  159.           infant_dob as dob,
  160.           infant_gender as gender
  161.         from
  162.           testing_mereenfant
  163.         where
  164.           date between @start_date
  165.           and @end_date
  166.         union
  167.         select
  168.           testing_specimen.id_patient,
  169.           date_of_birth as dob,
  170.           gender
  171.         from
  172.           testing_specimen
  173.           left join tracking_infant on testing_specimen.id_patient = tracking_infant.id_patient
  174.         where
  175.           date_blood_taken between @start_date
  176.           and @end_date
  177.         union
  178.         select
  179.           id_patient,
  180.           dob,
  181.           gender
  182.         from
  183.           tracking_infant
  184.         where
  185.           (
  186.             positive_pcr_1 between @start_date
  187.             and @end_date
  188.             or positive_pcr_2 between @start_date
  189.             and @end_date
  190.           )
  191.           or id_patient in (
  192.             select
  193.               *
  194.             from
  195.               (
  196.                 select
  197.                   id_patient
  198.                 from
  199.                   tracking_regime
  200.                 where
  201.                   category = 'regime_infant_treatment'
  202.                   and start_date between @start_date
  203.                   and @end_date
  204.                 union
  205.                 select
  206.                   id_patient
  207.                 from
  208.                   tracking_followup
  209.                 where
  210.                   date between @start_date
  211.                   and @end_date
  212.                 union
  213.                 select
  214.                   distinct id_patient
  215.                 from
  216.                   (
  217.                     select
  218.                       odk.patient_code,
  219.                       patient.id as id_patient
  220.                     from
  221.                       openfn.odk_child_visit as odk
  222.                       left join patient on patient.patient_code = odk.patient_code
  223.                     where
  224.                       is_available_at_time_visit = 1
  225.                       and date_of_visit between @start_date
  226.                       and @end_date
  227.                   ) e
  228.                 union
  229.                 select
  230.                   distinct session.id_patient
  231.                 from
  232.                   session
  233.                 where
  234.                   is_present = 1
  235.                   and id_club_session in (
  236.                     select
  237.                       club_session.id
  238.                     from
  239.                       club_session
  240.                       left join club on club.id = club_session.id_club
  241.                     where
  242.                       date between @start_date
  243.                       and @end_date
  244.                       and club_type != 1
  245.                   )
  246.               ) nj
  247.           )
  248.       ) n
  249.     where
  250.       dob is not null
  251.       and dob != '0000-00-00'
  252.       and (
  253.         gender = 1
  254.         or gender = 2
  255.       )
  256.       and id_patient not in (
  257.         select
  258.           id_patient
  259.         from
  260.           tracking_infant
  261.         where
  262.           (
  263.             is_dead = 1
  264.             and death_date < @start_date
  265.           )
  266.           or (
  267.             is_abandoned = 1
  268.             and abandoned_date < @start_date
  269.           )
  270.       )
  271.       and id_patient in (
  272.         select
  273.           id
  274.         from
  275.           patient
  276.         where
  277.           linked_to_id_patient = 0
  278.       )
  279.     group by
  280.       id_patient
  281.   )e
  282.   left join patient on patient.id = e.id_patient
  283.   left join tracking_infant ti on e.id_patient = ti.id_patient
  284.   left join testing_mereenfant tm on e.id_patient = tm.id_patient
  285.   left join testing_specimen ts on e.id_patient = ts.id_patient
  286.   left join (
  287.                             select id as id_patient from view_patient_positive
  288.                             union
  289.                             select id_patient from tracking_regime where category = 'regime_infant_treatment' and start_date between @start_date and @end_date
  290.                          )pos on pos.id_patient = e.id_patient
  291. group by patient.id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement