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

Adding CS Linelist MTCT for assessing PMTCT gaps #549

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
124 changes: 124 additions & 0 deletions Scripts/REPORTING/HIVCaseSurveillance/load_cs_LinelistMTCT.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
IF OBJECT_ID(N'[HIVCaseSurveillance].[dbo].[CsLinelistMTCT]', N'U') IS NOT NULL
DROP TABLE [HIVCaseSurveillance].[dbo].[CsLinelistMTCT]
Begin
WITH Unsuppressed_Viral_Load

As (
SELECT
vlhist.PatientKey,
pat.PatientPKHash,
row_number () over (partition by vlhist.PatientKey order by OrderedbyDatekey desc) as rank,
vlhist.FacilityKey,
fac.MFLCode,
vlhist.AgeGroupKey,
TestResult,
IsPBFW,
hei.MothersPatientKey,
fac.FacilityName
from NDWH.Fact.FactViralLoad_Historical as vlhist
left join NDWH.Dim.DimPatient as pat on pat.PatientKey=vlhist.Patientkey
left join NDWH.Dim.DimFacility as fac on fac.FacilityKey=vlhist.Facilitykey
left join NDWH.Fact.FactHEI as hei on hei.PatientKey=vlhist.PatientKey
where TRY_CAST(REPLACE(TestResult, ',', '') AS FLOAT) >= 200.00
),
Unsuppressed_Viralloads_LatestRecord as (
SELECT
PatientKey,
PatientPKHash,
MothersPatientKey,
MFLCode,
TestResult,
IsPBFW
from Unsuppressed_Viral_Load


where rank=1
),
HEIs AS (
SELECT
heis.Patientkey,
pat.DOB,
eomonth(DOB) as CohortYearMonth,
fac.MFLCode,
fac.FacilityName,
fac.County,
fac.SubCounty,
agency.AgencyName,
partner.PartnerName,
age.DATIMAgeGroup,
heis.MothersPatientKey,
Paired,
MotherOnART,
OnProhylaxis as InfantGivenProphylaxis,
InfectedAt24mnths,
HEIHIVStatus,
HasFinalAntibody,
age.DatimAgeGroup as AgeGroup,
pat.Gender,
case when vls.MothersPatientKey is not null then 1 Else 0 End as MotherUnsuppressedDuringPBF
from NDWH.Fact.FactHEI as heis
left join NDWH.Dim.DimFacility as fac on fac.FacilityKey=heis.FacilityKey
left join NDWH.Dim.DimAgency as agency on agency.AgencyKey=heis.AgencyKey
left join NDWH.Dim.DimPartner as partner on partner.PartnerKey=heis.PartnerKey
left join NDWH.Dim.DimAgeGroup as age on age.AgeGroupKey=heis.AgeGroupKey
left join NDWH.Dim.DimPatient as pat on pat.PatientKey=heis.PatientKey
left join Unsuppressed_Viralloads_LatestRecord as vls on vls.MothersPatientKey = heis.MothersPatientKey and vls.mflcode=fac.MFLCode


),
PBFW_StartDate as (
Select
ROW_NUMBER() OVER (PARTITION BY pbfw.Patientkey ORDER BY COALESCE(Ancdate1, Ancdate2, Ancdate3, Ancdate4) ASC) AS num,
pbfw.Patientkey,
pat.PatientPKHash,
fac.MFLCode,
coalesce (Ancdate1,Ancdate2,Ancdate3,Ancdate4) as FirstVisitDuringPBFW
from NDWH.Fact.Factpbfw as pbfw
left join NDWH.Dim.DimPatient as pat on pat.PatientKey=pbfw.Patientkey
left join NDWH.Dim.DimFacility as fac on fac.FacilityKey=pbfw.Facilitykey
where COALESCE(Ancdate1, Ancdate2, Ancdate3, Ancdate4) is not null
),
IIT as (
Select
arthist.PatientKey,
Row_number() OVER ( Partition BY arthist.Patientkey ORDER BY AsOfDate desc ) as num,
AsOfDate,
ARTOutcome
from NDWH.Fact.FactARTHistory as arthist
left join PBFW_StartDate on PBFW_StartDate.Patientkey=arthist.PatientKey
WHERE ARTOutcome IN ('UNDOCUMENTED LOSS', 'LOSS TO FOLLOW UP') AND AsOfDate BETWEEN
(SELECT MIN(FirstVisitDuringPBFW) FROM PBFW_StartDate)
AND (SELECT MAX(FirstVisitDuringPBFW) FROM PBFW_StartDate)
)
Select
HEIs.PatientKey,
DOB,
HEIs.MFLCode,
HEIs.FacilityName,
AgencyName,
PartnerName,
CohortYearMonth,
County,
SubCounty,
DATIMAgeGroup as AgeGroup,
MothersPatientKey,
Paired,
MotherOnART,
case when HEIs.PatientKey is not null then 1 Else 0 End as IsHEI,
case when MotherOnART=0 Then 1 Else 0 End as MothersNotonART,
InfantGivenProphylaxis,
case when InfantGivenProphylaxis =0 Then 1 Else 0 End as InfantNotGivenProphylaxis,
case when MothersPatientKey is not null then 1 Else 0 End as KPandP,
InfectedAt24mnths as PositiveInfants,
MotherUnsuppressedDuringPBF,
FirstVisitDuringPBFW,
case when IIT.PatientKey is not null then 1 Else 0 End as IITEpisode
Into [HIVCaseSurveillance].[dbo].[CsLinelistMTCT]
from HEIs
left JOIN PBFW_StartDate on PBFW_StartDate.Patientkey=HEIs.MothersPatientKey and PBFW_StartDate.MFLCode=HEIs.MFLCode
left join IIT on IIT.PatientKey=HEIs.PatientKey and IIT.num=1
End



--168,328