Advertisement
jeniferfleurant

ovc_SERV children detailed (2019) old

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