diff --git a/Scripts/NDWH/C&T FACT TABLES/load_FactViralLoad.sql b/Scripts/NDWH/C&T FACT TABLES/load_FactViralLoad.sql index ae0141e2..60047006 100644 --- a/Scripts/NDWH/C&T FACT TABLES/load_FactViralLoad.sql +++ b/Scripts/NDWH/C&T FACT TABLES/load_FactViralLoad.sql @@ -168,9 +168,40 @@ BEGIN PatientPK, SiteCode, replace(TestResult, ',', '') as FirstVL, - OrderedbyDate as FirstVLDate + OrderedbyDate as FirstVLDate , + Case WHEN (Isnumeric( TestResult) = 1 AND Cast(Replace( TestResult, ',', '') AS Float) < 200.00) + OR TestResult IN ('undetectable', 'NOT DETECTED', '0 copies/ml', 'LDL', 'Less than Low Detectable Level') + THEN 1 Else 0 + End As IsSuppressedInitialViralload from ODS.[Intermediate].Intermediate_BaseLineViralLoads + ), + second_vl as ( + select + PatientPK, + SiteCode, + replace(TestResult, ',', '') as SecondVL, + OrderedbyDate as SecondVLDate , + Case WHEN (Isnumeric( TestResult) = 1 AND Cast(Replace( TestResult, ',', '') AS Float) < 200.00) + OR TestResult IN ('undetectable', 'NOT DETECTED', '0 copies/ml', 'LDL', 'Less than Low Detectable Level') + THEN 1 Else 0 + End As IsSuppressedSecondFollowupViralloads + from ODS.[Intermediate].Intermediate_OrderedViralLoads + where rank=2 ), +Third_Vl as ( + select + PatientPK, + SiteCode, + replace(TestResult, ',', '') as ThirdVL, + OrderedbyDate as SecondVLDate , + Case WHEN (Isnumeric( TestResult) = 1 AND Cast(Replace( TestResult, ',', '') AS Float) < 200.00) + OR TestResult IN ('undetectable', 'NOT DETECTED', '0 copies/ml', 'LDL', 'Less than Low Detectable Level') + THEN 1 Else 0 + End As IsSuppressedThirdFollowupViralloads + from ODS.[Intermediate].Intermediate_OrderedViralLoads + where rank=3 +), + last_vl as ( select PatientPK, @@ -312,6 +343,12 @@ RepeatVlUnSupp as (Select patient_viral_load_intervals.[_24MonthVLSup], first_vl.FirstVL, first_vl.FirstVLDate, + IsSuppressedInitialViralload, + second_vl.SecondVL, + second_vl.SecondVLDate, + IsSuppressedSecondFollowupViralloads, + Third_Vl.ThirdVL, + IsSuppressedThirdFollowupViralloads, last_vl.LastVL, last_vl.LastVLDate, time_to_first_vl.TimetoFirstVL, @@ -344,6 +381,10 @@ RepeatVlUnSupp as (Select and patient_viral_load_intervals.SiteCode = patient.SiteCode left join first_vl on first_vl.PatientPK = patient.PatientPK and first_vl.SiteCode = patient.SiteCode + left join second_vl on second_vl.PatientPK = patient.PatientPK + and second_vl.SiteCode = patient.SiteCode + left join Third_Vl on Third_Vl.PatientPK = patient.PatientPK + and Third_Vl.SiteCode = patient.SiteCode left join last_vl on last_vl.PatientPK = patient.PatientPK and last_vl.SiteCode = patient.SiteCode left join time_to_first_vl_group on time_to_first_vl_group.PatientPK = patient.PatientPK @@ -410,7 +451,12 @@ RepeatVlUnSupp as (Select combined_viral_load_dataset.[_18MonthVLSup] as [18MonthVLSup], combined_viral_load_dataset.[_24MonthVLSup] as [24MonthVLSup], combined_viral_load_dataset.FirstVL, + combined_viral_load_dataset.SecondVL, + combined_viral_load_dataset.IsSuppressedInitialViralload, combined_viral_load_dataset.LastVL, + combined_viral_load_dataset.IsSuppressedSecondFollowupViralloads, + combined_viral_load_dataset.ThirdVL, + combined_viral_load_dataset.IsSuppressedThirdFollowupViralloads, combined_viral_load_dataset.TimetoFirstVL, combined_viral_load_dataset.TimeToFirstVLGrp, combined_viral_load_dataset.HighViremia, diff --git a/Scripts/REPORTING/HIVCaseSurveillance/load_Linelist_CsLinkages.sql b/Scripts/REPORTING/HIVCaseSurveillance/load_Linelist_CsLinkages.sql index e46fbb1f..79f0b86e 100644 --- a/Scripts/REPORTING/HIVCaseSurveillance/load_Linelist_CsLinkages.sql +++ b/Scripts/REPORTING/HIVCaseSurveillance/load_Linelist_CsLinkages.sql @@ -108,4 +108,3 @@ FROM confirmed_reported_cases_and_art confirmed_reported_cases_and_art.patientkey LEFT JOIN ndwh.dbo.dimagegroup age ON age.agegroupkey = confirmed_reported_cases_and_art.agegroupkey ---select top 50 * from [HIVCaseSurveillance].[dbo].[CsLinkage] \ No newline at end of file diff --git a/Scripts/REPORTING/HIVCaseSurveillance/load_cs_sentinel_events.sql b/Scripts/REPORTING/HIVCaseSurveillance/load_cs_sentinel_events.sql new file mode 100644 index 00000000..b49c4caf --- /dev/null +++ b/Scripts/REPORTING/HIVCaseSurveillance/load_cs_sentinel_events.sql @@ -0,0 +1,246 @@ +IF OBJECT_ID(N'[HIVCaseSurveillance].[dbo].[CsSentinelEvents]', N'U') IS NOT NULL + DROP TABLE [HIVCaseSurveillance].[dbo].[CsSentinelEvents]; +begin + with MFL_partner_agency_combination as ( + select + distinct MFL_Code, + SDP, + SDP_Agency as Agency + from ODS.Care.All_EMRSites + ), + confirmed_reported_cases_and_art as ( + select + ctpatients.PatientKey, + ctpatients.Gender, + art.AgeLastVisit, + art.Agegroupkey, + ctpatients.sitecode, + case when confirmed_date.Date is not null Then 1 Else 0 End as NewCaseReported, + Case when art_date.Date is not null then 1 Else 0 End as LinkedToART, + case when art_date.Date is null Then 1 Else 0 End as NotLinkedOnART, + confirmed_date.Date as DateConfirmedPositive, + eomonth(confirmed_date.Date) as CohortYearMonth, + case + when art_date.Date < confirmed_date.Date then confirmed_date.Date + else art_date.Date + end as StartARTDate, + DATEDIFF(year,ctpatients.DOB,confirmed_date.Date) as AgeatDiagnosis + from NDWH.Dim.DimPatient as ctpatients + left join NDWH.Fact.FACTART as art on ctpatients.patientkey=art.PatientKey + left join NDWH.Dim.DimDate as confirmed_date on confirmed_date.DateKey = ctpatients.DateConfirmedHIVPositiveKey + left join NDWH.Dim.DimDate as art_date on art_date.DateKey = art.StartARTDateKey + left join NDWH.Dim.DimAgeGroup as age on age.AgeGroupKey=art.AgeGroupKey + + where DateConfirmedHIVPositiveKey is not null + + ) , + + BaselineCD4s As ( + SELECT + PatientKey, + BaselineCD4, + BaselineCD4Date + from NDWH.Fact.FactCD4 + where BaselineCD4 is not null + ), + OtherCD4s As ( + Select + Patientkey, + SecondCD4 as OtherCD4s, + SecondCD4 asOtherCD4sDate, + LastCD4Percentage as OtherCD4Percent, + LastCD4Date as OtherCD4PercentDate + from NDWH.Fact.FactCD4 + + ), + BaselineWHO As ( + Select + Patientkey, + WHOStageATART, + AgeAtARTStart + from NDWH.Fact.FactARTBaselines + ), + Viralloads As ( + SELECT + + viralloads.Patientkey, + SiteCode, + FirstVL, + IsSuppressedInitialViralload, + SecondVL, + IsSuppressedSecondFollowupViralloads, + ThirdVL, + IsSuppressedThirdFollowupViralloads, + LastVL, + Case WHEN (Isnumeric( LastVL) = 1 AND Cast(Replace( LastVL, ',', '') AS Float) < 200.00) + OR LastVL IN ('undetectable', 'NOT DETECTED', '0 copies/ml', 'LDL', 'Less than Low Detectable Level') + THEN 1 Else 0 + End As IsSuppressedLatestViralload, + fac.FacilityKey +FROM + NDWH.Fact.FactViralLoads as viralloads + LEFT join NDWH.Dim.DimFacility fac on fac.FacilityKey = viralloads.FacilityKey + LEFT JOIN NDWH.Dim.DimAgency agency on agency.AgencyKey = viralloads.AgencyKey + LEFT JOIN NDWH.Dim.DimPatient pat on pat.PatientKey = viralloads.PatientKey + LEFT JOIN NDWH.Dim.DimPartner partner on partner.PartnerKey = viralloads.PartnerKey + + ), + InitialViralLoads As ( + SELECT + Viralloads.patientkey, + Viralloads.Facilitykey, + Viralloads.IsSuppressedInitialViralload + from Viralloads + + ), + FirstFollowupViralloads As ( + SELECT + Patientkey, + Facilitykey, + Viralloads.IsSuppressedSecondFollowupViralloads as IsSuppressedFirstFollowupViralloads + from Viralloads + + ), + SecondFollowupViralloads As ( + SELECT + Patientkey, + Facilitykey, + Viralloads.IsSuppressedThirdFollowupViralloads as IsSuppressedSecondFollowupViralloads + from Viralloads + + ), + RegimenChanges as ( + Select + Patientkey, + Facilitykey, + StartRegimen, + CurrentRegimen, + case when StartRegimen = CurrentRegimen Then 0 Else 1 End as RegimenChanged + from NDWH.Fact.FACTART + where StartRegimen is not null +), +OptimizedRegimen as ( + Select + Patientkey, + Facilitykey, + StartRegimen, + CurrentRegimen, + case when CurrentRegimen like '3TC+DTG+TDF' THEN 1 + Else 0 END AS OptimizedRegimen + from NDWH.Fact.FACTART +), + + SecondLatestHighVls as ( + SELECT + PatientKey, + FacilityKey, + cast (LatestVLDate2Key as date) as LatestVLDate2Key, + LatestVL2 + from NDWH.Fact.FactViralLoads as secondlatestvls + LEFT JOIN Ndwh.Dim.Dimdate AS SecondVLDate + ON SecondVLDate.Datekey = secondlatestvls.LatestVLDate2Key + where TRY_CAST(LatestVL2 as float) >=200 + AND DATEDIFF(month, LatestVLDate2Key , GETDATE()) <= 26 +), +ConsecutiveHighVls as ( + SELECT + latestvls.Patientkey, + latestvls.FacilityKey, + LatestVL1, + cast (LatestVLDate1Key as date ) as LatestVLDate + from NDWH.Fact.FactViralLoads as latestvls + inner join SecondLatestHighVls on SecondLatestHighVls.PatientKey=latestvls.PatientKey + LEFT JOIN Ndwh.Dim.Dimdate AS LatestVLDate + ON LatestVLDate.Datekey = latestvls.LatestVLDate1Key + where TRY_CAST(LatestVL1 as float) >=200 and datediff(month, LatestVLDate1Key , eomonth(dateadd(mm,-1,getdate()))) <= 14 + +), +LatestSuppressedVL as ( +Select + latestvls.Patientkey, + latestvls.FacilityKey, + LatestVL1 as LatestVLSuppressed, + cast (LatestVLDate1Key as date ) as LatestVLDate + from NDWH.Fact.FactViralLoads as latestvls + LEFT JOIN Ndwh.Dim.Dimdate AS LatestVLDate + ON latestvlDate.Datekey = latestvls.LatestVLDate1Key + where TRY_CAST(LatestVL2 as float) <200 + OR Latestvl1 IN ( 'undetectable', 'NOT DETECTED', + '0 copies/ml', + 'LDL', + 'Less than Low Detectable Level') +), +Retained as ( + Select + Patientkey, + FacilityKey, + case when artoutcomekey= 6 then 1 Else 0 End as PatientRetained + from NDWH.Fact.FACTART +) + select + confirmed_reported_cases_and_art.PatientKey, + Gender, + AgeLastVisit, + SiteCode, + SDP as PartnerName, + AgencyName, + NewCaseReported, + LinkedToART, + NotLinkedOnART, + DateConfirmedPositive, + CohortYearMonth, + StartARTDate, + AgeatDiagnosis, + case when BaselineCD4 is not null Then 1 Else 0 End as WithBaselineCD4, + case when BaselineCD4 is null Then 1 Else 0 End as WithoutBaselineCD4, + case when BaselineCD4 is not null and Try_CONVERT(FLOAT, BaselineCD4) < 200 Then 1 Else 0 End as CD4Lessthan200, + case when BaselineCD4 is not null and Try_CONVERT(FLOAT, BaselineCD4) >= 200 Then 1 Else 0 End as CD4Morethan200, +CASE + WHEN + (ISNUMERIC(OtherCD4s) = 1 AND TRY_CONVERT(float, OtherCD4s) IS NOT NULL AND TRY_CONVERT(float, OtherCD4s) < 200) + OR + (AgeatDiagnosis <= 5 AND ISNUMERIC(OtherCD4Percent) = 1 AND TRY_CONVERT(float, OtherCD4Percent) IS NOT NULL AND TRY_CONVERT(float, OtherCD4Percent) < 25) + THEN 1 + ELSE 0 + END AS AHD, + WHOStageATART, + AgeAtARTStart, + age.DATIMAgeGroup as ARTStartAgeGroup, + case when InitialViralLoads.patientkey is not null then 1 Else 0 End as WithInitialViralLoad, + case when InitialViralLoads.patientkey is null then 1 Else 0 End as WithoutInitialViralLoad, + coalesce(InitialViralLoads.IsSuppressedInitialViralload,0) As IsSuppressedInitialViralload, + case when FirstFollowupViralloads.patientkey is not null then 1 Else 0 End As WithFirstFollowupViralload, + coalesce (FirstFollowupViralloads.IsSuppressedFirstFollowupViralloads,0) as IsSuppressedFirstFollowupViralloads, + case when SecondFollowupViralloads.patientkey is not null then 1 Else 0 End As WithSecondFollowupViralloads, + coalesce (SecondFollowupViralloads.IsSuppressedSecondFollowupViralloads,0) As IssuppressedSecondFollowupViralloads, + case when WHOStageATART is null then 1 Else 0 End as NotStaged, + County, + SubCounty, + coalesce (RegimenChanged,0) as RegimenChanged, + case when RegimenChanged=0 Then 1 else 0 End as RegimenNotChanged, + OptimizedRegimen, + case when LatestVL1 is not null then 1 Else 0 End as TreatmentFailure, + case when LatestVLSuppressed is not null then 1 Else 0 End as LatestVLSuppressed, + case when LatestVLSuppressed is null then 1 Else 0 End as LatestVLNotSuppressed, + coalesce (PatientRetained,0) as PatientRetained, + case when PatientRetained=0 then 1 Else 0 End as PatientNotRetained + into [HIVCaseSurveillance].[dbo].[CsSentinelEvents] + from confirmed_reported_cases_and_art + left join BaselineCD4s on BaselineCD4s.PatientKey=confirmed_reported_cases_and_art.PatientKey + left join BaselineWHO on BaselineWHO.patientkey=confirmed_reported_cases_and_art.PatientKey + left join NDWH.Dim.DimAgeGroup age on age.AgeGroupKey = confirmed_reported_cases_and_art.AgeGroupKey + left join NDWH.Dim.DimFacility as facility on facility.MFLCode = confirmed_reported_cases_and_art.SiteCode + left join MFL_partner_agency_combination on MFL_partner_agency_combination.MFL_Code = confirmed_reported_cases_and_art.SiteCode + left join NDWH.Dim.DimPartner as partner on partner.PartnerName = MFL_partner_agency_combination.SDP + left join NDWH.Dim.DimAgency as agency on agency.AgencyName = MFL_partner_agency_combination.Agency + left join OtherCD4s on OtherCD4s.Patientkey=confirmed_reported_cases_and_art.Patientkey + left join InitialViralLoads on InitialViralLoads.patientkey=confirmed_reported_cases_and_art.PatientKey + left join FirstFollowupViralloads on FirstFollowupViralloads.patientkey=confirmed_reported_cases_and_art.PatientKey + left join SecondFollowupViralloads on SecondFollowupViralloads.patientkey=confirmed_reported_cases_and_art.PatientKey + left join RegimenChanges on RegimenChanges.Patientkey=confirmed_reported_cases_and_art.PatientKey + left join OptimizedRegimen on OptimizedRegimen.Patientkey=confirmed_reported_cases_and_art.PatientKey + left join ConsecutiveHighVls on ConsecutiveHighVls.PatientKey=confirmed_reported_cases_and_art.PatientKey + left join LatestSuppressedVL on LatestSuppressedVL.PatientKey=confirmed_reported_cases_and_art.PatientKey + left join Retained on Retained.Patientkey=confirmed_reported_cases_and_art.PatientKey + + end