Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COM-675 TX_TB - Number of ART patients who were screened for TB at least once - New on ART/Screen Positive #275

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions metadata/reportssql/treatment_report_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,112 @@ WHERE
END$$
DELIMITER ;

DROP FUNCTION IF EXISTS TREATMENT_Indicator7c;

DELIMITER $$
CREATE FUNCTION TREATMENT_Indicator7c(
p_startDate DATE,
p_endDate DATE,
p_startAge INT(11),
p_endAge INT (11),
p_includeEndAge TINYINT(1),
p_gender VARCHAR(1)) RETURNS INT(11)
DETERMINISTIC
BEGIN
DECLARE result INT(11) DEFAULT 0;

SELECT
COUNT(DISTINCT pat.patient_id) INTO result
FROM
patient pat
WHERE
patientGenderIs(pat.patient_id, p_gender) AND
patientHasEnrolledIntoHivProgram(pat.patient_id) = "Yes" AND
patientHasStartedARVTreatmentDuringReportingPeriod(pat.patient_id, p_startDate, p_endDate) AND
patientOrderedARVDuringReportingPeriod(pat.patient_id, p_startDate, p_endDate) AND
patientScreenedPositiveForTBDuringReportingPeriod(pat.patient_id, p_startDate, p_endDate) AND
patientAgeWhenRegisteredForHivProgramIsBetween(pat.patient_id, p_startAge, p_endAge, p_includeEndAge);

RETURN (result);
END$$
DELIMITER ;

-- patientScreenedPositiveForTBDuringReportingPeriod

DROP FUNCTION IF EXISTS patientScreenedPositiveForTBDuringReportingPeriod;

DELIMITER $$
CREATE FUNCTION patientScreenedPositiveForTBDuringReportingPeriod(
p_patientId INT(11),
p_startDate DATE,
p_endDate DATE) RETURNS TINYINT(1)
DETERMINISTIC
BEGIN

DECLARE result TINYINT(1) DEFAULT 0;
DECLARE tbTestDate DATE;
DECLARE tbResultUuid VARCHAR(38) DEFAULT "7a4899dc-68ff-444a-9c7e-7fbae547e326";
DECLARE tbTestDateUuid VARCHAR(38) DEFAULT "55185e73-e634-4dfc-8ec0-02086e8c54d0";
DECLARE tbPositiveUuid VARCHAR(38) DEFAULT "7acfafa4-f19b-485e-97a7-c9e002dbe37a";
DECLARE encounterIdOfTestDateObservation INT(11);

SELECT
o.value_datetime, o.encounter_id
INTO tbTestDate, encounterIdOfTestDateObservation
FROM obs o
JOIN concept c ON o.concept_id = c.concept_id AND c.retired = 0
WHERE o.voided = 0
AND o.value_datetime IS NOT NULL
AND o.value_datetime BETWEEN p_startDate AND p_endDate
AND o.person_id = p_patientId
AND c.uuid = tbTestDateUuid
ORDER BY o.value_datetime DESC
LIMIT 1;

SELECT TRUE INTO result
FROM obs o
JOIN concept c ON o.concept_id = c.concept_id AND c.retired = 0
WHERE o.voided = 0
AND o.encounter_id = encounterIdOfTestDateObservation
AND o.value_coded IS NOT NULL
AND o.value_coded = (SELECT concept.concept_id FROM concept WHERE concept.uuid = tbPositiveUuid)
AND o.person_id = p_patientId
AND c.uuid = tbResultUuid
ORDER BY o.obs_id DESC
LIMIT 1;

RETURN (result);
END$$
DELIMITER ;

-- patientOrderedARVDuringReportingPeriod

DROP FUNCTION IF EXISTS patientOrderedARVDuringReportingPeriod;

DELIMITER $$
CREATE FUNCTION patientOrderedARVDuringReportingPeriod(
p_patientId INT(11),
p_startDate DATE,
p_endDate DATE) RETURNS TINYINT(1)
DETERMINISTIC
BEGIN

DECLARE result TINYINT(1) DEFAULT 0;

SELECT TRUE INTO result
FROM orders o
JOIN drug_order do ON do.order_id = o.order_id
JOIN concept c ON do.duration_units = c.concept_id AND c.retired = 0
JOIN drug d ON d.drug_id = do.drug_inventory_id AND d.retired = 0
WHERE o.patient_id = p_patientId AND o.voided = 0
AND drugIsARV(d.concept_id)
AND o.scheduled_date BETWEEN p_startDate AND p_endDate
GROUP BY o.patient_id;

RETURN (result );
END$$
DELIMITER ;

-- getPatientDateOfEnrolmentInProgram

DROP FUNCTION IF EXISTS getPatientDateOfEnrolmentInProgram;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SELECT 'Not available' AS 'Not available',
0 AS '<15 M',
0 AS '<15 F',
0 AS '>= 15 M',
0 AS '>= 15 F',
SELECT 'New on ART/Screen Positive' AS 'Title',
TREATMENT_Indicator7c('#startDate#','#endDate#', 0, 15, 0, 'M') AS '<15 M',
TREATMENT_Indicator7c('#startDate#','#endDate#', 0, 15, 0, 'F') AS '<15 F',
TREATMENT_Indicator7c('#startDate#','#endDate#', 15, 200, 1, 'M') AS '>=15 M',
TREATMENT_Indicator7c('#startDate#','#endDate#', 15, 200, 1, 'F') AS '>=15 F',
0 AS 'Unknown M',
0 AS 'Unknown F';