Advertisement
thecassion

FROM JENI OVC_SERV children detailed (2019) WITH ARV INFORM

Apr 26th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 29.35 KB | None | 0 0
  1. USE caris_db;
  2. SET #start_date for Q1
  3.   @start_date = "2018-10-01";
  4. SET #end_date for Q1
  5.   @end_date = "2018-12-31";
  6. SET #start_date for Q2
  7.     @start_date_curr = "2019-01-01";
  8. SET #end_date for Q2
  9.  
  10.     @end_date_curr = "2019-03-31";
  11.     select alias.*, IF(ab.id_patient is not null, "yes","no") as on_arv from
  12. (
  13. select b.* from
  14. (SELECT
  15.   patient.id,
  16.   left(patient_code, 8) AS hospital_code,
  17.   patient_code,
  18.   if(
  19.     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
  20.     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',
  21.     'under1',
  22.     if(
  23.       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
  24.       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
  25.       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',
  26.       'bt_1_9',
  27.       if(
  28.         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
  29.         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'
  30.         and (
  31.           ti.gender = 1
  32.           or e.gender = 1
  33.           or tm.infant_gender = 1
  34.         ),
  35.         'm_under1',
  36.         if(
  37.           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
  38.           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'
  39.           and (
  40.             ti.gender = 2
  41.             or e.gender = 2
  42.             or tm.infant_gender = 2
  43.           ),
  44.           'f_under1',
  45.           if(
  46.             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
  47.             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
  48.             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'
  49.             and (
  50.               ti.gender = 1
  51.               or e.gender = 1
  52.               or tm.infant_gender = 1
  53.             ),
  54.             'm_bt_1_9',
  55.             if(
  56.               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
  57.               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
  58.               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'
  59.               and (
  60.                 ti.gender = 2
  61.                 or e.gender = 2
  62.                 or tm.infant_gender = 2
  63.               ),
  64.               'f_bt_1_9',
  65.               if(
  66.                 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
  67.                 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
  68.                 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'
  69.                 and (
  70.                   ti.gender = 1
  71.                   or e.gender = 1
  72.                   or tm.infant_gender = 1
  73.                 ),
  74.                 'm_bt_10_14',
  75.                 if(
  76.                   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
  77.                   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
  78.                   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'
  79.                   and (
  80.                     ti.gender = 2
  81.                     or e.gender = 2
  82.                     or tm.infant_gender = 2
  83.                   ),
  84.                   'f_bt_10_14',
  85.                   if(
  86.                     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
  87.                     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
  88.                     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'
  89.                     and (
  90.                       ti.gender = 1
  91.                       or e.gender = 1
  92.                       or tm.infant_gender = 1
  93.                     ),
  94.                     'm_bt_15_17',
  95.                     if(
  96.                       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
  97.                       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
  98.                       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'
  99.                       and (
  100.                         ti.gender = 2
  101.                         or e.gender = 2
  102.                         or tm.infant_gender = 2
  103.                       ),
  104.                       'f_bt_15_17',
  105.                       if(
  106.                         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
  107.                         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
  108.                         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'
  109.                         and (
  110.                           ti.gender = 1
  111.                           or e.gender = 1
  112.                           or tm.infant_gender = 1
  113.                         ),
  114.                         'm_bt_18_24',
  115.                         if(
  116.                           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
  117.                           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
  118.                           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'
  119.                           and (
  120.                             ti.gender = 2
  121.                             or e.gender = 2
  122.                             or tm.infant_gender = 2
  123.                           ),
  124.                           'f_bt_18_24',
  125.                           if(
  126.                             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
  127.                             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'
  128.                             and (
  129.                               ti.gender = 1
  130.                               or e.gender = 1
  131.                               or tm.infant_gender = 1
  132.                             ),
  133.                             'm_25',
  134.                             if(
  135.                               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
  136.                               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'
  137.                               and (
  138.                                 ti.gender = 2
  139.                                 or e.gender = 2
  140.                                 or tm.infant_gender = 2
  141.                               ),
  142.                               'f_25',
  143.                               ''
  144.                             )
  145.                           )
  146.                         )
  147.                       )
  148.                     )
  149.                   )
  150.                 )
  151.               )
  152.             )
  153.           )
  154.         )
  155.       )
  156.     )
  157.   ) as tranche_age,
  158.   if(pos.id_patient is null,'negative','positive') as status
  159. from
  160.   (
  161.     select
  162.       *
  163.     from
  164.       (
  165.         select
  166.           testing_mereenfant.id_patient,
  167.           infant_dob as dob,
  168.           infant_gender as gender
  169.         from
  170.           testing_mereenfant
  171.         where
  172.           date between @start_date
  173.           and @end_date
  174.         union
  175.         select
  176.           testing_specimen.id_patient,
  177.           date_of_birth as dob,
  178.           gender
  179.         from
  180.           testing_specimen
  181.           left join tracking_infant on testing_specimen.id_patient = tracking_infant.id_patient
  182.         where
  183.           date_blood_taken between @start_date
  184.           and @end_date
  185.         union
  186.         select
  187.           id_patient,
  188.           dob,
  189.           gender
  190.         from
  191.           tracking_infant
  192.         where
  193.           (
  194.             positive_pcr_1 between @start_date
  195.             and @end_date
  196.             or positive_pcr_2 between @start_date
  197.             and @end_date
  198.           )
  199.           or id_patient in (
  200.             select
  201.               *
  202.             from
  203.               (
  204.                 select
  205.                   id_patient
  206.                 from
  207.                   tracking_regime
  208.                 where
  209.                   category = 'regime_infant_treatment'
  210.                   and start_date between @start_date
  211.                   and @end_date
  212.                 union
  213.                 select
  214.                   id_patient
  215.                 from
  216.                   tracking_followup
  217.                 where
  218.                   date between @start_date
  219.                   and @end_date
  220.                 union
  221.                 select
  222.                   distinct id_patient
  223.                 from
  224.                   (
  225.                     select
  226.                       odk.patient_code,
  227.                       patient.id as id_patient
  228.                     from
  229.                       openfn.odk_child_visit as odk
  230.                       left join patient on patient.patient_code = odk.patient_code
  231.                     where
  232.                       is_available_at_time_visit = 1
  233.                       and date_of_visit between @start_date
  234.                       and @end_date
  235.                   ) e
  236.                 union
  237.                 select
  238.                   distinct session.id_patient
  239.                 from
  240.                   session
  241.                 where
  242.                   is_present = 1
  243.                   and id_club_session in (
  244.                     select
  245.                       club_session.id
  246.                     from
  247.                       club_session
  248.                       left join club on club.id = club_session.id_club
  249.                     where
  250.                       date between @start_date
  251.                       and @end_date
  252.                       and club_type != 1
  253.                   )
  254.               ) nj
  255.           )
  256.       ) n
  257.     where
  258.       dob is not null
  259.       and dob != '0000-00-00'
  260.       and (
  261.         gender = 1
  262.         or gender = 2
  263.       )
  264.       and id_patient not in (
  265.         select
  266.           id_patient
  267.         from
  268.           tracking_infant
  269.         where
  270.           (
  271.             is_dead = 1
  272.             and death_date < @start_date
  273.           )
  274.           or (
  275.             is_abandoned = 1
  276.             and abandoned_date < @start_date
  277.           )
  278.       )
  279.       and id_patient in (
  280.         select
  281.           id
  282.         from
  283.           patient
  284.         where
  285.           linked_to_id_patient = 0
  286.       )
  287.     group by
  288.       id_patient
  289.   )e
  290.   left join patient on patient.id = e.id_patient
  291.   left join tracking_infant ti on e.id_patient = ti.id_patient
  292.   left join testing_mereenfant tm on e.id_patient = tm.id_patient
  293.   left join testing_specimen ts on e.id_patient = ts.id_patient
  294.   left join (
  295.                             select id as id_patient from view_patient_positive
  296.                             union
  297.                             select id_patient from tracking_regime where category = 'regime_infant_treatment' and start_date between @start_date and @end_date
  298.                          )pos on pos.id_patient = e.id_patient
  299. group by patient.id
  300. )a
  301.  
  302. left join
  303.  
  304.  (SELECT
  305.   patient.id as id_patient,
  306.   left(patient_code, 8) AS hospital_code,
  307.   patient_code,
  308.   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) as birth_date,
  309.   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_curr) as age_up_to_date_curr,
  310.   IF( (ti.gender = 1  or e.gender = 1 or tm.infant_gender = 1),"male",IF ((ti.gender = 2 or e.gender = 2 or tm.infant_gender = 2),"female", "unknown")) as gender,
  311.  
  312.   if(
  313.     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_curr)< 1
  314.     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',
  315.     'under1',
  316.       if(
  317.         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_curr)< 1
  318.         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'
  319.         and (
  320.           ti.gender = 1
  321.           or e.gender = 1
  322.           or tm.infant_gender = 1
  323.         ),
  324.         'm_under1',
  325.         if(
  326.           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_curr)< 1
  327.           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'
  328.           and (
  329.             ti.gender = 2
  330.             or e.gender = 2
  331.             or tm.infant_gender = 2
  332.           ),
  333.           'f_under1',
  334.           if(
  335.             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_curr)>= 1
  336.             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_curr)< 5
  337.             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'
  338.             and (
  339.               ti.gender = 1
  340.               or e.gender = 1
  341.               or tm.infant_gender = 1
  342.             ),
  343.             'm_bt_1_4',
  344.             if(
  345.               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_curr)>= 1
  346.               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_curr)< 5
  347.               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'
  348.               and (
  349.                 ti.gender = 2
  350.                 or e.gender = 2
  351.                 or tm.infant_gender = 2
  352.               ),
  353.               'f_bt_1_4',
  354.           if(
  355.             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_curr)>= 5
  356.             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_curr)< 10
  357.             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'
  358.             and (
  359.               ti.gender = 1
  360.               or e.gender = 1
  361.               or tm.infant_gender = 1
  362.             ),
  363.             'm_bt_5_9',
  364.             if(
  365.               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_curr)>=5
  366.               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_curr)< 10
  367.               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'
  368.               and (
  369.                 ti.gender = 2
  370.                 or e.gender = 2
  371.                 or tm.infant_gender = 2
  372.               ),
  373.               'f_bt_5_9',
  374.              
  375.               if(
  376.                 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_curr)>= 10
  377.                 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_curr)< 15
  378.                 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'
  379.                 and (
  380.                   ti.gender = 1
  381.                   or e.gender = 1
  382.                   or tm.infant_gender = 1
  383.                 ),
  384.                 'm_bt_10_14',
  385.                 if(
  386.                   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_curr)>= 10
  387.                   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_curr)< 15
  388.                   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'
  389.                   and (
  390.                     ti.gender = 2
  391.                     or e.gender = 2
  392.                     or tm.infant_gender = 2
  393.                   ),
  394.                   'f_bt_10_14',
  395.                   if(
  396.                     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_curr)>= 15
  397.                     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_curr)< 18
  398.                     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'
  399.                     and (
  400.                       ti.gender = 1
  401.                       or e.gender = 1
  402.                       or tm.infant_gender = 1
  403.                     ),
  404.                     'm_bt_15_17',
  405.                     if(
  406.                       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_curr)>= 15
  407.                       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_curr)< 18
  408.                       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'
  409.                       and (
  410.                         ti.gender = 2
  411.                         or e.gender = 2
  412.                         or tm.infant_gender = 2
  413.                       ),
  414.                       'f_bt_15_17',
  415.                       if(
  416.                         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_curr)>= 18
  417.                         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_curr)< 25
  418.                         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'
  419.                         and (
  420.                           ti.gender = 1
  421.                           or e.gender = 1
  422.                           or tm.infant_gender = 1
  423.                         ),
  424.                         'm_bt_18_24',
  425.                         if(
  426.                           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_curr)>= 18
  427.                           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_curr)< 25
  428.                           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'
  429.                           and (
  430.                             ti.gender = 2
  431.                             or e.gender = 2
  432.                             or tm.infant_gender = 2
  433.                           ),
  434.                           'f_bt_18_24',
  435.                           if(
  436.                             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_curr)>= 25
  437.                             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'
  438.                             and (
  439.                               ti.gender = 1
  440.                               or e.gender = 1
  441.                               or tm.infant_gender = 1
  442.                             ),
  443.                             'm_25',
  444.                             if(
  445.                               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_curr)>= 25
  446.                               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'
  447.                               and (
  448.                                 ti.gender = 2
  449.                                 or e.gender = 2
  450.                                 or tm.infant_gender = 2
  451.                               ),
  452.                               'f_25',
  453.                               ''
  454.                             )
  455.                           )
  456.                         )
  457.                       )
  458.                     )
  459.                     )
  460.                   )
  461.                 )
  462.               )
  463.             )
  464.           )
  465.         )
  466.       )
  467.     )
  468.   ) as tranche_age,
  469.   if(pos.id_patient is null,'negative','positive') as status
  470. from
  471.   (
  472.     select
  473.       *
  474.     from
  475.       (
  476.         select
  477.           testing_mereenfant.id_patient,
  478.           infant_dob as dob,
  479.           infant_gender as gender
  480.         from
  481.           testing_mereenfant
  482.         where
  483.           date between @start_date_curr
  484.           and @end_date_curr
  485.         union
  486.         select
  487.           testing_specimen.id_patient,
  488.           date_of_birth as dob,
  489.           gender
  490.         from
  491.           testing_specimen
  492.           left join tracking_infant on testing_specimen.id_patient = tracking_infant.id_patient
  493.         where
  494.           date_blood_taken between @start_date_curr
  495.           and @end_date_curr
  496.         union
  497.         select
  498.           id_patient,
  499.           dob,
  500.           gender
  501.         from
  502.           tracking_infant
  503.         where
  504.           (
  505.             positive_pcr_1 between @start_date_curr
  506.             and @end_date_curr
  507.             or positive_pcr_2 between @start_date_curr
  508.             and @end_date_curr
  509.           )
  510.           or id_patient in (
  511.             select
  512.               *
  513.             from
  514.               (
  515.                 select
  516.                   id_patient
  517.                 from
  518.                   tracking_regime
  519.                 where
  520.                   category = 'regime_infant_treatment'
  521.                   and start_date between @start_date_curr
  522.                   and @end_date_curr
  523.                 union
  524.                 select
  525.                   id_patient
  526.                 from
  527.                   tracking_followup
  528.                 where
  529.                   date between @start_date_curr
  530.                   and @end_date_curr
  531.                 union
  532.                 select
  533.                   distinct id_patient
  534.                 from
  535.                   (
  536.                     select
  537.                       odk.patient_code,
  538.                       patient.id as id_patient
  539.                     from
  540.                       openfn.odk_child_visit as odk
  541.                       left join patient on patient.patient_code = odk.patient_code
  542.                     where
  543.                       is_available_at_time_visit = 1
  544.                       and date_of_visit between @start_date_curr
  545.                       and @end_date_curr
  546.                   ) e
  547.                 union
  548.                 select
  549.                   distinct session.id_patient
  550.                 from
  551.                   session
  552.                 where
  553.                   is_present = 1
  554.                   and id_club_session in (
  555.                     select
  556.                       club_session.id
  557.                     from
  558.                       club_session
  559.                       left join club on club.id = club_session.id_club
  560.                     where
  561.                       date between @start_date_curr
  562.                       and @end_date_curr
  563.                       and club_type != 1
  564.                   )
  565.               ) nj
  566.           )
  567.       ) n
  568.     where
  569.       dob is not null
  570.       and dob != '0000-00-00'
  571.       and (
  572.         gender = 1
  573.         or gender = 2
  574.       )
  575.       and id_patient not in (
  576.         select
  577.           id_patient
  578.         from
  579.           tracking_infant
  580.         where
  581.           (
  582.             is_dead = 1
  583.             and death_date < @start_date_curr
  584.           )
  585.           or (
  586.             is_abandoned = 1
  587.             and abandoned_date < @start_date_curr
  588.           )
  589.       )
  590.       and id_patient in (
  591.         select
  592.           id
  593.         from
  594.           patient
  595.         where
  596.           linked_to_id_patient = 0
  597.       )
  598.     group by
  599.       id_patient
  600.   )e
  601.   left join patient on patient.id = e.id_patient
  602.   left join tracking_infant ti on e.id_patient = ti.id_patient
  603.   left join testing_mereenfant tm on e.id_patient = tm.id_patient
  604.   left join testing_specimen ts on e.id_patient = ts.id_patient
  605.   left join (
  606.                             select id as id_patient from view_patient_positive
  607.                             union
  608.                             select id_patient from tracking_regime where category = 'regime_infant_treatment' and start_date between @start_date_curr and @end_date_curr
  609.                          )pos on pos.id_patient = e.id_patient
  610. group by patient.id
  611. )b on a.id=b.id_patient
  612. where id_patient is not null
  613.  
  614. ) alias
  615. ### TO DETERMINE IF A PATIENT IS ON ARV
  616. left join (
  617. SELECT
  618.     id_patient
  619. FROM
  620.     tracking_regime
  621. WHERE
  622.     category = 'regime_infant_treatment'
  623.         AND (id_arv > 0 OR start_date != '0000-00-00')
  624. UNION SELECT
  625.     id_patient
  626. FROM
  627.     tracking_followup
  628. WHERE
  629.     on_arv = 1
  630.  
  631. ) ab on ab.id_patient = alias.id_patient
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement