jeniferfleurant

Statut enfant

Aug 21st, 2018
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.95 KB | None | 0 0
  1. USE caris_db;
  2. SET @start_date="2017-04-01";
  3. SET @end_date= "2017-09-30";
  4.  
  5. select patient.patient_code, last_club_session
  6. , last_followup
  7. #, last_odk
  8. , last_arv_ini
  9. , e.date as date_mereenfant
  10. ,f.date as last_specimen
  11. , GREATEST(
  12.      COALESCE(last_club_session,0),
  13.      COALESCE(last_followup, 0),
  14.      COALESCE(last_arv_ini, 0),
  15.      COALESCE(e.date, 0),
  16.      COALESCE(f.date, 0)
  17.      #COALESCE(last_odk, 0)
  18.      ) as last_contact
  19. , if(coalesce(infant_gender, gender)=1,'male',if(coalesce(infant_gender, gender)=2,'female', if(coalesce(infant_gender, gender)=3,'unknown',''))) as gender
  20. , coalesce(infant_dob, dob, date_of_birth) as dob
  21. , timestampdiff(year, coalesce(infant_dob, dob, date_of_birth), @end_date) as age
  22. , if(is_dead=1, 'dead',if(timestampdiff(month,last_club_session, @end_date)<=6,'active',
  23.                         if(timestampdiff(month,last_followup, @end_date)<=6,'active',
  24.                             if(timestampdiff(month,last_followup, @end_date)<=6,'active',
  25.                                 if(timestampdiff(month,last_arv_ini, @end_date)<=6,'active',
  26.                                     if(timestampdiff(month,e.date, @end_date)<=6,'active',
  27.                                         if(timestampdiff(month,f.date, @end_date)<=6,'active','inactive'))))))) as statut
  28. , is_dead
  29. , if(h.id_patient is not null, "positive","neg") as statut_vih
  30. from patient
  31. left join (
  32.     select id_patient, date as last_club_session from session
  33.         left join
  34.             ( select id, date, id_club from club_session cs1 where date=(select max(date) from club_session cs2 where date between @start_date and @end_date and cs2.id_club=cs1.id_club)
  35.             )a1 on a1.id=id_club_session
  36.         left join club on club.id=id_club
  37.     where is_present=1 and club_type!=1
  38. )a on patient.id=a.id_patient
  39. left join (
  40.      select id_patient, date as last_followup from tracking_followup tf1 where date =(select max(date) from tracking_followup tf2 where date between @start_date and @end_date and tf1.id_patient=tf2.id_patient)
  41. )b on patient.id=b.id_patient
  42. #left join (
  43. #   select patient_code, date_of_visit as last_odk from openfn.odk_child_visit o1
  44. #    where timestampdiff(month,date_of_visit, @end_date)<=6 and date_of_visit=(select max(date_of_visit) from openfn.odk_child_visit o2 where date_of_visit between @start_date and @end_date and o1.patient_code=o2.patient_code )
  45. #)c on c.patient_code=patient.patient_code
  46. left join (
  47.     select id_patient, start_date as last_arv_ini from tracking_regime tr1 where category= 'regime_infant_treatment' and start_date=(select max(start_date) from tracking_regime tr2 where start_date between @start_date and @end_date and tr1.id_patient=tr2.id_patient)
  48. )d on d.id_patient=patient.id
  49. left join (select id_patient, date, infant_dob, infant_gender from testing_mereenfant where date between @start_date and @end_date
  50. )e on e.id_patient=patient.id
  51. left join (
  52. select * from(
  53.     select id_patient, date_blood_taken as date, which_pcr from testing_specimen
  54.     union
  55.     select id_patient, blood_draw_date as date, which_pcr from testing_result
  56.     )f1 where date=(select max(date) from (select * from (select id_patient, date_blood_taken as date, which_pcr from testing_specimen where date_blood_taken between @start_date and @end_date
  57.     union
  58.     select id_patient, blood_draw_date as date, which_pcr from testing_result where blood_draw_date between @start_date and @end_date)f2
  59.     )f4 where f1.id_patient=f4.id_patient
  60. )
  61. )f on f.id_patient=patient.id
  62. left join tracking_infant ti on ti.id_patient=patient.id
  63. left join (
  64.              select id_patient, date_of_birth from testing_specimen
  65.              group by id_patient
  66. ) g on g.id_patient= patient.id
  67. left join( select id as id_patient from view_patient_positive
  68. union
  69. select id_patient from tracking_regime where category='regime_infant_treatment'
  70. )h on h.id_patient=patient.id
  71. where
  72.  (a.id_patient is not null
  73.  or b.id_patient is not null
  74.  #or c.patient_code is not null
  75.  or d.id_patient is not null
  76.  or e.id_patient is not null
  77.  or f.id_patient is not null
  78.  )
  79. and linked_to_id_patient=0
  80. group by patient.id
Advertisement
Add Comment
Please, Sign In to add comment