- गनियारी, जिला - बिलासपुर
+ ART CLIENT HAND HELD CARD
-
-
- दिनाँक
-
-
:
-
{{patient.registrationDate | bahmniDate}}
-
-
-
- रजिस्ट्रेशन
-
-
:
-
{{patient.primaryIdentifier.identifier}}
-
-
-
- नाम
-
-
:
-
- {{patient.fullNameLocal()}}
-
-
-
-
- उम्र
-
-
:
-
{{patient.age | age}}
-
-
-
- लिंग
-
-
:
-
{{patient.gender}}
-
-
-
- पिता/पति
-
-
:
-
{{patient.primaryRelative}}
-
-
-
- गाँव
-
-
:
-
{{patient.address.cityVillage}}
-
-
-
- तहसील
-
-
:
-
{{patient.address.address3}}
-
+
+
+ Facility Name :
+
+
.....................
+
+
+
+ Full Names:
+
+
+ {{patient.fullNameLocal()}}
+
+
+
+
+ Age: {{patient.age | age}}
+
+
+ Sex: {{patient.gender}}
+
+
+
+
+
+
+ ART No: {{patient.primaryIdentifier.identifier}}
+
+
+ HEI No: .............
+
+
+
+
+ ART Start Date:
+
+
+ .... / .... / ....
+
+
+
+
+
Initial ART Regimen:
+
+
+ .......................
+
+
-
+
+
+
+
Carry this card and present it to the health worker at every visit.
+
Keep the appointment date as indicated in this card.
+
In case of new symptoms, visit the facility or call health worker.
+
If you can't come, call health worker on number below.
+
+
Health Facility Telephone No. ......................
+
+
+
-
-
-
+
+
+
+
diff --git a/openmrs/apps/reports/facilityReports.json b/openmrs/apps/reports/facilityReports.json
new file mode 100644
index 000000000..c7c2d9f15
--- /dev/null
+++ b/openmrs/apps/reports/facilityReports.json
@@ -0,0 +1,30 @@
+{
+ "missedAppointmentReport": {
+ "name": "Missed Appointment Report",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/missedAppointments.sql"
+ }
+ },
+ "summaryAppointmentReport": {
+ "name": "Appointment Summary Report",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/summaryAppointmentReport.sql"
+ }
+ },
+ "visitAppointmentReport": {
+ "name": "Visit Appointment Report",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/visitAppointmentReport.sql"
+ }
+ },
+ "walkinAppointmentReport": {
+ "name": "WalkIn Visit Report",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/walkinAppointmentReport.sql"
+ }
+ }
+}
diff --git a/openmrs/apps/reports/monthlyReports.json b/openmrs/apps/reports/monthlyReports.json
new file mode 100644
index 000000000..8a3d54c92
--- /dev/null
+++ b/openmrs/apps/reports/monthlyReports.json
@@ -0,0 +1,130 @@
+{
+ "artRegisterReport": {
+ "name": "ART Register Report",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/artRegister.sql"
+ }
+ },
+ "artDispensingReport": {
+ "name": "ART Dispensing Report",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/artDispensingReport.sql"
+ }
+ },
+ "artMonthlyStartedOnArtReport": {
+ "name": "ART Monthly Report - Started on ART (New and cumulative number of persons)",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/artMonthlyStartedOnArtReport.sql"
+ }
+ },
+ "artMonthlyViralLoadReport": {
+ "name": "ART Monthly Report - Viral Load",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/artMonthlyViralLoadReport.sql"
+ }
+ },
+ "ancMonthlyReport": {
+ "name": "ANC PMTCT Register",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/ancreport.sql"
+ }
+ },
+ "highViralloadReport": {
+ "name": "High Viral Load Monthly Report",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/highViralloadReport.sql"
+ }
+ },
+ "hivExposedInfants": {
+ "name": "HIV Exposed Infant Monthly Reporting",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/hivExposedInfants.sql"
+ }
+ },
+ "PMTCTAllreports": {
+ "name": "PMTCT All Reporting",
+ "type": "concatenated",
+ "config": {
+ "reports": [
+ {
+ "name": "PMTCT ANC Reporting",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/pmtctAnc.sql"
+ }
+ },
+ {
+ "name": "PMTCT HIV Reporting",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/pmtcHiv.sql"
+ }
+ },
+ {
+ "name": "PMTCT ART And Tb Reporting",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/pmtctArtandTb.sql"
+ }
+ },
+ {
+ "name": "PMTCT ANC Partner-Index contact HIV Counseling and testing",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/pmtcANCpartnerindex.sql"
+ }
+ },
+ {
+ "name": "PMTCT HIV Counseling and Testing at Maternity",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/pmtctHIVcounselling.sql"
+ }
+ }
+ ]
+ }
+ },
+ "ArtCohortAnalysisForm": {
+ "name": "ART Cohort Analysis Form",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/ArtCohortAnalysisForm.sql"
+ }
+ },
+ "Maternity": {
+ "name": "Maternity PMTCT",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/maternitypmtct.sql"
+ }
+ },
+ "hivExposedInfantRegisterReport": {
+ "name": "HIV Exposed Infant Register Report",
+ "type": "concatenated",
+ "config": {
+ "reports": [
+ {
+ "name": "HIV Exposed Infant Register Report Page 1",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/hivExposedInfantRegisterReportTabFirst.sql"
+ }
+ },
+ {
+ "name": "HIV Exposed Infant Register Report Page 2",
+ "type": "MRSGeneric",
+ "config": {
+ "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/hivExposedInfantRegisterReportTabSecond.sql"
+ }
+ }
+ ]
+ }
+ }
+}
diff --git a/openmrs/apps/reports/reports.json b/openmrs/apps/reports/reports.json
index 056c565d6..ec16d89ed 100644
--- a/openmrs/apps/reports/reports.json
+++ b/openmrs/apps/reports/reports.json
@@ -26,635 +26,8 @@
]
}
},
- "testReport": {
- "name": "Test Observation Report",
- "type": "observations",
- "config": {
- "patientAttributes": [
- "caste",
- "class",
- "education",
- "occupation",
- "primaryContact"
- ],
- "patientAddresses": [
- "address3",
- "city_village"
- ],
- "visitAttributes": [
- "Visit Status",
- "Admission Status"
- ],
- "showVisitInfo": true,
- "showProvider": true,
- "conceptNamesToFilter": [
- ],
- "conceptNameDisplayFormat": "shortNamePreferred",
- "conceptValuesToFilter": ["0..20","100..","True"],
- "conceptClassesToFilter": [],
- "locationTagsToFilter": [],
- "programsToFilter": [],
- "applyDateRangeFor": "obsDate",
- "encounterPerRow": true,
- "forDataAnalysis": true,
- "visitTypesToFilter": [
- "PHARMACY VISIT",
- "OPD"
- ]
- }
- },
- "testFormReport":{
- "name": "Test Forms Report",
- "type": "forms",
- "config": {
- "patientAttributes": [
- "caste",
- "class",
- "education",
- "occupation",
- "primaryContact"
- ],
- "patientAddresses": [
- "address3",
- "city_village"
- ],
- "visitAttributes": [
- "Visit Status",
- "Admission Status"
- ],
- "showVisitInfo": true,
- "showProvider": true,
- "formNamesToFilter": ["Vitals"],
- "conceptNameDisplayFormat": "shortNamePreferred",
- "locationTagsToFilter": [],
- "programsToFilter": [],
- "applyDateRangeFor": "obsDate",
- "visitTypesToFilter": []
- }
- },
- "formBuilderReport":{
- "name": "Form builder form Report",
- "type": "formBuilder",
- "config": {
- "patientAttributes": [
- "caste",
- "class",
- "education",
- "occupation"
- ],
- "patientAddresses": [
- "address3",
- "city_village"
- ],
- "visitAttributes": [
- "Visit Status"
- ],
- "showVisitInfo": true,
- "showProvider": true,
- "formNamesToFilter": [ "Test Form"],
- "conceptNameDisplayFormat": "shortNamePreferred",
- "locationTagsToFilter": [],
- "programsToFilter": [],
- "applyDateRangeFor": "obsDate",
- "visitTypesToFilter": []
- }
- },
- "chiefComplaintReport": {
- "name": "Chief Complaint Report",
- "type": "obsCount",
- "config": {
- "ageGroupName": "All Ages",
- "conceptNames": [
- "Chief Complaint"
- ],
- "countOnlyClosedVisits": "false",
- "visitTypes": [
- "IPD",
- "OPD"
- ]
- }
- },
- "inpatientMorbidity": {
- "name": "Inpatient Morbidity",
- "type": "diagnosisCount",
- "config": {
- "visitTypes": [
- "IPD"
- ]
- }
- },
- "outpatientMorbidity": {
- "name": "Outpatient Morbidity",
- "type": "diagnosisCount",
- "config": {
- "visitTypes": [
- "OPD"
- ]
- }
- },
- "laboratoryServices": {
- "name": "Laboratory Services",
- "type": "TestCount"
- },
- "genericLaboratoryServices": {
- "name": "Generic Laboratory Services",
- "type": "ElisGeneric",
- "config": {
- "sqlPath": "/var/www/bahmni_config/openmrs/apps/reports/sql/testCount.sql"
- }
- },
- "bloodPressure": {
- "name": "Blood Pressure",
- "type": "obsCount",
- "config": {
- "ageGroupName": "All Ages",
- "conceptNames": [
- "Systolic",
- "Diastolic"
- ],
- "countOnlyClosedVisits": "false",
- "countOncePerPatient": "false",
- "visitTypes": [
- "OPD",
- "IPD"
- ]
- }
- },
- "smokingHistoryReport": {
- "name": "Smoking History Report",
- "type": "obsCount",
- "config": {
- "ageGroupName": "All Ages",
- "conceptNames": [
- "Smoking History"
- ],
- "countOnlyClosedVisits": "false",
- "countOncePerPatient": "true"
- }
- },
- "vitalsTemplateReport": {
- "name": "Vitals Template Report",
- "type": "obsTemplate",
- "config": {
- "templateName": "Vitals",
- "patientAttributes": [
- "caste",
- "education"
- ],
- "applyDateRangeFor": "encounterCreateDate"
- }
- },
- "tuberculosisIntakeTemplateReport": {
- "name": "Tuberculosis Intake Template Report",
- "type": "obsTemplate",
- "config": {
- "templateName": "Tuberculosis Intake Template"
- }
- },
- "diabetes": {
- "name": "Diabetes",
- "type": "obsCount",
- "config": {
- "ageGroupName": "All Ages",
- "conceptNames": [
- "Diabetes Intake"
- ],
- "visitTypes": [
- "OPD",
- "IPD"
- ]
- }
- },
- "codedValueRangeBasedReport": {
- "name": "Coded Value Range Based Report",
- "type": "obsCount",
- "config": {
- "ageGroupName": "All Ages",
- "conceptNames": [
- "Diabetes, Cormorbidities"
- ],
- "countOncePerPatient": "true"
- }
- },
- "OPD/IPDVisitCount": {
- "name": "OPD/IPD Visit Count",
- "type": "IpdOpdVisitCount"
- },
- "haemoglobinRangeBasedReport": {
- "name": "Haemoglobin Range Based Report",
- "type": "NumericConceptValuesCount",
- "config": {
- "rangeGroupName": "Haemoglobin",
- "ageGroupName": "Haemoglobin",
- "conceptNames": [
- "Haemoglobin"
- ],
- "countOncePerPatient": "false"
- }
- },
- "radiology(X-Ray)Count": {
- "name": "Radiology(X-Ray) Count",
- "type": "ObsCountByConceptClass",
- "config": {
- "conceptClassNames": [
- "Radiology"
- ]
- }
- },
- "obsValuesCountsForAlbuminMCHOperationDateAndPosture(bloodPressure)": {
- "name": "Obs Values Counts for Albumin, MCH, Operation Date and Posture(blood pressure)",
- "type": "ObsValueCount",
- "config": {
- "conceptNames": [
- "Albumin",
- "MCH",
- "Date of Operation",
- "Posture"
- ]
- }
- },
- "tuberculosisTreatmentCompletionDates": {
- "name": "Tuberculosis Treatment Completion dates",
- "type": "DateConceptValuesPatientsList",
- "config": {
- "conceptNames": [
- "Tuberculosis, Treatment Completion Date"
- ],
- "templateName": "Tuberculosis Outcome Template"
- }
- },
- "patientsListWithAlbuminMCHAndHIVELISA(Blood)TestsResults": {
- "name": "Patients list with Albumin, MCH and HIV ELISA (Blood) tests results",
- "type": "PatientsWithLabtestResults",
- "config": {
- "conceptNames": [
- "HIV ELISA (Blood)",
- "Albumin",
- "MCH"
- ],
- "testOutcome": [
- "abnormal",
- "normal"
- ]
- }
- },
- "IPDPatientsReport": {
- "name": "IPD Patients Report",
- "type": "ipdPatients",
- "config": {
- "addressAttributes": [
- "address1",
- "city_village"
- ],
- "patientAttributes": [
- "caste",
- "education"
- ],
- "conceptNames": [
- "Height",
- "Weight"
- ],
- "filterBy": "Date of Discharge"
- }
- },
- "nutritionProgramNutritionalStatusOfRegisteredChildren": {
- "name": "Nutrition Program - Nutritional status of registered children",
- "type": "obsCount",
- "config": {
- "ageGroupName": "All Ages",
- "conceptNames": [
- "Nutrition, Nutritional Status"
- ],
- "countOncePerPatient": "true",
- "countOnlyClosedVisits": "false"
- }
- },
- "patientsProgramStateCountReport": {
- "name": "Patients Program State Count Report",
- "type": "programStateCount",
- "config": {
- "programName": "Malaria Program"
- }
- },
- "programPatientStateReport": {
- "name": "Program Patient State Report",
- "type": "PatientProgramReport",
- "config": {
- "programName": "Malaria Program"
- }
- },
- "programStateTransitionReport": {
- "name": "Program State Transition Report",
- "type": "programStateTransitionReport",
- "config": {
- "programName": "Malaria Program"
- }
- },
- "OrderFulfillmentReport": {
- "name": "Order Fulfillment Report",
- "type": "OrderFulfillmentReport",
- "config": {
- "type": "OrderFulfillmentReport",
- "orderTypes": [
- "Order",
- "Drug Order"
- ]
- }
- },
- "visitReport": {
- "name": "Visit Report",
- "type": "visitReport",
- "config": {
- "personAttributes": [
- "caste",
- "class",
- "education",
- "occupation",
- "primaryContact"
- ],
- "visitAttributes": [
- "Visit Status"
- ]
- }
- },
"patientReport": {
"name": "Patient Information",
"type": "PatientReport"
- },
- "obsCannedReport": {
- "name": "Obs Canned Report",
- "type": "obsCannedReport",
- "config": {
- "patientAttributes": [
- "caste",
- "education"
- ],
- "applyDateRangeFor": "ObsRecording",
- "addressAttributes": [
- "postal_code",
- "city_village"
- ],
- "conceptNames": [
- "Pulse",
- "WEIGHT",
- "Vitals, Systolic",
- "Temperature"
- ],
- "visitIndependentConcept": [
- "HEIGHT",
- "Temperature"
- ],
- "enrolledProgram": "HIV Program",
- "showObsOnlyForProgramDuration": false
- }
- },
- "drugOrderReport": {
- "name": "Drug Order report",
- "type": "DrugOrder"
- },
- "SampleGenericProgramReport": {
- "name": "Generic program sample report",
- "type": "programs",
- "config": {
- "patientAttributes": [
- "education",
- "primaryContact",
- "secondaryContact",
- "class",
- "cluster"
- ],
- "patientAddresses": [
- "city_village",
- "address3"
- ],
- "programAttributes": [],
- "forDataAnalysis": true,
- "showAllStates": true,
- "programNamesToFilter": [
- "HIV Program"
- ]
- }
- },
- "pivotReport": {
- "name": "Blood Pressure",
- "type": "aggregation",
- "config": {
- "report":{
- "type": "observations",
- "config": {
- "conceptNamesToFilter":["Diastolic Data", "Systolic Data"],
- "showVisitInfo" : true,
- "forDataAnalysis": true,
- "visitAttributes": ["Visit Status", "Admission Status"],
- "visitTypesToFilter": ["IPD"]
-
- }
- },
- "rowGroups": [
- "Gender"
- ],
- "columnGroups": [
- "Concept Name", "value"
- ],
- "distinctGroups": [
- "Patient Identifier"
- ]
- }
- },
- "pivotReportOfVisits": {
- "name": "Aggregation report for visits",
- "type": "aggregation",
- "config": {
- "report":{
- "type": "visits",
- "config": {
- "forDataAnalysis": true,
- "patientAttributes": [
- "caste",
- "class",
- "education",
- "occupation",
- "primaryContact"
- ],
- "visitAttributes": [
- "Visit Status",
- "Admission Status"
- ],
- "patientAddresses": [
- "address3",
- "city_village"
- ],
- "applyDateRangeFor": "visitStopDate",
- "visitTypesToFilter": [
- "PHARMACY VISIT",
- "OPD"
- ]
- }
- },
- "rowGroups": [
- "Gender"
- ],
- "columnGroups": [
- "Visit Status"
- ],
- "distinctGroups": [
- "Patient Identifier"
- ]
- }
- },
- "pivotReportForPrograms": {
- "name": "HIV Program Pivot Report",
- "type": "aggregation",
- "config": {
- "report": {
- "type": "programs",
- "config": {
- "patientAttributes": [
- ],
- "patientAddresses": [
- "city_village",
- "address3"
- ],
- "programAttributes": [],
- "forDataAnalysis": true,
- "showAllStates": true,
- "programNamesToFilter": [
- "TB Program",
- "HIV Program"
- ]
- }
- },
- "columnGroups": [
- "Program name"
- ],
- "rowGroups": [
- "Gender"
- ],
- "distinctGroups": [
- "Patient Identifier"
- ],
- "showTotalRow":true,
- "showTotalColumn":true
-
- }
- },
- "concatenatedReport": {
- "name": "ConcatenatedReport",
- "type": "concatenated",
- "config": {
- "reports": [
- {
- "name": "Visit Report",
- "type": "visits",
- "config": {
- "forDataAnalysis": true,
- "patientAttributes": [
- "caste",
- "class",
- "education",
- "occupation",
- "primaryContact"
- ],
- "visitAttributes": [
- "Visit Status",
- "Admission Status"
- ],
- "patientAddresses": [
- "address3",
- "city_village"
- ],
- "applyDateRangeFor": "visitStopDate",
- "visitTypesToFilter": [
- "PHARMACY VISIT",
- "OPD"
- ]
- }
- },
- {
- "name": "Obs Canned Report",
- "type": "obsCannedReport",
- "config": {
- "patientAttributes": [
- "caste",
- "education"
- ],
- "applyDateRangeFor": "ObsRecording",
- "addressAttributes": [
- "postal_code",
- "city_village"
- ],
- "conceptNames": [
- "Pulse",
- "WEIGHT",
- "Vitals, Systolic",
- "Temperature"
- ],
- "visitIndependentConcept": [
- "HEIGHT",
- "Temperature"
- ],
- "enrolledProgram": "HIV Program",
- "showObsOnlyForProgramDuration": false
- }
- }
- ]
- }
- },
- "GenericLabOrderReport": {
- "name": "Lab Order Results",
- "type": "labOrders",
- "config": {
- "patientAttributes": [
- "caste",
- "class",
- "education",
- "occupation",
- "primaryContact"
- ],
- "patientAddresses": [
- "address3",
- "city_village"
- ],
- "visitAttributes": [
- "Visit Status"
- ],
- "showVisitInfo": true,
- "showProvider": true,
- "programsToFilter": [],
- "showReferredOutTests": true,
- "conceptNamesToFilter": [
- "Haemoglobin (Blood)"
- ],
- "conceptValuesToFilter": [
- "10..100"
- ],
- "forDataAnalysis": true,
- "excludeColumns": [
- "address3"
- ],
- "showOrderDateTime": true
- }
- },
- "GenericLabOrderReportAggregation": {
- "name": "Test Name vs Gender",
- "type": "aggregation",
- "config": {
- "report": {
- "type": "labOrders",
- "config": {
- "forDataAnalysis": true,
- "showVisitInfo": true
- }
- },
- "rowGroups": [
- "Test Name"
- ],
- "columnGroups": [
- "Gender"
- ],
- "distinctGroups": [
- "Patient Identifier"
- ]
}
- }
}
diff --git a/openmrs/apps/reports/sql/ArtCohortAnalysisForm.sql b/openmrs/apps/reports/sql/ArtCohortAnalysisForm.sql
new file mode 100644
index 000000000..e98f666cc
--- /dev/null
+++ b/openmrs/apps/reports/sql/ArtCohortAnalysisForm.sql
@@ -0,0 +1,470 @@
+select 'Started ART In this Clinic (original Cohort)',
+(case when startdate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+(case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+(case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+(case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+(case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+(case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+(case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+ from
+(
+select distinct(o.patient_id) , min(o.date_created) as 'startdate',o.concept_id , dr.dosage_form , pa.person_id from orders o
+left join drug dr on o.concept_id = dr.concept_id
+left join person_attribute pa on o.patient_id = pa.person_id
+left join person p on pa.person_id = p.person_id
+where dr.dosage_form = (select concept_id from concept_name where uuid = '95955de4-7440-4482-88f6-e5daefc2d738') and pa.person_attribute_type_id = 33 and
+pa.value not in (SELECT concept_id FROM openmrs.concept_name where name = 'Transfer-in' and concept_name_type = 'FULLY_SPECIFIED')
+group by o.patient_id ) patients
+union all
+select 'Transfer in',
+(case when startdate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+(case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+(case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+(case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+(case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+(case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+(case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+ from
+(
+select distinct(o.patient_id) , min(o.date_created) as 'startdate',o.concept_id , dr.dosage_form , pa.person_id from orders o
+left join drug dr on o.concept_id = dr.concept_id
+left join person_attribute pa on o.patient_id = pa.person_id
+left join person p on pa.person_id = p.person_id
+where dr.dosage_form = (select concept_id from concept_name where uuid = '95955de4-7440-4482-88f6-e5daefc2d738') and pa.person_attribute_type_id = 33 and
+pa.value = (SELECT concept_id FROM openmrs.concept_name where name = 'Transfer-in' and concept_name_type = 'FULLY_SPECIFIED')
+group by o.patient_id ) patients
+union all
+select 'Transfer out',
+(case when obs.value_datetime between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+(case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+(case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+(case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+(case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+(case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+(case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+from orders o
+left join drug dr on o.concept_id = dr.concept_id
+left join concept_name cn on o.concept_id = cn.concept_id
+left join obs obs on o.patient_id = obs.person_id
+where dr.dosage_form = (select concept_id from concept_name where uuid = '95955de4-7440-4482-88f6-e5daefc2d738')
+and obs.concept_id = (select concept_id from concept_name where name = 'Transferred Out Date' and concept_name_type = 'FULLY_SPECIFIED'
+and obs.status = 'FINAL')
+union all
+select 'Net Current Cohort', (@initial1 + @tranferIn1 - @transferOut1) as 'start' , (@initial2 + @tranferIn2 - @transferOut2) as '6mo' , (@initial3 + @tranferIn3 - @transferOut3) as '12mo'
+,(@initial4 + @tranferIn4 - @transferOut4) as '24mo', (@initial5 + @tranferIn5 - @transferOut5) as '36mo', (@initial6 + @tranferIn6 - @transferOut6) as '48mo'
+,(@initial7 + @tranferIn7 - @transferOut7) as '60mo'
+from (
+select
+@initial1 := (case when startdate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+@initial2 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end),
+@initial3 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end),
+@initial4 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end),
+@initial5 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end),
+@initial6 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end),
+@initial7 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end)
+ from
+(
+select distinct(o.patient_id) , min(o.date_created) as 'startdate',o.concept_id , dr.dosage_form , pa.person_id from orders o
+left join drug dr on o.concept_id = dr.concept_id
+left join person_attribute pa on o.patient_id = pa.person_id
+left join person p on pa.person_id = p.person_id
+where dr.dosage_form = (select concept_id from concept_name where uuid = '95955de4-7440-4482-88f6-e5daefc2d738') and pa.person_attribute_type_id = 33 and
+pa.value not in (SELECT concept_id FROM openmrs.concept_name where name = 'Transfer-in' and concept_name_type = 'FULLY_SPECIFIED')
+group by o.patient_id ) patients
+union all
+select
+@tranferIn1 := (case when startdate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+@tranferIn2 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end),
+@tranferIn3 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end),
+@tranferIn4 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end),
+@tranferIn5 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end),
+@tranferIn6 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end),
+@tranferIn7 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end)
+ from
+(
+select distinct(o.patient_id) , min(o.date_created) as 'startdate',o.concept_id , dr.dosage_form , pa.person_id from orders o
+left join drug dr on o.concept_id = dr.concept_id
+left join person_attribute pa on o.patient_id = pa.person_id
+left join person p on pa.person_id = p.person_id
+where dr.dosage_form = (select concept_id from concept_name where uuid = '95955de4-7440-4482-88f6-e5daefc2d738') and pa.person_attribute_type_id = 33 and
+pa.value = (SELECT concept_id FROM openmrs.concept_name where name = 'Transfer-in' and concept_name_type = 'FULLY_SPECIFIED')
+group by o.patient_id ) patients
+union all
+select
+@transferOut1 := (case when obs.value_datetime between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+@transferOut2 := (case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+@transferOut3 := (case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+@transferOut4 := (case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+@transferOut5 := (case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+@transferOut6 := (case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+@transferOut7 := (case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+from orders o
+left join drug dr on o.concept_id = dr.concept_id
+left join concept_name cn on o.concept_id = cn.concept_id
+left join obs obs on o.patient_id = obs.person_id
+where dr.dosage_form = (select concept_id from concept_name where uuid = '95955de4-7440-4482-88f6-e5daefc2d738')
+and obs.concept_id = (select concept_id from concept_name where name = 'Transferred Out Date' and concept_name_type = 'FULLY_SPECIFIED'
+and obs.status = 'FINAL')
+) tNetCohort limit 1
+union all
+select 'On Original 1st Line Regimen',
+(case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+where cn.name in ('1a = AZT/3TC+EFV' , '1b = AZT/3TC/NVP', '1c = TDF/3TC/DTG','1d=ABC/3TC (600/300)/DTG',
+'1e = AZT/3TC +DTG','1f = TDF/3TC+EFV','1g = TDF/3TC+NVP', '1h = TDF/FTC/EFV') and cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen' and concept_name_type = 'Fully_specified')
+group by o.patient_id having count(o.patient_id) = 1) pp
+union all
+select 'On Alternate 1st Line Regimen (Substituted)',
+(case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+where cn.name in ('1a = AZT/3TC+EFV' , '1b = AZT/3TC/NVP', '1c = TDF/3TC/DTG','1d=ABC/3TC (600/300)/DTG',
+'1e = AZT/3TC +DTG','1f = TDF/3TC+EFV','1g = TDF/3TC+NVP', '1h = TDF/FTC/EFV') and cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen' and concept_name_type = 'Fully_specified')
+group by o.patient_id having count(o.patient_id) > 1) tAltenateFirstLine
+union all
+select 'On 2nd Line Regimen (Switched)',
+(case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+ from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+where cn.name in ('2a=AZT/3TC+DTG' , '2b=ABC/3TC+DTG', '2c=TDF+3TC+LPV/r','2d=TDF/3TC+ATV/r',
+'2e=TDF/FTC-LPV/r','2f=TDF/FTC-ATV/r','2g=AZT/3TC+LPV/r', '2h=AZT/3TC+ATV/r','2i=ABC/3TC+LPV/r','2j=ABC/3TC+ATV/r','2k=TDF/3TC/DTG') and cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen' and concept_name_type = 'Fully_specified')
+group by o.patient_id having count(o.patient_id) > 0) tSecondSubstitutedLine
+union all
+select 'Stopped',
+(case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+left join obs ob on o.patient_id = ob.person_id
+where cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen'
+and concept_name_type = 'Fully_specified')
+and ob.concept_id = (select concept_id from concept_name where name = 'Interruption type' and concept_name_type = 'Fully_specified')
+and ob.value_coded = (select concept_id from concept_name where name = 'Stop' and concept_name_type = 'Fully_specified')
+group by o.patient_id having count(o.patient_id) > 0) tRegimenStopped
+union all
+select 'Died',
+(case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(patient_id) else 0 end) as 'start',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(patient_id) else 0 end) as '6mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(patient_id) else 0 end) as '12mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(patient_id) else 0 end) as '24mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(patient_id) else 0 end) as '36mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(patient_id) else 0 end) as '48mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(patient_id) else 0 end) as '60mo'
+from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+left join obs ob on o.patient_id = ob.person_id
+where cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen'
+and concept_name_type = 'Fully_specified')
+and ob.concept_id = (select concept_id from concept_name where name = 'End Of Follow Up Reason' and concept_name_type = 'Fully_specified')
+and ob.value_coded = (select concept_id from concept_name where name = 'Death during treatment' and concept_name_type = 'Fully_specified')
+group by o.patient_id having count(o.patient_id) > 0 ) tPatientDied
+union all
+select 'Lost to Follow-up (DROP)',
+(case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59')then count(patient_id) else 0 end) as 'start',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH)then count(distinct(patient_id)) else 0 end) as '12mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+left join obs ob on o.patient_id = ob.person_id
+where cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen'
+and concept_name_type = 'Fully_specified')
+and ob.concept_id = (select concept_id from concept_name where name = 'End Of Follow Up Reason' and concept_name_type = 'Fully_specified')
+and ob.value_coded = (select concept_id from concept_name where name = 'Lost Follow Up(< 28days)' and concept_name_type = 'Fully_specified')
+group by o.patient_id having count(o.patient_id) > 0) tLostToFollowup
+union all
+select 'Alive and on ART (E+F+G)',sum(start) as 'start' , sum(6mo) as '6mo' , sum(12mo) as '12mo' , sum(24mo) as '24mo' , sum(36mo) as '36mo' , sum(48mo) as '48mo' , sum(60mo) as '60mo' from (
+select 'On Original 1st Line Regimen',
+(case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+where cn.name in ('1a = AZT/3TC+EFV' , '1b = AZT/3TC/NVP', '1c = TDF/3TC/DTG','1d=ABC/3TC (600/300)/DTG',
+'1e = AZT/3TC +DTG','1f = TDF/3TC+EFV','1g = TDF/3TC+NVP', '1h = TDF/FTC/EFV') and cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen' and concept_name_type = 'Fully_specified')
+group by o.patient_id having count(o.patient_id) = 1) pp
+union all
+select 'On Alternate 1st Line Regimen (Substituted)',
+(case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+where cn.name in ('1a = AZT/3TC+EFV' , '1b = AZT/3TC/NVP', '1c = TDF/3TC/DTG','1d=ABC/3TC (600/300)/DTG',
+'1e = AZT/3TC +DTG','1f = TDF/3TC+EFV','1g = TDF/3TC+NVP', '1h = TDF/FTC/EFV') and cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen' and concept_name_type = 'Fully_specified')
+group by o.patient_id having count(o.patient_id) > 1) tAltenateFirstLine
+union all
+select 'On 2nd Line Regimen (Switched)',
+(case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+ from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+where cn.name in ('2a=AZT/3TC+DTG' , '2b=ABC/3TC+DTG', '2c=TDF+3TC+LPV/r','2d=TDF/3TC+ATV/r',
+'2e=TDF/FTC-LPV/r','2f=TDF/FTC-ATV/r','2g=AZT/3TC+LPV/r', '2h=AZT/3TC+ATV/r','2i=ABC/3TC+LPV/r','2j=ABC/3TC+ATV/r','2k=TDF/3TC/DTG') and cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen' and concept_name_type = 'Fully_specified')
+group by o.patient_id having count(o.patient_id) > 0) tSecondSubstitutedLine
+)tefg
+union all
+select 'Percent of cohort alive and on ART[K / D * 100 ]', CEIL(@perc1) as 'start', CEIL(@perc2) as '6mo' , CEIL(@perc3) as '12mo' ,CEIL(@perc4) as '24mo' , CEIL(@perc5) as '36mo', CEIL(@perc6) as '48mo', CEIL(@perc7) as '60mo' from (
+select @perc1 := CEIL((@original1+@alternate1+@switched1) / (@initial1 + @tranferIn1 - @transferOut1) * 100) as 'start', @perc2 := CEIL((@original2+@alternate2+@switched2) / (@initial2 + @tranferIn2 - @transferOut2) * 100) as '6mo',@perc3 := CEIL((@original3+@alternate3+@switched3) / (@initial3 + @tranferIn3 - @transferOut3) * 100) as '12mo',
+@perc4 := CEIL((@original4+@alternate4+@switched4)/(@initial4 + @tranferIn4 - @transferOut4) * 100) as '24mo',
+@perc5 := CEIL((@original5+@alternate5+@switched5) / (@initial5 + @tranferIn5 - @transferOut5) * 100) as '36mo',@perc6 := CEIL((@original6+@alternate6+@switched6) / (@initial2 + @tranferIn2 - @transferOut2) * 100) as '48mo', @perc7 := CEIL((@original7+@alternate7+@switched7) /(@initial2 + @tranferIn2 - @transferOut2) * 100)as '60mo'from (
+select 'On Original 1st Line Regimen',
+@original1 := (case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+@original2 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+@original3 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+@original4 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+@original5 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+@original6 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+@original7 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+where cn.name in ('1a = AZT/3TC+EFV' , '1b = AZT/3TC/NVP', '1c = TDF/3TC/DTG','1d=ABC/3TC (600/300)/DTG',
+'1e = AZT/3TC +DTG','1f = TDF/3TC+EFV','1g = TDF/3TC+NVP', '1h = TDF/FTC/EFV') and cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen' and concept_name_type = 'Fully_specified')
+group by o.patient_id having count(o.patient_id) = 1) pp
+union all
+select 'On Alternate 1st Line Regimen (Substituted)',
+@alternate1 := (case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+@alternate2 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+@alternate3 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+@alternate4 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+@alternate5 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+@alternate6 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+@alternate7 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+where cn.name in ('1a = AZT/3TC+EFV' , '1b = AZT/3TC/NVP', '1c = TDF/3TC/DTG','1d=ABC/3TC (600/300)/DTG',
+'1e = AZT/3TC +DTG','1f = TDF/3TC+EFV','1g = TDF/3TC+NVP', '1h = TDF/FTC/EFV') and cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen' and concept_name_type = 'Fully_specified')
+group by o.patient_id having count(o.patient_id) > 1) tAltenateFirstLine
+union all
+select 'On 2nd Line Regimen (Switched)',
+@switched1 := (case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+@switched2 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+@switched3 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+@switched4 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+@switched5 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+@switched6 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+@switched7 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+ from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+where cn.name in ('2a=AZT/3TC+DTG' , '2b=ABC/3TC+DTG', '2c=TDF+3TC+LPV/r','2d=TDF/3TC+ATV/r',
+'2e=TDF/FTC-LPV/r','2f=TDF/FTC-ATV/r','2g=AZT/3TC+LPV/r', '2h=AZT/3TC+ATV/r','2i=ABC/3TC+LPV/r','2j=ABC/3TC+ATV/r','2k=TDF/3TC/DTG') and cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen' and concept_name_type = 'Fully_specified')
+group by o.patient_id having count(o.patient_id) > 0) tSecondSubstitutedLine
+)tefg limit 1
+union all
+select
+@initial1 := (case when startdate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+@initial2 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end),
+@initial3 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end),
+@initial4 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end),
+@initial5 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end),
+@initial6 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end),
+@initial7 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end)
+ from
+(
+select distinct(o.patient_id) , min(o.date_created) as 'startdate',o.concept_id , dr.dosage_form , pa.person_id from orders o
+left join drug dr on o.concept_id = dr.concept_id
+left join person_attribute pa on o.patient_id = pa.person_id
+left join person p on pa.person_id = p.person_id
+where dr.dosage_form = (select concept_id from concept_name where uuid = '95955de4-7440-4482-88f6-e5daefc2d738') and pa.person_attribute_type_id = 33 and
+pa.value not in (SELECT concept_id FROM openmrs.concept_name where name = 'Transfer-in' and concept_name_type = 'FULLY_SPECIFIED')
+group by o.patient_id ) patients
+union all
+select
+@tranferIn1 := (case when startdate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+@tranferIn2 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end),
+@tranferIn3 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end),
+@tranferIn4 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end),
+@tranferIn5 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end),
+@tranferIn6 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end),
+@tranferIn7 := (case when startdate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end)
+ from
+(
+select distinct(o.patient_id) , min(o.date_created) as 'startdate',o.concept_id , dr.dosage_form , pa.person_id from orders o
+left join drug dr on o.concept_id = dr.concept_id
+left join person_attribute pa on o.patient_id = pa.person_id
+left join person p on pa.person_id = p.person_id
+where dr.dosage_form = (select concept_id from concept_name where uuid = '95955de4-7440-4482-88f6-e5daefc2d738') and pa.person_attribute_type_id = 33 and
+pa.value = (SELECT concept_id FROM openmrs.concept_name where name = 'Transfer-in' and concept_name_type = 'FULLY_SPECIFIED')
+group by o.patient_id ) patients
+union all
+select
+@transferOut1 := (case when obs.value_datetime between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+@transferOut2 := (case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+@transferOut3 := (case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+@transferOut4 := (case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+@transferOut5 := (case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+@transferOut6 := (case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+@transferOut7 := (case when obs.value_datetime <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+from orders o
+left join drug dr on o.concept_id = dr.concept_id
+left join concept_name cn on o.concept_id = cn.concept_id
+left join obs obs on o.patient_id = obs.person_id
+where dr.dosage_form = (select concept_id from concept_name where uuid = '95955de4-7440-4482-88f6-e5daefc2d738')
+and obs.concept_id = (select concept_id from concept_name where name = 'Transferred Out Date' and concept_name_type = 'FULLY_SPECIFIED'
+and obs.status = 'FINAL') limit 1
+) hh
+union all
+select 'Fraction CD4 <200 (of adults with available CD4 at baseline)', CEIL(@baselineStart / @baseStart ) as 'start', CEIL(@baseline6 / @base6) as '6mo', CEIL(@baseline12 / @base12 ) as '12mo', CEIL(@baseline24 / @base24 ) as '24mo', CEIL(@baseline36 / @base36 ) as '36mo',
+CEIL(@baseline48 / @base48 ) as '48mo', CEIL(@baseline60 / @base60 ) as '60mo' from (
+select 'Fraction CD4 <200 (of adults with available CD4 at baseline)',
+@baselineStart := (case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+@baseline6 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+@baseline12 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+@baseline24 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+@baseline36 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+@baseline48 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+@baseline60 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+left join obs ob on o.patient_id = ob.person_id
+left join person pa on ob.person_id = pa.person_id
+where cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen'
+and concept_name_type = 'Fully_specified')
+and ob.concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'Fully_specified')
+and ob.value_numeric < 200 and TIMESTAMPDIFF(YEAR,pa.birthdate,NOW()) >= 18
+group by o.patient_id having count(o.patient_id) > 0) tb2
+union all
+select 'Fraction CD4 <200 (of adults with available CD4 at baseline)',
+@baseStart := (case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(distinct(patient_id)) else 0 end) as 'start',
+@base6 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+@base12 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+@base24 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+@base36 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+@base48 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+@base60 := (case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+left join obs ob on o.patient_id = ob.person_id
+left join person pa on ob.person_id = pa.person_id
+where cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen'
+and concept_name_type = 'Fully_specified')
+and ob.concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'Fully_specified')
+and ob.value_numeric > 0 and TIMESTAMPDIFF(YEAR,pa.birthdate,NOW()) >= 18
+group by o.patient_id having count(o.patient_id) > 0) tb1
+)tcd4lessthan200 LIMIT 1
+union all
+select 'Done Viral Load (VL)',
+(case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(patient_id) else 0 end) as 'start',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+ from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+left join obs ob on o.patient_id = ob.person_id
+where cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen'
+and concept_name_type = 'Fully_specified')
+and ob.concept_id = (select concept_id from concept_name where name = 'VL Results' and concept_name_type = 'Fully_specified')
+and ob.value_numeric > 0
+group by o.patient_id having count(o.patient_id) > 0) tDoneViralload
+union all
+select 'Viral Load < 1000 copies /ml',
+(case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(patient_id) else 0 end) as 'start',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+left join obs ob on o.patient_id = ob.person_id
+where cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen'
+and concept_name_type = 'Fully_specified')
+and ob.concept_id = (select concept_id from concept_name where name = 'VL Results' and concept_name_type = 'Fully_specified')
+and ob.value_numeric < 1000
+group by o.patient_id having count(o.patient_id) > 0) tVllessthanthousand
+union all
+select 'Presumptive TB (Pr TB)',
+(case when mindate between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then count(patient_id) else 0 end) as 'start',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) then count(distinct(patient_id)) else 0 end) as '6mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) then count(distinct(patient_id)) else 0 end) as '12mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) then count(distinct(patient_id)) else 0 end) as '24mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) then count(distinct(patient_id)) else 0 end) as '36mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 48 MONTH) then count(distinct(patient_id)) else 0 end) as '48mo',
+(case when mindate <= DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 60 MONTH) then count(distinct(patient_id)) else 0 end) as '60mo'
+ from (
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+left join obs ob on o.patient_id = ob.person_id
+where cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen'
+and concept_name_type = 'Fully_specified')
+and ob.concept_id = (select concept_id from concept_name where name = 'On TB Treatment' and concept_name_type = 'Fully_specified')
+and ob.value_coded = (select concept_id from concept_name where name = 'True' and concept_name_type =
+'fully_specified')
+group by o.patient_id having count(o.patient_id) > 0) tPresumptiveTb
diff --git a/openmrs/apps/reports/sql/ancreport.sql b/openmrs/apps/reports/sql/ancreport.sql
new file mode 100644
index 000000000..c7cdf47db
--- /dev/null
+++ b/openmrs/apps/reports/sql/ancreport.sql
@@ -0,0 +1,621 @@
+
+select tDemographics.fullnames as 'Full Names', tDemographics.UniqueARTs as 'Unique ART' , tDemographics.TelephoneNumber as 'Telephone Number',
+tDemographics.Age, tOccupation.Occupation , tRetestingDate.Retesting as 'HIV Retesting for ART initiation?' , tARTStartDate.ARTStartDate as 'ART Start Date',
+tLNMPDate.LNMP as 'LNMP' , tEDDDate.EDD as 'EDD' , tGestationPeriod.Gestationalage as 'Gestational age in weeks (GA)', tWeight.weight as 'Weight(Kg)', tMUAC.muac as 'Mid-Upper Arm Circumference (MUAC)',
+tTBDiagnosd.TBStatus , tWHOStage.whostage as 'WHO Stage' , tCdfour.cdfour as 'CD4 COUNT', tHivResult.partnerResult as 'Partner Result', tCotrimoxazole.Cotrimoxazole as 'CTX=Contrimoxazole or DAP=Dapsone',
+tBrx.tbrx as 'Date Started TBrx', tbRegNumber.tbRegNo as 'TB Reg No.', tDateVLCollected.dateVLSampleCollected as 'Date VL SampleCollected' , tVLResults.VLResults as 'VL result (Value)',
+tHeiNumber.HeiNumber as 'Unique ART No/HEI No', tOriginaFirstlineRegimen.originalfirstlineregimen as 'Original Firstline Regimen' , tOriginalRegimen.substitutionwithinfirstregimen as ' Substitutions within 1st line ',
+tSwitchedSecondlineRegimen.secondlineregimenchanged as '2nd: Substitution drug code', concat('weight(kgs)',"\n",'---------------',"\n",' CD4/VL') as month3,
+concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month4) as '4',concat(tttweight.weightMonth5,"\n",'------',"\n",' ',ttcd44.cd4month5) as '5',
+concat(tttweight.weightMonth6,"\n",'------',"\n",' ',ttcd44.cd4month6) as '6',concat(tttweight.weightMonth7,"\n",'------',"\n",' ',ttcd44.cd4month7) as '7',
+concat(tttweight.weightMonth8,"\n",'------',"\n",' ',ttcd44.cd4month8) as '8',concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month9) as '9',
+concat(tttweight.weightMonth10,"\n",'------',"\n",' ',ttcd44.cd4month10) as '10',concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month11) as '11',
+concat(tttweight.weightMonth12,"\n",'------',"\n",' ',ttcd44.cd4month12) as '12',concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month13) as '13',
+concat(tttweight.weightMonth14,"\n",'------',"\n",' ',ttcd44.cd4month14) as '14',concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month15) as '15',
+concat(tttweight.weightMonth16,"\n",'------',"\n",' ',ttcd44.cd4month16) as '16',concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month17) as '17',
+concat(tttweight.weightMonth18,"\n",'------',"\n",' ',ttcd44.cd4month18) as '18',concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month19) as '19',
+concat(tttweight.weightMonth20,"\n",'------',"\n",' ',ttcd44.cd4month20) as '20',concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month21) as '21',
+concat(tttweight.weightMonth22,"\n",'------',"\n",' ',ttcd44.cd4month22) as '22',concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month23) as '23',
+concat(tttweight.weightMonth24,"\n",'------',"\n",' ',ttcd44.cd4month24) as '24',concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month25) as '25',
+concat(tttweight.weightMonth26,"\n",'------',"\n",' ',ttcd44.cd4month26) as '26',concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month27) as '27',
+concat(tttweight.weightMonth28,"\n",'------',"\n",' ',ttcd44.cd4month28) as '28',concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month29) as '29',
+concat(tttweight.weightMonth30,"\n",'------',"\n",' ',ttcd44.cd4month30) as '30',concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month31) as '31',
+concat(tttweight.weightMonth32,"\n",'------',"\n",' ',ttcd44.cd4month32) as '32',concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month33) as '33',
+concat(tttweight.weightMonth34,"\n",'------',"\n",' ',ttcd44.cd4month34) as '34',concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month35) as '35',
+concat(tttweight.weightMonth36,"\n",'------',"\n",' ',ttcd44.cd4month34) as '36',concat(tttweight.weightMonth4,"\n",'------',"\n",' ',ttcd44.cd4month34) as '37',
+concat(tttweight.weightMonth38,"\n",'------',"\n",' ',ttcd44.cd4month34) as '38'
+from (
+select tART.person_id, tART.FullNames as 'fullnames' , tART.UNIQUEART as 'UniqueARTs' ,tMobile.TelephoneNumber as 'TelephoneNumber' ,
+ TIMESTAMPDIFF(YEAR,birthdate,NOW()) as 'Age'from ( select distinct(pn.person_id) as pid ,CONCAT(pn.given_name, ' ', pn.middle_name) as 'FullNames',
+ pa.value as 'UNIQUEART',p.birthdate , p.person_id
+ from person_name pn
+ left join person p on pn.person_id = p.person_id
+ left join person_attribute pa on p.person_id = pa.person_id
+ where p.gender = 'F'
+ and pa.person_attribute_type_id = (select person_attribute_type_id from person_attribute_type where name = 'UniqueArtNo')) tART
+ left join
+ (select * from ( select distinct(pn.person_id) as pid ,CONCAT(pn.given_name, ' ', pn.middle_name),
+ pa.value as 'TelephoneNumber' from person_name pn left join person p on pn.person_id = p.person_id
+ left join person_attribute pa on p.person_id = pa.person_id
+ where p.gender = 'F' and pa.person_attribute_type_id = (select person_attribute_type_id
+ from person_attribute_type where name = 'MobileNumber')) tART ) tMobile on tART.pid = tMobile.pid
+ )tDemographics
+ inner join
+ (
+select person_id , concept_id ,value_coded ,date_created from obs
+where concept_id = (select concept_id from concept_name where name = 'HIV - Entry Point'
+and concept_name_type = 'fully_specified' ) and
+value_coded = (select concept_id from concept_name where name = 'ANC Clinic' and concept_name_type = 'fully_specified')
+and date_created between '#startDate#' and '#endDate#'
+ )tAncPatients on tDemographics.person_id = tAncPatients.person_id
+ left join
+ (
+select distinct(person_id) , concept_short_name as 'Occupation' from(
+select obs.person_id , obs.value_coded,concept_view.concept_short_name,obs.obs_id
+from obs inner join concept_view on obs.value_coded=concept_view.concept_id inner join concept on obs.concept_id=concept.concept_id
+where concept.concept_id= (select concept_id from concept_name where name = 'Occupation' and concept_name_type = 'Fully_specified') and obs.voided = 0
+)tOccupa
+)tOccupation on tDemographics.person_id = tOccupation.person_id
+left join
+(
+select obs.person_id,obs.concept_id , obs.value_datetime as 'Dateretested' ,
+(case when obs.value_datetime is not null then 'Yes' else 'No' end) as 'Retesting'
+ from obs obs
+left join concept_name cn on obs.concept_id = cn.concept_id
+where cn.name = 'Date of HIV Retesting Before ART' and cn.concept_name_type = 'fully_specified' group by obs.person_id
+)tRetestingDate on tDemographics.person_id = tRetestingDate.person_id
+left join
+(
+select obs.person_id,obs.concept_id , obs.value_datetime as 'ARTStartDate'
+from obs obs
+left join concept_name cn on obs.concept_id = cn.concept_id
+where cn.name = 'ANC, ART Start Date' and cn.concept_name_type = 'fully_specified' and obs.voided = 0 group by obs.person_id
+) tARTStartDate on tDemographics.person_id = tARTStartDate.person_id
+left join
+(
+select obs.person_id,obs.concept_id , obs.value_datetime as 'LNMP'
+from obs obs
+left join concept_name cn on obs.concept_id = cn.concept_id
+where cn.name = 'Date of the first Day of LNMP' and cn.concept_name_type = 'fully_specified' and obs.voided = 0 group by obs.person_id
+)tLNMPDate on tDemographics.person_id = tLNMPDate.person_id
+left join
+(
+select obs.person_id,obs.concept_id , obs.value_datetime as 'EDD'
+from obs obs
+left join concept_name cn on obs.concept_id = cn.concept_id
+where cn.name = 'EDD' and cn.concept_name_type = 'fully_specified' and obs.voided = 0 group by obs.person_id
+)tEDDDate on tDemographics.person_id = tEDDDate.person_id
+left join
+(
+select obs.person_id,obs.concept_id , obs.value_numeric as 'Gestationalage'
+from obs obs
+left join concept_name cn on obs.concept_id = cn.concept_id
+where cn.name = 'Gestation(Weeks)' and cn.concept_name_type = 'fully_specified' and obs.voided = 0 group by obs.person_id
+)tGestationPeriod on tDemographics.person_id = tGestationPeriod.person_id
+left join
+(
+select t1.person_id , t1.concept_id , t1.value_numeric as 'weight',t1.date_created from obs t1
+where t1.date_created =
+(select MAX(t2.date_created) from obs t2 where t2.person_id = t1.person_id and t2.concept_id =
+(select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified')) and t1.concept_id =
+(select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified')
+and t1.date_created between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#endDate#'),'%Y-%m-%d 23:59:59')
+)tWeight on tDemographics.person_id = tWeight.person_id
+left join
+(
+select obs.person_id,obs.concept_id , obs.value_numeric as 'muac'
+from obs obs
+left join concept_name cn on obs.concept_id = cn.concept_id
+where cn.name = 'MUAC, Pregnancy Visit' and cn.concept_name_type = 'fully_specified' and obs.voided = 0 group by obs.person_id
+)tMUAC on tDemographics.person_id = tMUAC.person_id
+left join (
+select obs.person_id,obs.concept_id,
+(case when obs.value_coded = 1 then 'Positive' else 'Negative' end) as 'TBStatus'
+from obs obs
+left join concept_name cn on obs.concept_id = cn.concept_id
+where cn.name = 'TB Diagnosed?' and cn.concept_name_type = 'fully_specified' and obs.voided = 0 group by obs.person_id
+) tTBDiagnosd on tDemographics.person_id = tTBDiagnosd.person_id
+left join
+(
+select person_id , concept_short_name as 'whostage' from(
+select obs.person_id , obs.value_coded,concept_view.concept_short_name,obs.obs_id
+from obs inner join concept_view on obs.value_coded=concept_view.concept_id inner join concept on obs.concept_id=concept.concept_id
+where concept.concept_id= (select concept_id from concept_name where name = 'WHO Stage' and concept_name_type = 'Fully_specified') and obs.voided = 0 group by person_id
+)tOccupation
+)tWHOStage on tDemographics.person_id = tWHOStage.person_id
+left join
+(
+select t1.person_id , t1.concept_id , t1.value_numeric as 'cdfour',t1.date_created from obs t1
+where t1.date_created =
+(select MAX(t2.date_created) from obs t2 where t2.person_id = t1.person_id and t2.concept_id =
+(select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified')) and t1.concept_id =
+(select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified')
+and t1.date_created between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#endDate#'),'%Y-%m-%d 23:59:59')
+)tCdfour on tDemographics.person_id = tCdfour.person_id
+left join
+(
+select person_id , concept_short_name as 'partnerResult'
+ from(
+select obs.person_id , obs.value_coded,concept_view.concept_short_name,obs.obs_id
+from obs inner join concept_view on obs.value_coded=concept_view.concept_id inner join concept on obs.concept_id=concept.concept_id
+where concept.concept_id= (select concept_id from concept_name where name = 'Result' and concept_name_type = 'Fully_specified') and obs.voided = 0 group by person_id
+)tOccupation
+)tHivResult on tDemographics.person_id = tHivResult.person_id
+LEFT JOIN
+(
+select obs.person_id,obs.concept_id,
+(case when obs.value_coded = 1 then 'YES' else 'NO' end) as 'Cotrimoxazole'
+from obs obs
+left join concept_name cn on obs.concept_id = cn.concept_id
+where cn.name = 'Cotrimoxazole/Dapsone' and cn.concept_name_type = 'fully_specified' and obs.voided = 0 group by obs.person_id
+) tCotrimoxazole on tDemographics.person_id = tCotrimoxazole.person_id
+left join
+(
+select obs.person_id,obs.concept_id , obs.value_datetime as 'tbrx'
+from obs obs
+left join concept_name cn on obs.concept_id = cn.concept_id
+where cn.name = 'Date Started TB RX' and cn.concept_name_type = 'fully_specified' and obs.voided = 0 group by obs.person_id
+)tBrx on tDemographics.person_id = tBrx.person_id
+left join
+(
+select obs.person_id,obs.concept_id , obs.value_text as 'tbRegNo'
+from obs obs
+left join concept_name cn on obs.concept_id = cn.concept_id
+where cn.name = 'TB Unit Number' and cn.concept_name_type = 'fully_specified' and obs.voided = 0 group by obs.person_id
+)tbRegNumber on tDemographics.person_id = tbRegNumber.person_id
+left join
+(
+select obs.person_id,obs.concept_id , obs.value_datetime as 'dateVLSampleCollected'
+from obs obs
+left join concept_name cn on obs.concept_id = cn.concept_id
+where cn.name = 'Date VL Sample Collected? ' and cn.concept_name_type = 'fully_specified' and obs.voided = 0 group by obs.person_id
+)tDateVLCollected on tDemographics.person_id = tDateVLCollected.person_id
+left join
+(
+select obs.person_id,obs.concept_id , obs.value_numeric as 'VLResults'
+from obs obs
+left join concept_name cn on obs.concept_id = cn.concept_id
+where cn.name = 'VL Results' and cn.concept_name_type = 'fully_specified' and obs.voided = 0 group by obs.person_id
+)tVLResults on tDemographics.person_id = tVLResults.person_id
+left join
+(
+select obs.person_id,obs.concept_id , obs.value_text as 'HeiNumber'
+from obs obs
+left join concept_name cn on obs.concept_id = cn.concept_id
+where cn.name = 'Unique ART No/HEI No' and cn.concept_name_type = 'fully_specified' and obs.voided = 0 group by obs.person_id
+)tHeiNumber on tDemographics.person_id = tHeiNumber.person_id
+left join
+(
+select o.patient_id, o.concept_id , dr.dosage_form , min(o.date_created) as 'mindate' , cn.name as 'originalfirstlineregimen' from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+where cn.name in ('1a = AZT/3TC+EFV' , '1b = AZT/3TC/NVP', '1c = TDF/3TC/DTG','1d=ABC/3TC (600/300)/DTG',
+'1e = AZT/3TC +DTG','1f = TDF/3TC+EFV','1g = TDF/3TC+NVP', '1h = TDF/FTC/EFV') and cn.concept_name_type = 'Fully_specified' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen' and concept_name_type = 'Fully_specified')
+group by o.patient_id having count(o.patient_id) >= 1
+)tOriginaFirstlineRegimen on tDemographics.person_id = tOriginaFirstlineRegimen.patient_id
+left join
+(
+select ts.patient_id , ts.name as 'substitutionwithinfirstregimen' from (
+select o.patient_id, o.concept_id , dr.dosage_form , cn.name , max(o.date_created)
+from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+where cn.name in ('1a = AZT/3TC+EFV' , '1b = AZT/3TC/NVP', '1c = TDF/3TC/DTG','1d=ABC/3TC (600/300)/DTG',
+'1e = AZT/3TC +DTG','1f = TDF/3TC+EFV','1g = TDF/3TC+NVP', '1h = TDF/FTC/EFV')
+and cn.concept_name_type = 'Fully_specified' and
+ dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen' and concept_name_type = 'Fully_specified')
+ group by o.patient_id having count(o.patient_id)> 1
+ ) tt
+ left join
+ (
+ select o.patient_id , o.concept_id ,o.date_created , cn.name from orders o
+ left join drug dr on o.concept_id = dr.concept_id
+ left join concept_name cn on o.concept_id = cn.concept_id
+ inner join (select patient_id, concept_id , max(date_created) maxdate from orders group by patient_id ) b on o.patient_id = b.patient_id and o.date_created = b.maxdate
+ where dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen' and concept_name_type = 'Fully_specified')
+ and cn.name in ('1a = AZT/3TC+EFV' , '1b = AZT/3TC/NVP', '1c = TDF/3TC/DTG','1d=ABC/3TC (600/300)/DTG',
+'1e = AZT/3TC +DTG','1f = TDF/3TC+EFV','1g = TDF/3TC+NVP', '1h = TDF/FTC/EFV')
+and cn.concept_name_type = 'Fully_specified'
+) ts on tt.patient_id = ts.patient_id
+)tOriginalRegimen on tDemographics.person_id= tOriginalRegimen.patient_id
+left join
+(
+select tst.patient_id , tst.name as 'secondlineregimenchanged' from (
+select o.patient_id, o.concept_id , dr.dosage_form , cn.name , max(o.date_created)
+from orders o
+left join drug dr on o.concept_id = dr.concept_id
+inner join concept_name cn on o.concept_id = cn.concept_id
+where cn.name in ('1a = AZT/3TC+EFV' , '1b = AZT/3TC/NVP', '1c = TDF/3TC/DTG','1d=ABC/3TC (600/300)/DTG',
+'1e = AZT/3TC +DTG','1f = TDF/3TC+EFV','1g = TDF/3TC+NVP', '1h = TDF/FTC/EFV')
+and cn.concept_name_type = 'Fully_specified' and
+ dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen' and concept_name_type = 'Fully_specified')
+ group by o.patient_id having count(o.patient_id) > 0
+ ) tt
+ left join
+ (
+ select o.patient_id , o.concept_id ,o.date_created , cn.name from orders o
+ left join drug dr on o.concept_id = dr.concept_id
+ left join concept_name cn on o.concept_id = cn.concept_id
+ inner join (select patient_id, concept_id , max(date_created) maxdate from orders group by patient_id ) b on o.patient_id = b.patient_id and o.date_created = b.maxdate
+ where dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen' and concept_name_type = 'Fully_specified')
+ and cn.name in ('2a=AZT/3TC+DTG' , '2b=ABC/3TC+DTG', '2c=TDF+3TC+LPV/r','2d=TDF/3TC+ATV/r',
+'2e=TDF/FTC-LPV/r','2f=TDF/FTC-ATV/r','2g=AZT/3TC+LPV/r', '2h=AZT/3TC+ATV/r','2i=ABC/3TC+LPV/r','2j=ABC/3TC+ATV/r','2k=TDF/3TC/DTG')
+and cn.concept_name_type = 'Fully_specified'
+) tst on tt.patient_id = tst.patient_id
+) tSwitchedSecondlineRegimen on tDemographics.person_id = tSwitchedSecondlineRegimen.patient_id
+left join
+(
+select * from (
+select tcd4month0.person_id , month0,cd4month4 ,cd4month5 ,cd4month6 ,cd4month7 ,cd4month8,cd4month9,cd4month10,cd4month11,cd4month12,
+cd4month13 ,cd4month14 ,cd4month15 ,cd4month16,cd4month17,cd4month18,cd4month19,cd4month20,cd4month21 ,cd4month22 ,cd4month23 ,
+cd4month24,cd4month25,cd4month26,cd4month27,cd4month28 ,cd4month29,cd4month30,cd4month31,cd4month32,cd4month33,cd4month34,cd4month35 from (
+select person_id,
+(case when date_created between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then value_numeric else 0 end) as 'month0'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') group by person_id
+)tcd4month0
+left join (
+select person_id, value_numeric as 'cd4month4'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 4 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 4 MONTH) group by person_id
+)ttcd4month4 on tcd4month0.person_id = ttcd4month4.person_id
+left join (
+select person_id, value_numeric as 'cd4month5'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 5 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 5 MONTH) group by person_id
+)ttcd4month5 on tcd4month0.person_id = ttcd4month5.person_id
+left join (
+select person_id, value_numeric as 'cd4month6'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 6 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) group by person_id
+)ttcd4month6 on tcd4month0.person_id = ttcd4month6.person_id
+left join (
+select person_id, value_numeric as 'cd4month7'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 7 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 7 MONTH) group by person_id
+)ttcd4month7 on tcd4month0.person_id = ttcd4month7.person_id
+left join (
+select person_id, value_numeric as 'cd4month8'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 8 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 8 MONTH) group by person_id
+)ttcd4month8 on tcd4month0.person_id = ttcd4month8.person_id
+left join (
+select person_id, value_numeric as 'cd4month9'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 9 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 9 MONTH) group by person_id
+)ttcd4month9 on tcd4month0.person_id = ttcd4month9.person_id
+left join (
+select person_id, value_numeric as 'cd4month10'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 10 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 10 MONTH) group by person_id
+)ttcd4month10 on tcd4month0.person_id = ttcd4month10.person_id
+left join (
+select person_id, value_numeric as 'cd4month11'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 11 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 11 MONTH) group by person_id
+)ttcd4month11 on tcd4month0.person_id = ttcd4month11.person_id
+left join (
+select person_id, value_numeric as 'cd4month12'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 12 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) group by person_id
+)ttcd4month12 on tcd4month0.person_id = ttcd4month12.person_id
+left join (
+select person_id, value_numeric as 'cd4month13'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 13 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 13 MONTH) group by person_id
+)ttcd4month13 on tcd4month0.person_id = ttcd4month13.person_id
+left join (
+select person_id, value_numeric as 'cd4month14'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 14 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 14 MONTH) group by person_id
+)ttcd4month14 on tcd4month0.person_id = ttcd4month14.person_id
+left join (
+select person_id, value_numeric as 'cd4month15'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 15 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 15 MONTH) group by person_id
+)ttcd4month15 on tcd4month0.person_id = ttcd4month15.person_id
+left join (
+select person_id, value_numeric as 'cd4month16'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 16 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 16 MONTH) group by person_id
+)ttcd4month16 on tcd4month0.person_id = ttcd4month16.person_id
+left join (
+select person_id, value_numeric as 'cd4month17'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 17 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 17 MONTH) group by person_id
+)ttcd4month17 on tcd4month0.person_id = ttcd4month17.person_id
+left join (
+select person_id, value_numeric as 'cd4month18'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 18 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 18 MONTH) group by person_id
+)ttcd4month18 on tcd4month0.person_id = ttcd4month18.person_id
+left join (
+select person_id, value_numeric as 'cd4month19'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 19 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 19 MONTH) group by person_id
+)ttcd4month19 on tcd4month0.person_id = ttcd4month19.person_id
+left join (
+select person_id, value_numeric as 'cd4month20'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 20 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 20 MONTH) group by person_id
+)ttcd4month20 on tcd4month0.person_id = ttcd4month20.person_id
+left join (
+select person_id, value_numeric as 'cd4month21'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 21 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 21 MONTH) group by person_id
+)ttcd4month21 on tcd4month0.person_id = ttcd4month21.person_id
+left join (
+select person_id, value_numeric as 'cd4month22'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 22 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 22 MONTH) group by person_id
+)ttcd4month22 on tcd4month0.person_id = ttcd4month22.person_id
+left join (
+select person_id, value_numeric as 'cd4month23'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 23 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 23 MONTH) group by person_id
+)ttcd4month23 on tcd4month0.person_id = ttcd4month23.person_id
+left join (
+select person_id, value_numeric as 'cd4month24'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 24 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) group by person_id
+)ttcd4month24 on tcd4month0.person_id = ttcd4month24.person_id
+left join (
+select person_id, value_numeric as 'cd4month25'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 25 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 25 MONTH) group by person_id
+)ttcd4month25 on tcd4month0.person_id = ttcd4month25.person_id
+left join (
+select person_id, value_numeric as 'cd4month26'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 26 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 26 MONTH) group by person_id
+)ttcd4month26 on tcd4month0.person_id = ttcd4month26.person_id
+left join (
+select person_id, value_numeric as 'cd4month27'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 27 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 27 MONTH) group by person_id
+)ttcd4month27 on tcd4month0.person_id = ttcd4month27.person_id
+left join (
+select person_id, value_numeric as 'cd4month28'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 28 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 28 MONTH) group by person_id
+)ttcd4month28 on tcd4month0.person_id = ttcd4month28.person_id
+left join (
+select person_id, value_numeric as 'cd4month29'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 29 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 29 MONTH) group by person_id
+)ttcd4month29 on tcd4month0.person_id = ttcd4month29.person_id
+left join (
+select person_id, value_numeric as 'cd4month30'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 30 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 30 MONTH) group by person_id
+)ttcd4month30 on tcd4month0.person_id = ttcd4month30.person_id
+left join (
+select person_id, value_numeric as 'cd4month31'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 31 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 31 MONTH) group by person_id
+)ttcd4month31 on tcd4month0.person_id = ttcd4month31.person_id
+left join (
+select person_id, value_numeric as 'cd4month32'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 32 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 32 MONTH) group by person_id
+)ttcd4month32 on tcd4month0.person_id = ttcd4month32.person_id
+left join (
+select person_id, value_numeric as 'cd4month33'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 33 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 33 MONTH) group by person_id
+)ttcd4month33 on tcd4month0.person_id = ttcd4month33.person_id
+left join (
+select person_id, value_numeric as 'cd4month34'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 34 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 34 MONTH) group by person_id
+)ttcd4month34 on tcd4month0.person_id = ttcd4month34.person_id
+left join (
+select person_id, value_numeric as 'cd4month35'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 35 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 35 MONTH) group by person_id
+)ttcd4month35 on tcd4month0.person_id = ttcd4month35.person_id
+left join (
+select person_id, value_numeric as 'cd4month36'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 36 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) group by person_id
+)ttcd4month36 on tcd4month0.person_id = ttcd4month36.person_id
+left join (
+select person_id, value_numeric as 'cd4month37'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 37 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 37 MONTH) group by person_id
+)ttcd4month37 on tcd4month0.person_id = ttcd4month37.person_id
+left join (
+select person_id, value_numeric as 'cd4month38'
+from obs where concept_id = (select concept_id from concept_name where name = 'CD4' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 38 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 38 MONTH) group by person_id
+)ttcd4month38 on tcd4month0.person_id = ttcd4month38.person_id
+)ttcd4
+)ttcd44 on tDemographics.person_id = ttcd44.person_id
+left join
+(
+select * from (
+select tWeightmonth0.person_id , weightMonth4 , weightMonth5, weightMonth6, weightMonth7,weightMonth8 , weightMonth9, weightMonth10, weightMonth11,weightMonth12 , weightMonth13, weightMonth14, weightMonth15,
+weightMonth16 , weightMonth17, weightMonth18, weightMonth19,weightMonth20 , weightMonth21, weightMonth22, weightMonth23,weightMonth24 , weightMonth25, weightMonth26, weightMonth27,
+weightMonth28 , weightMonth29, weightMonth30, weightMonth31,weightMonth32 , weightMonth33, weightMonth34, weightMonth35 , weightMonth36, weightMonth37, weightMonth38 from (
+select person_id,
+(case when date_created between DATE_FORMAT('#startDate#','%Y-%m-01') and DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59') then value_numeric else 0 end) as 'month0'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') group by person_id
+)tWeightmonth0
+left join (
+select person_id, value_numeric as 'weightMonth4'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 4 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 4 MONTH) group by person_id
+)tWeightmonth4 on tWeightmonth0.person_id = tWeightmonth4.person_id
+left join (
+select person_id, value_numeric as 'weightMonth5'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 5 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 5 MONTH) group by person_id
+)tWeightmonth5 on tWeightmonth0.person_id = tWeightmonth5.person_id
+left join (
+select person_id, value_numeric as 'weightMonth6'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 6 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 6 MONTH) group by person_id
+)tWeightmonth6 on tWeightmonth0.person_id = tWeightmonth6.person_id
+left join (
+select person_id, value_numeric as 'weightMonth7'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 7 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 7 MONTH) group by person_id
+)tWeightmonth7 on tWeightmonth0.person_id = tWeightmonth7.person_id
+left join (
+select person_id, value_numeric as 'weightMonth8'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 8 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 8 MONTH) group by person_id
+)tWeightmonth8 on tWeightmonth0.person_id = tWeightmonth8.person_id
+left join (
+select person_id, value_numeric as 'weightMonth9'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 9 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 9 MONTH) group by person_id
+)tWeightmonth9 on tWeightmonth0.person_id = tWeightmonth9.person_id
+left join (
+select person_id, value_numeric as 'weightMonth10'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 10 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 10 MONTH) group by person_id
+)tWeightmonth10 on tWeightmonth0.person_id = tWeightmonth10.person_id
+left join (
+select person_id, value_numeric as 'weightMonth11'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 11 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 11 MONTH) group by person_id
+)tWeightmonth11 on tWeightmonth0.person_id = tWeightmonth11.person_id
+left join (
+select person_id, value_numeric as 'weightMonth12'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 12 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 12 MONTH) group by person_id
+)tWeightmonth12 on tWeightmonth0.person_id = tWeightmonth12.person_id
+left join (
+select person_id, value_numeric as 'weightMonth13'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 13 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 13 MONTH) group by person_id
+)tWeightmonth13 on tWeightmonth0.person_id = tWeightmonth13.person_id
+left join (
+select person_id, value_numeric as 'weightMonth14'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 14 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 14 MONTH) group by person_id
+)tWeightmonth14 on tWeightmonth0.person_id = tWeightmonth14.person_id
+left join (
+select person_id, value_numeric as 'weightMonth15'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 15 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 15 MONTH) group by person_id
+)tWeightmonth15 on tWeightmonth0.person_id = tWeightmonth15.person_id
+left join (
+select person_id, value_numeric as 'weightMonth16'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 16 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 16 MONTH) group by person_id
+)tWeightmonth16 on tWeightmonth0.person_id = tWeightmonth16.person_id
+left join (
+select person_id, value_numeric as 'weightMonth17'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 17 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 17 MONTH) group by person_id
+)tWeightmonth17 on tWeightmonth0.person_id = tWeightmonth17.person_id
+left join (
+select person_id, value_numeric as 'weightMonth18'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 18 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 18 MONTH) group by person_id
+)tWeightmonth18 on tWeightmonth0.person_id = tWeightmonth18.person_id
+left join (
+select person_id, value_numeric as 'weightMonth19'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 19 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 19 MONTH) group by person_id
+)tWeightmonth19 on tWeightmonth0.person_id = tWeightmonth19.person_id
+left join (
+select person_id, value_numeric as 'weightMonth20'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 20 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 20 MONTH) group by person_id
+)tWeightmonth20 on tWeightmonth0.person_id = tWeightmonth20.person_id
+left join (
+select person_id, value_numeric as 'weightMonth21'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 21 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 21 MONTH) group by person_id
+)tWeightmonth21 on tWeightmonth0.person_id = tWeightmonth21.person_id
+left join (
+select person_id, value_numeric as 'weightMonth22'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 22 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 22 MONTH) group by person_id
+)tWeightmonth22 on tWeightmonth0.person_id = tWeightmonth22.person_id
+left join (
+select person_id, value_numeric as 'weightMonth23'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 23 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 23 MONTH) group by person_id
+)tWeightmonth23 on tWeightmonth0.person_id = tWeightmonth23.person_id
+left join (
+select person_id, value_numeric as 'weightMonth24'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 24 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 24 MONTH) group by person_id
+)tWeightmonth24 on tWeightmonth0.person_id = tWeightmonth24.person_id
+left join (
+select person_id, value_numeric as 'weightMonth25'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 25 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 25 MONTH) group by person_id
+)tWeightmonth25 on tWeightmonth0.person_id = tWeightmonth25.person_id
+left join (
+select person_id, value_numeric as 'weightMonth26'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 26 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 26 MONTH) group by person_id
+)tWeightmonth26 on tWeightmonth0.person_id = tWeightmonth26.person_id
+left join (
+select person_id, value_numeric as 'weightMonth27'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 27 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 27 MONTH) group by person_id
+)tWeightmonth27 on tWeightmonth0.person_id = tWeightmonth27.person_id
+left join (
+select person_id, value_numeric as 'weightMonth28'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 28 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 28 MONTH) group by person_id
+)tWeightmonth28 on tWeightmonth0.person_id = tWeightmonth28.person_id
+left join (
+select person_id, value_numeric as 'weightMonth29'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 29 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 29 MONTH) group by person_id
+)tWeightmonth29 on tWeightmonth0.person_id = tWeightmonth29.person_id
+left join (
+select person_id, value_numeric as 'weightMonth30'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 30 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 30 MONTH) group by person_id
+)tWeightmonth30 on tWeightmonth0.person_id = tWeightmonth30.person_id
+left join (
+select person_id, value_numeric as 'weightMonth31'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 31 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 31 MONTH) group by person_id
+)tWeightmonth31 on tWeightmonth0.person_id = tWeightmonth31.person_id
+left join (
+select person_id, value_numeric as 'weightMonth32'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 32 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 32 MONTH) group by person_id
+)tWeightmonth32 on tWeightmonth0.person_id = tWeightmonth32.person_id
+left join (
+select person_id, value_numeric as 'weightMonth33'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 33 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 33 MONTH) group by person_id
+)tWeightmonth33 on tWeightmonth0.person_id = tWeightmonth33.person_id
+left join (
+select person_id, value_numeric as 'weightMonth34'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 34 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 34 MONTH) group by person_id
+)tWeightmonth34 on tWeightmonth0.person_id = tWeightmonth34.person_id
+left join (
+select person_id, value_numeric as 'weightMonth35'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 35 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 35 MONTH) group by person_id
+)tWeightmonth35 on tWeightmonth0.person_id = tWeightmonth35.person_id
+left join (
+select person_id, value_numeric as 'weightMonth36'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 36 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 36 MONTH) group by person_id
+)tWeightmonth36 on tWeightmonth0.person_id = tWeightmonth36.person_id
+left join (
+select person_id, value_numeric as 'weightMonth37'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 37 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 37 MONTH) group by person_id
+)tWeightmonth37 on tWeightmonth0.person_id = tWeightmonth37.person_id
+left join (
+select person_id, value_numeric as 'weightMonth38'
+from obs where concept_id = (select concept_id from concept_name where name = 'weight' and concept_name_type = 'fully_specified') and date_created between DATE_ADD(DATE_FORMAT('#startDate#','%Y-%m-01'), INTERVAL 38 MONTH) and
+ DATE_ADD(DATE_FORMAT(LAST_DAY('#startDate#'),'%Y-%m-%d 23:59:59'), INTERVAL 38 MONTH) group by person_id
+)tWeightmonth38 on tWeightmonth0.person_id = tWeightmonth38.person_id
+)ttweight
+)tttweight on tDemographics.person_id = tttweight.person_id
+
+
diff --git a/openmrs/apps/reports/sql/artDispensingReport.sql b/openmrs/apps/reports/sql/artDispensingReport.sql
new file mode 100644
index 000000000..bbf1d834d
--- /dev/null
+++ b/openmrs/apps/reports/sql/artDispensingReport.sql
@@ -0,0 +1,21 @@
+select pa.value as 'Unique ART Number' , concat(coalesce(given_name, ''), " ", coalesce(middle_name, ''), '', coalesce(family_name , '') ) as 'Client Name', e2.value as 'Clients Contact' ,
+ gender as sex ,floor(datediff(curdate(),p.birthdate) / 365) as 'Age', o.date_created As Date,
+GROUP_CONCAT(case when c.uuid = '13382e01-9f18-488b-b2d2-58ab54c82d82' and o.date_created BETWEEN DATE_FORMAT('#startDate#' , '%Y-%m-01 00:00:00')
+AND DATE_FORMAT(LAST_DAY('#endDate#'), '%Y-%m-%d 23:59:59') then concat(dr.name," = (" , do.quantity , ") " , " " , cm.name) end) as RegimenNames,
+GROUP_CONCAT(case when c.uuid in ('e2acae74-9559-49b7-a5a7-36b633a5427d','9844649e-0411-4011-a1f8-4a0da4155c76','f8c61966-0a8a-425f-858c-93dfabc1c82c') and o.date_created BETWEEN DATE_FORMAT('#startDate#', '%Y-%m-01 00:00:00')
+AND DATE_FORMAT(LAST_DAY('#endDate#'), '%Y-%m-%d 23:59:59') then concat(dr.name, " = (" , do.quantity , ") " , " " , cm.name) end) as CTXDapsone,
+GROUP_CONCAT(case when c.uuid NOT IN ('13382e01-9f18-488b-b2d2-58ab54c82d82' , 'e2acae74-9559-49b7-a5a7-36b633a5427d', '9844649e-0411-4011-a1f8-4a0da4155c76','f8c61966-0a8a-425f-858c-93dfabc1c82c') and o.date_created BETWEEN DATE_FORMAT('#startDate#' , '%Y-%m-01 00:00:00')
+AND DATE_FORMAT(LAST_DAY('#endDate#'), '%Y-%m-%d 23:59:59') then concat(dr.name, " = (" , do.quantity , ") " , " " , cm.name) end) as OtherDrugs
+from person_attribute as pa
+INNER JOIN person_attribute_type as pat on pa.person_attribute_type_id = pat.person_attribute_type_id
+INNER JOIN person as p on pa.person_id = p.person_id
+LEFT JOIN person_name as pn on p.person_id = pn.person_id
+LEFT JOIN patient as pt on p.person_id = pt.patient_id
+inner join person_attribute e2 on pa.person_id = e2.person_id
+inner JOIN orders as o on o.patient_id = pt.patient_id
+LEFT JOIN drug_order do on o.order_id = do.order_id
+INNER JOIN drug dr on o.concept_id = dr.concept_id
+LEFT JOIN concept_name as cm on cm.concept_id = do.quantity_units
+left join concept as c on dr.dosage_form = c.concept_id
+where pa.person_attribute_type_id = 29 and e2.person_attribute_type_id =32
+group by o.patient_id
\ No newline at end of file
diff --git a/openmrs/apps/reports/sql/artMonthlyStartedOnArtReport.sql b/openmrs/apps/reports/sql/artMonthlyStartedOnArtReport.sql
new file mode 100644
index 000000000..10140a4bf
--- /dev/null
+++ b/openmrs/apps/reports/sql/artMonthlyStartedOnArtReport.sql
@@ -0,0 +1,658 @@
+-- ART Monthly Report - New and cumulative number of persons started on ART
+
+SELECT
+ '<1 YRS' as 'Age Group',
+ count(maleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Male',
+ count(femaleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Female',
+ count(totalGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Total',
+ count(maleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Male',
+ count(femaleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Female',
+ count(pregnantWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Pregnant',
+ count(breastFeedingWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Breastfeeding',
+ count(totalGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Total',
+ count(maleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Male',
+ count(femaleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Female',
+ count(totalGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'M' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END maleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END femaleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END totalGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'M' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END maleGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END femaleGenderCurrentReportingPeriod,
+ CASE WHEN (pregnant = 1 and TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END pregnantWomens,
+ CASE WHEN (breastFeeding = 1 AND TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomens,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END totalGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'M' and artStartDate is not NULL) THEN 1 END maleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and artStartDate is not NULL) THEN 1 END femaleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and artStartDate is not NULL) THEN 1 END totalGenderSoFar
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'artStartDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS asd ON (asd.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '1-4 YRS' as 'Age Group',
+ count(maleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Male',
+ count(femaleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Female',
+ count(totalGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Total',
+ count(maleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Male',
+ count(femaleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Female',
+ count(pregnantWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Pregnant',
+ count(breastFeedingWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Breastfeeding',
+ count(totalGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Total',
+ count(maleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Male',
+ count(femaleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Female',
+ count(totalGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 1 and 4 and gender = 'M' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END maleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 1 and 4 and gender = 'F' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END femaleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 1 and 4 and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END totalGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 1 and 4 and gender = 'M' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END maleGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 1 and 4 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END femaleGenderCurrentReportingPeriod,
+ CASE WHEN (pregnant = 1 and TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 1 and 4 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END pregnantWomens,
+ CASE WHEN (breastFeeding = 1 AND TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 1 and 4 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomens,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 1 and 4 and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END totalGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 1 and 4 and gender = 'M' and artStartDate is not NULL) THEN 1 END maleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 1 and 4 and gender = 'F' and artStartDate is not NULL) THEN 1 END femaleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 1 and 4 and artStartDate is not NULL) THEN 1 END totalGenderSoFar
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'artStartDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS asd ON (asd.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '5-9 YRS' as 'Age Group',
+ count(maleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Male',
+ count(femaleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Female',
+ count(totalGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Total',
+ count(maleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Male',
+ count(femaleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Female',
+ count(pregnantWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Pregnant',
+ count(breastFeedingWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Breastfeeding',
+ count(totalGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Total',
+ count(maleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Male',
+ count(femaleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Female',
+ count(totalGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'M' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END maleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END femaleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END totalGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'M' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END maleGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END femaleGenderCurrentReportingPeriod,
+ CASE WHEN (pregnant = 1 and TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END pregnantWomens,
+ CASE WHEN (breastFeeding = 1 AND TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomens,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END totalGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'M' and artStartDate is not NULL) THEN 1 END maleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and artStartDate is not NULL) THEN 1 END femaleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and artStartDate is not NULL) THEN 1 END totalGenderSoFar
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'artStartDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS asd ON (asd.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '10-14 YRS' as 'Age Group',
+ count(maleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Male',
+ count(femaleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Female',
+ count(totalGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Total',
+ count(maleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Male',
+ count(femaleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Female',
+ count(pregnantWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Pregnant',
+ count(breastFeedingWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Breastfeeding',
+ count(totalGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Total',
+ count(maleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Male',
+ count(femaleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Female',
+ count(totalGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'M' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END maleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END femaleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END totalGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'M' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END maleGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END femaleGenderCurrentReportingPeriod,
+ CASE WHEN (pregnant = 1 and TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END pregnantWomens,
+ CASE WHEN (breastFeeding = 1 AND TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomens,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END totalGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'M' and artStartDate is not NULL) THEN 1 END maleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and artStartDate is not NULL) THEN 1 END femaleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and artStartDate is not NULL) THEN 1 END totalGenderSoFar
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'artStartDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS asd ON (asd.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '15-19 YRS' as 'Age Group',
+ count(maleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Male',
+ count(femaleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Female',
+ count(totalGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Total',
+ count(maleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Male',
+ count(femaleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Female',
+ count(pregnantWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Pregnant',
+ count(breastFeedingWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Breastfeeding',
+ count(totalGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Total',
+ count(maleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Male',
+ count(femaleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Female',
+ count(totalGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'M' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END maleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END femaleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END totalGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'M' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END maleGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END femaleGenderCurrentReportingPeriod,
+ CASE WHEN (pregnant = 1 and TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END pregnantWomens,
+ CASE WHEN (breastFeeding = 1 AND TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomens,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END totalGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'M' and artStartDate is not NULL) THEN 1 END maleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and artStartDate is not NULL) THEN 1 END femaleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and artStartDate is not NULL) THEN 1 END totalGenderSoFar
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'artStartDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS asd ON (asd.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '20-24 YRS' as 'Age Group',
+ count(maleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Male',
+ count(femaleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Female',
+ count(totalGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Total',
+ count(maleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Male',
+ count(femaleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Female',
+ count(pregnantWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Pregnant',
+ count(breastFeedingWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Breastfeeding',
+ count(totalGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Total',
+ count(maleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Male',
+ count(femaleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Female',
+ count(totalGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'M' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END maleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END femaleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END totalGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'M' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END maleGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END femaleGenderCurrentReportingPeriod,
+ CASE WHEN (pregnant = 1 and TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END pregnantWomens,
+ CASE WHEN (breastFeeding = 1 AND TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomens,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END totalGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'M' and artStartDate is not NULL) THEN 1 END maleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and artStartDate is not NULL) THEN 1 END femaleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and artStartDate is not NULL) THEN 1 END totalGenderSoFar
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'artStartDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS asd ON (asd.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '25-29 YRS' as 'Age Group',
+ count(maleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Male',
+ count(femaleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Female',
+ count(totalGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Total',
+ count(maleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Male',
+ count(femaleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Female',
+ count(pregnantWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Pregnant',
+ count(breastFeedingWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Breastfeeding',
+ count(totalGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Total',
+ count(maleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Male',
+ count(femaleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Female',
+ count(totalGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'M' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END maleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END femaleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END totalGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'M' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END maleGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END femaleGenderCurrentReportingPeriod,
+ CASE WHEN (pregnant = 1 and TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END pregnantWomens,
+ CASE WHEN (breastFeeding = 1 AND TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomens,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END totalGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'M' and artStartDate is not NULL) THEN 1 END maleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and artStartDate is not NULL) THEN 1 END femaleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and artStartDate is not NULL) THEN 1 END totalGenderSoFar
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'artStartDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS asd ON (asd.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '30-34 YRS' as 'Age Group',
+ count(maleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Male',
+ count(femaleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Female',
+ count(totalGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Total',
+ count(maleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Male',
+ count(femaleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Female',
+ count(pregnantWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Pregnant',
+ count(breastFeedingWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Breastfeeding',
+ count(totalGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Total',
+ count(maleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Male',
+ count(femaleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Female',
+ count(totalGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'M' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END maleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END femaleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END totalGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'M' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END maleGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END femaleGenderCurrentReportingPeriod,
+ CASE WHEN (pregnant = 1 and TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END pregnantWomens,
+ CASE WHEN (breastFeeding = 1 AND TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomens,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END totalGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'M' and artStartDate is not NULL) THEN 1 END maleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and artStartDate is not NULL) THEN 1 END femaleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and artStartDate is not NULL) THEN 1 END totalGenderSoFar
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'artStartDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS asd ON (asd.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '35-39 YRS' as 'Age Group',
+ count(maleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Male',
+ count(femaleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Female',
+ count(totalGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Total',
+ count(maleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Male',
+ count(femaleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Female',
+ count(pregnantWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Pregnant',
+ count(breastFeedingWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Breastfeeding',
+ count(totalGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Total',
+ count(maleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Male',
+ count(femaleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Female',
+ count(totalGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'M' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END maleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END femaleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END totalGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'M' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END maleGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END femaleGenderCurrentReportingPeriod,
+ CASE WHEN (pregnant = 1 and TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END pregnantWomens,
+ CASE WHEN (breastFeeding = 1 AND TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomens,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END totalGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'M' and artStartDate is not NULL) THEN 1 END maleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and artStartDate is not NULL) THEN 1 END femaleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and artStartDate is not NULL) THEN 1 END totalGenderSoFar
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'artStartDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS asd ON (asd.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '40-44 YRS' as 'Age Group',
+ count(maleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Male',
+ count(femaleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Female',
+ count(totalGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Total',
+ count(maleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Male',
+ count(femaleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Female',
+ count(pregnantWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Pregnant',
+ count(breastFeedingWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Breastfeeding',
+ count(totalGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Total',
+ count(maleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Male',
+ count(femaleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Female',
+ count(totalGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'M' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END maleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END femaleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END totalGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'M' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END maleGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END femaleGenderCurrentReportingPeriod,
+ CASE WHEN (pregnant = 1 and TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END pregnantWomens,
+ CASE WHEN (breastFeeding = 1 AND TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomens,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END totalGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'M' and artStartDate is not NULL) THEN 1 END maleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and artStartDate is not NULL) THEN 1 END femaleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and artStartDate is not NULL) THEN 1 END totalGenderSoFar
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'artStartDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS asd ON (asd.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '45-49 YRS' as 'Age Group',
+ count(maleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Male',
+ count(femaleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Female',
+ count(totalGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Total',
+ count(maleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Male',
+ count(femaleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Female',
+ count(pregnantWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Pregnant',
+ count(breastFeedingWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Breastfeeding',
+ count(totalGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Total',
+ count(maleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Male',
+ count(femaleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Female',
+ count(totalGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'M' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END maleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END femaleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END totalGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'M' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END maleGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END femaleGenderCurrentReportingPeriod,
+ CASE WHEN (pregnant = 1 and TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END pregnantWomens,
+ CASE WHEN (breastFeeding = 1 AND TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomens,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END totalGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'M' and artStartDate is not NULL) THEN 1 END maleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and artStartDate is not NULL) THEN 1 END femaleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and artStartDate is not NULL) THEN 1 END totalGenderSoFar
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'artStartDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS asd ON (asd.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '50-54 YRS' as 'Age Group',
+ count(maleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Male',
+ count(femaleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Female',
+ count(totalGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Total',
+ count(maleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Male',
+ count(femaleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Female',
+ count(pregnantWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Pregnant',
+ count(breastFeedingWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Breastfeeding',
+ count(totalGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Total',
+ count(maleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Male',
+ count(femaleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Female',
+ count(totalGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'M' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END maleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END femaleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END totalGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'M' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END maleGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END femaleGenderCurrentReportingPeriod,
+ CASE WHEN (pregnant = 1 and TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END pregnantWomens,
+ CASE WHEN (breastFeeding = 1 AND TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomens,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END totalGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'M' and artStartDate is not NULL) THEN 1 END maleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and artStartDate is not NULL) THEN 1 END femaleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and artStartDate is not NULL) THEN 1 END totalGenderSoFar
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'artStartDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS asd ON (asd.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '55+ YRS' as 'Age Group',
+ count(maleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Male',
+ count(femaleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Female',
+ count(totalGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Total',
+ count(maleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Male',
+ count(femaleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Female',
+ count(pregnantWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Pregnant',
+ count(breastFeedingWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Breastfeeding',
+ count(totalGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Total',
+ count(maleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Male',
+ count(femaleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Female',
+ count(totalGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'M' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END maleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END femaleGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END totalGenderPreviousReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'M' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END maleGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END femaleGenderCurrentReportingPeriod,
+ CASE WHEN (pregnant = 1 and TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END pregnantWomens,
+ CASE WHEN (breastFeeding = 1 AND TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomens,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END totalGenderCurrentReportingPeriod,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'M' and artStartDate is not NULL) THEN 1 END maleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and artStartDate is not NULL) THEN 1 END femaleGenderSoFar,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and artStartDate is not NULL) THEN 1 END totalGenderSoFar
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'artStartDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS asd ON (asd.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ 'Total' as 'Age Group',
+ count(maleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Male',
+ count(femaleGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Female',
+ count(totalGenderPreviousReportingPeriod) as 'Cumulative number of persons ever started on ART at this facility\nat the end of the previous reporting period\n Total',
+ count(maleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Male',
+ count(femaleGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Female',
+ count(pregnantWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Pregnant',
+ count(breastFeedingWomens) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Breastfeeding',
+ count(totalGenderCurrentReportingPeriod) as 'New persons started on ART at this facility\nduring the reporting period (Month)\n Total',
+ count(maleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Male',
+ count(femaleGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Female',
+ count(totalGenderSoFar) as 'Cumulative number of persons ever started on ART at this facility\nat end of the current reporting period (Month)\n Total'
+FROM (
+ SELECT
+ CASE WHEN (gender = 'M' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END maleGenderPreviousReportingPeriod,
+ CASE WHEN (gender = 'F' and MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END femaleGenderPreviousReportingPeriod,
+ CASE WHEN (MONTH(artStartDate) < MONTH(CURDATE())) THEN 1 END totalGenderPreviousReportingPeriod,
+ CASE WHEN (gender = 'M' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END maleGenderCurrentReportingPeriod,
+ CASE WHEN (gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END femaleGenderCurrentReportingPeriod,
+ CASE WHEN (pregnant = 1 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END pregnantWomens,
+ CASE WHEN (breastFeeding = 1 and gender = 'F' and MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomens,
+ CASE WHEN (MONTH(artStartDate) = MONTH(CURDATE()) AND YEAR(artStartDate) = YEAR(CURDATE())) THEN 1 END totalGenderCurrentReportingPeriod,
+ CASE WHEN (gender = 'M' and artStartDate is not NULL) THEN 1 END maleGenderSoFar,
+ CASE WHEN (gender = 'F' and artStartDate is not NULL) THEN 1 END femaleGenderSoFar,
+ CASE WHEN (artStartDate is not NULL and artStartDate is not NULL) THEN 1 END totalGenderSoFar
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'artStartDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS asd ON (asd.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p;
+
\ No newline at end of file
diff --git a/openmrs/apps/reports/sql/artMonthlyViralLoadReport.sql b/openmrs/apps/reports/sql/artMonthlyViralLoadReport.sql
new file mode 100644
index 000000000..a4391c41c
--- /dev/null
+++ b/openmrs/apps/reports/sql/artMonthlyViralLoadReport.sql
@@ -0,0 +1,979 @@
+-- ART Monthly Report - Viral Load
+
+SELECT
+ '<1 YRS' as 'Age Group',
+ count(maleGenderSampleCollected) as 'Sample Collected\n Male',
+ count(femaleGenderSampleCollected) as 'Sample Collected\n Female',
+ count(pregnantWomensSampleCollected) as 'Sample Collected\n Pregnant',
+ count(breastFeedingWomensSampleCollected) as 'Sample Collected\n Breastfeeding',
+ count(totalSampleCollected) as 'Sample Collected\n Total',
+ count(maleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Male',
+ count(femaleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected999) as 'Results Received\n <1000 copies/ml \n Total',
+ count(maleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Male',
+ count(femaleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Total',
+ count(maleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Male',
+ count(femaleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Female',
+ count(pregnantWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Pregnant',
+ count(breastFeedingWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Breastfeeding',
+ count(totalResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'M' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END maleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END femaleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and pregnant=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END pregnantWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and breastFeeding=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END totalSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END maleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END femaleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END pregnantWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END breastFeedingWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END totalResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END maleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END femaleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END pregnantWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END breastFeedingWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END totalResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END maleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END femaleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END pregnantWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END breastFeedingWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE())< 1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END totalResultCollected11000
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'viralSampleDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date VL Sample Collected?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vl ON (vl.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'vlResultDate', o.value_numeric AS 'vlResult' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="VL Results" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '1-4 YRS' as 'Age Group',
+ count(maleGenderSampleCollected) as 'Sample Collected\n Male',
+ count(femaleGenderSampleCollected) as 'Sample Collected\n Female',
+ count(pregnantWomensSampleCollected) as 'Sample Collected\n Pregnant',
+ count(breastFeedingWomensSampleCollected) as 'Sample Collected\n Breastfeeding',
+ count(totalSampleCollected) as 'Sample Collected\n Total',
+ count(maleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Male',
+ count(femaleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected999) as 'Results Received\n <1000 copies/ml \n Total',
+ count(maleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Male',
+ count(femaleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Total',
+ count(maleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Male',
+ count(femaleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Female',
+ count(pregnantWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Pregnant',
+ count(breastFeedingWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Breastfeeding',
+ count(totalResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'M' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END maleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END femaleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and pregnant=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END pregnantWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and breastFeeding=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END totalSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END maleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END femaleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END pregnantWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END breastFeedingWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END totalResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END maleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END femaleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END pregnantWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END breastFeedingWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END totalResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END maleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END femaleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END pregnantWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END breastFeedingWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END totalResultCollected11000
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'viralSampleDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date VL Sample Collected?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vl ON (vl.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'vlResultDate', o.value_numeric AS 'vlResult' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="VL Results" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '5-9 YRS' as 'Age Group',
+ count(maleGenderSampleCollected) as 'Sample Collected\n Male',
+ count(femaleGenderSampleCollected) as 'Sample Collected\n Female',
+ count(pregnantWomensSampleCollected) as 'Sample Collected\n Pregnant',
+ count(breastFeedingWomensSampleCollected) as 'Sample Collected\n Breastfeeding',
+ count(totalSampleCollected) as 'Sample Collected\n Total',
+ count(maleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Male',
+ count(femaleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected999) as 'Results Received\n <1000 copies/ml \n Total',
+ count(maleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Male',
+ count(femaleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Total',
+ count(maleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Male',
+ count(femaleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Female',
+ count(pregnantWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Pregnant',
+ count(breastFeedingWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Breastfeeding',
+ count(totalResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'M' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END maleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END femaleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and pregnant=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END pregnantWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and breastFeeding=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END totalSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END maleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END femaleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END pregnantWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END breastFeedingWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END totalResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END maleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END femaleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END pregnantWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END breastFeedingWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END totalResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END maleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END femaleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END pregnantWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END breastFeedingWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 5 and 9 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END totalResultCollected11000
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'viralSampleDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date VL Sample Collected?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vl ON (vl.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'vlResultDate', o.value_numeric AS 'vlResult' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="VL Results" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '10-14 YRS' as 'Age Group',
+ count(maleGenderSampleCollected) as 'Sample Collected\n Male',
+ count(femaleGenderSampleCollected) as 'Sample Collected\n Female',
+ count(pregnantWomensSampleCollected) as 'Sample Collected\n Pregnant',
+ count(breastFeedingWomensSampleCollected) as 'Sample Collected\n Breastfeeding',
+ count(totalSampleCollected) as 'Sample Collected\n Total',
+ count(maleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Male',
+ count(femaleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected999) as 'Results Received\n <1000 copies/ml \n Total',
+ count(maleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Male',
+ count(femaleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Total',
+ count(maleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Male',
+ count(femaleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Female',
+ count(pregnantWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Pregnant',
+ count(breastFeedingWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Breastfeeding',
+ count(totalResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'M' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END maleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END femaleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and pregnant=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END pregnantWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and breastFeeding=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END totalSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END maleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END femaleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END pregnantWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END breastFeedingWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END totalResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END maleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END femaleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END pregnantWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END breastFeedingWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END totalResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END maleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END femaleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END pregnantWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END breastFeedingWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END totalResultCollected11000
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'viralSampleDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date VL Sample Collected?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vl ON (vl.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'vlResultDate', o.value_numeric AS 'vlResult' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="VL Results" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '15-19 YRS' as 'Age Group',
+ count(maleGenderSampleCollected) as 'Sample Collected\n Male',
+ count(femaleGenderSampleCollected) as 'Sample Collected\n Female',
+ count(pregnantWomensSampleCollected) as 'Sample Collected\n Pregnant',
+ count(breastFeedingWomensSampleCollected) as 'Sample Collected\n Breastfeeding',
+ count(totalSampleCollected) as 'Sample Collected\n Total',
+ count(maleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Male',
+ count(femaleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected999) as 'Results Received\n <1000 copies/ml \n Total',
+ count(maleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Male',
+ count(femaleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Total',
+ count(maleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Male',
+ count(femaleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Female',
+ count(pregnantWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Pregnant',
+ count(breastFeedingWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Breastfeeding',
+ count(totalResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'M' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END maleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END femaleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and pregnant=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END pregnantWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and breastFeeding=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END totalSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END maleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END femaleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END pregnantWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END breastFeedingWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END totalResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END maleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END femaleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END pregnantWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END breastFeedingWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END totalResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END maleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END femaleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END pregnantWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END breastFeedingWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END totalResultCollected11000
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'viralSampleDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date VL Sample Collected?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vl ON (vl.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'vlResultDate', o.value_numeric AS 'vlResult' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="VL Results" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '20-24 YRS' as 'Age Group',
+ count(maleGenderSampleCollected) as 'Sample Collected\n Male',
+ count(femaleGenderSampleCollected) as 'Sample Collected\n Female',
+ count(pregnantWomensSampleCollected) as 'Sample Collected\n Pregnant',
+ count(breastFeedingWomensSampleCollected) as 'Sample Collected\n Breastfeeding',
+ count(totalSampleCollected) as 'Sample Collected\n Total',
+ count(maleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Male',
+ count(femaleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected999) as 'Results Received\n <1000 copies/ml \n Total',
+ count(maleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Male',
+ count(femaleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Total',
+ count(maleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Male',
+ count(femaleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Female',
+ count(pregnantWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Pregnant',
+ count(breastFeedingWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Breastfeeding',
+ count(totalResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'M' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END maleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END femaleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and pregnant=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END pregnantWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and breastFeeding=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END totalSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END maleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END femaleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END pregnantWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END breastFeedingWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END totalResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END maleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END femaleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END pregnantWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END breastFeedingWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END totalResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END maleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END femaleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END pregnantWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END breastFeedingWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END totalResultCollected11000
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'viralSampleDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date VL Sample Collected?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vl ON (vl.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'vlResultDate', o.value_numeric AS 'vlResult' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="VL Results" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '25-29 YRS' as 'Age Group',
+ count(maleGenderSampleCollected) as 'Sample Collected\n Male',
+ count(femaleGenderSampleCollected) as 'Sample Collected\n Female',
+ count(pregnantWomensSampleCollected) as 'Sample Collected\n Pregnant',
+ count(breastFeedingWomensSampleCollected) as 'Sample Collected\n Breastfeeding',
+ count(totalSampleCollected) as 'Sample Collected\n Total',
+ count(maleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Male',
+ count(femaleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected999) as 'Results Received\n <1000 copies/ml \n Total',
+ count(maleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Male',
+ count(femaleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Total',
+ count(maleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Male',
+ count(femaleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Female',
+ count(pregnantWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Pregnant',
+ count(breastFeedingWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Breastfeeding',
+ count(totalResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'M' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END maleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END femaleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and pregnant=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END pregnantWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and breastFeeding=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END totalSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END maleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END femaleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END pregnantWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END breastFeedingWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END totalResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END maleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END femaleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END pregnantWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END breastFeedingWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END totalResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END maleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END femaleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END pregnantWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END breastFeedingWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END totalResultCollected11000
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'viralSampleDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date VL Sample Collected?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vl ON (vl.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'vlResultDate', o.value_numeric AS 'vlResult' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="VL Results" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '30-34 YRS' as 'Age Group',
+ count(maleGenderSampleCollected) as 'Sample Collected\n Male',
+ count(femaleGenderSampleCollected) as 'Sample Collected\n Female',
+ count(pregnantWomensSampleCollected) as 'Sample Collected\n Pregnant',
+ count(breastFeedingWomensSampleCollected) as 'Sample Collected\n Breastfeeding',
+ count(totalSampleCollected) as 'Sample Collected\n Total',
+ count(maleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Male',
+ count(femaleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected999) as 'Results Received\n <1000 copies/ml \n Total',
+ count(maleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Male',
+ count(femaleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Total',
+ count(maleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Male',
+ count(femaleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Female',
+ count(pregnantWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Pregnant',
+ count(breastFeedingWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Breastfeeding',
+ count(totalResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'M' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END maleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END femaleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and pregnant=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END pregnantWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and breastFeeding=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END totalSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END maleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END femaleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END pregnantWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END breastFeedingWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END totalResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END maleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END femaleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END pregnantWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END breastFeedingWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END totalResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END maleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END femaleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END pregnantWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END breastFeedingWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END totalResultCollected11000
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'viralSampleDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date VL Sample Collected?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vl ON (vl.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'vlResultDate', o.value_numeric AS 'vlResult' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="VL Results" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '35-39 YRS' as 'Age Group',
+ count(maleGenderSampleCollected) as 'Sample Collected\n Male',
+ count(femaleGenderSampleCollected) as 'Sample Collected\n Female',
+ count(pregnantWomensSampleCollected) as 'Sample Collected\n Pregnant',
+ count(breastFeedingWomensSampleCollected) as 'Sample Collected\n Breastfeeding',
+ count(totalSampleCollected) as 'Sample Collected\n Total',
+ count(maleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Male',
+ count(femaleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected999) as 'Results Received\n <1000 copies/ml \n Total',
+ count(maleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Male',
+ count(femaleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Total',
+ count(maleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Male',
+ count(femaleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Female',
+ count(pregnantWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Pregnant',
+ count(breastFeedingWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Breastfeeding',
+ count(totalResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'M' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END maleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END femaleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and pregnant=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END pregnantWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and breastFeeding=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END totalSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END maleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END femaleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END pregnantWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END breastFeedingWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END totalResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END maleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END femaleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END pregnantWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END breastFeedingWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END totalResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END maleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END femaleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END pregnantWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END breastFeedingWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END totalResultCollected11000
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'viralSampleDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date VL Sample Collected?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vl ON (vl.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'vlResultDate', o.value_numeric AS 'vlResult' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="VL Results" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '40-44 YRS' as 'Age Group',
+ count(maleGenderSampleCollected) as 'Sample Collected\n Male',
+ count(femaleGenderSampleCollected) as 'Sample Collected\n Female',
+ count(pregnantWomensSampleCollected) as 'Sample Collected\n Pregnant',
+ count(breastFeedingWomensSampleCollected) as 'Sample Collected\n Breastfeeding',
+ count(totalSampleCollected) as 'Sample Collected\n Total',
+ count(maleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Male',
+ count(femaleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected999) as 'Results Received\n <1000 copies/ml \n Total',
+ count(maleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Male',
+ count(femaleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Total',
+ count(maleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Male',
+ count(femaleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Female',
+ count(pregnantWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Pregnant',
+ count(breastFeedingWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Breastfeeding',
+ count(totalResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'M' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END maleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END femaleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and pregnant=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END pregnantWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and breastFeeding=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END totalSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END maleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END femaleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END pregnantWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END breastFeedingWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END totalResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END maleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END femaleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END pregnantWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END breastFeedingWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END totalResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END maleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END femaleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END pregnantWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END breastFeedingWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END totalResultCollected11000
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'viralSampleDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date VL Sample Collected?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vl ON (vl.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'vlResultDate', o.value_numeric AS 'vlResult' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="VL Results" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '45-49 YRS' as 'Age Group',
+ count(maleGenderSampleCollected) as 'Sample Collected\n Male',
+ count(femaleGenderSampleCollected) as 'Sample Collected\n Female',
+ count(pregnantWomensSampleCollected) as 'Sample Collected\n Pregnant',
+ count(breastFeedingWomensSampleCollected) as 'Sample Collected\n Breastfeeding',
+ count(totalSampleCollected) as 'Sample Collected\n Total',
+ count(maleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Male',
+ count(femaleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected999) as 'Results Received\n <1000 copies/ml \n Total',
+ count(maleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Male',
+ count(femaleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Total',
+ count(maleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Male',
+ count(femaleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Female',
+ count(pregnantWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Pregnant',
+ count(breastFeedingWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Breastfeeding',
+ count(totalResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'M' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END maleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END femaleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and pregnant=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END pregnantWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and breastFeeding=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END totalSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END maleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END femaleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END pregnantWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END breastFeedingWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END totalResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END maleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END femaleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END pregnantWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END breastFeedingWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END totalResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END maleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END femaleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END pregnantWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END breastFeedingWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END totalResultCollected11000
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'viralSampleDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date VL Sample Collected?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vl ON (vl.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'vlResultDate', o.value_numeric AS 'vlResult' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="VL Results" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '50-54 YRS' as 'Age Group',
+ count(maleGenderSampleCollected) as 'Sample Collected\n Male',
+ count(femaleGenderSampleCollected) as 'Sample Collected\n Female',
+ count(pregnantWomensSampleCollected) as 'Sample Collected\n Pregnant',
+ count(breastFeedingWomensSampleCollected) as 'Sample Collected\n Breastfeeding',
+ count(totalSampleCollected) as 'Sample Collected\n Total',
+ count(maleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Male',
+ count(femaleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected999) as 'Results Received\n <1000 copies/ml \n Total',
+ count(maleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Male',
+ count(femaleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Total',
+ count(maleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Male',
+ count(femaleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Female',
+ count(pregnantWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Pregnant',
+ count(breastFeedingWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Breastfeeding',
+ count(totalResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'M' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END maleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END femaleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and pregnant=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END pregnantWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and breastFeeding=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END totalSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END maleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END femaleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END pregnantWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END breastFeedingWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END totalResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END maleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END femaleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END pregnantWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END breastFeedingWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END totalResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END maleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END femaleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END pregnantWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END breastFeedingWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 54 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END totalResultCollected11000
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'viralSampleDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date VL Sample Collected?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vl ON (vl.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'vlResultDate', o.value_numeric AS 'vlResult' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="VL Results" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '55+ YRS' as 'Age Group',
+ count(maleGenderSampleCollected) as 'Sample Collected\n Male',
+ count(femaleGenderSampleCollected) as 'Sample Collected\n Female',
+ count(pregnantWomensSampleCollected) as 'Sample Collected\n Pregnant',
+ count(breastFeedingWomensSampleCollected) as 'Sample Collected\n Breastfeeding',
+ count(totalSampleCollected) as 'Sample Collected\n Total',
+ count(maleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Male',
+ count(femaleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected999) as 'Results Received\n <1000 copies/ml \n Total',
+ count(maleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Male',
+ count(femaleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Total',
+ count(maleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Male',
+ count(femaleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Female',
+ count(pregnantWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Pregnant',
+ count(breastFeedingWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Breastfeeding',
+ count(totalResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'M' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END maleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END femaleGenderSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and pregnant=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END pregnantWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and breastFeeding=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomensSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END totalSampleCollected,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END maleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END femaleGenderResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END pregnantWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END breastFeedingWomensResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END totalResultCollected999,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END maleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END femaleGenderResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END pregnantWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END breastFeedingWomensResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END totalResultCollected1000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END maleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END femaleGenderResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END pregnantWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END breastFeedingWomensResultCollected11000,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 55 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END totalResultCollected11000
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'viralSampleDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date VL Sample Collected?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vl ON (vl.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'vlResultDate', o.value_numeric AS 'vlResult' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="VL Results" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p
+
+UNION ALL
+
+SELECT
+ 'Total' as 'Age Group',
+ count(maleGenderSampleCollected) as 'Sample Collected\n Male',
+ count(femaleGenderSampleCollected) as 'Sample Collected\n Female',
+ count(pregnantWomensSampleCollected) as 'Sample Collected\n Pregnant',
+ count(breastFeedingWomensSampleCollected) as 'Sample Collected\n Breastfeeding',
+ count(totalSampleCollected) as 'Sample Collected\n Total',
+ count(maleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Male',
+ count(femaleGenderResultCollected999) as 'Results Received\n <1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected999) as 'Results Received\n <1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected999) as 'Results Received\n <1000 copies/ml \n Total',
+ count(maleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Male',
+ count(femaleGenderResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Female',
+ count(pregnantWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Pregnant',
+ count(breastFeedingWomensResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Breastfeeding',
+ count(totalResultCollected1000) as 'Results Received\n >=1000 copies/ml \n Total',
+ count(maleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Male',
+ count(femaleGenderResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Female',
+ count(pregnantWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Pregnant',
+ count(breastFeedingWomensResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Breastfeeding',
+ count(totalResultCollected11000) as 'Clients with high VL\n (>=11000copies/ml) Traced \n Total'
+FROM (
+ SELECT
+ CASE WHEN (gender = 'M' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END maleGenderSampleCollected,
+ CASE WHEN (gender = 'F' and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END femaleGenderSampleCollected,
+ CASE WHEN (gender = 'F' and pregnant=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END pregnantWomensSampleCollected,
+ CASE WHEN (gender = 'F' and breastFeeding=1 and MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END breastFeedingWomensSampleCollected,
+ CASE WHEN (MONTH(viralSampleDate) = MONTH(CURDATE()) AND YEAR(viralSampleDate) = YEAR(CURDATE())) THEN 1 END totalSampleCollected,
+ CASE WHEN (gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END maleGenderResultCollected999,
+ CASE WHEN (gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END femaleGenderResultCollected999,
+ CASE WHEN (gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END pregnantWomensResultCollected999,
+ CASE WHEN (gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END breastFeedingWomensResultCollected999,
+ CASE WHEN (MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult < 1000) THEN 1 END totalResultCollected999,
+ CASE WHEN (gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END maleGenderResultCollected1000,
+ CASE WHEN (gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END femaleGenderResultCollected1000,
+ CASE WHEN (gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END pregnantWomensResultCollected1000,
+ CASE WHEN (gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END breastFeedingWomensResultCollected1000,
+ CASE WHEN (MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 1000) THEN 1 END totalResultCollected1000,
+ CASE WHEN (gender = 'M' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END maleGenderResultCollected11000,
+ CASE WHEN (gender = 'F' and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END femaleGenderResultCollected11000,
+ CASE WHEN (gender = 'F' and pregnant=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END pregnantWomensResultCollected11000,
+ CASE WHEN (gender = 'F' and breastFeeding=1 and MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END breastFeedingWomensResultCollected11000,
+ CASE WHEN (MONTH(vlResultDate) = MONTH(CURDATE()) AND YEAR(vlResultDate) = YEAR(CURDATE()) and vlResult >= 11000) THEN 1 END totalResultCollected11000
+ FROM person
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'viralSampleDate' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date VL Sample Collected?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vl ON (vl.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'vlResultDate', o.value_numeric AS 'vlResult' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="VL Results" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'pregnant' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="FP Pregnant" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'breastFeeding' FROM obs o JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Currently Breastfeeding?" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS bf ON (bf.visitPatientId = person_id)
+) p;
diff --git a/openmrs/apps/reports/sql/artRegister.sql b/openmrs/apps/reports/sql/artRegister.sql
new file mode 100644
index 000000000..98297ed79
--- /dev/null
+++ b/openmrs/apps/reports/sql/artRegister.sql
@@ -0,0 +1,50 @@
+SELECT DISTINCT IFNULL(DATE_FORMAT(artStartDate, "%d/%m/%Y"), '') AS "ART Start Date",
+IFNULL(IF(retestBeforeArt IS NULL OR retestBeforeArt = '', '0', '1'), '') AS "HIV Retesting for ART initiation",
+IFNULL(pUART.UniqueArtNo, '') AS "Unique ART Number", concat(pn.given_name, ' ', IF(pn.middle_name IS NULL OR pn.middle_name = '', '', concat(pn.middle_name, ' ')),
+IF(pn.family_name IS NULL OR pn.family_name = '', '', pn.family_name)) AS "Name in full", IFNULL(pai.identifier, '') AS "patient ID", IFNULL(pMobile.telephoneNo, '') AS "Telephone No.", IFNULL(p.gender, '') AS "Gender",
+TIMESTAMPDIFF(YEAR, p.birthdate, CURDATE()) AS "Age", IFNULL(DATE_FORMAT(v.date_started, "%d/%m/%Y"), '') AS "Appointment Date", IFNULL(HEIGHT, '') HEIGHT, IFNULL(WEIGHT, '') WEIGHT, IFNULL(BMI, '') BMI, IFNULL(CD4, '') CD4,
+IFNULL((select name from concept_name where concept_name_type="FULLY_SPECIFIED" and concept_id = WHOS), '') WHOS, IFNULL(CTX, '') CTX, IFNULL(date_startedTBRX, '') "Date Started TB RX",
+IFNULL(Breastfeeding, '') "Breast feeding", IFNULL((select name from concept_name where concept_name_type="FULLY_SPECIFIED" and concept_id = SR1stLA ), '') "Substitution Regimen (1st Line Adults)",
+IFNULL((select name from concept_name where concept_name_type="FULLY_SPECIFIED" and concept_id = RFRC), '') "Reason For Regimen Change",
+IFNULL((select name from concept_name where concept_name_type="FULLY_SPECIFIED" and concept_id = ARSW1L), '') "ART Regimen - Substitution within 1st Line",
+IFNULL((select name from concept_name where concept_name_type="FULLY_SPECIFIED" and concept_id = SR2LA), '') "Substitution Regimen (2nd Line Adults)",
+IFNULL((select name from concept_name where concept_name_type="FULLY_SPECIFIED" and concept_id = C1LR), '') "Child 1st Line Regimens",
+IFNULL((select name from concept_name where concept_name_type="FULLY_SPECIFIED" and concept_id = C2LR), '') "Child 2nd Line Regimens"
+FROM visit v
+LEFT JOIN person p ON p.person_id = v.patient_id AND v.voided IS FALSE
+LEFT JOIN person_name pn ON p.person_id = pn.person_id AND pn.voided IS FALSE
+LEFT JOIN patient_identifier pai ON (pai.patient_id = v.patient_id AND pai.preferred = 1)
+LEFT JOIN encounter e ON e.visit_id = v.visit_id
+LEFT JOIN (select paMobile.person_id as 'pMobilePersonId', paMobile.value AS 'telephoneNo' from person_attribute paMobile
+JOIN person_attribute_type patMobile ON patMobile.name = "MobileNumber" AND patMobile.retired IS FALSE AND patMobile.person_attribute_type_id = paMobile.person_attribute_type_id) AS pMobile ON v.patient_id = pMobile.pMobilePersonId
+LEFT JOIN (select paUART.person_id as 'pUARTPersonId', paUART.value AS 'UniqueArtNo' from person_attribute paUART
+JOIN person_attribute_type patUART ON patUART.name = "UniqueArtNo" AND patUART.retired IS FALSE AND patUART.person_attribute_type_id = paUART.person_attribute_type_id) AS pUART ON v.patient_id = pUART.pUARTPersonId
+LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'artStartDate' FROM obs o
+JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date" and o.concept_id = cn.concept_id)
+JOIN encounter enc ON enc.encounter_id = o.encounter_id
+JOIN visit v ON v.visit_id = enc.visit_id
+GROUP BY v.patient_id
+ORDER BY v.visit_id DESC) AS obsConcept ON obsConcept.visitPatientId = v.patient_id
+LEFT JOIN (
+ SELECT obs.person_id, obs.encounter_id,
+ REPLACE(Group_concat(IF (obs.concept_id = 3731, obs.value_coded, "")), ",", "") AS retestBeforeArt,
+ REPLACE(Group_concat(IF (obs.concept_id = 3767, obs.value_coded, "")), ",", "") AS WHOS,
+ REPLACE(Group_concat(IF (obs.concept_id = 118, obs.value_numeric, "")), ",", "") AS HEIGHT,
+ REPLACE(Group_concat(IF (obs.concept_id = 119, obs.value_numeric, "")), ",", "") AS WEIGHT,
+ REPLACE(Group_concat(IF (obs.concept_id = 120, obs.value_numeric, "")), ",", "") AS BMI,
+ REPLACE(Group_concat(IF (obs.concept_id = 1187, obs.value_numeric, "")), ",", "") AS CD4,
+ REPLACE(Group_concat(IF (obs.concept_id = 3764, obs.value_datetime, "")), ",", "") AS CTX,
+ REPLACE(Group_concat(IF (obs.concept_id = 3782, obs.value_datetime, "")), ",", "") AS date_startedTBRX,
+ REPLACE(Group_concat(IF (obs.concept_id = 2041, obs.value_numeric, "")), ",", "") AS Breastfeeding,
+ REPLACE(Group_concat(IF (obs.concept_id = 3652, obs.value_coded, "")), ",", "") AS SR1stLA,
+ REPLACE(Group_concat(IF (obs.concept_id = 3654, obs.value_coded, "")), ",", "") AS RFRC,
+ REPLACE(Group_concat(IF (obs.concept_id = 3679, obs.value_coded, "")), ",", "") AS ARSW1L,
+ REPLACE(Group_concat(IF (obs.concept_id = 3663, obs.value_coded, "")), ",", "") AS SR2LA,
+ REPLACE(Group_concat(IF (obs.concept_id = 3664, obs.value_coded, "")), ",", "") AS ARS2L,
+ REPLACE(Group_concat(IF (obs.concept_id = 3958, obs.value_coded, "")), ",", "") AS C1LR,
+ REPLACE(Group_concat(IF (obs.concept_id = 3968, obs.value_coded, "")), ",", "") AS C2LR
+ FROM obs
+ WHERE obs.concept_id IN ( 3731, 3767, 118, 119, 120,1187, 3767, 3764, 3782, 2041, 3652, 3654, 3679, 3663, 3664, 3958, 3968) and obs.voided=0
+ GROUP BY person_id, encounter_id
+) AS nutvalues ON e.encounter_id = nutvalues.encounter_id;
+-- WHERE pa.start_date_time BETWEEN '#startDate#' AND '#endDate#' ORDER BY pa.start_date_time DESC;
diff --git a/openmrs/apps/reports/sql/highViralloadReport.sql b/openmrs/apps/reports/sql/highViralloadReport.sql
new file mode 100644
index 000000000..f0e14a5bb
--- /dev/null
+++ b/openmrs/apps/reports/sql/highViralloadReport.sql
@@ -0,0 +1,166 @@
+SELECT tVlresults.registrationdate as 'Registration Date', tDemographics.artnumber as 'ART Number', tDemographics.ClientName as 'Client Fullnames' , tDemographics.ClientsContact as 'Clients Contacts',tDemographics.sex as 'Sex', tDemographics.Age as 'Age',
+tSampleCollectDate.DateSampleCollected as 'Date Sample Collected' , tSampleReceived.DateSampleReceived, tVlresults.VLResults as 'Results(Copies/ml)' ,
+tFirstEACsessionDate.FirstEACSession as 'First EAC Session', tSecondEACDate.SecondEACSession as 'Second EAC Session',
+tThirdEACDate.ThirdEACSession as 'Third EAC Session' , tadherence.Adherence_status as 'Classification of Adherence After EAC' ,
+tRepeatViralCollDate.samplecolldaterviral as 'Sample Collection Date (Repeat Viral)' , tResultsarrivaldate.arrivalresultsrepeatviral as 'Results Arrival Date (Repeat Viral)',
+tRepeatviralresults.repeatviralresults as 'Results(Repeat Viral) copies/ml' ,
+tadherenceOutcome.Adherence_outcome as 'Adherence Outcome' , tMdtheld.mdtheldQstn as 'MDT HELD?' , tregimenSwitched.regimenSwitched as 'Was Regimen Switched?' , tregimenSwitched.actualRegimenSwitchedDate as 'Actual Regimen Change Date'
+FROM
+(
+select pa.person_id, pa.value as 'artnumber' , concat(coalesce(given_name, ''), " ", coalesce(middle_name, ''), ' ', coalesce(family_name , '') ) as 'ClientName', e2.value as 'ClientsContact' ,
+gender as sex ,floor(datediff(curdate(),p.birthdate) / 365) as 'Age'
+from person_attribute as pa
+INNER JOIN person_attribute_type as pat on pa.person_attribute_type_id = pat.person_attribute_type_id
+INNER JOIN person as p on pa.person_id = p.person_id
+LEFT JOIN person_name as pn on p.person_id = pn.person_id
+LEFT JOIN patient as pt on p.person_id = pt.patient_id
+inner join person_attribute e2 on pa.person_id = e2.person_id
+where pa.person_attribute_type_id = 29 and e2.person_attribute_type_id =32 ) tDemographics
+inner join
+(select o.person_id, cn.concept_id , cn.name , cn.concept_name_type , o.value_numeric as VLResults , o.obs_datetime as 'registrationdate'
+from concept_name cn
+left join obs o on cn.concept_id = o.concept_id where cn.name = 'VL Results' and cn.concept_name_type = 'FULLY_SPECIFIED'
+and o.voided = false and o.status = 'final' and o.value_numeric >= 1000 and o.obs_datetime between '#startDate#' and '#endDate#') tVlresults
+on tVlresults.person_id = tDemographics.person_id
+LEFT JOIN
+(select o.person_id, cn.concept_id , cn.name , cn.concept_name_type , o.value_datetime as DateSampleCollected
+from concept_name cn
+left join obs o on cn.concept_id = o.concept_id
+where cn.name = 'Date VL Sample Collected?' and cn.concept_name_type = 'FULLY_SPECIFIED' and o.voided = false and o.status = 'final') tSampleCollectDate
+ON tVlresults.person_id=tSampleCollectDate.person_id
+LEFT JOIN
+(
+select o.person_id, cn.concept_id , cn.name , cn.concept_name_type , o.value_datetime as 'DateSampleReceived'
+from concept_name cn
+left join obs o on cn.concept_id = o.concept_id where cn.name = 'Date Sample Received at Testing Lab' and
+ cn.concept_name_type = 'FULLY_SPECIFIED' and o.voided = false and o.status = 'final') tSampleReceived
+on tVlresults.person_id = tSampleReceived.person_id
+LEFT JOIN
+(select o.person_id, cn.concept_id , cn.name , cn.concept_name_type , o.value_datetime as 'FirstEACSession'
+from concept_name cn
+left join obs o on cn.concept_id = o.concept_id where cn.name = 'First EAC Session Date'
+and cn.concept_name_type = 'FULLY_SPECIFIED' and o.voided = false and o.status = 'final') tFirstEACsessionDate
+on tVlresults.person_id = tFirstEACsessionDate.person_id
+LEFT JOIN
+(select o.person_id, cn.concept_id , cn.name , cn.concept_name_type , o.value_datetime as 'SecondEACSession'
+from concept_name cn
+left join obs o on cn.concept_id = o.concept_id where cn.name = 'Second EAC Session Date' and cn.concept_name_type = 'FULLY_SPECIFIED'
+and o.voided = false and o.status = 'final') tSecondEACDate
+on tVlresults.person_id = tSecondEACDate.person_id
+LEFT JOIN
+(select o.person_id, cn.concept_id , cn.name , cn.concept_name_type , o.value_datetime as 'ThirdEACSession'
+from concept_name cn
+left join obs o on cn.concept_id = o.concept_id where cn.name = 'Third EAC Session Date' and cn.concept_name_type = 'FULLY_SPECIFIED'
+and o.voided = false and o.status = 'final') tThirdEACDate
+on tVlresults.person_id = tThirdEACDate.person_id
+LEFT JOIN
+(select o.person_id, cn.concept_id , cn.name , cn.concept_name_type , o.value_coded as 'Adherence'
+from concept_name cn
+left join obs o on cn.concept_id = o.concept_id where cn.name = 'Classification Of Adherence After EAC'
+and cn.concept_name_type = 'FULLY_SPECIFIED'
+and o.voided = false and o.status = 'final' ) tAdherence
+on tVlresults.person_id = tAdherence.person_id
+LEFT JOIN
+(
+select o.person_id, cn.concept_id , cn.name , cn.concept_name_type , o.value_datetime as 'samplecolldaterviral'
+from concept_name cn
+left join obs o on cn.concept_id = o.concept_id where cn.name = 'Repeat Viral,Sample Collection Date' and
+cn.concept_name_type = 'FULLY_SPECIFIED' and o.voided = false and o.status = 'final') tRepeatViralCollDate
+on tVlresults.person_id = tRepeatViralCollDate.person_id
+LEFT JOIN
+(
+select o.person_id, cn.concept_id , cn.name , cn.concept_name_type , o.value_datetime as 'arrivalresultsrepeatviral'
+from concept_name cn
+left join obs o on cn.concept_id = o.concept_id where cn.name = 'Repeat Viral,Date of Arrival Of Results' and
+cn.concept_name_type = 'FULLY_SPECIFIED' and o.voided = false and o.status = 'final') tResultsarrivaldate
+on tVlresults.person_id = tResultsarrivaldate.person_id
+LEFT JOIN
+(
+select o.person_id, cn.concept_id , cn.name , cn.concept_name_type , o.value_numeric as 'repeatviralresults'
+from concept_name cn
+left join obs o on cn.concept_id = o.concept_id where cn.name = 'Viral Load Value , Adherence Failure' and
+cn.concept_name_type = 'FULLY_SPECIFIED' and o.voided = false and o.status = 'final') tRepeatviralresults
+on tVlresults.person_id = tRepeatviralresults.person_id
+LEFT JOIN
+(
+select o.person_id, cn.concept_id , cn.name , cn.concept_name_type , o.value_coded as 'repeatviraloutcome'
+from concept_name cn
+left join obs o on cn.concept_id = o.concept_id where cn.name = 'Repeat Viral,Outcome Action' and
+cn.concept_name_type = 'FULLY_SPECIFIED' and o.voided = false and o.status = 'final') tRepeatviraloutcome
+on tVlresults.person_id = tRepeatviraloutcome.person_id
+LEFT JOIN
+(
+select tadherencestatus.Adherence , ans.name as 'Adherence_status' , tadherencestatus.personid as 'person_id' from (
+select tadherence.name, tadherence.Adherence , tadherence.person_id as 'personid' from (
+select o.person_id, cn.concept_id , cn.name , cn.concept_name_type , o.value_coded as 'Adherence'
+from concept_name cn
+left join obs o on cn.concept_id = o.concept_id where cn.name = 'Classification Of Adherence After EAC'
+and cn.concept_name_type = 'FULLY_SPECIFIED'
+and o.voided = false and o.status = 'final' ) tadherence
+left join
+(select A.concept_id, name from concept_name A
+where A.concept_id in ( select B.value_coded from obs B where B.value_coded = B.value_coded
+and A.concept_name_type = 'FULLY_SPECIFIED'
+)) tgetAdherence
+on tadherence.concept_id = tgetAdherence.concept_id
+) tadherencestatus
+left join
+concept_name ans on tadherencestatus.Adherence = ans.concept_id and ans.concept_name_type = 'SHORT') tadherence
+on tVlresults.person_id = tadherence.person_id
+LEFT JOIN
+(
+select tadherenceAfterEAC.Adherence , ans.name as 'Adherence_outcome' , tadherenceAfterEAC.personid as 'person_id' from (
+select tadherenceOutcome.name, tadherenceOutcome.Adherence , tadherenceOutcome.person_id as 'personid' from (
+select o.person_id, cn.concept_id , cn.name , cn.concept_name_type , o.value_coded as 'Adherence'
+from concept_name cn
+left join obs o on cn.concept_id = o.concept_id where cn.name = 'Repeat Viral,Outcome Action'
+and cn.concept_name_type = 'FULLY_SPECIFIED'
+and o.voided = false and o.status = 'final' ) tadherenceOutcome
+left join
+(select A.concept_id, name from concept_name A
+where A.concept_id in ( select B.value_coded from obs B where B.value_coded = B.value_coded
+and A.concept_name_type = 'FULLY_SPECIFIED'
+)) tgetAdherence
+on tadherenceOutcome.concept_id = tgetAdherence.concept_id
+) tadherenceAfterEAC
+left join
+concept_name ans on tadherenceAfterEAC.Adherence = ans.concept_id and ans.concept_name_type = 'SHORT') tadherenceOutcome
+on tVlresults.person_id = tadherenceOutcome.person_id
+LEFT JOIN
+(
+select tadherenceAfterEAC.Adherence , ans.name as 'mdtheldQstn' , tadherenceAfterEAC.personid as 'person_id' from (
+select tMdtheld.name, tMdtheld.Adherence , tMdtheld.person_id as 'personid' from (
+select o.person_id, cn.concept_id , cn.name , cn.concept_name_type , o.value_coded as 'Adherence'
+from concept_name cn
+left join obs o on cn.concept_id = o.concept_id where cn.name = 'MDT Held?'
+and cn.concept_name_type = 'FULLY_SPECIFIED'
+and o.voided = false and o.status = 'final' ) tMdtheld
+left join
+(select A.concept_id, name from concept_name A
+where A.concept_id in ( select B.value_coded from obs B where B.value_coded = B.value_coded
+and A.concept_name_type = 'FULLY_SPECIFIED'
+)) tgetAdherence
+on tMdtheld.concept_id = tgetAdherence.concept_id
+) tadherenceAfterEAC
+left join
+concept_name ans on tadherenceAfterEAC.Adherence = ans.concept_id and ans.concept_name_type = 'FULLY_SPECIFIED') tMdtheld
+on tVlresults.person_id = tMdtheld.person_id
+LEFT JOIN
+(
+select o.person_id, cn.concept_id , cn.name , cn.concept_name_type , o.value_datetime as 'dateMdtHeld'
+from concept_name cn
+left join obs o on cn.concept_id = o.concept_id where cn.name = 'Date MDT Held' and
+ cn.concept_name_type = 'FULLY_SPECIFIED' and o.voided = false and o.status = 'final') tDateMDTHeld
+on tVlresults.person_id = tDateMDTHeld.person_id
+LEFT JOIN
+(
+select dr.dosage_form, o.patient_id ,
+(case when dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen') then count(distinct(cn.concept_id)) else null end) as 'Count',
+(case when ((case when dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen') then count(distinct(cn.concept_id)) else null end)) > 1 then 'True' else 'False' end) as 'regimenSwitched',
+(case when ((case when dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen') then count(distinct(cn.concept_id)) else null end)) > 1 then max(o.date_created) else 'N/A' end) as 'actualRegimenSwitchedDate'
+from concept_name cn
+left join orders o on cn.concept_id = o.concept_id
+left join drug dr on o.concept_id = dr.concept_id
+where concept_name_type = 'FULLY_SPECIFIED' and dr.dosage_form = (select concept_id from concept_name where name = 'HIVTC, ART Regimen') group by o.patient_id) tregimenSwitched
+on tVlresults.person_id = tregimenSwitched.patient_id
+
diff --git a/openmrs/apps/reports/sql/hivExposedInfantRegisterReportTabFirst.sql b/openmrs/apps/reports/sql/hivExposedInfantRegisterReportTabFirst.sql
new file mode 100644
index 000000000..34ce1fe3c
--- /dev/null
+++ b/openmrs/apps/reports/sql/hivExposedInfantRegisterReportTabFirst.sql
@@ -0,0 +1,259 @@
+-- HIV Exposed Infant Register Report page 1
+
+SELECT (@row_number1 := @row_number1 + 1) AS 'Serial No.',
+ DATE_FORMAT(v1.date_enrolled,'%d-%m-%Y') AS 'Date Of Enrollment', p1.hei_no AS 'Exposed Infant Number',
+ CONCAT(pn.given_name, ' ', IFNULL(pn.family_name, ''), '\n', IFNULL(pn.middle_name,'')) AS 'Infant\'s Name',
+ CONCAT(IF(paddr.country IS NULL OR paddr.country = '', '',CONCAT(paddr.country, ' ')),
+ IF(paddr.address4 IS NULL OR paddr.address4 = '', '', CONCAT(paddr.address4, ' ')),
+ IF(paddr.address3 IS NULL OR paddr.address3 = '', '', CONCAT(paddr.address3, ' ')),
+ IF(paddr.address2 IS NULL OR paddr.address2 = '', '', CONCAT(paddr.address2, ' ')),
+ IF(paddr.address1 IS NULL OR paddr.address1 = '', '', CONCAT(paddr.address1, ' ')),
+ IF(paddr.address5 IS NULL OR paddr.address5 = '', '', CONCAT(paddr.address5, ' ')),
+ IF(paddr.address6 IS NULL OR paddr.address6 = '', '', CONCAT(paddr.address6, ' ')),
+ IF(paddr.city_village IS NULL OR paddr.city_village = '', '', paddr.city_village)) AS 'Physical Address',
+ CONCAT(DATE_FORMAT(p.birthdate, '%d-%m-%Y'),'\n',TIMESTAMPDIFF(YEAR, p.birthdate, CURDATE())) AS 'Date of Birth\n Age',
+ p.gender AS 'Sex\n(M/F)',
+ v3.entryPointEnrollment AS 'Clinic\nReferred\nFrom',
+ TIMESTAMPDIFF(MONTH, p.birthdate, CURDATE()) - TIMESTAMPDIFF(MONTH, v4.ageAtArv, CURDATE()) AS 'Age at ARV\nProphylaxis\nInitiation\n(Months)',
+ TIMESTAMPDIFF(MONTH, p.birthdate, CURDATE()) - TIMESTAMPDIFF(MONTH, v5.ageAtCtx, CURDATE()) AS 'Age at\nCotrimaxazole\nInitiation\n(Months)',
+ CONCAT(p2.caregiverName,'\n\n',p3.caregiverContact) AS 'Mother/Caregiver\'s Name\n\nTelephone Number',
+ p4.caregiverArtNo AS 'Mother\'s ART No.',
+ '' AS 'Antenatal',
+ '' AS 'Delivery',
+ '' AS 'Post natal',
+ '' AS 'Infant\'s Risk\nStatus\n(Low or High)',
+ v6.infantsArv AS 'Infant\'s ARVs\nfor PMTCT:\n(1=NVP;\n2=NVP+AZT;\n3=None)',
+ CONCAT(DATE_FORMAT(v7.firstPcrTestDate, '%d-%m-%Y'),'\n',DATE_FORMAT(v8.repeatPcrTestDate, '%d-%m-%Y')) AS 'Initial\nSample\n-------\nRepeat',
+ DATE_FORMAT(v7.firstPcrTestDate, '%d-%m-%Y') AS 'Date DBS\nwas\ncollected',
+ TIMESTAMPDIFF(MONTH, p.birthdate, v7.firstPcrTestDate) AS 'Age at\n1st DBS\n(Weeks/\nMonth)',
+ v9.infantFeeding AS 'Infant\nFeeding\nStatus',
+ v10.firstPcrTestResults AS 'Test\nResult',
+ DATE_FORMAT(v10.firstPcrTestResultsDate, '%d-%m-%Y') AS 'Date Result\nReceived',
+ DATE_FORMAT(v11.firstPcrResultDateCaregiver, '%d-%m-%Y') AS 'Date Result\ngiven to\nCaregiver'
+FROM
+ patient pt
+ inner JOIN (SELECT @row_number1 := 0) AS r
+ LEFT JOIN person p ON p.person_id = pt.patient_id AND pt.voided is FALSE AND p.voided is FALSE
+ LEFT JOIN patient_identifier pin ON pin.patient_id = pt.patient_id and pin.preferred = 1
+ LEFT JOIN person_name pn ON p.person_id = pn.person_id
+ AND pn.voided IS FALSE
+ LEFT JOIN person_address paddr ON p.person_id = paddr.person_id
+ AND paddr.voided IS FALSE
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ o.value_coded AS 'enrol_atart'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'HEI Treatment - Enrolled AT ART Clinic'
+ and o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v2 ON v2.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ o.value_datetime AS 'date_enrolled'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'HEI Testing (18Months Rapid Test Date)'
+ AND o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v1 ON v1.visitPatientId = p.person_id AND v2.enrol_atart = 1
+ LEFT JOIN (
+ select
+ distinct pa.person_id as 'paPersonId',
+ pa.value AS 'hei_no'
+ FROM
+ person_attribute pa
+ JOIN person_attribute_type pat ON pat.name = 'HIVExposedInfant(HEI)No'
+ AND pat.retired IS FALSE
+ AND pat.person_attribute_type_id = pa.person_attribute_type_id
+ ) AS p1 ON p.person_id = p1.paPersonId
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entryPointEnrollment'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'Entry Point(Enrollement)'
+ and o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v3 ON v3.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ o.value_datetime AS 'ageAtArv'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'Date of ARV Prophylaxis Start'
+ and o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v4 ON v4.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ o.value_datetime AS 'ageAtCtx'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'CTX or Dapose Start Date'
+ and o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v5 ON v5.visitPatientId = p.person_id
+ LEFT JOIN (
+ select
+ distinct pa.person_id as 'paPersonId',
+ pa.value AS 'caregiverName'
+ FROM
+ person_attribute pa
+ JOIN person_attribute_type pat ON pat.name = 'HeiChildMothersName'
+ AND pat.retired IS FALSE
+ AND pat.person_attribute_type_id = pa.person_attribute_type_id
+ ) AS p2 ON p.person_id = p2.paPersonId
+ LEFT JOIN (
+ select
+ distinct pa.person_id as 'paPersonId',
+ pa.value AS 'caregiverContact'
+ FROM
+ person_attribute pa
+ JOIN person_attribute_type pat ON pat.name = 'MothersContactNumber'
+ AND pat.retired IS FALSE
+ AND pat.person_attribute_type_id = pa.person_attribute_type_id
+ ) AS p3 ON p.person_id = p3.paPersonId
+ LEFT JOIN (
+ select
+ distinct pa.person_id as 'paPersonId',
+ pa.value AS 'caregiverArtNo'
+ FROM
+ person_attribute pa
+ JOIN person_attribute_type pat ON pat.name = 'MothersArtNo'
+ AND pat.retired IS FALSE
+ AND pat.person_attribute_type_id = pa.person_attribute_type_id
+ ) AS p4 ON p.person_id = p4.paPersonId
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'infantsArv'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'Infant\'s PMTCT ARVS'
+ and o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v6 ON v6.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ o.value_datetime AS 'firstPcrTestDate'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'HEI Testing (First PCR Test Date)'
+ and o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v7 ON v7.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ o.value_datetime AS 'repeatPcrTestDate'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'HEI Testing (Repeat PCR Test Date)'
+ and o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v8 ON v8.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'infantFeeding'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'Infant Feeding'
+ and o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v9 ON v9.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ o.obs_datetime AS 'firstPcrTestResultsDate',
+ (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'firstPcrTestResults'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'HEI Testing (First PCR Results)'
+ and o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v10 ON v10.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ o.value_datetime AS 'firstPcrResultDateCaregiver'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'HEI Testing (First PCR Date Result Given to Caregiver)'
+ and o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v11 ON v11.visitPatientId = p.person_id
+order by CAST('Serial No.' AS UNSIGNED) asc;
diff --git a/openmrs/apps/reports/sql/hivExposedInfantRegisterReportTabSecond.sql b/openmrs/apps/reports/sql/hivExposedInfantRegisterReportTabSecond.sql
new file mode 100644
index 000000000..f1fb6e460
--- /dev/null
+++ b/openmrs/apps/reports/sql/hivExposedInfantRegisterReportTabSecond.sql
@@ -0,0 +1,239 @@
+-- HIV Exposed Infant Register Report page 2
+
+SELECT CONCAT(pn.given_name, ' ', IFNULL(pn.family_name, ''), '\n', IFNULL(pn.middle_name,'')) AS 'Infants Name',
+ CONCAT(DATE_FORMAT(v1.secondPcrTestDate, '%d-%m-%Y'),'\n',TIMESTAMPDIFF(MONTH,p.birthdate,v1.secondPcrTestDate),'\n',v2.secondPcrTestResults)
+ AS 'Date of Test\nAge\n(Months)\nRapid Test Result\n(Pos/Neg)',
+ CONCAT(DATE_FORMAT(v1.secondPcrTestDate, '%d-%m-%Y'),'\n',DATE_FORMAT(v3.repeatPcrTestDate, '%d-%m-%Y')) AS 'Initial\n-------\nRepeat',
+ DATE_FORMAT(v1.secondPcrTestDate, '%d-%m-%Y') AS 'Date DBS\nwas collected',
+ '' AS 'Reason for\n2nd PCR\n(see the code\nbelow:\n1, 2, 3)',
+ '' AS 'Age at\n2nd DBS\n(Months)',
+ v4.infantFeedingStatus AS 'Infant\nFeeding\nStatus',
+ v2.secondPcrTestResults AS 'Test Result',
+ DATE_FORMAT(v2.secondPcrTestResultsDate, '%d-%m-%Y') AS 'Date Result\nReceived',
+ DATE_FORMAT(v5.secondPcrResultDateCaregiver, '%d-%m-%Y') AS 'Date Result\ngiven to\nCaretaker',
+ CONCAT('Appointment Date:','\n','Date of Visit:','\n','Age (mo):','\n','Feeding Code:','\n','Immunization Codes:','\n','CTX/ARV Prophylaxis (Y/N):') AS 'Visit Details',
+ CONCAT(DATE_FORMAT(v6.firstVisitAppointmentDate, '%d-%m-%Y'),'\n',DATE_FORMAT(v6.visitDate1, '%d-%m-%Y'),'\n',TIMESTAMPDIFF(MONTH, p.birthdate, CURDATE()),'\n','','\n','','\n',IF(v13.arvProphylaxisStartDate IS null OR v13.arvProphylaxisStartDate = '','No','Yes')) AS 'Visit 1',
+ CONCAT(DATE_FORMAT(v10.secondVisitAppointmentDate, '%d-%m-%Y'),'\n',DATE_FORMAT(v10.visitDate2, '%d-%m-%Y'),'\n',TIMESTAMPDIFF(MONTH, p.birthdate, CURDATE()),'\n','','\n','','\n',IF(v13.arvProphylaxisStartDate IS NULL OR v13.arvProphylaxisStartDate = '','No','Yes')) AS 'Visit 2',
+ CONCAT(DATE_FORMAT(v11.thirdVisitAppointmentDate, '%d-%m-%Y'),'\n',DATE_FORMAT(v11.visitDate3, '%d-%m-%Y'),'\n',TIMESTAMPDIFF(MONTH, p.birthdate, CURDATE()),'\n','','\n','','\n',IF(v13.arvProphylaxisStartDate IS NULL OR v13.arvProphylaxisStartDate = '','No','Yes')) AS 'Visit 3',
+ CONCAT(DATE_FORMAT(v12.fourthVisitAppointmentDate, '%d-%m-%Y'),'\n',DATE_FORMAT(v12.visitDate4, '%d-%m-%Y'),'\n',TIMESTAMPDIFF(MONTH, p.birthdate, CURDATE()),'\n','','\n','','\n',IF(v13.arvProphylaxisStartDate IS NULL OR v13.arvProphylaxisStartDate = '','No','Yes')) AS 'Visit 4',
+ '' AS 'Visit 5', '' AS 'Visit 6', '' AS 'Visit 7', '' AS 'Visit 8', '' AS 'Visit 9', '' AS 'Visit 10',
+ CONCAT(DATE_FORMAT(v7.months18RapidTestDate, '%d-%m-%Y'),'\n',TIMESTAMPDIFF(MONTH, p.birthdate, CURDATE()),'\n',v8.months18RapidTestResults) AS 'Date of Test\nAge\n(months)\nRapid Test Result\n(Pos=Positive,\nNeg=Negative)',
+ v9.finalStatus AS 'Final Outcome'
+FROM
+ patient pt
+ LEFT JOIN person p ON p.person_id = pt.patient_id AND pt.voided is FALSE AND p.voided is FALSE
+ LEFT JOIN person_name pn ON p.person_id = pn.person_id AND pn.voided IS FALSE
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ o.value_datetime AS 'secondPcrTestDate'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'HEI Testing (Second PCR Test Date)'
+ AND o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v1 ON v1.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ o.obs_datetime AS 'secondPcrTestResultsDate',
+ (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'secondPcrTestResults'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'HEI Testing (Second PCR Results)'
+ and o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v2 ON v2.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ o.value_datetime AS 'repeatPcrTestDate'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'HEI Testing (Repeat PCR Test Date)'
+ AND o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v3 ON v3.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'infantFeedingStatus'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'Infant Feeding'
+ and o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v4 ON v4.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ o.value_datetime AS 'secondPcrResultDateCaregiver'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'HEI Testing (Second PCR Date Result Given to Caregiver)'
+ and o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v5 ON v5.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ enc.encounter_datetime AS 'visitDate1',
+ o.value_datetime AS 'firstVisitAppointmentDate'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'Hei End Of Follow up (First Attempt Date)'
+ AND o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v6 ON v6.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ o.value_datetime AS 'months18RapidTestDate'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'HEI Testing (18Months Rapid Test Date)'
+ AND o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v7 ON v7.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'months18RapidTestResults'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'HEI Testing (18Months Rapid Test Results)'
+ and o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v8 ON v8.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'finalStatus'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'Final Status'
+ and o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v9 ON v9.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ enc.encounter_datetime AS 'visitDate2',
+ o.value_datetime AS 'secondVisitAppointmentDate'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'Hei End Of Follow up (Second Attempt Date)'
+ AND o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v10 ON v10.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ enc.encounter_datetime AS 'visitDate3',
+ o.value_datetime AS 'thirdVisitAppointmentDate'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'Hei End Of Follow up (Third Attempt Date)'
+ AND o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v11 ON v11.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ enc.encounter_datetime AS 'visitDate4',
+ o.value_datetime AS 'fourthVisitAppointmentDate'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'Hei End Of Follow up (Fourth Attempt Date)'
+ AND o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v12 ON v12.visitPatientId = p.person_id
+ LEFT JOIN (
+ SELECT
+ distinct v.patient_id AS 'visitPatientId',
+ o.obs_datetime AS 'obs_datetime',
+ o.value_datetime AS 'arvProphylaxisStartDate'
+ FROM
+ obs o
+ JOIN concept_name cn ON (
+ cn.concept_name_type = 'FULLY_SPECIFIED'
+ AND cn.voided is false
+ AND cn.name = 'Date of ARV Prophylaxis Start'
+ AND o.concept_id = cn.concept_id
+ )
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ ) AS v13 ON v13.visitPatientId = p.person_id
+group by CAST('Serial No.' AS UNSIGNED);
diff --git a/openmrs/apps/reports/sql/hivExposedInfants.sql b/openmrs/apps/reports/sql/hivExposedInfants.sql
new file mode 100644
index 000000000..f34ff895d
--- /dev/null
+++ b/openmrs/apps/reports/sql/hivExposedInfants.sql
@@ -0,0 +1,812 @@
+-- HIV-exposed Infants Monthly Report
+SELECT
+ 'Number of New HIV-Exposed Infants Enrolled' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 2 and gender = 'M' and MONTH(enrollmentDate) = MONTH(CURDATE()) AND YEAR(enrollmentDate) = YEAR(CURDATE()) and enrollmentResult is not null) THEN 1 END maleGender,
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 2 and gender = 'F' and MONTH(enrollmentDate) = MONTH(CURDATE()) AND YEAR(enrollmentDate) = YEAR(CURDATE()) and enrollmentResult is not null) THEN 1 END femaleGender,
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 2 and MONTH(enrollmentDate) = MONTH(CURDATE()) AND YEAR(enrollmentDate) = YEAR(CURDATE()) and enrollmentResult is not null) THEN 1 END totalAll
+ FROM visit v
+ JOIN person pn on pn.person_id = v.patient_id
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship" ))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'enrollmentDate', o.value_coded AS 'enrollmentResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Entry Point(Enrollement)"
+ and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+) p
+
+UNION ALL
+
+SELECT
+ 'Number Of HIV-Exposed Infants seen (New And Old)' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+FROM (
+ SELECT
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'M' and enrollmentResult is not null) THEN 1 END maleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'F' and enrollmentResult is not null) THEN 1 END femaleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and enrollmentResult is not null) THEN 1 END totalAll
+ FROM visit v
+ JOIN person pn on pn.person_id = v.patient_id
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship" ))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'enrollmentDate', o.value_coded AS 'enrollmentResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Entry Point(Enrollement)"
+ and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+) p
+
+UNION ALL
+
+SELECT
+ 'PCR test for HIV-Exposed Infants Upto 2 Months of age' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+ FROM(
+ SELECT
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 2) and gender = 'M' and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and pcrDateResult is not null and MONTH(heiResultDate) = MONTH(CURDATE()) AND YEAR(heiResultDate) = YEAR(CURDATE()) and heiResult = "HEI Results Positive") THEN 1 END maleGender,
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 2) and gender = 'F' and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and pcrDateResult is not null and MONTH(heiResultDate) = MONTH(CURDATE()) AND YEAR(heiResultDate) = YEAR(CURDATE()) and heiResult = "HEI Results Positive") THEN 1 END femaleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 2) and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and pcrDateResult is not null and MONTH(heiResultDate) = MONTH(CURDATE()) AND YEAR(heiResultDate) = YEAR(CURDATE()) and heiResult = "HEI Results Positive") THEN 1 END totalAll
+ FROM person pn
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship"))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_datetime AS 'pcrDateResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="HEI Testing (First PCR Test Date)" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'heiResultDate', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'heiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (First PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ ) p
+
+ UNION ALL
+
+SELECT
+ 'PCR test for HIV-Exposed Infants between 2-12 Months of age' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+ FROM(
+ SELECT
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 2 and 12) and gender = 'M' and pcrResult is not null and rapidTestresult is not null and secondresult is not null and repeatresult is not null ) THEN 1 END maleGender,
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 2 and 12) and gender = 'F' and pcrResult is not null and rapidTestresult is not null and secondresult is not null and repeatresult is not null ) THEN 1 END femaleGender,
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 2 and 12) and pcrResult is not null and rapidTestresult is not null and secondresult is not null and repeatresult is not null ) THEN 1 END totalAll
+
+ FROM person pn
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship"))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_coded AS 'pcrResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="HEI Testing (First PCR Feeding Method)" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate2', o.value_coded AS 'rapidTestresult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Rapid Test At 9 Months Feeding Method)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate3', o.value_coded AS 'secondresult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Second PCR Feeding Method)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS sr ON (sr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate4', o.value_coded AS 'repeatresult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Repeat PCR Feeding Method)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS rr ON (rr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'PCR test for HIV-Exposed Infants between 12-18 Months of age' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+ FROM(
+ SELECT
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 12 and 18) and gender = 'M' and pcrResult is not null and rapidTestresult is not null and secondresult is not null and repeatresult is not null and 18repeatresult is not null) THEN 1 END maleGender,
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 12 and 18) and gender = 'F' and pcrResult is not null and rapidTestresult is not null and secondresult is not null and repeatresult is not null and 18repeatresult is not null ) THEN 1 END femaleGender,
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 12 and 18) and pcrResult is not null and rapidTestresult is not null and secondresult is not null and repeatresult is not null and 18repeatresult is not null ) THEN 1 END totalAll
+
+ FROM person pn
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship"))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_coded AS 'pcrResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="HEI Testing (First PCR Feeding Method)" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate2', o.value_coded AS 'rapidTestresult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Rapid Test At 9 Months Feeding Method)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate3', o.value_coded AS 'secondresult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Second PCR Feeding Method)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS sr ON (sr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate4', o.value_coded AS 'repeatresult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Repeat PCR Feeding Method)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS rr ON (rr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate4', o.value_coded AS '18repeatresult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (18Months Rapid Test Feeding Method)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS rs ON (rs.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Total number of results received from Public Health laboratory\n(regardless sample collected)' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+ FROM(
+ SELECT
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'M' and datePhl is not null ) THEN 1 END maleGender,
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'F' and datePhl is not null) THEN 1 END femaleGender,
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and datePhl is not null) THEN 1 END totalAll
+
+ FROM visit v
+ JOIN person pn on pn.person_id = v.patient_id
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship" ))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_datetime AS 'datePhl' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date sample receipt by PHL confirmed(First PCR Test)" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Number of results received from Public Health Laboratory\n (Only from sample collected this month)' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+ FROM(
+ SELECT
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'M' and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and datePhl is not null ) THEN 1 END maleGender,
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'F' and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and datePhl is not null) THEN 1 END femaleGender,
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and datePhl is not null) THEN 1 END totalAll
+
+ FROM visit v
+ JOIN person pn on pn.person_id = v.patient_id
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship" ))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_datetime AS 'datePhl' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date sample receipt by PHL confirmed(First PCR Test)" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Total number of positive results received' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+ FROM (
+ SELECT
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'M' and heiResult = cnpr.concept_id and rapidheiResult = cnpr.concept_id and secondheiResult = cnpr.concept_id and repeatheiResult = cnpr.concept_id and eightrapidheiResult = cnpr.concept_id) THEN 1 END maleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'F' and heiResult = cnpr.concept_id and rapidheiResult = cnpr.concept_id and secondheiResult = cnpr.concept_id and repeatheiResult = cnpr.concept_id and eightrapidheiResult = cnpr.concept_id) THEN 1 END femaleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and heiResult = cnpr.concept_id and rapidheiResult = cnpr.concept_id and secondheiResult = cnpr.concept_id and repeatheiResult = cnpr.concept_id and eightrapidheiResult = cnpr.concept_id) THEN 1 END totalAll
+ FROM person pn
+
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_coded AS 'heiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (First PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'rapidheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Rapid Test At 9 Months Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS rr ON (rr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'secondheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Second PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS sr ON (sr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'repeatheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Repeat PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS er ON (er.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'eightrapidheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (18Months Rapid Test Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship"))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN concept_name cnpr ON (cnpr.concept_name_type = "FULLY_SPECIFIED" AND cnpr.voided is false AND cnpr.name="HEI Results Positive")
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Total number of positive results received\n(only from samples collected this reporting months)' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+ FROM (
+ SELECT
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'M' and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and heiResult = cnpr.concept_id and rapidheiResult = cnpr.concept_id and secondheiResult = cnpr.concept_id and repeatheiResult = cnpr.concept_id and eightrapidheiResult = cnpr.concept_id) THEN 1 END maleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'F' and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and heiResult = cnpr.concept_id and rapidheiResult = cnpr.concept_id and secondheiResult = cnpr.concept_id and repeatheiResult = cnpr.concept_id and eightrapidheiResult = cnpr.concept_id) THEN 1 END femaleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and heiResult = cnpr.concept_id and rapidheiResult = cnpr.concept_id and secondheiResult = cnpr.concept_id and repeatheiResult = cnpr.concept_id and eightrapidheiResult = cnpr.concept_id) THEN 1 END totalAll
+ FROM person pn
+
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_coded AS 'heiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (First PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'rapidheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Rapid Test At 9 Months Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS rr ON (rr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'secondheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Second PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS sr ON (sr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'repeatheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Repeat PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS er ON (er.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'eightrapidheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (18Months Rapid Test Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship"))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN concept_name cnpr ON (cnpr.concept_name_type = "FULLY_SPECIFIED" AND cnpr.voided is false AND cnpr.name="HEI Results Positive")
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Number of HIV Infected Infants started on ART' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+ FROM(
+ SELECT
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'M' and arvDateProphylaxis is not null ) THEN 1 END maleGender,
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'F' and arvDateProphylaxis is not null) THEN 1 END femaleGender,
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and arvDateProphylaxis is not null) THEN 1 END totalAll
+
+ FROM visit v
+ JOIN person pn on pn.person_id = v.patient_id
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship" ))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_datetime AS 'arvDateProphylaxis' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Date of ARV Prophylaxis Start" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ ) p
+
+
+UNION ALL
+
+SELECT
+ 'Number of EID Samples rejected by Public Health Laboratory' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+ FROM(
+ SELECT
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'M' and pcrResult is not null and pcrResult = 1 and rapidTestresult is not null and rapidTestresult = 1 and secondresult is not null and secondresult = 1 and repeatresult is not null and repeatresult = 1 and 18repeatresult is not null and 18repeatresult = 1) THEN 1 END maleGender,
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'F' and pcrResult is not null and pcrResult = 1 and rapidTestresult is not null and rapidTestresult = 1 and secondresult is not null and secondresult = 1 and repeatresult is not null and repeatresult = 1 and 18repeatresult is not null and 18repeatresult = 1 ) THEN 1 END femaleGender,
+ CASE WHEN ( (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and pcrResult is not null and pcrResult = 1 and rapidTestresult is not null and rapidTestresult = 1 and secondresult is not null and secondresult = 1 and repeatresult is not null and repeatresult = 1 and 18repeatresult is not null and 18repeatresult = 1 ) THEN 1 END totalAll
+
+ FROM person pn
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship"))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_coded AS 'pcrResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Was Sample Rejected(First PCR Test)" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate2', o.value_coded AS 'rapidTestresult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Was Sample Rejected(Rapid Test At 9 Months)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate3', o.value_coded AS 'secondresult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Was Sample Rejected(Second PCR Test)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS sr ON (sr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate4', o.value_coded AS 'repeatresult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Was Sample Rejected(Repeat PCR Test)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS rr ON (rr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate4', o.value_coded AS '18repeatresult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Was Sample Rejected(18Months Rapid Test)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS rs ON (rs.visitPatientId = pn.person_id)
+ ) p
+
+
+UNION ALL
+
+SELECT
+ 'HIV-Exposed Infants Upto 2 Months on Cotrimoxazole' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+ FROM(
+ SELECT
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'M' and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and cotrimoxazoleResult is not null and cotrimoxazoleResult = 1 ) THEN 1 END maleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'F' and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and cotrimoxazoleResult is not null and cotrimoxazoleResult = 1 ) THEN 1 END femaleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and cotrimoxazoleResult is not null and cotrimoxazoleResult = 1 ) THEN 1 END totalAll
+ FROM person pn
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship"))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_coded AS 'cotrimoxazoleResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Cotrimoxazole/Dapsone" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Number HIV-exposed infants received NVP' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+ FROM(
+ SELECT
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'M' and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and nvpDateResult is not null and MONTH(nvpResultDate) = MONTH(CURDATE()) AND YEAR(nvpResultDate) = YEAR(CURDATE()) and nvpResult = "Daily NVP") THEN 1 END maleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'F' and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and nvpDateResult is not null and MONTH(nvpResultDate) = MONTH(CURDATE()) AND YEAR(nvpResultDate) = YEAR(CURDATE()) and nvpResult = "Daily NVP") THEN 1 END femaleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and nvpDateResult is not null and MONTH(nvpResultDate) = MONTH(CURDATE()) AND YEAR(nvpResultDate) = YEAR(CURDATE()) and nvpResult = "Daily NVP" ) THEN 1 END totalAll
+ FROM person pn
+ LEFT JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship"))
+ LEFT JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_datetime AS 'nvpDateResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Daily NVP Date" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'nvpResultDate', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'nvpResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Infant's PMTCT ARVS" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Number HIV-exposed infants received AZT+NVP' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+ FROM(
+ SELECT
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'M' and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and aztnvpDateResult is not null and MONTH(aztnvpResultDate) = MONTH(CURDATE()) AND YEAR(aztnvpResultDate) = YEAR(CURDATE()) and aztnvpResult = "AZT+NV") THEN 1 END maleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'F' and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and aztnvpDateResult is not null and MONTH(aztnvpResultDate) = MONTH(CURDATE()) AND YEAR(aztnvpResultDate) = YEAR(CURDATE()) and aztnvpResult = "AZT+NV" ) THEN 1 END femaleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and MONTH(obsDate) = MONTH(CURDATE()) AND YEAR(obsDate) = YEAR(CURDATE()) and aztnvpDateResult is not null and MONTH(aztnvpResultDate) = MONTH(CURDATE()) AND YEAR(aztnvpResultDate) = YEAR(CURDATE()) and aztnvpResult = "AZT+NV" ) THEN 1 END totalAll
+ FROM person pn
+ LEFT JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship"))
+ LEFT JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_datetime AS 'aztnvpDateResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="AZT+NV Date" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'aztnvpResultDate', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'aztnvpResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Infant's PMTCT ARVS" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'HIV-exposed infants whose \n feeding practice was assessed' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 2 and gender = 'M' and infantFeedResult is not null) THEN 1 END maleGender,
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 2 and gender = 'F' and infantFeedResult is not null) THEN 1 END femaleGender,
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 2 and infantFeedResult is not null) THEN 1 END totalAll
+ FROM visit v
+ JOIN person pn on pn.person_id = v.patient_id
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship" ))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_coded AS 'infantFeedResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Infant Feeding" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ ) p
+
+
+UNION ALL
+
+SELECT
+ 'Total HIV-exposed Infants turned \n 12 months of age in this reporting period' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+FROM (
+ SELECT
+ CASE WHEN ((pa.value = (select concept_id from concept_name where name = "HeiRelationship") or pa.value = (select concept_id from concept_name where name = "ExistingHeiRelationship")) and (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 12 and gender = 'M') ) THEN 1 END maleGender,
+ CASE WHEN ((pa.value = (select concept_id from concept_name where name = "HeiRelationship") or pa.value = (select concept_id from concept_name where name = "ExistingHeiRelationship")) and (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 12 and gender = 'F') ) THEN 1 END femaleGender,
+ CASE WHEN ((pa.value = (select concept_id from concept_name where name = "HeiRelationship") or pa.value = (select concept_id from concept_name where name = "ExistingHeiRelationship")) and (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 12) ) THEN 1 END totalAll
+ FROM visit v
+ LEFT JOIN person p1 on p1.person_id = v.patient_id
+ LEFT JOIN person_attribute pa on pa.person_id = p1.person_id
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+) p
+
+
+UNION ALL
+
+SELECT
+ 'Received initial PCR test between 2–12 months' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 2 and 12 and gender = 'M' and rapidTestResult is not null) THEN 1 END maleGender,
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 2 and 12 and gender = 'F' and rapidTestResult is not null) THEN 1 END femaleGender,
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 2 and 12 and rapidTestResult is not null) THEN 1 END totalAll
+ FROM visit v
+ JOIN person pn on pn.person_id = v.patient_id
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship" ))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'rapidTestResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="HEI Testing (First PCR Results)" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+) p
+
+UNION ALL
+
+SELECT
+ 'Received rapid HIV antibody test 9 months' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 9 and gender = 'M' and rapidTestResult is not null) THEN 1 END maleGender,
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 9 and gender = 'F' and rapidTestResult is not null) THEN 1 END femaleGender,
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 9 and rapidTestResult is not null) THEN 1 END totalAll
+ FROM visit v
+ JOIN person pn on pn.person_id = v.patient_id
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship" ))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'rapidTestResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="HEI Testing (18Months Rapid Test Results)" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Total HIV-exposed infants turned\n18 months in this reporting period' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18 and gender = 'M' ) THEN 1 END maleGender,
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18 and gender = 'F' ) THEN 1 END femaleGender,
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18 ) THEN 1 END totalAll
+ FROM visit v
+ JOIN person pn on pn.person_id = v.patient_id
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship" ))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Total number of children tested using Anti body rapid teast at 18 months' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18 and gender = 'M' and rapidTestResult is not null) THEN 1 END maleGender,
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18 and gender = 'F' and rapidTestResult is not null) THEN 1 END femaleGender,
+ CASE WHEN (TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18 and rapidTestResult is not null) THEN 1 END totalAll
+ FROM visit v
+ JOIN person pn on pn.person_id = v.patient_id
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship" ))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'rapidTestResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="HEI Testing (18Months Rapid Test Results)" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Number of children tested HIV-positive' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+ FROM (
+ SELECT
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'M' and heiResult = cnpr.concept_id and rapidheiResult = cnpr.concept_id and secondheiResult = cnpr.concept_id and repeatheiResult = cnpr.concept_id and eightrapidheiResult = cnpr.concept_id) THEN 1 END maleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'F' and heiResult = cnpr.concept_id and rapidheiResult = cnpr.concept_id and secondheiResult = cnpr.concept_id and repeatheiResult = cnpr.concept_id and eightrapidheiResult = cnpr.concept_id) THEN 1 END femaleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and heiResult = cnpr.concept_id and rapidheiResult = cnpr.concept_id and secondheiResult = cnpr.concept_id and repeatheiResult = cnpr.concept_id and eightrapidheiResult = cnpr.concept_id) THEN 1 END totalAll
+ FROM person pn
+
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_coded AS 'heiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (First PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'rapidheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Rapid Test At 9 Months Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS rr ON (rr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'secondheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Second PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS sr ON (sr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'repeatheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Repeat PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS er ON (er.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'eightrapidheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (18Months Rapid Test Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship"))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN concept_name cnpr ON (cnpr.concept_name_type = "FULLY_SPECIFIED" AND cnpr.voided is false AND cnpr.name="HEI Results Positive")
+ ) p
+
+
+UNION ALL
+
+SELECT
+ 'Number of children tested HIV- negative and breastfeeding' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+ FROM (
+ SELECT
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'M' and heiResult = cnpr.concept_id and rapidheiResult = cnpr.concept_id and secondheiResult = cnpr.concept_id and repeatheiResult = cnpr.concept_id and eightrapidheiResult = cnpr.concept_id and pcrResult1 is not null and rapidTestresult1 is not null and secondresult1 is not null and repeatresult1 is not null ) THEN 1 END maleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'F' and heiResult = cnpr.concept_id and rapidheiResult = cnpr.concept_id and secondheiResult = cnpr.concept_id and repeatheiResult = cnpr.concept_id and eightrapidheiResult = cnpr.concept_id and pcrResult1 is not null and rapidTestresult1 is not null and secondresult1 is not null and repeatresult1 is not null ) THEN 1 END femaleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and heiResult = cnpr.concept_id and rapidheiResult = cnpr.concept_id and secondheiResult = cnpr.concept_id and repeatheiResult = cnpr.concept_id and eightrapidheiResult = cnpr.concept_id and pcrResult1 is not null and rapidTestresult1 is not null and secondresult1 is not null and repeatresult1 is not null ) THEN 1 END totalAll
+ FROM person pn
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship"))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN concept_name cnpr ON (cnpr.concept_name_type = "FULLY_SPECIFIED" AND cnpr.voided is false AND cnpr.name="HEI Results Positive")
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_coded AS 'heiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (First PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'rapidheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Rapid Test At 9 Months Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS rr ON (rr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'secondheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Second PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS sr ON (sr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'repeatheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Repeat PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS er ON (er.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'eightrapidheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (18Months Rapid Test Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+
+
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_coded AS 'pcrResult1' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="HEI Testing (First PCR Feeding Method)" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr1 ON (vr1.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate2', o.value_coded AS 'rapidTestresult1' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Rapid Test At 9 Months Feeding Method)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate3', o.value_coded AS 'secondresult1' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Second PCR Feeding Method)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS sr1 ON (sr1.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate4', o.value_coded AS 'repeatresult1' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Repeat PCR Feeding Method)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS rr1 ON (rr1.visitPatientId = pn.person_id)
+ ) p
+
+
+UNION ALL
+
+SELECT
+ 'Number of children tested HIV- negative and no longer breastfeeding' as 'Title',
+ count(maleGender) as 'Male',
+ count(femaleGender) as 'Female',
+ count(totalAll) as 'Total'
+ FROM (
+ SELECT
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'M' and heiResult = cnpr.concept_id and rapidheiResult = cnpr.concept_id and secondheiResult = cnpr.concept_id and repeatheiResult = cnpr.concept_id and eightrapidheiResult = cnpr.concept_id and heiResult1 = cnpr1.concept_id and rapidheiResult1 = cnpr1.concept_id and secondheiResult1 = cnpr1.concept_id and repeatheiResult1 = cnpr1.concept_id and eightrapidheiResult1 = cnpr1.concept_id) THEN 1 END maleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and gender = 'F' and heiResult = cnpr.concept_id and rapidheiResult = cnpr.concept_id and secondheiResult = cnpr.concept_id and repeatheiResult = cnpr.concept_id and eightrapidheiResult = cnpr.concept_id and heiResult1 = cnpr1.concept_id and rapidheiResult1 = cnpr1.concept_id and secondheiResult1 = cnpr1.concept_id and repeatheiResult1 = cnpr1.concept_id and eightrapidheiResult1 = cnpr1.concept_id) THEN 1 END femaleGender,
+ CASE WHEN ((TIMESTAMPDIFF(MONTH, birthdate, CURDATE()) between 0 and 18) and heiResult = cnpr.concept_id and rapidheiResult = cnpr.concept_id and secondheiResult = cnpr.concept_id and repeatheiResult = cnpr.concept_id and eightrapidheiResult = cnpr.concept_id and heiResult1 = cnpr1.concept_id and rapidheiResult1 = cnpr1.concept_id and secondheiResult1 = cnpr1.concept_id and repeatheiResult1 = cnpr1.concept_id and eightrapidheiResult1 = cnpr1.concept_id) THEN 1 END totalAll
+ FROM person pn
+ JOIN person_attribute pa on pa.person_id = pn.person_id and pa.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship"))
+ JOIN person_attribute_type pat on (pat.person_attribute_type_id = pa.person_attribute_type_id and pat.retired = 0 and pat.name = "TypeofPatient")
+ JOIN concept_name cnpr ON (cnpr.concept_name_type = "FULLY_SPECIFIED" AND cnpr.voided is false AND cnpr.name="HEI Results Negative")
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_coded AS 'heiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (First PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'rapidheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Rapid Test At 9 Months Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS rr ON (rr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'secondheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Second PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS sr ON (sr.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'repeatheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Repeat PCR Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS er ON (er.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',o.value_coded AS 'eightrapidheiResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (18Months Rapid Test Results)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate', o.value_coded AS 'heiResult1' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="HEI Testing (First PCR Feeding Method)" AND o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr1 ON (vr1.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate2', o.value_coded AS 'rapidheiResult1' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Rapid Test At 9 Months Feeding Method)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate3', o.value_coded AS 'secondheiResult1' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Second PCR Feeding Method)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS sr1 ON (sr1.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate4', o.value_coded AS 'repeatheiresult1' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (Repeat PCR Feeding Method)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS rr1 ON (rr1.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.obs_datetime AS 'obsDate4', o.value_coded AS 'eightrapidheiResult1' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HEI Testing (18Months Rapid Test Feeding Method)" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr2 ON (vr2.visitPatientId = pn.person_id)
+ JOIN concept_name cnpr1 ON (cnpr1.concept_name_type = "FULLY_SPECIFIED" AND cnpr1.voided is false AND cnpr1.name="No longer Breastfeed")
+ ) p
+
+
diff --git a/openmrs/apps/reports/sql/maternitypmtct.sql b/openmrs/apps/reports/sql/maternitypmtct.sql
new file mode 100644
index 000000000..943fb994e
--- /dev/null
+++ b/openmrs/apps/reports/sql/maternitypmtct.sql
@@ -0,0 +1,91 @@
+SELECT @a:=@a+1 Serial,
+(DATE_FORMAT(v.date_started, "%d/%m/%Y")) as "Date of visit",
+IFNULL(pa.value ,' ') as "Mother Name",
+IFNULL(pa1.value ,' ') as "Mothers Number",
+CONCAT (TIMESTAMPDIFF(Year, p.birthdate, CURDATE()) , 'Y ' ,TIMESTAMPDIFF(Month, p.birthdate, CURDATE()) , 'M') AS "Age",
+CONCAT(IF(paddr.country IS NULL OR paddr.country = '', '',CONCAT(paddr.country, ', ')),
+IF(paddr.address4 IS NULL OR paddr.address4 = '', '', CONCAT(paddr.address4, ', ')),
+ IF(paddr.address3 IS NULL OR paddr.address3 = '', '', CONCAT(paddr.address3, ', ')),
+ IF(paddr.address2 IS NULL OR paddr.address2 = '', '', CONCAT(paddr.address2, ', ')),
+ IF(paddr.address1 IS NULL OR paddr.address1 = '', '', CONCAT(paddr.address1, ', ')),
+ IF(paddr.address5 IS NULL OR paddr.address5 = '', '', CONCAT(paddr.address5, ', ')),
+ IF(paddr.address6 IS NULL OR paddr.address6 = '', '', CONCAT(paddr.address6, ', ')),
+ IF(paddr.city_village IS NULL OR paddr.city_village = '', '', paddr.city_village)) AS 'Name of village or Address',
+IFNULL(pa2.value ,' ') as "Next of Kin and Phone Number",
+'' as "ANC Number",
+'' as "Maternity No.",
+pai.identifier as "ART NO.",
+'' as "Previous HIV test result (recent/ within 6 months) \n Test result",
+'' as "Place Tested",
+'' as "Date tested",
+'' as "Date Tested in Maternity",
+'' as "Final Test result at maternity \n (Neg = Negative Pos = Positive INC = Inconclusive)",
+IFNULL(motherArt,' ') as "ART Regimen During Pregnancy",
+IFNULL(durationArt ,' ') as "Duration on ART During Pregnancy",
+'' as "Infant's risk status (Low or High)",
+'' as "ART: Newly started in Labor",
+IFNULL(infantArvs ,' ') as "Infant ARV Prophylaxis and Date 1=NVP 2=AZT+NVP",
+CONCAT((IFNULL(nvpDate,' ')),'/',(IFNULL(AZTDate,' '))) as "NVP/AZT Date given",
+'' as "ARV Baby Discharged with \n 1=NVP 2=AZT+NVP",
+IFNULL(infantFeed ,' ') as "Infant Feeding practice 1=EBF 2= RF 3=MF",
+IFNULL(pa3.value ,' ') as "Exposed Infant Number",
+'' as "Comments",
+'' as "Provider initials"
+From person p
+JOIN (SELECT @a:= 0) a
+JOIN person_name pn on pn.person_id= p.person_id
+LEFT JOIN visit v on v.patient_id=p.person_id
+LEFT JOIN patient_identifier pai ON (pai.patient_id = v.patient_id AND pai.preferred = 1)
+JOIN person_attribute par on par.person_id = p.person_id and par.value in (select concept_id from concept_name where name in ("HeiRelationship", "ExistingHeiRelationship" ))
+JOIN person_attribute_type patr on (patr.person_attribute_type_id = par.person_attribute_type_id and patr.retired = 0 and patr.name = "TypeofPatient")
+LEFT JOIN person_address paddr ON (p.person_id = paddr.person_id AND paddr.voided = 0 )
+Left JOIN person_attribute pa on p.person_id=pa.person_id
+JOIN person_attribute_type pat on pat.person_attribute_type_id = pa.person_attribute_type_id And pat.name = "HeiChildMothersName"
+Left JOIN person_attribute pa1 on p.person_id=pa1.person_id
+JOIN person_attribute_type pat1 on pat1.person_attribute_type_id = pa1.person_attribute_type_id And pat1.name = "MothersContactNumber"
+Left JOIN person_attribute pa2 on p.person_id=pa2.person_id
+JOIN person_attribute_type pat2 on pat1.person_attribute_type_id = pa2.person_attribute_type_id And pat2.name = "TreatmentSupporterTelephoneNumber"
+Left JOIN person_attribute pa3 on p.person_id=pa3.person_id
+JOIN person_attribute_type pat3 on pat3.person_attribute_type_id = pa3.person_attribute_type_id And pat3.name = "HIVExposedInfant(HEI)No"
+LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'motherArt' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Mother's ART Regimen"
+ and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = p.person_id)
+LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'durationArt' FROM obs o
+ JOIN concept_name cn1 ON (cn1.concept_name_type = "FULLY_SPECIFIED" AND cn1.voided is false AND cn1.name="Duration on ART?"
+ and o.concept_id = cn1.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr1 ON (vr1.visitPatientId = p.person_id)
+LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'infantArvs' FROM obs o
+ JOIN concept_name cn2 ON (cn2.concept_name_type = "FULLY_SPECIFIED" AND cn2.voided is false AND cn2.name="Infant's PMTCT ARVS"
+ and o.concept_id = cn2.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr2 ON (vr2.visitPatientId = p.person_id)
+LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'infantFeed' FROM obs o
+ JOIN concept_name cn3 ON (cn3.concept_name_type = "FULLY_SPECIFIED" AND cn3.voided is false AND cn3.name="Infant Feeding"
+ and o.concept_id = cn3.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr3 ON (vr3.visitPatientId = p.person_id)
+LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'nvpDate' FROM obs o
+ JOIN concept_name cn4 ON (cn4.concept_name_type = "FULLY_SPECIFIED" AND cn4.voided is false AND cn4.name="Daily NVP Date"
+ and o.concept_id = cn4.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr4 ON (vr4.visitPatientId = p.person_id)
+LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_datetime AS 'AZTDate' FROM obs o
+ JOIN concept_name cn5 ON (cn5.concept_name_type = "FULLY_SPECIFIED" AND cn5.voided is false AND cn5.name="AZT+NV Date"
+ and o.concept_id = cn5.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr5 ON (vr5.visitPatientId = p.person_id)
\ No newline at end of file
diff --git a/openmrs/apps/reports/sql/missedAppointments.sql b/openmrs/apps/reports/sql/missedAppointments.sql
new file mode 100644
index 000000000..8010febdd
--- /dev/null
+++ b/openmrs/apps/reports/sql/missedAppointments.sql
@@ -0,0 +1,22 @@
+SELECT DISTINCT
+ pai.identifier AS 'patient ID',
+ pn.given_name AS 'First Name',
+ ifnull(pn.family_name,'') AS 'Last Name',
+ pMobile.telephoneNo AS 'Telephone No.',
+ p.gender AS 'Gender',
+ DATE_FORMAT(obsConcept.artStartDate, "%d/%m/%Y") AS 'ART Start Date',
+ DATE_FORMAT(start_date_time, "%d/%m/%Y") AS 'Appointment Date'
+FROM patient_appointment pa
+ LEFT JOIN person p ON p.person_id = pa.patient_id AND pa.voided IS FALSE
+ LEFT JOIN person_name pn ON p.person_id = pn.person_id AND pn.voided IS FALSE
+ LEFT JOIN patient_identifier pai ON (pai.patient_id = pa.patient_id AND pai.preferred = 1)
+ LEFT JOIN (select paMobile.person_id as 'pMobilePersonId', paMobile.value AS 'telephoneNo' from person_attribute paMobile
+ JOIN person_attribute_type patMobile ON patMobile.name = "MobileNumber" AND patMobile.retired IS FALSE
+ AND patMobile.person_attribute_type_id = paMobile.person_attribute_type_id) AS pMobile ON pa.patient_id = pMobile.pMobilePersonId
+ LEFT JOIN (SELECT o.value_datetime AS 'artStartDate', v.patient_id AS 'visitPatientId', v.visit_id AS visitId FROM visit v
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="ANC, ART Start Date")
+ JOIN encounter enc ON enc.visit_id = v.visit_id
+ JOIN obs o ON (o.encounter_id = enc.encounter_id and o.concept_id = cn.concept_id AND o.person_id = v.patient_id)
+ HAVING v.visit_id = (SELECT MAX(maxV.visit_id) as maxVisitId
+ FROM visit maxV WHERE maxV.patient_id = v.patient_id)) AS obsConcept ON obsConcept.visitPatientId = pa.patient_id
+WHERE pa.status = 'Missed' and pa.start_date_time BETWEEN '#startDate#' AND '#endDate#' ORDER BY pa.start_date_time DESC;
diff --git a/openmrs/apps/reports/sql/pmtcANCpartnerindex.sql b/openmrs/apps/reports/sql/pmtcANCpartnerindex.sql
new file mode 100644
index 000000000..f15182345
--- /dev/null
+++ b/openmrs/apps/reports/sql/pmtcANCpartnerindex.sql
@@ -0,0 +1,275 @@
+-- ANC Partner Index contact HIV Counseling and testing
+
+SELECT
+ 'Couples Pre-test counseled jointly' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypoint' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Partners who had HIV testing' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypoint' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'artResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Transferred in on ART?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Partners testing HIV positive' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'hivSampleResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV Sample" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Partners with discordant results' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'hivSampleResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV Sample" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Index case contacts Tested'as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypointhiv' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS hr ON (hr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'hivSampleResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV Sample" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Index Case contacts positive'as'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+ FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypoint' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'artResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Transferred in on ART?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvsResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Were ARVS Received?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS ar ON (ar.visitPatientId = pn.person_id)
+ ) p
\ No newline at end of file
diff --git a/openmrs/apps/reports/sql/pmtcHiv.sql b/openmrs/apps/reports/sql/pmtcHiv.sql
new file mode 100644
index 000000000..06046ca4a
--- /dev/null
+++ b/openmrs/apps/reports/sql/pmtcHiv.sql
@@ -0,0 +1,371 @@
+
+
+SELECT
+ 'Known positive status (at entry into) - ANC' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypoint' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Known positive status at entry into ANC on ART' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypoint' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'artResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Transferred in on ART?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Women of unknown status tested for HIV (1st ANC Visit)' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'hivSampleResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV Sample" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Women of unknown status testing HIV positive (1st ANC visit)' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'hivSampleResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV Sample" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Total of HIV Positive women \n attending ANC (a+b)'as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypointhiv' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS hr ON (hr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'hivSampleResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV Sample" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'HIV infected pregnant women newly started on ART (Option B+)'as'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+ FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypoint' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'artResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Transferred in on ART?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvsResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Were ARVS Received?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS ar ON (ar.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Total of HIV Positive \n Pregnant women enrolled in PMTCT'as'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+ FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypoint' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pmtcrResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="PMTCT - HIV & ART Follow up" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS ar ON (ar.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Known positive status on ART Breastfeeding 'as'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+ FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypoint' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ ) p
diff --git a/openmrs/apps/reports/sql/pmtctAnc.sql b/openmrs/apps/reports/sql/pmtctAnc.sql
new file mode 100644
index 000000000..58c973f34
--- /dev/null
+++ b/openmrs/apps/reports/sql/pmtctAnc.sql
@@ -0,0 +1,426 @@
+-- ANC Visits, Syphilis and HBV Testing
+
+SELECT
+ 'Total ANC Visits' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitNumberResult is not null) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitNumberResult is not null) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitNumberResult is not null) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitNumberResult is not null) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitNumberResult is not null) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitNumberResult is not null) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitNumberResult is not null) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitNumberResult is not null) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitNumberResult is not null) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F'and visitNumberResult is not null) THEN 1 END overFifty,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitNumberResult is not null) THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'visitNumberResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Visit Number" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+) p
+
+UNION ALL
+
+SELECT
+ 'First ANC visits' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'First ANC Mothers tested for Syphilis' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'rprvdrlResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="RPR/VDRL" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'First ANC Mothers testing Syphilis positive' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'rprvdrlResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="RPR/VDRL" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Mothers tested for Syphilis (None First ANC visit mothers)' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'rprvdrlResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="RPR/VDRL" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Mothers testing Syphilis positive (None First ANC visit mothers)' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'rprvdrlResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="RPR/VDRL" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Attending ANC four visits' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Attending ANC more than four visits' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Mothers tested for HBV ' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitNumberResult is not null) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitNumberResult is not null) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitNumberResult is not null) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitNumberResult is not null) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitNumberResult is not null) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitNumberResult is not null) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitNumberResult is not null) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitNumberResult is not null) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitNumberResult is not null) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F'and visitNumberResult is not null) THEN 1 END overFifty,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitNumberResult is not null) THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_text AS 'visitNumberResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Hepatitis - B Sample" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+) p
+
+UNION ALL
+
+SELECT
+ 'Mothers tested HBV Positive' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END overFifty,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_text AS 'hepBResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Hepatitis - B Sample" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'visitNumberResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Visit Number" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr1 ON (vr1.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'rprvdrlResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="RPR/VDRL" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+) p
+
diff --git a/openmrs/apps/reports/sql/pmtctAncvisitandHivcouselling.sql b/openmrs/apps/reports/sql/pmtctAncvisitandHivcouselling.sql
new file mode 100644
index 000000000..d57bc9a3f
--- /dev/null
+++ b/openmrs/apps/reports/sql/pmtctAncvisitandHivcouselling.sql
@@ -0,0 +1,797 @@
+-- ANC Visits, Syphilis and HBV Testing
+
+SELECT
+ 'Total ANC Visits' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitNumberResult is not null) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitNumberResult is not null) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitNumberResult is not null) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitNumberResult is not null) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitNumberResult is not null) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitNumberResult is not null) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitNumberResult is not null) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitNumberResult is not null) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitNumberResult is not null) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F'and visitNumberResult is not null) THEN 1 END overFifty,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitNumberResult is not null) THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'visitNumberResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Visit Number" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+) p
+
+UNION ALL
+
+SELECT
+ 'First ANC visits' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'First ANC Mothers tested for Syphilis' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'rprvdrlResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="RPR/VDRL" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'First ANC Mothers testing Syphilis positive' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'rprvdrlResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="RPR/VDRL" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Mothers tested for Syphilis (None First ANC visit mothers)' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'rprvdrlResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="RPR/VDRL" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Mothers testing Syphilis positive (None First ANC visit mothers)' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'rprvdrlResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="RPR/VDRL" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Attending ANC four visits' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Attending ANC more than four visits' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Mothers tested for HBV ' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitNumberResult is not null) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitNumberResult is not null) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitNumberResult is not null) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitNumberResult is not null) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitNumberResult is not null) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitNumberResult is not null) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitNumberResult is not null) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitNumberResult is not null) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitNumberResult is not null) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F'and visitNumberResult is not null) THEN 1 END overFifty,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitNumberResult is not null) THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_text AS 'visitNumberResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Hepatitis - B Sample" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+) p
+
+UNION ALL
+
+SELECT
+ 'Mothers tested HBV Positive' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END overFifty,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_text AS 'hepBResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Hepatitis - B Sample" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'visitNumberResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Visit Number" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr1 ON (vr1.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'rprvdrlResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="RPR/VDRL" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+) p
+
+UNION ALL
+
+SELECT
+ 'Known positive status (at entry into) - ANC' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypoint = "ANC Clinic") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypoint' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Known positive status at entry into ANC on ART' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypoint = "ANC Clinic" and artResult = "True") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypoint' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'artResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Transferred in on ART?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Women of unknown status tested for HIV (1st ANC Visit)' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult is not null) THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'hivSampleResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV Sample" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Women of unknown status testing HIV positive (1st ANC visit)' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'hivSampleResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV Sample" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Total of HIV Positive women \n attending ANC (a+b)'as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypointhiv = "ANC Clinic" and visitResult = "1 = Second Contact" and hivSampleResult ="Positive") THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypointhiv' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS hr ON (hr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'hivSampleResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV Sample" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'HIV infected pregnant women newly started on ART (Option B+)'as'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+ FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypoint not in ("ANC Clinic") and artResult = "True" and arvsResult = "True") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypoint' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'artResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Transferred in on ART?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvsResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Were ARVS Received?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS ar ON (ar.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Total of HIV Positive \n Pregnant women enrolled in PMTCT'as'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+ FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" and pmtcrResult = "True") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypoint' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pmtcrResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="PMTCT - HIV & ART Follow up" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS ar ON (ar.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Known positive status on ART Breastfeeding 'as'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+ FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True" ) THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and entrypoint not in ("ANC Clinic") and pregResult = "True") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'entrypoint' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="HIV - Entry Point" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ ) p
diff --git a/openmrs/apps/reports/sql/pmtctArtandTb.sql b/openmrs/apps/reports/sql/pmtctArtandTb.sql
new file mode 100644
index 000000000..a837d2fa2
--- /dev/null
+++ b/openmrs/apps/reports/sql/pmtctArtandTb.sql
@@ -0,0 +1,1164 @@
+-- Number of PMTCT Clients who received ART during this reporting period by Regimen (Current on OptionB+)
+
+SELECT
+ 'Adult 1st Line Regimens:' as 'Regimen',
+ 'M' as '<10 Male',
+ 'F' as '<10 Female',
+ 'M' as'10-15 Male' ,
+ 'F' as'10-15 Female',
+ 'M' as '15-49 Male',
+ 'F' as '15-49 Female',
+ 'M' as '50+ Male',
+ 'F' as '50+ Female',
+ '' as 'Total',
+ '' as'BreastFeeding',
+ '' as 'Pregnant'
+ From DUAL
+
+UNION All
+
+SELECT
+ '1a = AZT/3TC+ EFV' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "1a") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "1a") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "1a") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "1a") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "1a") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "1a") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "1a") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "1a") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "1a") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "1a" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "1a" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+) p
+
+UNION ALL
+
+SELECT
+ '1b = AZT/3TC/NVP' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "1b") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "1b") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "1b") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "1b") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "1b") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "1b") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "1b") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "1b") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "1b") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "1b" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "1b" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '1c = TDF/3TC/DTG' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "1c") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "1c") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "1c") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "1c") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "1c") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "1c") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "1c") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "1c") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "1c") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "1c" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "1c" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '1d = ABC/3TC (600/300) /DTG' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "1d") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "1d") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "1d") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "1d") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "1d") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "1d") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "1d") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "1d") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "1d") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "1d" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "1d" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '1e = AZT/3TC + DTG' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "1e") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "1e") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "1e") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "1e") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "1e") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "1e") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "1e") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "1e") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "1e") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "1e" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "1e" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '1f = TDF/3TC/EFV' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "1f") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "1f") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "1f") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "1f") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "1f") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "1f") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "1f") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "1f") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "1f") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "1f" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "1f" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '1g = TDF/3TC+NVP' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "1g") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "1g") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "1g") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "1g") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "1g") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "1g") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "1g") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "1g") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "1g") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "1g" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "1g" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '1h = TDF/FTC/ EFV' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "1h") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "1h") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "1h") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "1h") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "1h") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "1h") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "1h") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "1h") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "1h") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "1h" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "1h" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '1J = TDF/FTC+NVP' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "1j") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "1j") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "1j") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "1j") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "1j") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "1j") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "1j") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "1j") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "1j") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "1j" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "1j" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Adult 2nd Line Regimens:' as 'Regimen',
+ '' as '<10 Male',
+ '' as '<10 Female',
+ '' as'10-15 Male' ,
+ '' as'10-15 Female',
+ '' as '15-49 Male',
+ '' as '15-49 Female',
+ '' as '50+ Male',
+ '' as '50+ Female',
+ '' as 'Total',
+ '' as'BreastFeeding',
+ '' as 'Pregnant'
+From DUAL
+
+UNION ALL
+
+SELECT
+ '2a = AZT/3TC + DTG' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "2a") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "2a") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "2a") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "2a") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "2a") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "2a") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "2a") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "2a") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "2a") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "2a" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "2a" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '2b = ABC/3TC + DTG' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "2b") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "2b") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "2b") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "2b") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "2b") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "2b") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "2b") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "2b") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "2b") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "2b" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "2b" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '2c = TDF+3TC+LPV/r' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "2c") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "2c") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "2c") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "2c") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "2c") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "2c") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "2c") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "2c") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "2c") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "2c" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "2c" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '2d = TDF/3TC+ATV/r' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "2d") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "2d") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "2d") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "2d") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "2d") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "2d") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "2d") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "2d") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "2d") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "2d" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "2d" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '2e = TDF/FTC-LPV/r' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "2e") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "2e") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "2e") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "2e") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "2e") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "2e") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "2e") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "2e") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "2e") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "2e" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "2e" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '2f = TDF/FTC-ATV/r' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "2f") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "2f") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "2f") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "2f") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "2f") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "2f") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "2f") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "2f") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "2f") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "2f" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "2f" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '2g = AZT/3TC+LPV/r' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "2g") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "2g") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "2g") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "2g") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "2g") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "2g") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "2g") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "2g") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "2g") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "2g" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "2g" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '2h = AZT/3TC+ATV/r' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "2h") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "2h") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "2h") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "2h") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "2h") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "2h") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "2h") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "2h") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "2h") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "2h" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "2h" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '2i = ABC/3TC + LPV/r' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "2i") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "2i") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "2i") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "2i") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "2i") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "2i") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "2i") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "2i") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "2i") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "2i" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "2i" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '2J = ABC/3TC + ATV/r' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "2j") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "2j") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "2j") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "2j") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "2j") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "2j") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "2j") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "2j") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "2j") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "2j" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "2j" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ '2k = TDF/3TC/DTG' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and arvResult = "2k") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and arvResult = "2k") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and arvResult = "2k") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and arvResult = "2k") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and arvResult = "2k") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and arvResult = "2k") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and arvResult = "2k") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and arvResult = "2k") THEN 1 END overFiftyfemale,
+ CASE WHEN (arvResult = "2k") THEN 1 END totalAll,
+ CASE WHEN (arvResult = "2k" and bresResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (arvResult = "2k" and pregResult = "True") THEN 1 END Pregnant
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'arvResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="ARV Regimen" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'pregResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="FP Pregnant" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ Left JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'bresResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Currently Breastfeeding?" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr2 ON (pr2.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'No signs of TB' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and cuCoughResult = "False" and tbFeverResult = "False" and tbWeightResult = "False" and tbNightsResult = "False") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and cuCoughResult = "False" and tbFeverResult = "False" and tbWeightResult = "False" and tbNightsResult = "False") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and cuCoughResult = "False" and tbFeverResult = "False" and tbWeightResult = "False" and tbNightsResult = "False") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and cuCoughResult = "False" and tbFeverResult = "False" and tbWeightResult = "False" and tbNightsResult = "False") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and cuCoughResult = "False" and tbFeverResult = "False" and tbWeightResult = "False" and tbNightsResult = "False") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and cuCoughResult = "False" and tbFeverResult = "False" and tbWeightResult = "False" and tbNightsResult = "False") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and cuCoughResult = "False" and tbFeverResult = "False" and tbWeightResult = "False" and tbNightsResult = "False") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and cuCoughResult = "False" and tbFeverResult = "False" and tbWeightResult = "False" and tbNightsResult = "False") THEN 1 END overFiftyfemale,
+ CASE WHEN (cuCoughResult = "False" and tbFeverResult = "False" and tbWeightResult = "False" and tbNightsResult = "False") THEN 1 END totalAll,
+ CASE WHEN (cuCoughResult = "False" and tbFeverResult = "False" and tbWeightResult = "False" and tbNightsResult = "False") THEN 1 END breastfeeding,
+ CASE WHEN (cuCoughResult = "False" and tbFeverResult = "False" and tbWeightResult = "False" and tbNightsResult = "False") THEN 1 END Pregnant
+FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'cuCoughResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Current Cough" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS cr ON (cr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'tbFeverResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="TB Screening , Fever" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS fr ON (fr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'tbWeightResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="TB Screening ,Weight loss" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS wr ON (wr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'tbNightsResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="TB Screening , Night Sweats" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS nr ON (nr.visitPatientId = pn.person_id)
+) p
+
+UNION ALL
+
+SELECT
+ 'Presumptive TB case' as 'Regimen',
+ count(belowtenmale) as '<10 Male',
+ count(belowtenfemale) as '<10 Female',
+ count(tenTofifteenmale) as '10-15 Male',
+ count(tenTofifteenfemale) as '10-15 Female',
+ count(fifteenTOfourtyninemale) as '15-49 Male',
+ count(fifteenTOfourtyninefemale) as '15-49 Female',
+ count(overFiftymale) as '50+ Male',
+ count(overFiftyfemale) as '50+ Female',
+ count(totalAll) as 'Total',
+ count(breastfeeding) as 'BreastFeeding',
+ count(pregnant) as 'Pregnant'
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'M' and cuCoughResult = "True" and tbFeverResult = "True" and tbWeightResult = "True" and tbNightsResult = "True") THEN 1 END belowtenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 10 and gender = 'F' and cuCoughResult = "True" and tbFeverResult = "True" and tbWeightResult = "True" and tbNightsResult = "True") THEN 1 END belowtenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'M' and cuCoughResult = "True" and tbFeverResult = "True" and tbWeightResult = "True" and tbNightsResult = "True") THEN 1 END tenTofifteenmale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 15 and gender = 'F' and cuCoughResult = "True" and tbFeverResult = "True" and tbWeightResult = "True" and tbNightsResult = "True") THEN 1 END tenTofifteenfemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'M' and cuCoughResult = "True" and tbFeverResult = "True" and tbWeightResult = "True" and tbNightsResult = "True") THEN 1 END fifteenTOfourtyninemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 49 and gender = 'F' and cuCoughResult = "True" and tbFeverResult = "True" and tbWeightResult = "True" and tbNightsResult = "True") THEN 1 END fifteenTOfourtyninefemale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'M' and cuCoughResult = "True" and tbFeverResult = "True" and tbWeightResult = "True" and tbNightsResult = "True") THEN 1 END overFiftymale,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 115 and gender = 'F' and cuCoughResult = "True" and tbFeverResult = "True" and tbWeightResult = "True" and tbNightsResult = "True") THEN 1 END overFiftyfemale,
+ CASE WHEN (cuCoughResult = "True" and tbFeverResult = "True" and tbWeightResult = "True" and tbNightsResult = "True") THEN 1 END totalAll,
+ CASE WHEN (cuCoughResult = "True" and tbFeverResult = "True" and tbWeightResult = "True" and tbNightsResult = "True") THEN 1 END breastfeeding,
+ CASE WHEN (cuCoughResult = "True" and tbFeverResult = "True" and tbWeightResult = "True" and tbNightsResult = "True") THEN 1 END Pregnant
+FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'cuCoughResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Current Cough" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS cr ON (cr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'tbFeverResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="TB Screening , Fever" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS fr ON (fr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'tbWeightResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="TB Screening ,Weight loss" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS wr ON (wr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'tbNightsResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="TB Screening , Night Sweats" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS nr ON (nr.visitPatientId = pn.person_id)
+) p
+
diff --git a/openmrs/apps/reports/sql/pmtctHIVcounselling.sql b/openmrs/apps/reports/sql/pmtctHIVcounselling.sql
new file mode 100644
index 000000000..5893fdc19
--- /dev/null
+++ b/openmrs/apps/reports/sql/pmtctHIVcounselling.sql
@@ -0,0 +1,426 @@
+-- HIV Counseling and Testing at Maternity
+
+SELECT
+ 'Mothers delivered in this facility (Total)' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitNumberResult is not null) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitNumberResult is not null) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitNumberResult is not null) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitNumberResult is not null) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitNumberResult is not null) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitNumberResult is not null) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitNumberResult is not null) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitNumberResult is not null) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitNumberResult is not null) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F'and visitNumberResult is not null) THEN 1 END overFifty,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitNumberResult is not null) THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'visitNumberResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Visit Number" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+) p
+
+UNION ALL
+
+SELECT
+ 'Known positive status at admision into maternity ' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "1 = Second Contact") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Known positive status at\nadmision into maternity on ART' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult in ("Positive","Negative")) THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'rprvdrlResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="RPR/VDRL" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Women of Unknown HIV Status at\nadmission into maternity' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "1 = Second Contact" and rprvdrlResult = "Positive") THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'rprvdrlResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="RPR/VDRL" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Women of unknown status tested for HIV in maternity' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult in ("Positive","Negative")) THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'rprvdrlResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="RPR/VDRL" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Women of unknown status testing HIV positive at maternity' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult not in ("1 = Second Contact") and rprvdrlResult = "Positive") THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'rprvdrlResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="RPR/VDRL" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr1 ON (pr1.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'women testing HIV positive in maternity started on ART (new)' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult = "4 = Fourth Contact") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Total of HIV positive women delivered ' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END overFifty,
+ CASE WHEN (gender = 'F' and visitResult in ("5 = Fifth Contact","6 = Sixth Contact","7 = Seventh Contact","8 = Eight Contact","9 = Eight Contact")) THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', (select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'visitResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="Visit Number" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+ ) p
+
+UNION ALL
+
+SELECT
+ 'Infants started on Nevirapine Prohylaxis ' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitNumberResult is not null) THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and visitNumberResult is not null) THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and visitNumberResult is not null) THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and visitNumberResult is not null) THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and visitNumberResult is not null) THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and visitNumberResult is not null) THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and visitNumberResult is not null) THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and visitNumberResult is not null) THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and visitNumberResult is not null) THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F'and visitNumberResult is not null) THEN 1 END overFifty,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and visitNumberResult is not null) THEN 1 END totalAll
+
+ FROM person pn
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_text AS 'visitNumberResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Hepatitis - B Sample" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+) p
+
+UNION ALL
+
+SELECT
+ 'Infants linked to HEI Care ' as 'Title',
+ count(unkownage) as 'Unknown age',
+ count(tenTofourteen) as '10-14 YRS',
+ count(fifteenTonineteen) as '15-19 YRS',
+ count(twentyTotwentyfour) as '20-24 YRS',
+ count(twentyfiveTotwentynine) as '25-29 YRS',
+ count(thirtyTothirtyfour) as '30-34 YRS',
+ count(thirtyfiveTothirtynine) as '35-39 YRS',
+ count(fourtyToFourtyfour) as '40-44 YRS',
+ count(fourtyfiveToFourtynine) as '45-49 YRS',
+ count(overFifty) as '50+ YRS',
+ count(totalAll) as 'Total'
+
+FROM (
+ SELECT
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END unkownage,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 10 and 14 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END tenTofourteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 15 and 19 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END fifteenTonineteen,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 20 and 24 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END twentyTotwentyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 25 and 29 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END twentyfiveTotwentynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 30 and 34 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END thirtyTothirtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 35 and 39 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END thirtyfiveTothirtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 40 and 44 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END fourtyToFourtyfour,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 45 and 49 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END fourtyfiveToFourtynine,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 50 and 100 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END overFifty,
+ CASE WHEN (TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) between 0 and 100 and gender = 'F' and hepBResult is not null and visitNumberResult is not null and rprvdrlResult = "Positive") THEN 1 END totalAll
+
+ FROM person pn
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_text AS 'hepBResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Hepatitis - B Sample" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr ON (vr.visitPatientId = pn.person_id)
+ JOIN (SELECT distinct v.patient_id AS 'visitPatientId', o.value_coded AS 'visitNumberResult' FROM obs o
+ JOIN concept_name cn ON (cn.concept_name_type = "FULLY_SPECIFIED" AND cn.voided is false AND cn.name="Visit Number" and o.concept_id = cn.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS vr1 ON (vr1.visitPatientId = pn.person_id)
+ LEFT JOIN (SELECT distinct v.patient_id AS 'visitPatientId',(select name from concept_name where concept_id = o.value_coded and concept_name_type = "FULLY_SPECIFIED") AS 'rprvdrlResult' FROM obs o
+ JOIN concept_name cnr ON (cnr.concept_name_type = "FULLY_SPECIFIED" AND cnr.voided is false AND cnr.name="RPR/VDRL" and o.concept_id = cnr.concept_id)
+ JOIN encounter enc ON enc.encounter_id = o.encounter_id
+ JOIN visit v ON v.visit_id = enc.visit_id
+ GROUP BY v.patient_id
+ ORDER BY v.visit_id DESC) AS pr ON (pr.visitPatientId = pn.person_id)
+) p
+
diff --git a/openmrs/apps/reports/sql/summaryAppointmentReport.sql b/openmrs/apps/reports/sql/summaryAppointmentReport.sql
new file mode 100644
index 000000000..110152c51
--- /dev/null
+++ b/openmrs/apps/reports/sql/summaryAppointmentReport.sql
@@ -0,0 +1,17 @@
+SELECT DISTINCT
+ pai.identifier AS 'patient ID',
+ pn.given_name AS 'First Name',
+ ifnull(pn.family_name,'') AS 'Last Name',
+ DATE_FORMAT(pa.start_date_time, "%d/%m/%Y") AS 'Appointment Date',
+ pa.status AS 'Status',
+ pMobile.telephoneNo AS 'Contact No.',
+ p.gender AS 'Gender'
+FROM patient_appointment pa
+ LEFT JOIN person p ON p.person_id = pa.patient_id AND pa.voided IS FALSE
+ LEFT JOIN person_name pn ON p.person_id = pn.person_id AND pn.voided IS FALSE
+ LEFT JOIN patient_identifier pai ON (pai.patient_id = pa.patient_id AND pai.preferred = 1)
+ LEFT JOIN (select paMobile.person_id as 'pMobilePersonId', paMobile.value AS 'telephoneNo' from person_attribute paMobile
+ JOIN person_attribute_type patMobile ON patMobile.name = "MobileNumber" AND patMobile.retired IS FALSE
+ AND patMobile.person_attribute_type_id = paMobile.person_attribute_type_id) AS pMobile ON pa.patient_id = pMobile.pMobilePersonId
+WHERE pa.appointment_kind = 'Scheduled' AND pa.start_date_time BETWEEN '#startDate#' AND '#endDate#'
+ORDER BY pa.start_date_time DESC;
diff --git a/openmrs/apps/reports/sql/visitAppointmentReport.sql b/openmrs/apps/reports/sql/visitAppointmentReport.sql
new file mode 100644
index 000000000..9d975d967
--- /dev/null
+++ b/openmrs/apps/reports/sql/visitAppointmentReport.sql
@@ -0,0 +1,22 @@
+SELECT DISTINCT
+ pai.identifier AS 'patient ID',
+ pn.given_name AS 'First Name',
+ ifnull(pn.family_name,'') AS 'Last Name',
+ DATE_FORMAT(pa.start_date_time, "%d/%m/%Y") AS 'Visit Date',
+ pa.appointment_kind AS 'Visit Type',
+ pMobile.telephoneNo AS 'Contact No.',
+ p.gender AS 'Gender'
+FROM patient_appointment pa
+ LEFT JOIN person p ON p.person_id = pa.patient_id AND pa.voided IS FALSE
+ LEFT JOIN person_name pn ON p.person_id = pn.person_id AND pn.voided IS FALSE
+ LEFT JOIN patient_identifier pai ON (pai.patient_id = pa.patient_id AND pai.preferred = 1)
+ LEFT JOIN (select paMobile.person_id as 'pMobilePersonId', paMobile.value AS 'telephoneNo' from person_attribute paMobile
+ JOIN person_attribute_type patMobile ON patMobile.name = "MobileNumber" AND patMobile.retired IS FALSE
+ AND patMobile.person_attribute_type_id = paMobile.person_attribute_type_id) AS pMobile ON pa.patient_id = pMobile.pMobilePersonId
+ LEFT JOIN (SELECT v.patient_id AS 'visitPatientId',
+ v.visit_id AS visitId, v.date_started AS visitStartDate, vt.name AS visitType FROM visit v
+ JOIN visit_type vt ON (v.visit_type_id = vt.visit_type_id and vt.retired IS FALSE)
+ HAVING v.visit_id = (SELECT MAX(maxV.visit_id) AS maxVisitId
+ FROM visit maxV WHERE maxV.patient_id = v.patient_id)) AS patientVisit ON patientVisit.visitPatientId = pa.patient_id
+WHERE pa.start_date_time BETWEEN '#startDate#' AND '#endDate#'
+ORDER BY pa.start_date_time DESC;
diff --git a/openmrs/apps/reports/sql/walkinAppointmentReport.sql b/openmrs/apps/reports/sql/walkinAppointmentReport.sql
new file mode 100644
index 000000000..42b30d80b
--- /dev/null
+++ b/openmrs/apps/reports/sql/walkinAppointmentReport.sql
@@ -0,0 +1,17 @@
+SELECT DISTINCT
+ pai.identifier AS 'patient ID',
+ pn.given_name AS 'First Name',
+ ifnull(pn.family_name,'') AS 'Last Name',
+ DATE_FORMAT(pa.start_date_time, "%d/%m/%Y") AS 'Visit Date',
+ pa.appointment_kind AS 'Visit Type',
+ pMobile.telephoneNo AS 'Contact No.',
+ p.gender AS 'Gender'
+FROM patient_appointment pa
+ LEFT JOIN person p ON p.person_id = pa.patient_id AND pa.voided IS FALSE
+ LEFT JOIN person_name pn ON p.person_id = pn.person_id AND pn.voided IS FALSE
+ LEFT JOIN patient_identifier pai ON (pai.patient_id = pa.patient_id AND pai.preferred = 1)
+ LEFT JOIN (select paMobile.person_id as 'pMobilePersonId', paMobile.value AS 'telephoneNo' from person_attribute paMobile
+ JOIN person_attribute_type patMobile ON patMobile.name = "MobileNumber" AND patMobile.retired IS FALSE
+ AND patMobile.person_attribute_type_id = paMobile.person_attribute_type_id) AS pMobile ON pa.patient_id = pMobile.pMobilePersonId
+WHERE pa.appointment_kind = 'WalkIn' AND pa.start_date_time BETWEEN '#startDate#' AND '#endDate#'
+ORDER BY pa.start_date_time DESC;
diff --git a/openmrs/i18n/clinical/locale_en.json b/openmrs/i18n/clinical/locale_en.json
index aeae74e36..a75ccbb09 100644
--- a/openmrs/i18n/clinical/locale_en.json
+++ b/openmrs/i18n/clinical/locale_en.json
@@ -1,9 +1,12 @@
{
"MODULE_LABEL_ACTIVE_KEY": "Active",
- "MODULE_LABEL_PROGRAMS_KEY": "Programs",
+ "MODULE_LABEL_HEI_KEY": "HEI",
"MODULE_LABEL_MY_PATIENTS_KEY": "My Patients",
"MODULE_LABEL_ACTIVE_PATIENTS_BY_LOCATION": "PatientsByLocation",
- "MODULE_LABEL_OPD_1_KEY": "OPD-1",
+ "MODULE_LABEL_OPD_1_KEY": "CONSULTATION",
+ "MODULE_LABEL_PMTCT_KEY": "PMTCT",
+ "MODULE_LABEL_DRUGS_PATIENTS_KEY": "PATIENTS DRUG QUEUE",
+ "MODULE_LABEL_VIRAL_LOAD_PATIENTS_KEY": "VIRAL LOAD",
"MODULE_LABEL_ALL_KEY": "All",
"DASHBOARD_TITLE_LAB_RESULTS_KEY": "Lab Results",
@@ -13,10 +16,13 @@
"DASHBOARD_TITLE_VISITS_KEY": "Visits",
"DASHBOARD_TITLE_PROGRAMS_KEY": "Programs",
"DASHBOARD_TITLE_TREATMENTS_KEY": "Treatments",
- "DASHBOARD_TITLE_DISPOSITION_KEY": "Disposition",
+ "DASHBOARD_TITLE_DISPOSITION_KEY": "Referral",
"DASHBOARD_TITLE_DIAGNOSIS_KEY": "Diagnosis",
"DASHBOARD_TITLE_ADMISSION_DETAILS_KEY": "Admission Details",
"DASHBOARD_TITLE_NUTRITIONAL_VALUES_KEY": "Nutritional Values",
+ "DASHBOARD_TITLE_VIRAL_LOAD_AND_EAC_RESULTS_KEY": "Viral load And EAC Dates",
+ "DASHBOARD_TITLE_CD4_KEY": "CD4 Details",
+ "DASHBOARD_TITLE_TB_DIAGNOSIS": "TB DIAGNOSIS STATUS",
"DASHBOARD_TITLE_SECOND_VITALS_KEY": "Second Vitals",
"DASHBOARD_TITLE_GYNAECOLOGY_KEY": "Gynaecology",
"DASHBOARD_TITLE_HISTORY_AND_EXAMINATIONS_KEY": "History and Examinations",
@@ -53,11 +59,11 @@
"OBSERVATIONS_BOARD_LABEL_KEY":"Observations",
- "MEDICATIONS_BOARD_LABEL_KEY":"Medications",
+ "MEDICATIONS_BOARD_LABEL_KEY":"ARV Regimen / Medications",
"ORDERS_BOARD_LABEL_KEY":"Orders",
"BACTERIOLOGY_BOARD_LABEL_KEY":"Bacteriology",
"CONSULTATION_BOARD_LABEL_KEY":"Consultation",
- "DISPOSITION_BOARD_LABEL_KEY":"Disposition",
+ "DISPOSITION_BOARD_LABEL_KEY":"Referral",
"DIAGNOSIS_BOARD_LABEL_KEY":"Diagnosis",
"CLINICAL_ORDER_RADIOLOGY_NEED_PRINT": "Need Print for this order.",
diff --git a/openmrs/i18n/registration/locale_en.json b/openmrs/i18n/registration/locale_en.json
index f3141e655..6cd53393d 100644
--- a/openmrs/i18n/registration/locale_en.json
+++ b/openmrs/i18n/registration/locale_en.json
@@ -21,5 +21,6 @@
"REGISTRATION_INSTITUTE_ADDRESS": "Ganiyari, District - Bilaspur",
"FEE_INFORMATION_LOCALE_KEY": "Fee Information",
"NUTRITIONAL_VALUES_LOCALE_KEY": "Nutritional Values",
- "REGISTRATION_PRINT_WITH_BARCODE": "Barcode Print"
+ "REGISTRATION_PRINT_WITH_BARCODE": "Barcode Print",
+ "TREATMENT_SUPPORTER_LOCALE_KEY": "Treatment Supporter"
}
\ No newline at end of file
diff --git a/openmrs/migrations/bedManagement/PatientWardDetails.sql b/openmrs/migrations/bedManagement/PatientWardDetails.sql
deleted file mode 100644
index 9c0b463b1..000000000
--- a/openmrs/migrations/bedManagement/PatientWardDetails.sql
+++ /dev/null
@@ -1,29 +0,0 @@
-DELETE FROM global_property WHERE property = 'emrapi.sqlGet.allWardsListDetails';
-SELECT uuid() INTO @uuid;
-INSERT INTO global_property (`property`, `property_value`, `description`, `uuid`)
-VALUES ('emrapi.sqlGet.allWardsListDetails',
-"SELECT
- bed_tags.bed_tags AS 'Bed Tags',
- bed.bed_number AS 'Bed Number',
- patient_identifier.identifier AS 'Patient ID',
- CONCAT(pn.given_name, ' ', pn.family_name) AS 'Patient Name',
- TIMESTAMPDIFF(YEAR, p.birthdate, CURDATE()) AS 'Age'
-FROM
- bed
- INNER JOIN bed_location_map blm ON blm.bed_id = bed.bed_id
- INNER JOIN location l ON l.location_id = blm.location_id AND l.name = ${location_name} AND l.name LIKE '%Ward%' AND l.retired IS FALSE
- LEFT OUTER JOIN bed_patient_assignment_map bpam ON bpam.bed_id = bed.bed_id AND bpam.date_stopped IS NULL
- LEFT OUTER JOIN person p ON p.person_id = bpam.patient_id AND p.voided IS FALSE
- LEFT OUTER JOIN person_name pn ON pn.person_id = p.person_id AND pn.voided IS FALSE
- LEFT OUTER JOIN patient_identifier ON patient_identifier.patient_id = p.person_id AND patient_identifier.voided IS FALSE
- LEFT OUTER JOIN person_address address ON address.person_id = p.person_id AND address.voided IS FALSE
- LEFT OUTER JOIN (
- SELECT
- bed_tag_map.bed_id AS 'bed_id',
- GROUP_CONCAT(DISTINCT bed_tag.name ORDER BY bed_tag.name) AS 'bed_tags'
- FROM
- bed_tag_map
- INNER JOIN bed_tag ON bed_tag.bed_tag_id = bed_tag_map.bed_tag_id AND bed_tag_map.voided IS FALSE
- GROUP BY bed_tag_map.bed_id
- ) bed_tags ON bed_tags.bed_id = bed.bed_id
-GROUP BY bed.bed_number;",'SQL query to get list of bed details in ward',@uuid);
diff --git a/openmrs/migrations/liquibase.xml b/openmrs/migrations/liquibase.xml
index a70d1c277..064dc3d4f 100644
--- a/openmrs/migrations/liquibase.xml
+++ b/openmrs/migrations/liquibase.xml
@@ -49,10 +49,6 @@
);
-
- Updating SQL query for Bed Management List View
-
-