From f8c858d4349cd577e4bfc66ecbb855c6b5e1ce17 Mon Sep 17 00:00:00 2001 From: Marymary-dev Date: Tue, 2 Jul 2024 22:18:23 +0300 Subject: [PATCH 01/21] Adding the model for HIV Advanced Disease using asofDate --- .../load_csaggregate_HIVAdvancedDisease.sql | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Scripts/REPORTING/load_csaggregate_HIVAdvancedDisease.sql diff --git a/Scripts/REPORTING/load_csaggregate_HIVAdvancedDisease.sql b/Scripts/REPORTING/load_csaggregate_HIVAdvancedDisease.sql new file mode 100644 index 00000000..d42fa66c --- /dev/null +++ b/Scripts/REPORTING/load_csaggregate_HIVAdvancedDisease.sql @@ -0,0 +1,76 @@ +If Object_id(N'HIVCaseSurveillance.dbo.CsAggregateAdvanceHIVDisease', N'U') Is Not Null + Drop Table Hivcasesurveillance.Dbo.CsAggregateAdvanceHIVDisease; + +WITH VisitData AS ( + SELECT + pat.PatientKey, + facility.FacilityName, + partner.PartnerName, + agency.AgencyName, + pat.Gender, + County, + SubCounty, + EOMONTH (DateConfirmedHIVPositiveKey) As CohortYearMonth, + visits.WHOStage, + EOMONTH(VisitDateKey) AS AsofDate + FROM NDWH.dbo.FactVisits as visits +left join NDWH.dbo.DimFacility as facility on facility.FacilityKey = visits.FacilityKey +Left join NDWH.dbo.DimPatient as pat on pat.PatientKey=visits.PatientKey +left join NDWH.dbo.DimPartner as partner on partner.PartnerKey = visits.PartnerKey +left join NDWH.dbo.DimAgency as agency on agency.AgencyKey = visits.AgencyKey + WHERE visits.PatientKey is not null + +), +RankedVisits AS ( + SELECT + VisitData.PatientKey, + FacilityName, + PartnerName, + AgencyName, + Gender, + County, + SubCounty, + AsofDate, + CohortYearMonth, + VisitData.WHOStage, + ROW_NUMBER() OVER (PARTITION BY PatientKey, AsofDate ORDER BY AsofDate DESC) AS VisitRank + FROM VisitData +), +LatestVisits AS ( + SELECT + PatientKey, + FacilityName, + PartnerName, + AgencyName, + Gender, + County, + SubCounty, + AsofDate, + CohortYearMonth, + WHOStage + FROM RankedVisits + WHERE VisitRank = 1 +) +SELECT + AsofDate, + CohortYearMonth, + FacilityName + Partnername, + AgencyName, + Gender, + County, + SubCounty, + WHOStage, + COUNT(*) AS WHOStageCount + into Hivcasesurveillance.dbo.CsAggregateAdvanceHIVDisease +FROM LatestVisits +GROUP BY + AsofDate, + CohortYearMonth, + FacilityName, + PartnerName, + AgencyName, + Gender, + County, + SubCounty, + WHOStage From de452c50b173383c98ea08b8939a714dc35bf618 Mon Sep 17 00:00:00 2001 From: Marymary-dev Date: Mon, 15 Jul 2024 18:26:58 +0300 Subject: [PATCH 02/21] Adding linelist AHD --- .../REPORTING/load_CsadvancedHIVDisease.sql | 101 ++++++++++++++++++ .../load_csaggregate_HIVAdvancedDisease.sql | 76 ------------- 2 files changed, 101 insertions(+), 76 deletions(-) create mode 100644 Scripts/REPORTING/load_CsadvancedHIVDisease.sql delete mode 100644 Scripts/REPORTING/load_csaggregate_HIVAdvancedDisease.sql diff --git a/Scripts/REPORTING/load_CsadvancedHIVDisease.sql b/Scripts/REPORTING/load_CsadvancedHIVDisease.sql new file mode 100644 index 00000000..836331bb --- /dev/null +++ b/Scripts/REPORTING/load_CsadvancedHIVDisease.sql @@ -0,0 +1,101 @@ +If Object_id(N'HIVCaseSurveillance.dbo.CsLinelistAdvanceHIVDisease', N'U') Is + Not + Null + Drop Table Hivcasesurveillance.Dbo.Cslinelistadvancehivdisease; + +With Visitdata + As (Select Facilityname, + Partnername, + Agencyname, + County, + Subcounty, + Visits.Patientkey, + Whostage, + Gender, + Try_convert(Date, Visitdatekey) As VisitDate, + Eomonth(Try_convert(Date, Visitdatekey)) As AsofDate, + Datediff(Year, Try_convert(Date, Pat.Dob), + Try_convert(Date, Eomonth(Visitdatekey))) As Age + From Ndwh.Dbo.Facthistoricalvisits As Visits + Left Join Ndwh.Dbo.Dimpatient As Pat + On Pat.Patientkey = Visits.Patientkey + Left Join Ndwh.Dbo.Dimfacility As Facility + On Facility.Facilitykey = Visits.Facilitykey + Left Join Ndwh.Dbo.Dimpartner As Partner + On Partner.Partnerkey = Visits.Partnerkey + Left Join Ndwh.Dbo.Dimagency As Agency + On Agency.Agencykey = Visits.Agencykey + Where Visits.Patientkey Is Not Null), + Rankedvisits + As (Select Visitdata.Patientkey, + Facilityname, + Partnername, + Agencyname, + County, + Subcounty, + Asofdate, + Gender, + Visitdata.Whostage, + Age, + Row_number() + Over ( + Partition By Patientkey, Asofdate + Order By Asofdate Desc) As VisitRank + From Visitdata), + Latestvisits + As (Select Patientkey, + Facilityname, + Partnername, + Agencyname, + County, + Subcounty, + Asofdate, + Whostage, + Gender, + Age + From Rankedvisits As Latestvisits + Where Visitrank = 1), + Cd4s + As (Select Patientkey, + Lastcd4, + Lastcd4date + From Ndwh.Dbo.Factcd4) +Select Visits.Patientkey, + Asofdate, + Facilityname, + Partnername, + Agencyname, + County, + Subcounty, + Whostage, + Visits.Gender, + Eomonth(Dateconfirmed.Date)As CohortYearMonth, + Visits.Age, + Age.Datimagegroup, + Case + When ( Visits.Age >= 5 + And Visits.Whostage In ( 3, 4 ) ) + Or Visits.Age < 5 + Or ( Visits.Age >= 5 + And Convert(Float, Cd4s.Lastcd4) < 200 ) Then 1 + Else 0 + End As AHD, + Case + When Visits.Whostage In ( 3, 4 ) Then 1 + Else 0 + End As WhoStage3and4, + Case + When Visits.Age >= 5 + And Convert(Float, Cd4s.Lastcd4) < 200 Then 1 + Else 0 + End As CD4Lessthan200 +Into Hivcasesurveillance.Dbo.Cslinelistadvancehivdisease +From Latestvisits As Visits + Left Join Cd4s + On Cd4s.Patientkey = Visits.Patientkey + Left Join Ndwh.Dbo.Dimpatient As Pat + On Pat.Patientkey = Visits.Patientkey + Left Join Ndwh.Dbo.Dimdate As Dateconfirmed + On Dateconfirmed.Datekey = Pat.Dateconfirmedhivpositivekey + Left Join Ndwh.Dbo.Dimagegroup Age + On Age.Age = Visits.Age \ No newline at end of file diff --git a/Scripts/REPORTING/load_csaggregate_HIVAdvancedDisease.sql b/Scripts/REPORTING/load_csaggregate_HIVAdvancedDisease.sql deleted file mode 100644 index d42fa66c..00000000 --- a/Scripts/REPORTING/load_csaggregate_HIVAdvancedDisease.sql +++ /dev/null @@ -1,76 +0,0 @@ -If Object_id(N'HIVCaseSurveillance.dbo.CsAggregateAdvanceHIVDisease', N'U') Is Not Null - Drop Table Hivcasesurveillance.Dbo.CsAggregateAdvanceHIVDisease; - -WITH VisitData AS ( - SELECT - pat.PatientKey, - facility.FacilityName, - partner.PartnerName, - agency.AgencyName, - pat.Gender, - County, - SubCounty, - EOMONTH (DateConfirmedHIVPositiveKey) As CohortYearMonth, - visits.WHOStage, - EOMONTH(VisitDateKey) AS AsofDate - FROM NDWH.dbo.FactVisits as visits -left join NDWH.dbo.DimFacility as facility on facility.FacilityKey = visits.FacilityKey -Left join NDWH.dbo.DimPatient as pat on pat.PatientKey=visits.PatientKey -left join NDWH.dbo.DimPartner as partner on partner.PartnerKey = visits.PartnerKey -left join NDWH.dbo.DimAgency as agency on agency.AgencyKey = visits.AgencyKey - WHERE visits.PatientKey is not null - -), -RankedVisits AS ( - SELECT - VisitData.PatientKey, - FacilityName, - PartnerName, - AgencyName, - Gender, - County, - SubCounty, - AsofDate, - CohortYearMonth, - VisitData.WHOStage, - ROW_NUMBER() OVER (PARTITION BY PatientKey, AsofDate ORDER BY AsofDate DESC) AS VisitRank - FROM VisitData -), -LatestVisits AS ( - SELECT - PatientKey, - FacilityName, - PartnerName, - AgencyName, - Gender, - County, - SubCounty, - AsofDate, - CohortYearMonth, - WHOStage - FROM RankedVisits - WHERE VisitRank = 1 -) -SELECT - AsofDate, - CohortYearMonth, - FacilityName - Partnername, - AgencyName, - Gender, - County, - SubCounty, - WHOStage, - COUNT(*) AS WHOStageCount - into Hivcasesurveillance.dbo.CsAggregateAdvanceHIVDisease -FROM LatestVisits -GROUP BY - AsofDate, - CohortYearMonth, - FacilityName, - PartnerName, - AgencyName, - Gender, - County, - SubCounty, - WHOStage From c6d12c742b7c023e2aaba2c9480c39016c5a01f2 Mon Sep 17 00:00:00 2001 From: Marymary-dev Date: Fri, 19 Jul 2024 21:07:17 +0300 Subject: [PATCH 03/21] Adding Linelist for reported cases risks --- .../load_CsLinelistReportedCasesRisks.sql | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 Scripts/REPORTING/HIVCaseSurveillance/load_CsLinelistReportedCasesRisks.sql diff --git a/Scripts/REPORTING/HIVCaseSurveillance/load_CsLinelistReportedCasesRisks.sql b/Scripts/REPORTING/HIVCaseSurveillance/load_CsLinelistReportedCasesRisks.sql new file mode 100644 index 00000000..111ceae9 --- /dev/null +++ b/Scripts/REPORTING/HIVCaseSurveillance/load_CsLinelistReportedCasesRisks.sql @@ -0,0 +1,144 @@ +--truncate table first +truncate table [HIVCaseSurveillance].[dbo].[CsLinelistReportedCasesRisks]; + +--declare start and end dates i.e. within the last 12 months form reporting period +declare @start_date date; +select @start_date = dateadd(month, -11, eomonth(dateadd(month, -1, getdate()))); + +declare @end_date date; +select @end_date = eomonth(dateadd(month, -1, getdate())); + + +--- create a temp table to store end of month for each month +with dates as ( + + select datefromparts(year(@start_date), month(@start_date), 1) as dte + union all + select dateadd(month, 1, dte) --incrementing month by month until the date is less than or equal to @end_date + from dates + where dateadd(month, 1, dte) <= @end_date + ) +select + eomonth(dte) as end_date +into #months +from dates +option (maxrecursion 0); + +--declare as of date +declare @as_of_date as date; + +--declare cursor +declare cursor_AsOfDates cursor for +select * from #months + +open cursor_AsOfDates + +fetch next from cursor_AsOfDates into @as_of_date +while @@FETCH_STATUS = 0 + +begin + + with confirmed_reported_cases_and_art as ( + select + art.PatientKey, + patient.Gender, + art.AgeLastVisit, + art.FacilityKey, + PartnerKey, + AgencyKey, + 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,patient.DOB,confirmed_date.Date) as AgeatDiagnosis + from NDWH.dbo.FACTART as art + left join NDWH.dbo.DimPatient as patient on patient.PatientKey = art.PatientKey + left join NDWH.dbo.DimDate as confirmed_date on confirmed_date.DateKey = patient.DateConfirmedHIVPositiveKey + left join NDWH.dbo.DimDate as art_date on art_date.DateKey = art.StartARTDateKey + + ), + RiskFactors as ( + Select + PatientKey, + row_number() OVER (PARTITION BY Patientkey ORDER BY VisitDatekey DESC) AS NUM, + case when MultiplePartners= 'YES' Then 1 Else 0 End as HasMultiplePartners, + case when NumberOfPartners is not null then NumberOfPartners Else 0 End as NumerofSexualPartners, + case when HIVRiskCategory in ('High','Moderate','Very high') Then 1 Else 0 End as HTSHighRiskCategory + from NDWH.dbo.FactHTSEligibilityextract as eligibility + where PatientKey is not null + ), + LatestRiskFactors as ( + Select + PatientKey, + HasMultiplePartners, + NumerofSexualPartners, + HTSHighRiskCategory + from RiskFactors + where NUM=1 + ), + PrepVisits as ( + SELECT + PatientKey, + row_number() OVER (PARTITION BY Patientkey ORDER BY VisitDatekey DESC) AS NUM + from NDWH.dbo.FactPrepVisits + ), + LatestPrepVisits as ( + Select + PatientKey + from PrepVisits + where NUM=1 + ), + PBFWNotOnART as ( + SELECT + Patientkey, + startregimen, + startartdatekey + from NDWH.dbo.FactART + where ispbfwatconfirmationpositive=1 and (startartdatekey is null and startregimen is null) + ) , + InfantsNotOnProphylaxis as ( + SELECT + PatientKey + from ndwh.dbo.FactHEI + where onprohylaxis <> 1 + ) + select + confirmed_reported_cases_and_art.PatientKey, + Gender, + AgeLastVisit, + FacilityName, + PartnerName, + AgencyName, + CohortYearMonth, + StartARTDate, + HasMultiplePartners, + NumerofSexualPartners, + HTSHighRiskCategory, + case when LatestPrepVisits.PatientKey is not null then 1 else 0 End as Secoronverted, + case when AgeatDiagnosis <15 Then 1 Else 0 End as IsChild, + case when PBFWNotOnART.patientkey is not null then 1 Else 0 end as PbfwNotOnART, + case when InfantsNotOnProphylaxis.patientkey is not null then 1 else 0 End as InfantsNotOnProphylaxis + into [HIVCaseSurveillance].[dbo].[CsLinelistReportedCasesRisks] + from confirmed_reported_cases_and_art + left join LatestRiskFactors as risks on confirmed_reported_cases_and_art.PatientKey = risks.PatientKey + left join NDWH.dbo.DimFacility as facility on facility.FacilityKey=confirmed_reported_cases_and_art.FacilityKey + left join NDWH.dbo.DimPartner as partner on partner.PartnerKey=confirmed_reported_cases_and_art.PartnerKey + left join NDWH.dbo.DimAgency as agency on agency.AgencyKey=confirmed_reported_cases_and_art.AgencyKey + left join LatestPrepVisits on LatestPrepVisits.PatientKey=confirmed_reported_cases_and_art.PatientKey + left join PBFWNotOnART on PBFWNotOnART.Patientkey=confirmed_reported_cases_and_art.Patientkey + left join InfantsNotOnProphylaxis on InfantsNotOnProphylaxis.patientkey=confirmed_reported_cases_and_art.PatientKey + + + + + fetch next from cursor_AsOfDates into @as_of_date + +end + +--free up objects +--drop table #months; +--close cursor_AsOfDates; +--deallocate cursor_AsOfDates; +--drop table [HIVCaseSurveillance].[dbo].[CsLinelistReportedCasesRisks] + From d2f14b6d75652ebdeef430bdff7322832db380b6 Mon Sep 17 00:00:00 2001 From: Marymary-dev Date: Mon, 22 Jul 2024 22:14:40 +0300 Subject: [PATCH 04/21] Removing the cursor for asofdates --- .../load_CsLinelistReportedCasesRisks.sql | 48 +------------------ 1 file changed, 2 insertions(+), 46 deletions(-) diff --git a/Scripts/REPORTING/HIVCaseSurveillance/load_CsLinelistReportedCasesRisks.sql b/Scripts/REPORTING/HIVCaseSurveillance/load_CsLinelistReportedCasesRisks.sql index 111ceae9..e4f1d33d 100644 --- a/Scripts/REPORTING/HIVCaseSurveillance/load_CsLinelistReportedCasesRisks.sql +++ b/Scripts/REPORTING/HIVCaseSurveillance/load_CsLinelistReportedCasesRisks.sql @@ -1,43 +1,6 @@ ---truncate table first -truncate table [HIVCaseSurveillance].[dbo].[CsLinelistReportedCasesRisks]; - ---declare start and end dates i.e. within the last 12 months form reporting period -declare @start_date date; -select @start_date = dateadd(month, -11, eomonth(dateadd(month, -1, getdate()))); - -declare @end_date date; -select @end_date = eomonth(dateadd(month, -1, getdate())); - - ---- create a temp table to store end of month for each month -with dates as ( - - select datefromparts(year(@start_date), month(@start_date), 1) as dte - union all - select dateadd(month, 1, dte) --incrementing month by month until the date is less than or equal to @end_date - from dates - where dateadd(month, 1, dte) <= @end_date - ) -select - eomonth(dte) as end_date -into #months -from dates -option (maxrecursion 0); - ---declare as of date -declare @as_of_date as date; - ---declare cursor -declare cursor_AsOfDates cursor for -select * from #months - -open cursor_AsOfDates - -fetch next from cursor_AsOfDates into @as_of_date -while @@FETCH_STATUS = 0 - -begin +IF OBJECT_ID(N'[HIVCaseSurveillance].[dbo].[CsLinelistReportedCasesRisks]', N'U') IS NOT NULL + DROP TABLE [HIVCaseSurveillance].[dbo].[CsLinelistReportedCasesRisks]; with confirmed_reported_cases_and_art as ( select art.PatientKey, @@ -132,13 +95,6 @@ begin - fetch next from cursor_AsOfDates into @as_of_date -end ---free up objects ---drop table #months; ---close cursor_AsOfDates; ---deallocate cursor_AsOfDates; ---drop table [HIVCaseSurveillance].[dbo].[CsLinelistReportedCasesRisks] From 81b52eba33f9b8462c6ce160d412d2af2d76f093 Mon Sep 17 00:00:00 2001 From: NewtonMutugi Date: Tue, 23 Jul 2024 10:43:39 +0300 Subject: [PATCH 05/21] Move retired scripts to Remove_CT_ODS_Duplicates --- .../Remove_CT_ODS_Duplicates/{Retired => }/CT_ARTPatient.sql | 0 .../Remove_CT_ODS_Duplicates/{Retired => }/CT_AdverseEvents.sql | 0 .../{Retired => }/CT_AllergiesChronicIllness.sql | 0 .../Remove_CT_ODS_Duplicates/{Retired => }/CT_CancerScreening.sql | 0 .../Remove_CT_ODS_Duplicates/{Retired => }/CT_ContactListing.sql | 0 .../CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_Covid.sql | 0 .../{Retired => }/CT_DefaulterTracing.sql | 0 .../{Retired => }/CT_DepressionScreening.sql | 0 .../{Retired => }/CT_DrugAlcoholScreening.sql | 0 .../{Retired => }/CT_EnhancedAdherenceCouselling.sql | 0 .../Remove_CT_ODS_Duplicates/{Retired => }/CT_GbvScreening.sql | 0 .../CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_Ipt.sql | 0 .../CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_Otz.sql | 0 .../CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_Ovc.sql | 0 .../{Retired => }/CT_PatientBaselines.sql | 0 .../Remove_CT_ODS_Duplicates/{Retired => }/CT_PatientLabs.sql | 0 .../Remove_CT_ODS_Duplicates/{Retired => }/CT_PatientPharmacy.sql | 0 .../Remove_CT_ODS_Duplicates/{Retired => }/CT_PatientStatus.sql | 0 .../Remove_CT_ODS_Duplicates/{Retired => }/CT_PatientVisits.sql | 0 .../Remove_CT_ODS_Duplicates/{Retired => }/CT_Relationships.sql | 0 20 files changed, 0 insertions(+), 0 deletions(-) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_ARTPatient.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_AdverseEvents.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_AllergiesChronicIllness.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_CancerScreening.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_ContactListing.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_Covid.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_DefaulterTracing.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_DepressionScreening.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_DrugAlcoholScreening.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_EnhancedAdherenceCouselling.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_GbvScreening.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_Ipt.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_Otz.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_Ovc.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_PatientBaselines.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_PatientLabs.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_PatientPharmacy.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_PatientStatus.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_PatientVisits.sql (100%) rename Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/{Retired => }/CT_Relationships.sql (100%) diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_ARTPatient.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_ARTPatient.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_ARTPatient.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_ARTPatient.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_AdverseEvents.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_AdverseEvents.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_AdverseEvents.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_AdverseEvents.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_AllergiesChronicIllness.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_AllergiesChronicIllness.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_AllergiesChronicIllness.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_AllergiesChronicIllness.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_CancerScreening.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_CancerScreening.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_CancerScreening.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_CancerScreening.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_ContactListing.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_ContactListing.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_ContactListing.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_ContactListing.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_Covid.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_Covid.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_Covid.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_Covid.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_DefaulterTracing.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_DefaulterTracing.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_DefaulterTracing.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_DefaulterTracing.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_DepressionScreening.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_DepressionScreening.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_DepressionScreening.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_DepressionScreening.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_DrugAlcoholScreening.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_DrugAlcoholScreening.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_DrugAlcoholScreening.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_DrugAlcoholScreening.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_EnhancedAdherenceCouselling.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_EnhancedAdherenceCouselling.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_EnhancedAdherenceCouselling.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_EnhancedAdherenceCouselling.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_GbvScreening.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_GbvScreening.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_GbvScreening.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_GbvScreening.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_Ipt.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_Ipt.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_Ipt.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_Ipt.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_Otz.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_Otz.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_Otz.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_Otz.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_Ovc.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_Ovc.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_Ovc.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_Ovc.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_PatientBaselines.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_PatientBaselines.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_PatientBaselines.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_PatientBaselines.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_PatientLabs.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_PatientLabs.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_PatientLabs.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_PatientLabs.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_PatientPharmacy.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_PatientPharmacy.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_PatientPharmacy.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_PatientPharmacy.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_PatientStatus.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_PatientStatus.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_PatientStatus.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_PatientStatus.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_PatientVisits.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_PatientVisits.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_PatientVisits.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_PatientVisits.sql diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_Relationships.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_Relationships.sql similarity index 100% rename from Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/Retired/CT_Relationships.sql rename to Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_Relationships.sql From 68ec547988bab7bdcb302d5ff3111f1ede35203e Mon Sep 17 00:00:00 2001 From: Marymary-dev Date: Tue, 23 Jul 2024 12:09:35 +0300 Subject: [PATCH 06/21] Adding cursor to the line list and filtering using the visit dates --- .../REPORTING/load_CsadvancedHIVDisease.sql | 256 +++++++++++------- 1 file changed, 156 insertions(+), 100 deletions(-) diff --git a/Scripts/REPORTING/load_CsadvancedHIVDisease.sql b/Scripts/REPORTING/load_CsadvancedHIVDisease.sql index 836331bb..cc0ae540 100644 --- a/Scripts/REPORTING/load_CsadvancedHIVDisease.sql +++ b/Scripts/REPORTING/load_CsadvancedHIVDisease.sql @@ -1,101 +1,157 @@ -If Object_id(N'HIVCaseSurveillance.dbo.CsLinelistAdvanceHIVDisease', N'U') Is - Not - Null - Drop Table Hivcasesurveillance.Dbo.Cslinelistadvancehivdisease; -With Visitdata - As (Select Facilityname, - Partnername, - Agencyname, - County, - Subcounty, - Visits.Patientkey, - Whostage, - Gender, - Try_convert(Date, Visitdatekey) As VisitDate, - Eomonth(Try_convert(Date, Visitdatekey)) As AsofDate, - Datediff(Year, Try_convert(Date, Pat.Dob), - Try_convert(Date, Eomonth(Visitdatekey))) As Age - From Ndwh.Dbo.Facthistoricalvisits As Visits - Left Join Ndwh.Dbo.Dimpatient As Pat - On Pat.Patientkey = Visits.Patientkey - Left Join Ndwh.Dbo.Dimfacility As Facility - On Facility.Facilitykey = Visits.Facilitykey - Left Join Ndwh.Dbo.Dimpartner As Partner - On Partner.Partnerkey = Visits.Partnerkey - Left Join Ndwh.Dbo.Dimagency As Agency - On Agency.Agencykey = Visits.Agencykey - Where Visits.Patientkey Is Not Null), - Rankedvisits - As (Select Visitdata.Patientkey, - Facilityname, - Partnername, - Agencyname, - County, - Subcounty, - Asofdate, - Gender, - Visitdata.Whostage, - Age, - Row_number() - Over ( - Partition By Patientkey, Asofdate - Order By Asofdate Desc) As VisitRank - From Visitdata), - Latestvisits - As (Select Patientkey, - Facilityname, - Partnername, - Agencyname, - County, - Subcounty, - Asofdate, - Whostage, - Gender, - Age - From Rankedvisits As Latestvisits - Where Visitrank = 1), - Cd4s - As (Select Patientkey, - Lastcd4, - Lastcd4date - From Ndwh.Dbo.Factcd4) -Select Visits.Patientkey, - Asofdate, - Facilityname, - Partnername, - Agencyname, - County, - Subcounty, - Whostage, - Visits.Gender, - Eomonth(Dateconfirmed.Date)As CohortYearMonth, - Visits.Age, - Age.Datimagegroup, - Case - When ( Visits.Age >= 5 - And Visits.Whostage In ( 3, 4 ) ) - Or Visits.Age < 5 - Or ( Visits.Age >= 5 - And Convert(Float, Cd4s.Lastcd4) < 200 ) Then 1 - Else 0 - End As AHD, - Case - When Visits.Whostage In ( 3, 4 ) Then 1 - Else 0 - End As WhoStage3and4, - Case - When Visits.Age >= 5 - And Convert(Float, Cd4s.Lastcd4) < 200 Then 1 - Else 0 - End As CD4Lessthan200 -Into Hivcasesurveillance.Dbo.Cslinelistadvancehivdisease -From Latestvisits As Visits - Left Join Cd4s - On Cd4s.Patientkey = Visits.Patientkey - Left Join Ndwh.Dbo.Dimpatient As Pat - On Pat.Patientkey = Visits.Patientkey - Left Join Ndwh.Dbo.Dimdate As Dateconfirmed - On Dateconfirmed.Datekey = Pat.Dateconfirmedhivpositivekey - Left Join Ndwh.Dbo.Dimagegroup Age - On Age.Age = Visits.Age \ No newline at end of file +--truncate table first +truncate table Hivcasesurveillance.Dbo.Cslinelistadvancehivdisease; + +--declare start and end dates i.e. within the last 12 months form reporting period +declare @start_date date; +select @start_date = dateadd(month, -11, eomonth(dateadd(month, -1, getdate()))); + +declare @end_date date; +select @end_date = eomonth(dateadd(month, -1, getdate())); + + +--- create a temp table to store end of month for each month +with dates as ( + + select datefromparts(year(@start_date), month(@start_date), 1) as dte + union all + select dateadd(month, 1, dte) --incrementing month by month until the date is less than or equal to @end_date + from dates + where dateadd(month, 1, dte) <= @end_date + ) +select + eomonth(dte) as end_date +into #months +from dates +option (maxrecursion 0); + +--declare as of date +declare @as_of_date as date; + +--declare cursor +declare cursor_AsOfDates cursor for +select * from #months + +open cursor_AsOfDates + +fetch next from cursor_AsOfDates into @as_of_date +while @@FETCH_STATUS = 0 + +Begin +With Visitdata As ( + Select + Facilityname, + Partnername, + Agencyname, + County, + Subcounty, + Visits.Patientkey, + Whostage, + Gender, + Try_convert(Date, Visitdatekey) As VisitDate, + Eomonth(Try_convert(Date, Visitdatekey)) As AsofDate, + Datediff(Year, Try_convert(Date, Pat.Dob), Try_convert(Date, Eomonth(Visitdatekey))) As Age + From + Ndwh.Dbo.Facthistoricalvisits As Visits + Left Join Ndwh.Dbo.Dimpatient As Pat On Pat.Patientkey = Visits.Patientkey + Left Join Ndwh.Dbo.Dimfacility As Facility On Facility.Facilitykey = Visits.Facilitykey + Left Join Ndwh.Dbo.Dimpartner As Partner On Partner.Partnerkey = Visits.Partnerkey + Left Join Ndwh.Dbo.Dimagency As Agency On Agency.Agencykey = Visits.Agencykey + Where + Visits.Patientkey Is Not Null +), Rankedvisits As ( + Select + Visitdata.Patientkey, + Facilityname, + Partnername, + Agencyname, + County, + Subcounty, + Asofdate, + VisitDate, + Gender, + Visitdata.Whostage, + Age, + Row_number() Over ( + Partition By Patientkey, Asofdate + Order By Asofdate Desc + ) As VisitRank + From + Visitdata +), Latestvisits As ( + Select + Patientkey, + Facilityname, + Partnername, + Agencyname, + County, + Subcounty, + Asofdate, + VisitDate, + Whostage, + Gender, + Age + From + Rankedvisits + Where + Visitrank = 1 +), Cd4s As ( + Select + Patientkey, + Lastcd4, + Lastcd4date + From + Ndwh.Dbo.Factcd4 +) +insert into [HIVCaseSurveillance].[dbo].[Cslinelistadvancehivdisease] + +Select + Visits.Patientkey, + VisitDate, + @as_of_date as AsOfDate, + Facilityname, + Partnername, + Agencyname, + County, + Subcounty, + Whostage, + Visits.Gender, + Eomonth(Dateconfirmed.Date) As CohortYearMonth, + Visits.Age, + Age.Datimagegroup as Agegroup, + Case + When ( Visits.Age >= 5 And Visits.Whostage In ( 3, 4 ) ) + Or Visits.Age < 5 + Or ( Visits.Age >= 5 And Convert(Float, Cd4s.Lastcd4) < 200 ) + Then 1 + Else 0 + End As AHD, + Case + When Visits.Whostage In ( 3, 4 ) + Then 1 + Else 0 + End As WhoStage3and4, + Case + When Visits.Age >= 5 And Convert(Float, Cd4s.Lastcd4) < 200 + Then 1 + Else 0 + End As CD4Lessthan200, + LastCD4Date + +From + Latestvisits As Visits + Left Join Cd4s On Cd4s.Patientkey = Visits.Patientkey + Left Join Ndwh.Dbo.Dimpatient As Pat On Pat.Patientkey = Visits.Patientkey + Left Join Ndwh.Dbo.Dimdate As Dateconfirmed On Dateconfirmed.Datekey = Pat.Dateconfirmedhivpositivekey + Left Join Ndwh.Dbo.Dimagegroup Age On Age.Age = Visits.Age + where VisitDate <= @as_of_date + fetch next from cursor_AsOfDates into @as_of_date + +end + +--free up objects +--drop table #months; +--close cursor_AsOfDates; +--deallocate cursor_AsOfDates +--drop table Hivcasesurveillance.Dbo.Cslinelistadvancehivdisease; + From 6ee47ed29f21eaac47924cb58465b9a900bdccf0 Mon Sep 17 00:00:00 2001 From: NewtonMutugi Date: Tue, 23 Jul 2024 12:30:04 +0300 Subject: [PATCH 07/21] Removed ODS_logs insert statements from ODS CT_Docket scripts --- Scripts/ODS/CT_DOCKET/CT_AdverseEvents.sql | 38 +-- Scripts/ODS/CT_DOCKET/CT_PatientPharmacy.sql | 62 ++--- Scripts/ODS/CT_DOCKET/CT_PatientStatus.sql | 236 +++++++++--------- Scripts/ODS/CT_DOCKET/CT_PatientVisits.sql | 68 ++--- Scripts/ODS/CT_DOCKET/Load_CT_ARTPatient.sql | 58 ++--- .../Load_CT_AllergiesChronicIllness.sql | 74 +++--- Scripts/ODS/CT_DOCKET/Load_CT_Covid.sql | 48 ++-- .../CT_DOCKET/Load_CT_DefaulterTracing.sql | 50 ++-- .../CT_DOCKET/Load_CT_DepressionScreening.sql | 50 ++-- .../Load_CT_DrugAlcoholScreening.sql | 52 ++-- .../Load_CT_EnhancedAdherenceCounselling.sql | 59 +++-- .../ODS/CT_DOCKET/Load_CT_GbvScreening.sql | 52 ++-- Scripts/ODS/CT_DOCKET/Load_CT_Ipt.sql | 54 ++-- Scripts/ODS/CT_DOCKET/Load_CT_Otz.sql | 48 ++-- Scripts/ODS/CT_DOCKET/Load_CT_Ovz.sql | 46 ++-- Scripts/ODS/CT_DOCKET/Load_CT_PatientLabs.sql | 66 ++--- 16 files changed, 530 insertions(+), 531 deletions(-) diff --git a/Scripts/ODS/CT_DOCKET/CT_AdverseEvents.sql b/Scripts/ODS/CT_DOCKET/CT_AdverseEvents.sql index 64b4a88c..8f83b14a 100644 --- a/Scripts/ODS/CT_DOCKET/CT_AdverseEvents.sql +++ b/Scripts/ODS/CT_DOCKET/CT_AdverseEvents.sql @@ -1,5 +1,5 @@ BEGIN ; - + DECLARE @MaxAdverseEventStartDate DATETIME, @AdverseEventStartDate DATETIME, @MaxCreatedDate DATETIME @@ -46,10 +46,10 @@ using(SELECT DISTINCT P.[patientcccnumber] AS PatientID, PA.[date_last_modified], PA.recorduuid, PA.voided, - VoidingSource = Case + VoidingSource = Case when PA.voided = 1 Then 'Source' Else Null - END + END FROM [DWAPICentral].[dbo].[patientextract](nolock) P INNER JOIN [DWAPICentral].[dbo].patientadverseeventextract(nolock) PA ON PA.[patientid] = P.id @@ -72,12 +72,12 @@ using(SELECT DISTINCT P.[patientcccnumber] AS PatientID, ,InnerPA.AdverseEvent ,VisitDate ,InnerPA.voided - ) tm - ON f.code = tm.[SiteCode] and - p.PatientPID=tm.PatientPK and + ) tm + ON f.code = tm.[SiteCode] and + p.PatientPID=tm.PatientPK and PA.VisitDate = tm.VisitDate and cast(PA.created as date) = tm.Maxdatecreated and - PA.ID = tm.Max_ID + PA.ID = tm.Max_ID ) AS b @@ -85,7 +85,7 @@ ON( a.sitecode = b.sitecode AND a.patientpk = b.patientpk AND a.AdverseEvent = b.AdverseEvent AND a.visitdate = b.visitdate AND - a.voided = b.voided + a.voided = b.voided ) WHEN NOT matched THEN INSERT(patientid, @@ -155,14 +155,14 @@ SET loadenddatetime = Getdate() WHERE maxadverseeventstartdate = @AdverseEventStartDate; - INSERT INTO [ODS_Logs].[dbo].[ct_adverseeventcount_log] - ([sitecode], - [createddate], - [adverseeventcount]) - SELECT sitecode, - Getdate(), - Count(Concat(sitecode, patientpk)) AS AdverseEventCount - FROM [ODS].[dbo].[ct_adverseevents] - GROUP BY sitecode; - -END +-- INSERT INTO [ODS_Logs].[dbo].[ct_adverseeventcount_log] +-- ([sitecode], +-- [createddate], +-- [adverseeventcount]) +-- SELECT sitecode, +-- Getdate(), +-- Count(Concat(sitecode, patientpk)) AS AdverseEventCount +-- FROM [ODS].[dbo].[ct_adverseevents] +-- GROUP BY sitecode; + +END diff --git a/Scripts/ODS/CT_DOCKET/CT_PatientPharmacy.sql b/Scripts/ODS/CT_DOCKET/CT_PatientPharmacy.sql index 23fcac5c..c1535533 100644 --- a/Scripts/ODS/CT_DOCKET/CT_PatientPharmacy.sql +++ b/Scripts/ODS/CT_DOCKET/CT_PatientPharmacy.sql @@ -1,21 +1,21 @@ BEGIN - + DECLARE @MaxDispenseDate_Hist DATETIME, @DispenseDate DATETIME, @MaxCreatedDate DATETIME SELECT @MaxDispenseDate_Hist = MAX(MaxDispenseDate) FROM [ODS_logs].[dbo].[CT_PharmacyVisit_Log] (NoLock) - SELECT @DispenseDate = MAX(DispenseDate) FROM [DWAPICentral].[dbo].[PatientPharmacyExtract] WITH (NOLOCK) - + SELECT @DispenseDate = MAX(DispenseDate) FROM [DWAPICentral].[dbo].[PatientPharmacyExtract] WITH (NOLOCK) + INSERT INTO [ODS_logs].[dbo].[CT_PharmacyVisit_Log](MaxDispenseDate,LoadStartDateTime) VALUES(@DispenseDate,GETDATE()) MERGE [ODS].[dbo].[CT_PatientPharmacy] AS a - USING(SELECT Distinct + USING(SELECT Distinct P.[PatientCccNumber] AS PatientID ,P.[PatientPID] AS PatientPK - ,F.[Name] AS FacilityName + ,F.[Name] AS FacilityName ,F.Code AS SiteCode ,PP.[VisitID] As VisitID ,PP.[Drug] As Drug @@ -26,13 +26,13 @@ BEGIN ,PP.[PeriodTaken] As PeriodTaken ,PP.[ProphylaxisType] As ProphylaxisType ,P.[Emr] As Emr - ,CASE P.[Project] - WHEN 'I-TECH' THEN 'Kenya HMIS II' + ,CASE P.[Project] + WHEN 'I-TECH' THEN 'Kenya HMIS II' WHEN 'HMIS' THEN 'Kenya HMIS II' - ELSE P.[Project] - END AS [Project] + ELSE P.[Project] + END AS [Project] ,PP.[Voided] As Voided - ,VoidingSource = Case + ,VoidingSource = Case when PP.voided = 1 Then 'Source' Else Null END @@ -43,13 +43,13 @@ BEGIN ,PP.RegimenChangedSwitched As RegimenChangedSwitched ,PP.RegimenChangeSwitchReason As RegimenChangeSwitchReason ,PP.StopRegimenReason As StopRegimenReason - ,PP.StopRegimenDate As StopRegimenDate + ,PP.StopRegimenDate As StopRegimenDate ,PP.ID ,PP.[Date_Created] ,PP.[Date_Last_Modified] ,PP.RecordUUID - FROM [DWAPICentral].[dbo].[PatientExtract] P - INNER JOIN [DWAPICentral].[dbo].[PatientPharmacyExtract] PP ON PP.[PatientId]= P.ID + FROM [DWAPICentral].[dbo].[PatientExtract] P + INNER JOIN [DWAPICentral].[dbo].[PatientPharmacyExtract] PP ON PP.[PatientId]= P.ID INNER JOIN [DWAPICentral].[dbo].[Facility] F ON P.[FacilityId] = F.Id AND F.Voided=0 INNER JOIN ( SELECT F.code as SiteCode @@ -58,28 +58,28 @@ BEGIN ,InnerPP.DispenseDate ,max(InnerPP.ID) As maxID ,MAX(InnerPP.created )AS Maxdatecreated - FROM [DWAPICentral].[dbo].[PatientExtract] P WITH (NoLock) - INNER JOIN [DWAPICentral].[dbo].[PatientPharmacyExtract] InnerPP WITH(NoLock) ON InnerPP.[PatientId]= P.ID + FROM [DWAPICentral].[dbo].[PatientExtract] P WITH (NoLock) + INNER JOIN [DWAPICentral].[dbo].[PatientPharmacyExtract] InnerPP WITH(NoLock) ON InnerPP.[PatientId]= P.ID INNER JOIN [DWAPICentral].[dbo].[Facility] F WITH(NoLock) ON P.[FacilityId] = F.Id AND F.Voided=0 GROUP BY F.code,p.[PatientPID],InnerPP.voided,InnerPP.DispenseDate - ) tm - ON f.code = tm.[SiteCode] and - p.PatientPID=tm.PatientPK and - PP.voided = tm.voided and + ) tm + ON f.code = tm.[SiteCode] and + p.PatientPID=tm.PatientPK and + PP.voided = tm.voided and PP.created = tm.Maxdatecreated and PP.ID =tm.maxID and PP.DispenseDate = tm.DispenseDate WHERE p.gender!='Unknown' AND F.code >0 - ) AS b + ) AS b ON( a.SiteCode = b.SiteCode and a.PatientPK = b.PatientPK and a.DispenseDate = b.DispenseDate and - a.voided = b.voided + a.voided = b.voided ) - WHEN NOT MATCHED THEN + WHEN NOT MATCHED THEN INSERT( ID ,PatientID @@ -107,7 +107,7 @@ BEGIN ,voided ,VoidingSource ,LoadDate - ) + ) VALUES( ID ,PatientID @@ -136,9 +136,9 @@ BEGIN ,VoidingSource ,Getdate() ) - + WHEN MATCHED THEN - UPDATE SET + UPDATE SET a.PatientID =b.PatientID, a.FacilityName =b.FacilityName, a.PeriodTaken =b.PeriodTaken, @@ -152,16 +152,16 @@ BEGIN a.RecordUUID =b.RecordUUID, a.voided =b.voided; - + UPDATE [ODS_logs].[dbo].[CT_PharmacyVisit_Log] SET LoadEndDateTime = GETDATE() WHERE MaxDispenseDate = @DispenseDate; - INSERT INTO [ODS_logs].[dbo].[CT_PatientPharmacyCount_Log] ([SiteCode],[CreatedDate],[PatientPharmacyCount]) - SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS PatientPharmacyCount - FROM [ODS].[dbo].[CT_PatientPharmacy] - GROUP BY SiteCode; + -- INSERT INTO [ODS_logs].[dbo].[CT_PatientPharmacyCount_Log] ([SiteCode],[CreatedDate],[PatientPharmacyCount]) + -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS PatientPharmacyCount + -- FROM [ODS].[dbo].[CT_PatientPharmacy] + -- GROUP BY SiteCode; + - END diff --git a/Scripts/ODS/CT_DOCKET/CT_PatientStatus.sql b/Scripts/ODS/CT_DOCKET/CT_PatientStatus.sql index 9ee65d67..f74bd20c 100644 --- a/Scripts/ODS/CT_DOCKET/CT_PatientStatus.sql +++ b/Scripts/ODS/CT_DOCKET/CT_PatientStatus.sql @@ -1,96 +1,96 @@ BEGIN; - DECLARE @MaxExitDate_Hist DATETIME, - @ExitDate DATETIME - SELECT - @MaxExitDate_Hist = Max(maxexitdate) + DECLARE @MaxExitDate_Hist DATETIME, + @ExitDate DATETIME + SELECT + @MaxExitDate_Hist = Max(maxexitdate) FROM [ODS_logs].[dbo].[CT_patientStatus_Log] (nolock); - SELECT - @ExitDate = Max(exitdate) + SELECT + @ExitDate = Max(exitdate) FROM [DWAPICentral].[dbo].[patientstatusextract] WITH (nolock); INSERT INTO [ODS_logs].[dbo].[CT_patientStatus_Log] ( - maxexitdate, + maxexitdate, loadstartdatetime - ) - VALUES + ) + VALUES ( - @ExitDate, + @ExitDate, Getdate() ); ---- Refresh [ODS].[dbo].[CT_PatientStatus] MERGE [ODS].[dbo].[ct_patientstatus] AS a using( - SELECT DISTINCT - P.[patientcccnumber] AS PatientID, - P.[patientpid] AS PatientPK, - F.NAME AS FacilityName, - F.code AS SiteCode, - PS.[exitdescription] ExitDescription, - PS.[exitdate] ExitDate, - PS.[exitreason] ExitReason, - P.[emr] Emr, - CASE P.[project] - WHEN 'I-TECH' THEN 'Kenya HMIS II' - WHEN 'HMIS' THEN 'Kenya HMIS II' - ELSE P.[project] - END AS [Project], - PS.[voided] Voided, - VoidingSource = Case + SELECT DISTINCT + P.[patientcccnumber] AS PatientID, + P.[patientpid] AS PatientPK, + F.NAME AS FacilityName, + F.code AS SiteCode, + PS.[exitdescription] ExitDescription, + PS.[exitdate] ExitDate, + PS.[exitreason] ExitReason, + P.[emr] Emr, + CASE P.[project] + WHEN 'I-TECH' THEN 'Kenya HMIS II' + WHEN 'HMIS' THEN 'Kenya HMIS II' + ELSE P.[project] + END AS [Project], + PS.[voided] Voided, + VoidingSource = Case when PS.voided = 1 Then 'Source' Else Null END, - PS.[processed] Processed, - PS.[created] Created, - [reasonfordeath], - [specificdeathreason], - Cast([deathdate] AS DATE) [DeathDate], - effectivediscontinuationdate, - PS.toverified TOVerified, - PS.toverifieddate TOVerifiedDate, - PS.reenrollmentdate ReEnrollmentDate, - PS.[date_created], - PS.[date_last_modified], - PS.[recorduuid] - FROM [DWAPICentral].[dbo].[patientextract] P WITH (nolock) - INNER JOIN [DWAPICentral].[dbo].[patientstatusextract]PS WITH (nolock) ON PS.[patientid] = P.id - INNER JOIN [DWAPICentral].[dbo].[facility] F (nolock) ON P.[facilityid] = F.id - AND F.voided = 0 + PS.[processed] Processed, + PS.[created] Created, + [reasonfordeath], + [specificdeathreason], + Cast([deathdate] AS DATE) [DeathDate], + effectivediscontinuationdate, + PS.toverified TOVerified, + PS.toverifieddate TOVerifiedDate, + PS.reenrollmentdate ReEnrollmentDate, + PS.[date_created], + PS.[date_last_modified], + PS.[recorduuid] + FROM [DWAPICentral].[dbo].[patientextract] P WITH (nolock) + INNER JOIN [DWAPICentral].[dbo].[patientstatusextract]PS WITH (nolock) ON PS.[patientid] = P.id + INNER JOIN [DWAPICentral].[dbo].[facility] F (nolock) ON P.[facilityid] = F.id + AND F.voided = 0 INNER JOIN ( - SELECT - P.patientpid, - F.code, - exitdate, - ps.voided, - max(PS.ID) As Max_ID, + SELECT + P.patientpid, + F.code, + exitdate, + ps.voided, + max(PS.ID) As Max_ID, Max( - Cast(Ps.created AS DATE)) MaxCreated - FROM [DWAPICentral].[dbo].[patientextract] P WITH (nolock) - INNER JOIN [DWAPICentral].[dbo].[patientstatusextract]PS WITH (nolock) ON PS.[patientid] = P.id - INNER JOIN [DWAPICentral].[dbo].[facility] F (nolock) ON P.[facilityid] = F.id - AND F.voided = 0 - GROUP BY - P.patientpid, - F.code, - exitdate, + Cast(Ps.created AS DATE)) MaxCreated + FROM [DWAPICentral].[dbo].[patientextract] P WITH (nolock) + INNER JOIN [DWAPICentral].[dbo].[patientstatusextract]PS WITH (nolock) ON PS.[patientid] = P.id + INNER JOIN [DWAPICentral].[dbo].[facility] F (nolock) ON P.[facilityid] = F.id + AND F.voided = 0 + GROUP BY + P.patientpid, + F.code, + exitdate, ps.voided - ) tn + ) tn ON P.patientpid = tn.patientpid AND f.code = tn.code AND PS.exitdate = tn.exitdate AND Cast(PS.created AS DATE) = tn.maxcreated AND - PS.ID = tn.Max_ID + PS.ID = tn.Max_ID WHERE p.gender != 'Unknown' AND F.code > 0 - ) AS b + ) AS b ON( a.patientpk = b.patientpk AND a.sitecode = b.sitecode AND a.exitdate = b.exitdate AND a.exitreason = b.exitreason AND a.voided = b.voided - ) WHEN NOT matched - THEN + ) WHEN NOT matched + THEN INSERT( patientid ,sitecode @@ -114,68 +114,68 @@ BEGIN; ,loaddate ,Voided ,VoidingSource - ) - VALUES + ) + VALUES ( - patientid, - sitecode, - facilityname, - exitdescription, - exitdate, - exitreason, - patientpk, - emr, - project, - toverified, - toverifieddate, - reenrollmentdate, - deathdate, - effectivediscontinuationdate, - reasonfordeath, - specificdeathreason, - [date_created], - [recorduuid], - [date_last_modified], + patientid, + sitecode, + facilityname, + exitdescription, + exitdate, + exitreason, + patientpk, + emr, + project, + toverified, + toverifieddate, + reenrollmentdate, + deathdate, + effectivediscontinuationdate, + reasonfordeath, + specificdeathreason, + [date_created], + [recorduuid], + [date_last_modified], Voided, VoidingSource, Getdate() - ) - WHEN matched - THEN - UPDATE - SET - a.[patientid] = b.[patientid], - a.[facilityname] = b.[facilityname], - a.[exitdescription] = b.[exitdescription], - a.[exitdate] = b.[exitdate], - a.[exitreason] = b.[exitreason], - a.[emr] = b.[emr], - a.[project] = b.[project], - a.[toverified] = b.[toverified], - a.[toverifieddate] = b.[toverifieddate], - a.[reenrollmentdate] = b.[reenrollmentdate], - a.[reasonfordeath] = b.[reasonfordeath], - a.[specificdeathreason] = b.[specificdeathreason], - a.[deathdate] = b.[deathdate], - a.[effectivediscontinuationdate] = b.[effectivediscontinuationdate], - a.[date_last_modified] = b.[date_last_modified], - a.[date_created] = b.[date_created], - a.[recorduuid] = b.[recorduuid], + ) + WHEN matched + THEN + UPDATE + SET + a.[patientid] = b.[patientid], + a.[facilityname] = b.[facilityname], + a.[exitdescription] = b.[exitdescription], + a.[exitdate] = b.[exitdate], + a.[exitreason] = b.[exitreason], + a.[emr] = b.[emr], + a.[project] = b.[project], + a.[toverified] = b.[toverified], + a.[toverifieddate] = b.[toverifieddate], + a.[reenrollmentdate] = b.[reenrollmentdate], + a.[reasonfordeath] = b.[reasonfordeath], + a.[specificdeathreason] = b.[specificdeathreason], + a.[deathdate] = b.[deathdate], + a.[effectivediscontinuationdate] = b.[effectivediscontinuationdate], + a.[date_last_modified] = b.[date_last_modified], + a.[date_created] = b.[date_created], + a.[recorduuid] = b.[recorduuid], a.[voided] = b.[voided]; - UPDATE - [ODS_logs].[dbo].[CT_patientStatus_Log] - SET loadenddatetime = Getdate() - WHERE + UPDATE + [ODS_logs].[dbo].[CT_patientStatus_Log] + SET loadenddatetime = Getdate() + WHERE maxexitdate = @ExitDate; - INSERT INTO [ODS_logs].[dbo].[CT_PatientStatusCount_Log] - ([sitecode], - [createddate], - [patientstatuscount]) - SELECT sitecode, - Getdate(), - Count(Concat(sitecode, patientpk)) AS PatientStatusCount - FROM [ODS].[dbo].[ct_patientstatus] - GROUP BY sitecode; + -- INSERT INTO [ODS_logs].[dbo].[CT_PatientStatusCount_Log] + -- ([sitecode], + -- [createddate], + -- [patientstatuscount]) + -- SELECT sitecode, + -- Getdate(), + -- Count(Concat(sitecode, patientpk)) AS PatientStatusCount + -- FROM [ODS].[dbo].[ct_patientstatus] + -- GROUP BY sitecode; END diff --git a/Scripts/ODS/CT_DOCKET/CT_PatientVisits.sql b/Scripts/ODS/CT_DOCKET/CT_PatientVisits.sql index 3856e082..ac0f21f2 100644 --- a/Scripts/ODS/CT_DOCKET/CT_PatientVisits.sql +++ b/Scripts/ODS/CT_DOCKET/CT_PatientVisits.sql @@ -1,13 +1,13 @@ BEGIN - + DECLARE @MaxVisitDate_Hist DATETIME, @VisitDate DATETIME, @MaxCreatedDate DATETIME - + SELECT @MaxVisitDate_Hist = MAX(MaxVisitDate) FROM [ODS_Logs].[dbo].[CT_Visit_Log] (NoLock); SELECT @VisitDate = MAX(VisitDate) FROM [DWAPICentral].[dbo].[PatientVisitExtract] WITH (NOLOCK) ; SELECT @MaxCreatedDate = MAX(CreatedDate) FROM [ODS_logs].[dbo].[CT_VisitCount_Log] WITH (NOLOCK) ; - + INSERT INTO [ODS_Logs].[dbo].[CT_Visit_Log](MaxVisitDate,LoadStartDateTime) VALUES(@VisitDate,GETDATE()); @@ -44,21 +44,21 @@ BEGIN ,PV.[NextAppointmentDate] As NextAppointmentDate ,P.[Emr] As Emr ,CASE P.[Project] - WHEN 'I-TECH' THEN 'Kenya HMIS II' + WHEN 'I-TECH' THEN 'Kenya HMIS II' WHEN 'HMIS' THEN 'Kenya HMIS II' - ELSE P.[Project] - END AS [Project] + ELSE P.[Project] + END AS [Project] ,PV.[Voided] As Voided - ,VoidingSource = Case + ,VoidingSource = Case when PV.voided = 1 Then 'Source' Else Null - END + END ,pv.[StabilityAssessment] As StabilityAssessment ,pv.[DifferentiatedCare] As DifferentiatedCare ,pv.[PopulationType]As PopulationType ,pv.[KeyPopulationType] As KeyPopulationType ,PV.[Processed] As Processed - ,PV.[Created] As Created + ,PV.[Created] As Created ,[GeneralExamination] ,[SystemExamination] ,[Skin] @@ -99,8 +99,8 @@ BEGIN ,[WHOStagingOI] ,PV.[AppointmentReminderWillingness] ,PV.[WantsToGetPregnant] - FROM [DWAPICentral].[dbo].[PatientExtract] P WITH (NoLock) - INNER JOIN [DWAPICentral].[dbo].[PatientVisitExtract] PV WITH(NoLock) ON PV.[PatientId]= P.ID + FROM [DWAPICentral].[dbo].[PatientExtract] P WITH (NoLock) + INNER JOIN [DWAPICentral].[dbo].[PatientVisitExtract] PV WITH(NoLock) ON PV.[PatientId]= P.ID INNER JOIN [DWAPICentral].[dbo].[Facility] F WITH(NoLock) ON P.[FacilityId] = F.Id AND F.Voided=0 INNER JOIN ( SELECT F.code as SiteCode @@ -108,33 +108,33 @@ BEGIN ,[VisitId] ,visitDate ,InnerPV.voided, - max(InnerPV.ID) maxID, + max(InnerPV.ID) maxID, MAX(InnerPV.created) AS Maxdatecreated - FROM [DWAPICentral].[dbo].[PatientExtract] P WITH (NoLock) - INNER JOIN [DWAPICentral].[dbo].[PatientVisitExtract] InnerPV WITH(NoLock) ON InnerPV.[PatientId]= P.ID + FROM [DWAPICentral].[dbo].[PatientExtract] P WITH (NoLock) + INNER JOIN [DWAPICentral].[dbo].[PatientVisitExtract] InnerPV WITH(NoLock) ON InnerPV.[PatientId]= P.ID INNER JOIN [DWAPICentral].[dbo].[Facility] F WITH(NoLock) ON P.[FacilityId] = F.Id AND F.Voided=0 GROUP BY F.code ,p.[PatientPID] ,[VisitId] ,visitDate ,InnerPV.voided - ) tm - ON f.code = tm.[SiteCode] and - p.PatientPID=tm.PatientPK and - pv.[VisitId] = tm.[VisitId] and - pv.visitDate = tm.visitDate and - pv.voided = tm.voided and + ) tm + ON f.code = tm.[SiteCode] and + p.PatientPID=tm.PatientPK and + pv.[VisitId] = tm.[VisitId] and + pv.visitDate = tm.visitDate and + pv.voided = tm.voided and pv.created = tm.Maxdatecreated and PV.ID =tm. maxID - WHERE p.gender!='Unknown' AND F.code >0) AS b + WHERE p.gender!='Unknown' AND F.code >0) AS b ON( - a.PatientPK = b.PatientPK + a.PatientPK = b.PatientPK AND a.SiteCode = b.SiteCode AND a.visitID = b.[VisitId] - and a.visitDate = b.visitDate - and a.voided = b.voided + and a.visitDate = b.visitDate + and a.voided = b.voided ) - WHEN NOT MATCHED THEN + WHEN NOT MATCHED THEN INSERT( PatientID ,FacilityName @@ -196,7 +196,7 @@ BEGIN ,[AppointmentReminderWillingness] ,[WantsToGetPregnant] ,LoadDate - ) + ) VALUES( PatientID ,FacilityName @@ -258,9 +258,9 @@ BEGIN ,[WantsToGetPregnant] ,Getdate() ) - + WHEN MATCHED THEN - UPDATE SET + UPDATE SET a.PatientID =b.PatientID, a.[SERVICE] = b.[SERVICE], a.VisitType = b.VisitType, @@ -323,12 +323,12 @@ BEGIN UPDATE [ODS_Logs].[dbo].[CT_Visit_Log] SET LoadEndDateTime = GETDATE() - WHERE MaxVisitDate = @VisitDate; + WHERE MaxVisitDate = @VisitDate; + + -- INSERT INTO [ODS_logs].[dbo].[CT_VisitCount_Log]([SiteCode],[CreatedDate],[VisitCount]) + -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS VisitCount + -- FROM [ODS].[dbo].[CT_PatientVisits] + -- GROUP BY SiteCode; - INSERT INTO [ODS_logs].[dbo].[CT_VisitCount_Log]([SiteCode],[CreatedDate],[VisitCount]) - SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS VisitCount - FROM [ODS].[dbo].[CT_PatientVisits] - GROUP BY SiteCode; - END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_ARTPatient.sql b/Scripts/ODS/CT_DOCKET/Load_CT_ARTPatient.sql index b6bd0674..6a8e2508 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_ARTPatient.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_ARTPatient.sql @@ -27,10 +27,10 @@ BEGIN ,PA.[ExitReason] ,PA.[ExitDate] ,P.[Emr] - ,CASE P.[Project] - WHEN 'I-TECH' THEN 'Kenya HMIS II' + ,CASE P.[Project] + WHEN 'I-TECH' THEN 'Kenya HMIS II' WHEN 'HMIS' THEN 'Kenya HMIS II' - ELSE P.[Project] + ELSE P.[Project] END AS [Project] ,PA.[DOB] ,PA.[PreviousARTUse] @@ -41,38 +41,38 @@ BEGIN ,GETDATE () AS DateAsOf ,PA.RecordUUID ,PA.voided - ,VoidingSource = Case + ,VoidingSource = Case when PA.voided = 1 Then 'Source' Else Null - END - FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[PatientArtExtract](NoLock) PA ON PA.[PatientId]= P.ID - INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided=0 - INNER JOIN (SELECT + END + FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P + INNER JOIN [DWAPICentral].[dbo].[PatientArtExtract](NoLock) PA ON PA.[PatientId]= P.ID + INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided=0 + INNER JOIN (SELECT a.PatientPID ,c.code ,Max(b.ID) As MaxID ,Max(cast(b.created as date))MaxCreated FROM [DWAPICentral].[dbo].[PatientExtract] a with (NoLock) - INNER JOIN [DWAPICentral].[dbo].[PatientArtExtract] b with(NoLock) ON b.[PatientId]= a.ID - INNER JOIN [DWAPICentral].[dbo].[Facility] c with (NoLock) ON a.[FacilityId] = c.Id AND c.Voided=0 + INNER JOIN [DWAPICentral].[dbo].[PatientArtExtract] b with(NoLock) ON b.[PatientId]= a.ID + INNER JOIN [DWAPICentral].[dbo].[Facility] c with (NoLock) ON a.[FacilityId] = c.Id AND c.Voided=0 GROUP BY a.PatientPID ,c.code )tn - ON P.PatientPID = tn.PatientPID and - F.code = tn.code and + ON P.PatientPID = tn.PatientPID and + F.code = tn.code and PA.ID = tn.MaxID and cast(PA.Created as date) = tn.MaxCreated - WHERE p.gender!='Unknown' AND F.code >0) AS b + WHERE p.gender!='Unknown' AND F.code >0) AS b ON( a.PatientPK = b.PatientPK and a.SiteCode = b.SiteCode and a.lastvisit = b.lastvisit and - a.voided = b.voided - + a.voided = b.voided + ) - - WHEN NOT MATCHED THEN + + WHEN NOT MATCHED THEN INSERT( ID @@ -115,7 +115,7 @@ BEGIN ,VoidingSource ,LoadDate ) - + VALUES( ID ,PatientID @@ -159,10 +159,10 @@ BEGIN ) WHEN MATCHED THEN - UPDATE SET + UPDATE SET a.PatientID =b.PatientID, a.[AgeEnrollment] =b.[AgeEnrollment], - a.[AgeARTStart] =b.[AgeARTStart], + a.[AgeARTStart] =b.[AgeARTStart], a.[AgeLastVisit] =b.[AgeLastVisit], a.[FacilityName] =b.[FacilityName], a.[RegistrationDate] =b.[RegistrationDate], @@ -174,15 +174,15 @@ BEGIN a.[StartARTAtThisFacility] =b.[StartARTAtThisFacility], a.[StartRegimen] =b.[StartRegimen], a.[StartRegimenLine] =b.[StartRegimenLine], - a.[LastARTDate] =b.[LastARTDate], - a.[LastRegimen] =b.[LastRegimen], - a.[LastRegimenLine] =b.[LastRegimenLine], + a.[LastARTDate] =b.[LastARTDate], + a.[LastRegimen] =b.[LastRegimen], + a.[LastRegimenLine] =b.[LastRegimenLine], a.[Duration] =b.[Duration], a.[ExpectedReturn] =b.[ExpectedReturn], a.[Provider] =b.[Provider], a.[ExitReason] =b.[ExitReason] , a.[ExitDate] =b.[ExitDate], - a.[Emr] =b.[Emr], + a.[Emr] =b.[Emr], a.[PreviousARTUse] =b.[PreviousARTUse] , a.[PreviousARTPurpose] =b.[PreviousARTPurpose], a.[DateLastUsed] =b.[DateLastUsed], @@ -192,9 +192,9 @@ BEGIN a.RecordUUID =b.RecordUUID, a.voided =b.voided; - INSERT INTO [ODS_logs].[dbo].[CT_ARTPatientsCount_Log]([SiteCode],[CreatedDate],ARTPatientsCount) - SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS PatientStatusCount - FROM [ODS].[dbo].[CT_ARTPatients] - group by SiteCode + -- INSERT INTO [ODS_logs].[dbo].[CT_ARTPatientsCount_Log]([SiteCode],[CreatedDate],ARTPatientsCount) + -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS PatientStatusCount + -- FROM [ODS].[dbo].[CT_ARTPatients] + -- group by SiteCode END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_AllergiesChronicIllness.sql b/Scripts/ODS/CT_DOCKET/Load_CT_AllergiesChronicIllness.sql index 5f59e031..bc08db25 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_AllergiesChronicIllness.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_AllergiesChronicIllness.sql @@ -1,9 +1,9 @@ BEGIN - --;with cte AS ( Select - -- P.PatientPID, - -- ACI.PatientId, + --;with cte AS ( Select + -- P.PatientPID, + -- ACI.PatientId, -- F.code, -- ACI.VisitID, -- ACI.VisitDate, @@ -13,24 +13,24 @@ BEGIN -- INNER JOIN [DWAPICentral].[dbo].[AllergiesChronicIllnessExtract](NoLock) ACI ON ACI.[PatientId] = P.ID AND ACI.Voided = 0 -- INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 - -- WHERE P.gender != 'Unknown') - + -- WHERE P.gender != 'Unknown') + -- delete ACI from [DWAPICentral].[dbo].[AllergiesChronicIllnessExtract] (NoLock) ACI - -- inner join [DWAPICentral].[dbo].[PatientExtract](NoLock) P ON ACI.[PatientId]= P.ID AND ACI.Voided = 0 - -- inner join [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided=0 - -- inner join cte on ACI.PatientId = cte.PatientId - -- and cte.Created = ACI.created - -- and cte.Code = f.Code + -- inner join [DWAPICentral].[dbo].[PatientExtract](NoLock) P ON ACI.[PatientId]= P.ID AND ACI.Voided = 0 + -- inner join [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided=0 + -- inner join cte on ACI.PatientId = cte.PatientId + -- and cte.Created = ACI.created + -- and cte.Code = f.Code -- and cte.VisitID = ACI.VisitID -- and cte.VisitDate = ACI.VisitDate -- where Row_Num > 1; - + DECLARE @MaxVisitDate_Hist DATETIME, @VisitDate DATETIME - + SELECT @MaxVisitDate_Hist = MAX(MaxVisitDate) FROM [ODS_logs].[dbo].[CT_AllergiesChronicIllness_Log] (NoLock) - SELECT @VisitDate = MAX(VisitDate) FROM [DWAPICentral].[dbo].[AllergiesChronicIllnessExtract] WITH (NOLOCK) - + SELECT @VisitDate = MAX(VisitDate) FROM [DWAPICentral].[dbo].[AllergiesChronicIllnessExtract] WITH (NOLOCK) + INSERT INTO [ODS_logs].[dbo].[CT_AllergiesChronicIllness_Log](MaxVisitDate,LoadStartDateTime) VALUES(@VisitDate,GETDATE()) @@ -69,13 +69,13 @@ BEGIN ,ACI.[Date_Created] ,ACI.[Date_Last_Modified] ,ACI.RecordUUID,ACI.voided - ,VoidingSource = Case + ,VoidingSource = Case when ACI.voided = 1 Then 'Source' Else Null - END + END ,ACI.Controlled FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[AllergiesChronicIllnessExtract](NoLock) ACI ON ACI.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[AllergiesChronicIllnessExtract](NoLock) ACI ON ACI.[PatientId] = P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 INNER JOIN ( SELECT F.code as SiteCode @@ -86,33 +86,33 @@ BEGIN ,max(InnerACI.ID) As Max_ID ,MAX(cast(InnerACI.created as date)) AS Maxdatecreated FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[AllergiesChronicIllnessExtract](NoLock) InnerACI ON InnerACI.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[AllergiesChronicIllnessExtract](NoLock) InnerACI ON InnerACI.[PatientId] = P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 GROUP BY F.code ,p.[PatientPID] ,InnerACI.visitDate ,InnerACI.VisitID ,InnerACI.voided - ) tm - ON f.code = tm.[SiteCode] and - p.PatientPID=tm.PatientPK and - ACI.visitDate = tm.visitDate and - ACI.VisitID = tm.VisitID and + ) tm + ON f.code = tm.[SiteCode] and + p.PatientPID=tm.PatientPK and + ACI.visitDate = tm.visitDate and + ACI.VisitID = tm.VisitID and ACI.voided = tm.voided and cast(ACI.created as date) = tm.Maxdatecreated and ACI.ID = tm.Max_ID WHERE P.gender != 'Unknown' AND F.code >0 - ) AS b + ) AS b ON( - a.PatientPK = b.PatientPK + a.PatientPK = b.PatientPK and a.SiteCode = b.SiteCode and a.VisitDate = b.VisitDate and a.VisitID = b.VisitID and a.voided = b.voided ) - WHEN NOT MATCHED THEN + WHEN NOT MATCHED THEN INSERT( ID ,AllergiesChronicIllnessUnique_ID @@ -146,7 +146,7 @@ BEGIN ,VoidingSource ,Controlled ,LoadDate - ) + ) VALUES( ID ,ID @@ -181,10 +181,10 @@ BEGIN ,Controlled ,Getdate() ) - + WHEN MATCHED THEN - UPDATE SET - a.PatientID =b.PatientID, + UPDATE SET + a.PatientID =b.PatientID, a.ChronicIllness =b.ChronicIllness, a.ChronicOnsetDate =b.ChronicOnsetDate, a.knownAllergies =b.knownAllergies, @@ -205,16 +205,16 @@ BEGIN a.RecordUUID =b.RecordUUID, a.voided =b.voided, a.Controlled = b.Controlled; - - + + UPDATE [ODS_logs].[dbo].[CT_AllergiesChronicIllness_Log] SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @VisitDate; - INSERT INTO [ODS_logs].[dbo].[CT_AllergiesChronicIllnessCount_Log]([SiteCode],[CreatedDate],[AllergiesChronicIllnessCount]) - SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS PatientPharmacyCount - FROM [ODS].[dbo].[CT_AllergiesChronicIllness] - GROUP BY SiteCode; - + -- INSERT INTO [ODS_logs].[dbo].[CT_AllergiesChronicIllnessCount_Log]([SiteCode],[CreatedDate],[AllergiesChronicIllnessCount]) + -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS PatientPharmacyCount + -- FROM [ODS].[dbo].[CT_AllergiesChronicIllness] + -- GROUP BY SiteCode; + END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_Covid.sql b/Scripts/ODS/CT_DOCKET/Load_CT_Covid.sql index e866180e..57a20635 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_Covid.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_Covid.sql @@ -3,10 +3,10 @@ BEGIN DECLARE @MaxCovid19AssessmentDate_Hist DATETIME, @Covid19AssessmentDate DATETIME - + SELECT @MaxCovid19AssessmentDate_Hist = MAX(MaxCovid19AssessmentDate) FROM [ODS_Logs].[dbo].[CT_Covid_Log] (NoLock) - SELECT @Covid19AssessmentDate = MAX(Covid19AssessmentDate) FROM [DWAPICentral].[dbo].[CovidExtract](NoLock) - + SELECT @Covid19AssessmentDate = MAX(Covid19AssessmentDate) FROM [DWAPICentral].[dbo].[CovidExtract](NoLock) + INSERT INTO [ODS_Logs].[dbo].[CT_Covid_Log](MaxCovid19AssessmentDate,LoadStartDateTime) VALUES(@MaxCovid19AssessmentDate_Hist,GETDATE()) @@ -17,7 +17,7 @@ BEGIN ,P.[Emr] ,P.[Project] ,F.Code AS SiteCode - ,F.Name AS FacilityName + ,F.Name AS FacilityName ,C.[VisitID] ,Cast(C.[Covid19AssessmentDate] as Date)[Covid19AssessmentDate] ,[ReceivedCOVID19Vaccine] @@ -55,14 +55,14 @@ BEGIN ,C.[Date_Last_Modified] ,C.RecordUUID ,C.voided - ,VoidingSource = Case + ,VoidingSource = Case when C.voided = 1 Then 'Source' Else Null - END - FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[CovidExtract](NoLock) C ON C.[PatientId]= P.ID + END + FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P + INNER JOIN [DWAPICentral].[dbo].[CovidExtract](NoLock) C ON C.[PatientId]= P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided=0 - INNER JOIN (SELECT + INNER JOIN (SELECT a.PatientPID ,f.code ,InnerC.visitID @@ -71,7 +71,7 @@ BEGIN ,Max(InnerC.ID) As MaxID ,Max(cast(InnerC.created as date))MaxCreated FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) a - INNER JOIN [DWAPICentral].[dbo].[CovidExtract](NoLock) InnerC ON InnerC.[PatientId]= a.ID + INNER JOIN [DWAPICentral].[dbo].[CovidExtract](NoLock) InnerC ON InnerC.[PatientId]= a.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON a.[FacilityId] = F.Id AND F.Voided=0 GROUP BY a.PatientPID ,f.code @@ -79,21 +79,21 @@ BEGIN ,InnerC.Covid19AssessmentDate ,InnerC.voided )tn - ON P.PatientPID = tn.PatientPID and - F.code = tn.code and + ON P.PatientPID = tn.PatientPID and + F.code = tn.code and C.ID = tn.MaxID and cast(C.Created as date) = tn.MaxCreated - WHERE P.gender != 'Unknown' AND F.code >0) AS b + WHERE P.gender != 'Unknown' AND F.code >0) AS b ON( a.SiteCode = b.SiteCode - and a.PatientPK = b.PatientPK + and a.PatientPK = b.PatientPK and a.visitID = b.visitID AND a.Covid19AssessmentDate = b.Covid19AssessmentDate and a.voided = b.voided --and a.ID = b.ID ) - WHEN NOT MATCHED THEN + WHEN NOT MATCHED THEN INSERT( ID ,PatientPK @@ -140,7 +140,7 @@ BEGIN ,voided ,VoidingSource ,LoadDate - ) + ) VALUES( ID ,PatientPK @@ -188,10 +188,10 @@ BEGIN ,VoidingSource ,Getdate() ) - + WHEN MATCHED THEN - UPDATE SET - a.PatientID =b.PatientID, + UPDATE SET + a.PatientID =b.PatientID, a.DateGivenFirstDose =b.DateGivenFirstDose, a.FirstDoseVaccineAdministered =b.FirstDoseVaccineAdministered, a.DateGivenSecondDose =b.DateGivenSecondDose, @@ -225,16 +225,16 @@ BEGIN a.[Date_Last_Modified] =b.[Date_Last_Modified], a.RecordUUID =b.RecordUUID, a.voided =b.voided; - + UPDATE [ODS_Logs].[dbo].[CT_Covid_Log] SET LoadEndDateTime = GETDATE() WHERE MaxCovid19AssessmentDate = @MaxCovid19AssessmentDate_Hist; - INSERT INTO [ODS_Logs].[dbo].[CT_CovidCount_Log]([SiteCode],[CreatedDate],[CovidCount]) - SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS CovidCount - FROM [ODS].[dbo].[CT_Covid] - GROUP BY SiteCode; + -- INSERT INTO [ODS_Logs].[dbo].[CT_CovidCount_Log]([SiteCode],[CreatedDate],[CovidCount]) + -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS CovidCount + -- FROM [ODS].[dbo].[CT_Covid] + -- GROUP BY SiteCode; END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_DefaulterTracing.sql b/Scripts/ODS/CT_DOCKET/Load_CT_DefaulterTracing.sql index a7596288..6aee25f9 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_DefaulterTracing.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_DefaulterTracing.sql @@ -1,10 +1,10 @@ BEGIN DECLARE @MaxVisitDate_Hist DATETIME, @VisitDate DATETIME - + SELECT @MaxVisitDate_Hist = MAX(MaxVisitDate) FROM [ODS_Logs].[dbo].[CT_DefaulterTracing_Log] (NoLock) - SELECT @VisitDate = MAX(VisitDate) FROM [DWAPICentral].[dbo].[DefaulterTracingExtract](NoLock) - + SELECT @VisitDate = MAX(VisitDate) FROM [DWAPICentral].[dbo].[DefaulterTracingExtract](NoLock) + INSERT INTO [ODS_Logs].[dbo].[CT_DefaulterTracing_Log](MaxVisitDate,LoadStartDateTime) VALUES(@MaxVisitDate_Hist,GETDATE()) ---- Refresh [ODS].[dbo].[CT_DefaulterTracing] @@ -14,7 +14,7 @@ BEGIN ,P.[Emr] ,P.[Project] ,F.Code AS SiteCode - ,F.Name AS FacilityName + ,F.Name AS FacilityName ,C.[VisitID] ,Cast(C.[VisitDate] As Date)[VisitDate] ,[EncounterId] @@ -34,12 +34,12 @@ BEGIN ,P.ID ,C.[Date_Created] ,C.[Date_Last_Modified] - ,VoidingSource = Case + ,VoidingSource = Case when C.voided = 1 Then 'Source' Else Null - END - FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[DefaulterTracingExtract](NoLock) C ON C.[PatientId]= P.ID + END + FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P + INNER JOIN [DWAPICentral].[dbo].[DefaulterTracingExtract](NoLock) C ON C.[PatientId]= P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided=0 INNER JOIN ( SELECT F.code as SiteCode @@ -50,23 +50,23 @@ BEGIN ,max(InnerC.ID) As maxID ,MAX(InnerC.created )AS Maxdatecreated FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[OvcExtract](NoLock) InnerC ON InnerC.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[OvcExtract](NoLock) InnerC ON InnerC.[PatientId] = P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 GROUP BY F.code ,p.[PatientPID] ,InnerC.voided ,InnerC.VisitDate ,InnerC.VisitID - ) tm - ON f.code = tm.[SiteCode] and - p.PatientPID=tm.PatientPK and - C.voided = tm.voided and + ) tm + ON f.code = tm.[SiteCode] and + p.PatientPID=tm.PatientPK and + C.voided = tm.voided and C.created = tm.Maxdatecreated and C.ID =tm.maxID and C.VisitDate = tm.VisitDate - WHERE P.gender != 'Unknown' AND F.code >0) AS b + WHERE P.gender != 'Unknown' AND F.code >0) AS b ON( - a.PatientPK = b.PatientPK + a.PatientPK = b.PatientPK and a.SiteCode = b.SiteCode and a.VisitID = b.VisitID and a.VisitDate = b.VisitDate @@ -74,7 +74,7 @@ BEGIN --and a.ID =b.ID ) - WHEN NOT MATCHED THEN + WHEN NOT MATCHED THEN INSERT( ID ,PatientPK @@ -97,13 +97,13 @@ BEGIN ,[Date_Created] ,[Date_Last_Modified] ,RecordUUID - ,voided + ,voided ,VoidingSource ,[DatePromisedToCome] ,[ReasonForMissedAppointment] ,[DateOfMissedAppointment] ,LoadDate - ) + ) VALUES( ID ,PatientPK @@ -126,16 +126,16 @@ BEGIN ,[Date_Created] ,[Date_Last_Modified] ,RecordUUID - ,voided + ,voided ,VoidingSource ,[DatePromisedToCome] ,[ReasonForMissedAppointment] ,[DateOfMissedAppointment] ,Getdate() ) - + WHEN MATCHED THEN - UPDATE SET + UPDATE SET a.PatientID =b.PatientID, a.TracingType =b.TracingType, a.TracingOutcome=b.TracingOutcome, @@ -158,9 +158,9 @@ BEGIN SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @MaxVisitDate_Hist; - INSERT INTO [ODS_Logs].[dbo].[CT_DefaulterTracingCount_Log]([SiteCode],[CreatedDate],[DefaulterTracingCount]) - SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS DefaulterTracingCount - FROM [ODS].[dbo].CT_DefaulterTracing - GROUP BY SiteCode; + -- INSERT INTO [ODS_Logs].[dbo].[CT_DefaulterTracingCount_Log]([SiteCode],[CreatedDate],[DefaulterTracingCount]) + -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS DefaulterTracingCount + -- FROM [ODS].[dbo].CT_DefaulterTracing + -- GROUP BY SiteCode; END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_DepressionScreening.sql b/Scripts/ODS/CT_DOCKET/Load_CT_DepressionScreening.sql index 1c4e8f10..9717bd04 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_DepressionScreening.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_DepressionScreening.sql @@ -2,10 +2,10 @@ BEGIN DECLARE @MaxVisitDate_Hist DATETIME, @VisitDate DATETIME - + SELECT @MaxVisitDate_Hist = MAX(MaxVisitDate) FROM [ODS_logs].[dbo].[CT_DepressionScreening_Log] (NoLock) - SELECT @VisitDate = MAX(VisitDate) FROM [DWAPICentral].[dbo].[DepressionScreeningExtract](NoLock) - + SELECT @VisitDate = MAX(VisitDate) FROM [DWAPICentral].[dbo].[DepressionScreeningExtract](NoLock) + INSERT INTO [ODS_logs].[dbo].[CT_DepressionScreening_Log](MaxVisitDate,LoadStartDateTime) VALUES(@MaxVisitDate_Hist,GETDATE()) @@ -34,18 +34,18 @@ BEGIN ,DS.[PHQ9_8] ,DS.[PHQ9_9] ,DS.[PHQ_9_rating] - ,DS.[DepressionAssesmentScore] + ,DS.[DepressionAssesmentScore] ,P.ID ,DS.[Date_Created] ,DS.[Date_Last_Modified] ,DS.RecordUUID ,DS.voided - ,VoidingSource = Case + ,VoidingSource = Case when DS.voided = 1 Then 'Source' Else Null - END + END FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[DepressionScreeningExtract](NoLock) DS ON DS.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[DepressionScreeningExtract](NoLock) DS ON DS.[PatientId] = P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 INNER JOIN ( SELECT F.code as SiteCode @@ -56,24 +56,24 @@ BEGIN ,max(InnerDS.ID) As maxID ,MAX(InnerDS.created )AS Maxdatecreated FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[DepressionScreeningExtract](NoLock) InnerDS ON InnerDS.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[DepressionScreeningExtract](NoLock) InnerDS ON InnerDS.[PatientId] = P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 GROUP BY F.code ,p.[PatientPID] ,InnerDS.voided ,InnerDS.VisitDate ,InnerDS.VisitID - ) tm - ON f.code = tm.[SiteCode] and - p.PatientPID=tm.PatientPK and - DS.voided = tm.voided and + ) tm + ON f.code = tm.[SiteCode] and + p.PatientPID=tm.PatientPK and + DS.voided = tm.voided and DS.created = tm.Maxdatecreated and DS.ID =tm.maxID and DS.VisitDate = tm.VisitDate WHERE P.gender != 'Unknown' AND F.code >0 - ) AS b + ) AS b ON( - a.PatientPK = b.PatientPK + a.PatientPK = b.PatientPK and a.SiteCode = b.SiteCode and a.VisitID = b.VisitID and a.VisitDate = b.VisitDate @@ -81,7 +81,7 @@ BEGIN ) - WHEN NOT MATCHED THEN + WHEN NOT MATCHED THEN INSERT( ID ,PatientID @@ -109,7 +109,7 @@ BEGIN ,voided ,VoidingSource ,LoadDate - ) + ) VALUES( ID ,PatientID @@ -138,9 +138,9 @@ BEGIN ,VoidingSource ,Getdate() ) - + WHEN MATCHED THEN - UPDATE SET + UPDATE SET a.PatientID =b.PatientID, a.PHQ9_1 =b.PHQ9_1, a.PHQ9_2 =b.PHQ9_2, @@ -157,17 +157,17 @@ BEGIN a.[Date_Last_Modified] =b.[Date_Last_Modified], a.RecordUUID =b.RecordUUID, a.voided =b.voided; - - + + UPDATE [ODS_logs].[dbo].[CT_DepressionScreening_Log] SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @MaxVisitDate_Hist; - INSERT INTO [ODS_logs].[dbo].[CT_DepressionScreeningCount_Log]([SiteCode],[CreatedDate],[DepressionScreeningCount]) - SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS DepressionScreeningCount - FROM [ODS].[dbo].[CT_DepressionScreening] - GROUP BY SiteCode; + -- INSERT INTO [ODS_logs].[dbo].[CT_DepressionScreeningCount_Log]([SiteCode],[CreatedDate],[DepressionScreeningCount]) + -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS DepressionScreeningCount + -- FROM [ODS].[dbo].[CT_DepressionScreening] + -- GROUP BY SiteCode; - END \ No newline at end of file + END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_DrugAlcoholScreening.sql b/Scripts/ODS/CT_DOCKET/Load_CT_DrugAlcoholScreening.sql index 90fd1f45..7987fcfa 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_DrugAlcoholScreening.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_DrugAlcoholScreening.sql @@ -1,10 +1,10 @@ BEGIN DECLARE @MaxVisitDate_Hist DATETIME, @VisitDate DATETIME - + SELECT @MaxVisitDate_Hist = MAX(MaxVisitDate) FROM [ODS_logs].[dbo].[CT_DrugAlcoholScreening_Log] (NoLock) - SELECT @VisitDate = MAX(VisitDate) FROM [DWAPICentral].[dbo].[DrugAlcoholScreeningExtract] WITH (NOLOCK) - + SELECT @VisitDate = MAX(VisitDate) FROM [DWAPICentral].[dbo].[DrugAlcoholScreeningExtract] WITH (NOLOCK) + INSERT INTO [ODS_logs].[dbo].[CT_DrugAlcoholScreening_Log](MaxVisitDate,LoadStartDateTime) VALUES(@VisitDate,GETDATE()) @@ -27,17 +27,17 @@ BEGIN ,DAS.[DrinkingAlcohol] AS DrinkingAlcohol ,DAS.[Smoking] AS Smoking ,DAS.[DrugUse] AS DrugUse - ,DAS.ID + ,DAS.ID ,DAS.[Date_Created] ,DAS.[Date_Last_Modified] ,DAS.RecordUUID ,DAS.voided - ,VoidingSource = Case + ,VoidingSource = Case when DAS.voided = 1 Then 'Source' Else Null - END + END FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[DrugAlcoholScreeningExtract](NoLock) DAS ON DAS.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[DrugAlcoholScreeningExtract](NoLock) DAS ON DAS.[PatientId] = P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 INNER JOIN ( SELECT F.code as SiteCode @@ -48,32 +48,32 @@ BEGIN ,max(InnerDAS.ID) As maxID ,MAX(InnerDAS.created )AS Maxdatecreated FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[DrugAlcoholScreeningExtract](NoLock) InnerDAS ON InnerDAS.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[DrugAlcoholScreeningExtract](NoLock) InnerDAS ON InnerDAS.[PatientId] = P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 GROUP BY F.code ,p.[PatientPID] ,InnerDAS.voided ,InnerDAS.VisitDate ,InnerDAS.VisitID - ) tm - ON f.code = tm.[SiteCode] and - p.PatientPID=tm.PatientPK and - DAS.voided = tm.voided and + ) tm + ON f.code = tm.[SiteCode] and + p.PatientPID=tm.PatientPK and + DAS.voided = tm.voided and DAS.created = tm.Maxdatecreated and DAS.ID =tm.maxID and DAS.VisitDate = tm.VisitDate WHERE P.gender != 'Unknown' AND F.code >0 - ) AS b + ) AS b ON( - a.PatientPK = b.PatientPK + a.PatientPK = b.PatientPK and a.SiteCode = b.SiteCode and a.VisitID = b.VisitID and a.VisitDate =b.VisitDate and a.voided = b.voided --and a.ID =b.ID ) - - WHEN NOT MATCHED THEN + + WHEN NOT MATCHED THEN INSERT( ID ,PatientID @@ -93,7 +93,7 @@ BEGIN ,voided ,VoidingSource ,LoadDate - ) + ) VALUES( ID ,PatientID @@ -114,10 +114,10 @@ BEGIN ,VoidingSource ,Getdate() ) - + WHEN MATCHED THEN - UPDATE SET - a.PatientID =b.PatientID, + UPDATE SET + a.PatientID =b.PatientID, a.DrinkingAlcohol =b.DrinkingAlcohol, a.Smoking =b.Smoking, a.DrugUse =b.DrugUse, @@ -125,16 +125,16 @@ BEGIN a.[Date_Last_Modified] =b.[Date_Last_Modified], a.RecordUUID =b.RecordUUID, a.voided =b.voided; - - + + UPDATE [ODS_logs].[dbo].[CT_DrugAlcoholScreening_Log] SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @VisitDate; - INSERT INTO [ODS_logs].[dbo].[CT_DrugAlcoholScreeningCount_Log]([SiteCode],[CreatedDate],[DrugAlcoholScreeningCount]) - SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS DrugAlcoholScreeningCount - FROM [ODS].[dbo].[CT_DrugAlcoholScreening] - GROUP BY [SiteCode]; + -- INSERT INTO [ODS_logs].[dbo].[CT_DrugAlcoholScreeningCount_Log]([SiteCode],[CreatedDate],[DrugAlcoholScreeningCount]) + -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS DrugAlcoholScreeningCount + -- FROM [ODS].[dbo].[CT_DrugAlcoholScreening] + -- GROUP BY [SiteCode]; END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_EnhancedAdherenceCounselling.sql b/Scripts/ODS/CT_DOCKET/Load_CT_EnhancedAdherenceCounselling.sql index 0397ab3e..efd90a93 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_EnhancedAdherenceCounselling.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_EnhancedAdherenceCounselling.sql @@ -1,11 +1,11 @@ -BEGIN +BEGIN DECLARE @MaxVisitDate_Hist DATETIME, @VisitDate DATETIME - + SELECT @MaxVisitDate_Hist = MAX(MaxVisitDate) FROM [ODS_logs].[dbo].[CT_EnhancedAdherenceCounselling_Log] (NoLock) SELECT @VisitDate = MAX(VisitDate) FROM [DWAPICentral].[dbo].[EnhancedAdherenceCounsellingExtract](NoLock) - + INSERT INTO [ODS_logs].[dbo].[CT_EnhancedAdherenceCounselling_Log](MaxVisitDate,LoadStartDateTime) VALUES(@MaxVisitDate_Hist,GETDATE()) @@ -67,17 +67,17 @@ BEGIN ,EAC.[EACHomevisit] ,EAC.[EACAdherencePlan] ,EAC.[EACFollowupDate] - ,EAC.ID + ,EAC.ID ,EAC.[Date_Created] ,EAC.[Date_Last_Modified] ,EAC.RecordUUID ,EAC.voided - ,VoidingSource = Case + ,VoidingSource = Case when EAC.voided = 1 Then 'Source' Else Null - END + END FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[EnhancedAdherenceCounsellingExtract](NoLock) EAC ON EAC.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[EnhancedAdherenceCounsellingExtract](NoLock) EAC ON EAC.[PatientId] = P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 INNER JOIN ( SELECT F.code as SiteCode @@ -88,33 +88,33 @@ BEGIN ,max(InnerEAC.ID) As Max_ID ,MAX(cast(InnerEAC.created as date)) AS Maxdatecreated FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[AllergiesChronicIllnessExtract](NoLock) InnerEAC ON InnerEAC.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[AllergiesChronicIllnessExtract](NoLock) InnerEAC ON InnerEAC.[PatientId] = P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 GROUP BY F.code ,p.[PatientPID] ,InnerEAC.visitDate ,InnerEAC.VisitID ,InnerEAC.voided - ) tm - ON f.code = tm.[SiteCode] and - p.PatientPID=tm.PatientPK and - EAC.visitDate = tm.visitDate and - EAC.VisitID = tm.VisitID and + ) tm + ON f.code = tm.[SiteCode] and + p.PatientPID=tm.PatientPK and + EAC.visitDate = tm.visitDate and + EAC.VisitID = tm.VisitID and EAC.voided = tm.voided and cast(EAC.created as date) = tm.Maxdatecreated and EAC.ID = tm.Max_ID WHERE P.gender != 'Unknown' AND F.code >0 - ) AS b + ) AS b ON( - a.PatientPK = b.PatientPK + a.PatientPK = b.PatientPK and a.SiteCode = b.SiteCode and a.VisitID =b.VisitID and a.VisitDate =b.VisitDate and a.voided = b.voided - + ) - - WHEN NOT MATCHED THEN + + WHEN NOT MATCHED THEN INSERT( ID ,PatientID @@ -173,7 +173,7 @@ BEGIN ,voided ,VoidingSource ,LoadDate - ) + ) VALUES( ID ,PatientID @@ -203,7 +203,7 @@ BEGIN ,EACWayForward ,EACCognitiveBarrier ,EACBehaviouralBarrier_1 - ,EACBehaviouralBarrier_2 + ,EACBehaviouralBarrier_2 ,EACBehaviouralBarrier_3 ,EACBehaviouralBarrier_4 ,EACBehaviouralBarrier_5 @@ -233,10 +233,10 @@ BEGIN ,VoidingSource ,Getdate() ) - + WHEN MATCHED THEN - UPDATE SET - a.PatientID =b.PatientID, + UPDATE SET + a.PatientID =b.PatientID, a.DateOfFirstSession =b.DateOfFirstSession, a.PillCountAdherence =b.PillCountAdherence, a.MMAS4_1 =b.MMAS4_1, @@ -283,17 +283,16 @@ BEGIN a.RecordUUID =b.RecordUUID, a.voided =b.voided ; - + UPDATE [ODS_logs].[dbo].[CT_EnhancedAdherenceCounselling_Log] SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @MaxVisitDate_Hist; - - INSERT INTO [ODS_logs].[dbo].[CT_EnhancedAdherenceCounsellingCount_Log]([SiteCode],[CreatedDate],[EnhancedAdherenceCounsellingCount]) - SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS EnhancedAdherenceCounsellingCount - FROM [ODS].[dbo].[CT_EnhancedAdherenceCounselling] - GROUP BY SiteCode; + + -- INSERT INTO [ODS_logs].[dbo].[CT_EnhancedAdherenceCounsellingCount_Log]([SiteCode],[CreatedDate],[EnhancedAdherenceCounsellingCount]) + -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS EnhancedAdherenceCounsellingCount + -- FROM [ODS].[dbo].[CT_EnhancedAdherenceCounselling] + -- GROUP BY SiteCode; END - \ No newline at end of file diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_GbvScreening.sql b/Scripts/ODS/CT_DOCKET/Load_CT_GbvScreening.sql index f85b8671..53180c17 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_GbvScreening.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_GbvScreening.sql @@ -1,11 +1,11 @@ BEGIN DECLARE @MaxVisitDate_Hist DATETIME, @VisitDate DATETIME - + SELECT @MaxVisitDate_Hist = MAX(MaxVisitDate) FROM [ODS_logs].[dbo].[CT_GbvScreening_Log] (NoLock) SELECT @VisitDate = MAX(VisitDate) FROM [DWAPICentral].[dbo].[GbvScreeningExtract](NoLock) - - + + INSERT INTO [ODS_logs].[dbo].[CT_GbvScreening_Log](MaxVisitDate,LoadStartDateTime) VALUES(@MaxVisitDate_Hist,GETDATE()) ---- Refresh [ODS].[dbo].[CT_GbvScreening] @@ -28,18 +28,18 @@ BEGIN ,GSE.[PhysicalIPV] ,GSE.[EmotionalIPV] ,GSE.[SexualIPV] - ,GSE.[IPVRelationship] + ,GSE.[IPVRelationship] ,GSE.ID ,GSE.[Date_Created] ,GSE.[Date_Last_Modified] ,GSE.RecordUUID ,GSE.voided - ,VoidingSource = Case + ,VoidingSource = Case when GSE.voided = 1 Then 'Source' Else Null - END + END FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[GbvScreeningExtract](NoLock) GSE ON GSE.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[GbvScreeningExtract](NoLock) GSE ON GSE.[PatientId] = P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 INNER JOIN ( SELECT F.code as SiteCode @@ -50,32 +50,32 @@ BEGIN ,max(InnerGSE.ID) As Max_ID ,MAX(cast(InnerGSE.created as date)) AS Maxdatecreated FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[AllergiesChronicIllnessExtract](NoLock) InnerGSE ON InnerGSE.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[AllergiesChronicIllnessExtract](NoLock) InnerGSE ON InnerGSE.[PatientId] = P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 GROUP BY F.code ,p.[PatientPID] ,InnerGSE.visitDate ,InnerGSE.VisitID ,InnerGSE.voided - ) tm - ON f.code = tm.[SiteCode] and - p.PatientPID=tm.PatientPK and - GSE.visitDate = tm.visitDate and - GSE.VisitID = tm.VisitID and + ) tm + ON f.code = tm.[SiteCode] and + p.PatientPID=tm.PatientPK and + GSE.visitDate = tm.visitDate and + GSE.VisitID = tm.VisitID and GSE.voided = tm.voided and cast(GSE.created as date) = tm.Maxdatecreated and GSE.ID = tm.Max_ID WHERE P.gender != 'Unknown' AND F.code >0 - ) AS b + ) AS b ON( - a.PatientPK = b.PatientPK + a.PatientPK = b.PatientPK and a.SiteCode = b.SiteCode and a.VisitID =b.VisitID and a.VisitDate =b.VisitDate - and a.voided = b.voided + and a.voided = b.voided ) - WHEN NOT MATCHED THEN + WHEN NOT MATCHED THEN INSERT( ID ,PatientID @@ -97,7 +97,7 @@ BEGIN ,voided ,VoidingSource ,LoadDate - ) + ) VALUES( ID ,PatientID @@ -120,9 +120,9 @@ BEGIN ,VoidingSource ,Getdate() ) - + WHEN MATCHED THEN - UPDATE SET + UPDATE SET a.PatientID =b.PatientID, a.IPV =b.IPV, a.PhysicalIPV =b.PhysicalIPV, @@ -134,16 +134,16 @@ BEGIN a.RecordUUID =b.RecordUUID, a.voided =b.voided ; - - + + UPDATE [ODS_logs].[dbo].[CT_GbvScreening_Log] SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @MaxVisitDate_Hist; - INSERT INTO [ODS_logs].[dbo].[CT_GbvScreeningCount_Log]([SiteCode],[CreatedDate],[GbvScreeningCount]) - SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS GbvScreeningCount - FROM [ODS].[dbo].[CT_GbvScreening] - GROUP BY SiteCode; + -- INSERT INTO [ODS_logs].[dbo].[CT_GbvScreeningCount_Log]([SiteCode],[CreatedDate],[GbvScreeningCount]) + -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS GbvScreeningCount + -- FROM [ODS].[dbo].[CT_GbvScreening] + -- GROUP BY SiteCode; END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_Ipt.sql b/Scripts/ODS/CT_DOCKET/Load_CT_Ipt.sql index 81213980..53d57146 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_Ipt.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_Ipt.sql @@ -4,11 +4,11 @@ BEGIN DECLARE @MaxVisitDate_Hist DATETIME, @VisitDate DATETIME - + SELECT @MaxVisitDate_Hist = MAX(MaxVisitDate) FROM [ODS_logs].[dbo].[CT_Ipt_Log] (NoLock); SELECT @VisitDate = MAX(VisitDate) FROM [DWAPICentral].[dbo].[IptExtract](NoLock); - - + + INSERT INTO [ODS_logs].[dbo].[CT_Ipt_Log](MaxVisitDate,LoadStartDateTime) VALUES(@VisitDate,GETDATE()); @@ -55,17 +55,17 @@ BEGIN ,IE.DateOfDiscontinuation ,IE.RecordUUID ,IE.voided - ,VoidingSource = Case + ,VoidingSource = Case when IE.voided = 1 Then 'Source' Else Null - END - ,IE.[Adherence] + END + ,IE.[Adherence] ,IE.Hepatoxicity ,IE.PeripheralNeruopath ,IE.Rash FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[IptExtract](NoLock) IE ON IE.[PatientId] = P.ID - INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 + INNER JOIN [DWAPICentral].[dbo].[IptExtract](NoLock) IE ON IE.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 INNER JOIN ( SELECT F.code as SiteCode ,p.[PatientPID] as PatientPK @@ -75,30 +75,30 @@ BEGIN ,max(InnerIE.ID) As Max_ID ,MAX(cast(InnerIE.created as date)) AS Maxdatecreated FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[IptExtract](NoLock) InnerIE ON InnerIE.[PatientId] = P.ID - INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 + INNER JOIN [DWAPICentral].[dbo].[IptExtract](NoLock) InnerIE ON InnerIE.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 GROUP BY F.code ,p.[PatientPID] --,visitID ,VisitDate ,InnerIE.voided - ) tm - ON f.code = tm.[SiteCode] and - p.PatientPID=tm.PatientPK and + ) tm + ON f.code = tm.[SiteCode] and + p.PatientPID=tm.PatientPK and IE.VisitDate = tm.VisitDate and cast(IE.created as date) = tm.Maxdatecreated and IE.ID = tm.Max_ID WHERE P.gender != 'Unknown' AND F.code >0 - ) AS b + ) AS b ON( - a.PatientPK = b.PatientPK + a.PatientPK = b.PatientPK and a.SiteCode = b.SiteCode and a.VisitID =b.VisitID and a.VisitDate =b.VisitDate and a.voided = b.voided ) - - WHEN NOT MATCHED THEN + + WHEN NOT MATCHED THEN INSERT( ID ,PatientID @@ -141,7 +141,7 @@ BEGIN ,PeripheralNeruopath ,Rash ,LoadDate - ) + ) VALUES( ID ,PatientID @@ -185,10 +185,10 @@ BEGIN ,Rash ,Getdate() ) - + WHEN MATCHED THEN - UPDATE SET - a.PatientID =b.PatientID, + UPDATE SET + a.PatientID =b.PatientID, a.OnTBDrugs =b.OnTBDrugs, a.OnIPT =b.OnIPT, a.EverOnIPT =b.EverOnIPT, @@ -219,14 +219,14 @@ BEGIN ,a.Hepatoxicity = b.Hepatoxicity ,a.PeripheralNeruopath = b.PeripheralNeruopath ,a.Rash = b.Rash; - + UPDATE [ODS_logs].[dbo].[CT_Ipt_Log] SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @VisitDate; - - INSERT INTO [ODS_logs].[dbo].[CT_IptCount_Log]([SiteCode],[CreatedDate],[IptCount]) - SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS IptCount - FROM [ODS].[dbo].[CT_Ipt] - GROUP BY SiteCode; + + -- INSERT INTO [ODS_logs].[dbo].[CT_IptCount_Log]([SiteCode],[CreatedDate],[IptCount]) + -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS IptCount + -- FROM [ODS].[dbo].[CT_Ipt] + -- GROUP BY SiteCode; END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_Otz.sql b/Scripts/ODS/CT_DOCKET/Load_CT_Otz.sql index 028d248b..269f1415 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_Otz.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_Otz.sql @@ -4,10 +4,10 @@ BEGIN DECLARE @MaxVisitDate_Hist DATETIME, @VisitDate DATETIME - + SELECT @MaxVisitDate_Hist = MAX(MaxVisitDate) FROM [ODS_Logs].[dbo].[CT_Otz_Log] (NoLock) SELECT @VisitDate = MAX(VisitDate) FROM [DWAPICentral].[dbo].[OtzExtract](NoLock) - + INSERT INTO [ODS_Logs].[dbo].[CT_Otz_Log](MaxVisitDate,LoadStartDateTime) VALUES(@MaxVisitDate_Hist,GETDATE()) @@ -39,12 +39,12 @@ BEGIN ,OE.[Date_Last_Modified] ,OE.RecordUUID ,OE.voided - ,VoidingSource = Case + ,VoidingSource = Case when OE.voided = 1 Then 'Source' Else Null - END + END FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[OtzExtract](NoLock) OE ON OE.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[OtzExtract](NoLock) OE ON OE.[PatientId] = P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 INNER JOIN ( SELECT F.code as SiteCode @@ -55,33 +55,33 @@ BEGIN ,max(InnerOE.ID) As maxID ,MAX(InnerOE.created )AS Maxdatecreated FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[OtzExtract](NoLock) InnerOE ON InnerOE.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[OtzExtract](NoLock) InnerOE ON InnerOE.[PatientId] = P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 GROUP BY F.code ,p.[PatientPID] ,InnerOE.voided ,InnerOE.VisitDate ,InnerOE.VisitID - ) tm - ON f.code = tm.[SiteCode] and - p.PatientPID=tm.PatientPK and - OE.voided = tm.voided and + ) tm + ON f.code = tm.[SiteCode] and + p.PatientPID=tm.PatientPK and + OE.voided = tm.voided and OE.created = tm.Maxdatecreated and OE.ID =tm.maxID and OE.VisitDate = tm.VisitDate WHERE P.gender != 'Unknown' AND F.code >0 - ) AS b + ) AS b ON( - a.PatientPK = b.PatientPK + a.PatientPK = b.PatientPK and a.SiteCode = b.SiteCode and a.VisitID =b.VisitID and a.VisitDate =b.VisitDate and a.voided = b.voided --and a.ID =b.ID ) - - WHEN NOT MATCHED THEN + + WHEN NOT MATCHED THEN INSERT( ID ,PatientID @@ -106,7 +106,7 @@ BEGIN ,voided ,VoidingSource ,LoadDate - ) + ) VALUES( ID ,PatientID @@ -132,10 +132,10 @@ BEGIN ,VoidingSource ,Getdate() ) - + WHEN MATCHED THEN - UPDATE SET - a.PatientID =b.PatientID, + UPDATE SET + a.PatientID =b.PatientID, a.TransferInStatus =b.TransferInStatus, a.ModulesPreviouslyCovered =b.ModulesPreviouslyCovered, a.ModulesCompletedToday =b.ModulesCompletedToday, @@ -148,16 +148,16 @@ BEGIN a.OTZEnrollmentDate =b.OTZEnrollmentDate, a.voided =b.voided ; - + UPDATE [ODS_Logs].[dbo].[CT_Otz_Log] SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @MaxVisitDate_Hist; - INSERT INTO [ODS_Logs].[dbo].[CT_OtzCount_Log]([SiteCode],[CreatedDate],[OtzCount]) - SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS OtzCount - FROM [ODS].[dbo].[CT_Otz] - GROUP BY SiteCode; + -- INSERT INTO [ODS_Logs].[dbo].[CT_OtzCount_Log]([SiteCode],[CreatedDate],[OtzCount]) + -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS OtzCount + -- FROM [ODS].[dbo].[CT_Otz] + -- GROUP BY SiteCode; + - END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_Ovz.sql b/Scripts/ODS/CT_DOCKET/Load_CT_Ovz.sql index 0914c755..80935de5 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_Ovz.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_Ovz.sql @@ -2,11 +2,11 @@ BEGIN DECLARE @MaxVisitDate_Hist DATETIME, @VisitDate DATETIME - + SELECT @MaxVisitDate_Hist = MAX(MaxVisitDate) FROM [ODS_logs].[dbo].[CT_Ovc_Log] (NoLock) SELECT @VisitDate = MAX(VisitDate) FROM [DWAPICentral].[dbo].[OvcExtract](NoLock) - + INSERT INTO [ODS_logs].[dbo].[CT_Ovc_Log](MaxVisitDate,LoadStartDateTime) VALUES(@MaxVisitDate_Hist,GETDATE()) @@ -33,17 +33,17 @@ BEGIN ,OE.[PartnerOfferingOVCServices] ,OE.[OVCExitReason] ,OE.[ExitDate] - ,P.ID + ,P.ID ,OE.[Date_Created] ,OE.[Date_Last_Modified] ,OE.RecordUUID ,OE.voided - ,VoidingSource = Case + ,VoidingSource = Case when OE.voided = 1 Then 'Source' Else Null - END + END FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[OvcExtract](NoLock) OE ON OE.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[OvcExtract](NoLock) OE ON OE.[PatientId] = P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 INNER JOIN ( SELECT F.code as SiteCode @@ -54,23 +54,23 @@ BEGIN ,max(InnerOE.ID) As maxID ,MAX(InnerOE.created )AS Maxdatecreated FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[OvcExtract](NoLock) InnerOE ON InnerOE.[PatientId] = P.ID + INNER JOIN [DWAPICentral].[dbo].[OvcExtract](NoLock) InnerOE ON InnerOE.[PatientId] = P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided = 0 GROUP BY F.code ,p.[PatientPID] ,InnerOE.voided ,InnerOE.VisitDate ,InnerOE.VisitID - ) tm - ON f.code = tm.[SiteCode] and - p.PatientPID=tm.PatientPK and - OE.voided = tm.voided and + ) tm + ON f.code = tm.[SiteCode] and + p.PatientPID=tm.PatientPK and + OE.voided = tm.voided and OE.created = tm.Maxdatecreated and OE.ID =tm.maxID and OE.VisitDate = tm.VisitDate - WHERE P.gender != 'Unknown'AND F.code >0 ) AS b + WHERE P.gender != 'Unknown'AND F.code >0 ) AS b ON( - a.PatientPK = b.PatientPK + a.PatientPK = b.PatientPK and a.SiteCode = b.SiteCode and a.VisitID =b.VisitID and a.VisitDate =b.VisitDate @@ -79,7 +79,7 @@ BEGIN --and a.ID = b.ID ) - WHEN NOT MATCHED THEN + WHEN NOT MATCHED THEN INSERT( ID ,PatientID @@ -103,7 +103,7 @@ BEGIN ,voided ,VoidingSource ,LoadDate - ) + ) VALUES( ID ,PatientID @@ -128,11 +128,11 @@ BEGIN ,VoidingSource ,Getdate() ) - + WHEN MATCHED THEN - UPDATE SET + UPDATE SET a.PatientID =b.PatientID, - a.FacilityName =b.FacilityName, + a.FacilityName =b.FacilityName, a.RelationshipToClient =b.RelationshipToClient, a.EnrolledinCPIMS =b.EnrolledinCPIMS, a.CPIMSUniqueIdentifier =b.CPIMSUniqueIdentifier, @@ -140,15 +140,15 @@ BEGIN a.OVCExitReason =b.OVCExitReason, a.[Date_Created] =b.[Date_Created], a.[Date_Last_Modified] =b.[Date_Last_Modified]; - + UPDATE [ODS_logs].[dbo].[CT_Ovc_Log] SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @MaxVisitDate_Hist; - INSERT INTO [ODS_logs].[dbo].[CT_OvcCount_Log]([SiteCode],[CreatedDate],[OvcCount]) - SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS OVCCount - FROM [ODS].[dbo].[CT_Ovc] - GROUP BY SiteCode; + -- INSERT INTO [ODS_logs].[dbo].[CT_OvcCount_Log]([SiteCode],[CreatedDate],[OvcCount]) + -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS OVCCount + -- FROM [ODS].[dbo].[CT_Ovc] + -- GROUP BY SiteCode; END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_PatientLabs.sql b/Scripts/ODS/CT_DOCKET/Load_CT_PatientLabs.sql index baa83d39..826dee0e 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_PatientLabs.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_PatientLabs.sql @@ -2,13 +2,13 @@ BEGIN DECLARE @MaxOrderedbyDate_Hist DATETIME, @OrderedbyDate DATETIME - + SELECT @MaxOrderedbyDate_Hist = MAX(MaxOrderedbyDate) FROM [ODS_Logs].[dbo].[CT_PatientLabs_Log] (NoLock) - SELECT @OrderedbyDate = MAX(OrderedbyDate) FROM [DWAPICentral].[dbo].[PatientLaboratoryExtract] WITH (NOLOCK) - + SELECT @OrderedbyDate = MAX(OrderedbyDate) FROM [DWAPICentral].[dbo].[PatientLaboratoryExtract] WITH (NOLOCK) + INSERT INTO [ODS_Logs].[dbo].[CT_PatientLabs_Log](MaxOrderedbyDate,LoadStartDateTime) VALUES( @OrderedbyDate,GETDATE()) - + ---- Refresh [ODS].[dbo].[CT_PatientLabs] MERGE [ODS].[dbo].[CT_PatientLabs] AS a USING(SELECT distinct @@ -23,25 +23,25 @@ BEGIN ,PL.[EnrollmentTest] ,PL.[TestResult] ,P.[Emr] - ,CASE P.[Project] - WHEN 'I-TECH' THEN 'Kenya HMIS II' + ,CASE P.[Project] + WHEN 'I-TECH' THEN 'Kenya HMIS II' WHEN 'HMIS' THEN 'Kenya HMIS II' - ELSE P.[Project] - END AS [Project] + ELSE P.[Project] + END AS [Project] ,PL.DateSampleTaken ,PL.SampleType - ,p.ID + ,p.ID ,reason ,PL.[Date_Created] ,PL.[Date_Last_Modified] ,PL.RecordUUID ,PL.voided - ,VoidingSource = Case + ,VoidingSource = Case when PL.voided = 1 Then 'Source' Else Null - END - FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P - INNER JOIN [DWAPICentral].[dbo].[PatientLaboratoryExtract](NoLock) PL ON PL.[PatientId]= P.ID + END + FROM [DWAPICentral].[dbo].[PatientExtract](NoLock) P + INNER JOIN [DWAPICentral].[dbo].[PatientLaboratoryExtract](NoLock) PL ON PL.[PatientId]= P.ID INNER JOIN [DWAPICentral].[dbo].[Facility](NoLock) F ON P.[FacilityId] = F.Id AND F.Voided=0 INNER JOIN ( SELECT F.code as SiteCode @@ -52,8 +52,8 @@ BEGIN ,InnerPL.voided ,max(InnerPL.ID) As Max_ID ,MAX(cast(InnerPL.created as date)) AS Maxdatecreated - FROM [DWAPICentral].[dbo].[PatientExtract] P WITH (NoLock) - INNER JOIN [DWAPICentral].[dbo].[PatientLaboratoryExtract] InnerPL WITH(NoLock) ON InnerPL.[PatientId]= P.ID + FROM [DWAPICentral].[dbo].[PatientExtract] P WITH (NoLock) + INNER JOIN [DWAPICentral].[dbo].[PatientLaboratoryExtract] InnerPL WITH(NoLock) ON InnerPL.[PatientId]= P.ID INNER JOIN [DWAPICentral].[dbo].[Facility] F WITH(NoLock) ON P.[FacilityId] = F.Id AND F.Voided=0 GROUP BY F.code ,p.[PatientPID] @@ -61,25 +61,25 @@ BEGIN ,InnerPL.TestName ,InnerPL.OrderedbyDate ,InnerPL.voided - ) tm - ON f.code = tm.[SiteCode] and - p.PatientPID=tm.PatientPK and - PL.TestResult = tm.TestResult and - PL.TestName = tm.TestName and + ) tm + ON f.code = tm.[SiteCode] and + p.PatientPID=tm.PatientPK and + PL.TestResult = tm.TestResult and + PL.TestName = tm.TestName and PL.OrderedbyDate = tm.OrderedbyDate and PL.voided = tm.voided and cast(PL.created as date) = tm.Maxdatecreated and PL.ID = tm.Max_ID WHERE p.gender!='Unknown' - ) AS b + ) AS b ON( - a.PatientPK = b.PatientPK + a.PatientPK = b.PatientPK and a.SiteCode = b.SiteCode and a.VisitID =b.VisitID and a.OrderedbyDate =b.OrderedbyDate and a.voided = b.voided - and a.TestResult = b.TestResult - and a.TestName = b.TestName + and a.TestResult = b.TestResult + and a.TestName = b.TestName and a.voided = b.voided --and a.[Date_Created] = b.[Date_Created] --and a.RecordUUID = b.RecordUUID @@ -87,8 +87,8 @@ BEGIN --and a.[Date_Last_Modified] = b.[Date_Last_Modified] ) - - WHEN NOT MATCHED THEN + + WHEN NOT MATCHED THEN INSERT( ID @@ -113,7 +113,7 @@ BEGIN ,voided ,VoidingSource ,LoadDate - ) + ) VALUES( ID ,PatientID @@ -140,7 +140,7 @@ BEGIN ) WHEN MATCHED THEN - UPDATE SET + UPDATE SET a.[PatientID] = b.[PatientID], a.[FacilityName] = b.[FacilityName], a.[VisitID] = b.[VisitID], @@ -158,17 +158,17 @@ BEGIN a.[Date_Created] = b.[Date_Created], a.[RecordUUID] = b.[RecordUUID], a.[voided] = b.[voided]; - + UPDATE [ODS_Logs].[dbo].[CT_PatientLabs_Log] SET LoadEndDateTime = GETDATE() WHERE MaxOrderedbyDate = @OrderedbyDate; - INSERT INTO [ODS_logs].[dbo].[CT_PatientLabsCount_Log]([SiteCode],[CreatedDate],[PatientLabsCount]) - SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS PatientLabsCount - FROM [ODS].[dbo].[CT_PatientLabs] - GROUP BY SiteCode; + -- INSERT INTO [ODS_logs].[dbo].[CT_PatientLabsCount_Log]([SiteCode],[CreatedDate],[PatientLabsCount]) + -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS PatientLabsCount + -- FROM [ODS].[dbo].[CT_PatientLabs] + -- GROUP BY SiteCode; END From e4c35c96dd53fa87ab005b64fe0ce255d127cbf1 Mon Sep 17 00:00:00 2001 From: NewtonMutugi Date: Tue, 23 Jul 2024 12:53:08 +0300 Subject: [PATCH 08/21] Removed ODS_logs insert statements from ODS CT_Docket scripts (commented out parts removed) --- Scripts/ODS/CT_DOCKET/CT_AdverseEvents.sql | 9 --------- Scripts/ODS/CT_DOCKET/CT_PatientPharmacy.sql | 6 ------ Scripts/ODS/CT_DOCKET/CT_PatientStatus.sql | 10 ---------- Scripts/ODS/CT_DOCKET/CT_PatientVisits.sql | 6 ------ Scripts/ODS/CT_DOCKET/Load_CT_ARTPatient.sql | 4 ---- .../ODS/CT_DOCKET/Load_CT_AllergiesChronicIllness.sql | 4 ---- Scripts/ODS/CT_DOCKET/Load_CT_Covid.sql | 4 ---- Scripts/ODS/CT_DOCKET/Load_CT_DefaulterTracing.sql | 4 ---- Scripts/ODS/CT_DOCKET/Load_CT_DepressionScreening.sql | 5 ----- Scripts/ODS/CT_DOCKET/Load_CT_DrugAlcoholScreening.sql | 5 ----- .../CT_DOCKET/Load_CT_EnhancedAdherenceCounselling.sql | 5 ----- Scripts/ODS/CT_DOCKET/Load_CT_GbvScreening.sql | 5 ----- Scripts/ODS/CT_DOCKET/Load_CT_Ipt.sql | 4 ---- Scripts/ODS/CT_DOCKET/Load_CT_Otz.sql | 5 ----- Scripts/ODS/CT_DOCKET/Load_CT_Ovz.sql | 5 ----- Scripts/ODS/CT_DOCKET/Load_CT_PatientLabs.sql | 5 ----- 16 files changed, 86 deletions(-) diff --git a/Scripts/ODS/CT_DOCKET/CT_AdverseEvents.sql b/Scripts/ODS/CT_DOCKET/CT_AdverseEvents.sql index 8f83b14a..c99e0cd0 100644 --- a/Scripts/ODS/CT_DOCKET/CT_AdverseEvents.sql +++ b/Scripts/ODS/CT_DOCKET/CT_AdverseEvents.sql @@ -155,14 +155,5 @@ SET loadenddatetime = Getdate() WHERE maxadverseeventstartdate = @AdverseEventStartDate; --- INSERT INTO [ODS_Logs].[dbo].[ct_adverseeventcount_log] --- ([sitecode], --- [createddate], --- [adverseeventcount]) --- SELECT sitecode, --- Getdate(), --- Count(Concat(sitecode, patientpk)) AS AdverseEventCount --- FROM [ODS].[dbo].[ct_adverseevents] --- GROUP BY sitecode; END diff --git a/Scripts/ODS/CT_DOCKET/CT_PatientPharmacy.sql b/Scripts/ODS/CT_DOCKET/CT_PatientPharmacy.sql index c1535533..427819e6 100644 --- a/Scripts/ODS/CT_DOCKET/CT_PatientPharmacy.sql +++ b/Scripts/ODS/CT_DOCKET/CT_PatientPharmacy.sql @@ -158,10 +158,4 @@ BEGIN WHERE MaxDispenseDate = @DispenseDate; - -- INSERT INTO [ODS_logs].[dbo].[CT_PatientPharmacyCount_Log] ([SiteCode],[CreatedDate],[PatientPharmacyCount]) - -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS PatientPharmacyCount - -- FROM [ODS].[dbo].[CT_PatientPharmacy] - -- GROUP BY SiteCode; - - END diff --git a/Scripts/ODS/CT_DOCKET/CT_PatientStatus.sql b/Scripts/ODS/CT_DOCKET/CT_PatientStatus.sql index f74bd20c..896746a3 100644 --- a/Scripts/ODS/CT_DOCKET/CT_PatientStatus.sql +++ b/Scripts/ODS/CT_DOCKET/CT_PatientStatus.sql @@ -168,14 +168,4 @@ BEGIN; WHERE maxexitdate = @ExitDate; - - -- INSERT INTO [ODS_logs].[dbo].[CT_PatientStatusCount_Log] - -- ([sitecode], - -- [createddate], - -- [patientstatuscount]) - -- SELECT sitecode, - -- Getdate(), - -- Count(Concat(sitecode, patientpk)) AS PatientStatusCount - -- FROM [ODS].[dbo].[ct_patientstatus] - -- GROUP BY sitecode; END diff --git a/Scripts/ODS/CT_DOCKET/CT_PatientVisits.sql b/Scripts/ODS/CT_DOCKET/CT_PatientVisits.sql index ac0f21f2..3ddfbec1 100644 --- a/Scripts/ODS/CT_DOCKET/CT_PatientVisits.sql +++ b/Scripts/ODS/CT_DOCKET/CT_PatientVisits.sql @@ -325,10 +325,4 @@ BEGIN SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @VisitDate; - - -- INSERT INTO [ODS_logs].[dbo].[CT_VisitCount_Log]([SiteCode],[CreatedDate],[VisitCount]) - -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS VisitCount - -- FROM [ODS].[dbo].[CT_PatientVisits] - -- GROUP BY SiteCode; - END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_ARTPatient.sql b/Scripts/ODS/CT_DOCKET/Load_CT_ARTPatient.sql index 6a8e2508..14dae9a7 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_ARTPatient.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_ARTPatient.sql @@ -192,9 +192,5 @@ BEGIN a.RecordUUID =b.RecordUUID, a.voided =b.voided; - -- INSERT INTO [ODS_logs].[dbo].[CT_ARTPatientsCount_Log]([SiteCode],[CreatedDate],ARTPatientsCount) - -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS PatientStatusCount - -- FROM [ODS].[dbo].[CT_ARTPatients] - -- group by SiteCode END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_AllergiesChronicIllness.sql b/Scripts/ODS/CT_DOCKET/Load_CT_AllergiesChronicIllness.sql index bc08db25..7ee4dc82 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_AllergiesChronicIllness.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_AllergiesChronicIllness.sql @@ -211,10 +211,6 @@ BEGIN SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @VisitDate; - -- INSERT INTO [ODS_logs].[dbo].[CT_AllergiesChronicIllnessCount_Log]([SiteCode],[CreatedDate],[AllergiesChronicIllnessCount]) - -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS PatientPharmacyCount - -- FROM [ODS].[dbo].[CT_AllergiesChronicIllness] - -- GROUP BY SiteCode; END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_Covid.sql b/Scripts/ODS/CT_DOCKET/Load_CT_Covid.sql index 57a20635..c048fcb2 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_Covid.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_Covid.sql @@ -231,10 +231,6 @@ BEGIN SET LoadEndDateTime = GETDATE() WHERE MaxCovid19AssessmentDate = @MaxCovid19AssessmentDate_Hist; - -- INSERT INTO [ODS_Logs].[dbo].[CT_CovidCount_Log]([SiteCode],[CreatedDate],[CovidCount]) - -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS CovidCount - -- FROM [ODS].[dbo].[CT_Covid] - -- GROUP BY SiteCode; END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_DefaulterTracing.sql b/Scripts/ODS/CT_DOCKET/Load_CT_DefaulterTracing.sql index 6aee25f9..a4daba58 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_DefaulterTracing.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_DefaulterTracing.sql @@ -158,9 +158,5 @@ BEGIN SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @MaxVisitDate_Hist; - -- INSERT INTO [ODS_Logs].[dbo].[CT_DefaulterTracingCount_Log]([SiteCode],[CreatedDate],[DefaulterTracingCount]) - -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS DefaulterTracingCount - -- FROM [ODS].[dbo].CT_DefaulterTracing - -- GROUP BY SiteCode; END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_DepressionScreening.sql b/Scripts/ODS/CT_DOCKET/Load_CT_DepressionScreening.sql index 9717bd04..a1d5b52a 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_DepressionScreening.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_DepressionScreening.sql @@ -163,11 +163,6 @@ BEGIN SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @MaxVisitDate_Hist; - -- INSERT INTO [ODS_logs].[dbo].[CT_DepressionScreeningCount_Log]([SiteCode],[CreatedDate],[DepressionScreeningCount]) - -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS DepressionScreeningCount - -- FROM [ODS].[dbo].[CT_DepressionScreening] - -- GROUP BY SiteCode; - END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_DrugAlcoholScreening.sql b/Scripts/ODS/CT_DOCKET/Load_CT_DrugAlcoholScreening.sql index 7987fcfa..c1a19caa 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_DrugAlcoholScreening.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_DrugAlcoholScreening.sql @@ -131,10 +131,5 @@ BEGIN SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @VisitDate; - -- INSERT INTO [ODS_logs].[dbo].[CT_DrugAlcoholScreeningCount_Log]([SiteCode],[CreatedDate],[DrugAlcoholScreeningCount]) - -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS DrugAlcoholScreeningCount - -- FROM [ODS].[dbo].[CT_DrugAlcoholScreening] - -- GROUP BY [SiteCode]; - END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_EnhancedAdherenceCounselling.sql b/Scripts/ODS/CT_DOCKET/Load_CT_EnhancedAdherenceCounselling.sql index efd90a93..fac9b928 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_EnhancedAdherenceCounselling.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_EnhancedAdherenceCounselling.sql @@ -289,10 +289,5 @@ BEGIN WHERE MaxVisitDate = @MaxVisitDate_Hist; - -- INSERT INTO [ODS_logs].[dbo].[CT_EnhancedAdherenceCounsellingCount_Log]([SiteCode],[CreatedDate],[EnhancedAdherenceCounsellingCount]) - -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS EnhancedAdherenceCounsellingCount - -- FROM [ODS].[dbo].[CT_EnhancedAdherenceCounselling] - -- GROUP BY SiteCode; - END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_GbvScreening.sql b/Scripts/ODS/CT_DOCKET/Load_CT_GbvScreening.sql index 53180c17..2f950dfc 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_GbvScreening.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_GbvScreening.sql @@ -140,10 +140,5 @@ BEGIN SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @MaxVisitDate_Hist; - -- INSERT INTO [ODS_logs].[dbo].[CT_GbvScreeningCount_Log]([SiteCode],[CreatedDate],[GbvScreeningCount]) - -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS GbvScreeningCount - -- FROM [ODS].[dbo].[CT_GbvScreening] - -- GROUP BY SiteCode; - END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_Ipt.sql b/Scripts/ODS/CT_DOCKET/Load_CT_Ipt.sql index 53d57146..140b75e5 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_Ipt.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_Ipt.sql @@ -225,8 +225,4 @@ BEGIN SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @VisitDate; - -- INSERT INTO [ODS_logs].[dbo].[CT_IptCount_Log]([SiteCode],[CreatedDate],[IptCount]) - -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS IptCount - -- FROM [ODS].[dbo].[CT_Ipt] - -- GROUP BY SiteCode; END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_Otz.sql b/Scripts/ODS/CT_DOCKET/Load_CT_Otz.sql index 269f1415..0ffa9744 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_Otz.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_Otz.sql @@ -154,10 +154,5 @@ BEGIN SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @MaxVisitDate_Hist; - -- INSERT INTO [ODS_Logs].[dbo].[CT_OtzCount_Log]([SiteCode],[CreatedDate],[OtzCount]) - -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS OtzCount - -- FROM [ODS].[dbo].[CT_Otz] - -- GROUP BY SiteCode; - END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_Ovz.sql b/Scripts/ODS/CT_DOCKET/Load_CT_Ovz.sql index 80935de5..7a07079d 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_Ovz.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_Ovz.sql @@ -146,9 +146,4 @@ BEGIN SET LoadEndDateTime = GETDATE() WHERE MaxVisitDate = @MaxVisitDate_Hist; - -- INSERT INTO [ODS_logs].[dbo].[CT_OvcCount_Log]([SiteCode],[CreatedDate],[OvcCount]) - -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS OVCCount - -- FROM [ODS].[dbo].[CT_Ovc] - -- GROUP BY SiteCode; - END diff --git a/Scripts/ODS/CT_DOCKET/Load_CT_PatientLabs.sql b/Scripts/ODS/CT_DOCKET/Load_CT_PatientLabs.sql index 826dee0e..7a54bd6f 100644 --- a/Scripts/ODS/CT_DOCKET/Load_CT_PatientLabs.sql +++ b/Scripts/ODS/CT_DOCKET/Load_CT_PatientLabs.sql @@ -165,10 +165,5 @@ BEGIN WHERE MaxOrderedbyDate = @OrderedbyDate; - -- INSERT INTO [ODS_logs].[dbo].[CT_PatientLabsCount_Log]([SiteCode],[CreatedDate],[PatientLabsCount]) - -- SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS PatientLabsCount - -- FROM [ODS].[dbo].[CT_PatientLabs] - -- GROUP BY SiteCode; - END From 7faf18ca355877e9a765169b10375c9a1f82745b Mon Sep 17 00:00:00 2001 From: NewtonMutugi Date: Tue, 23 Jul 2024 14:39:02 +0300 Subject: [PATCH 09/21] Updated table names for Mhealth applications --- .../ODS/Mhealth/Load_UShauri_PatientLabs.sql | 4 +-- Scripts/ODS/Mhealth/Load_Ushauri_HEI.sql | 24 ++++++++--------- .../ODS/Mhealth/Load_Ushauri_HW_Exposures.sql | 4 +-- Scripts/ODS/Mhealth/Load_Ushauri_Patient.sql | 26 +++++++++---------- .../Load_Ushauri_PatientAppointments.sql | 22 ++++++++-------- .../Mhealth/Load_Ushauri_PatientReferral.sql | 18 ++++++------- 6 files changed, 49 insertions(+), 49 deletions(-) diff --git a/Scripts/ODS/Mhealth/Load_UShauri_PatientLabs.sql b/Scripts/ODS/Mhealth/Load_UShauri_PatientLabs.sql index 48dc2fa9..1ea02dfc 100644 --- a/Scripts/ODS/Mhealth/Load_UShauri_PatientLabs.sql +++ b/Scripts/ODS/Mhealth/Load_UShauri_PatientLabs.sql @@ -1,8 +1,8 @@ ---- Loads data from MhealthCentral to ODS ------truncate table[ODS].[dbo].[Ushauri_PatientLabs] +-----truncate table[ODS].[dbo].[Mhealth_mLab_PatientLab] BEGIN - MERGE [ODS].[dbo].[Ushauri_PatientLabs] AS a + MERGE [ODS].[dbo].[Mhealth_mLab_PatientLab] AS a USING ( SELECT DISTINCT [PatientPK] ,[PatientPKHash] diff --git a/Scripts/ODS/Mhealth/Load_Ushauri_HEI.sql b/Scripts/ODS/Mhealth/Load_Ushauri_HEI.sql index 92cdd491..8e080743 100644 --- a/Scripts/ODS/Mhealth/Load_Ushauri_HEI.sql +++ b/Scripts/ODS/Mhealth/Load_Ushauri_HEI.sql @@ -1,6 +1,6 @@ BEGIN - MERGE [ODS].[dbo].[Ushauri_HEI] AS a + MERGE [ODS].[dbo].[Mhealth_Ushauri_HEI] AS a USING(SELECT Distinct PatientPK As UshauriPatientPK,PatientPKHash As UshauriPatientPKHash,PartnerName,SiteCode,SiteType,Emr,Project, FacilityName,PatientMNCH_ID,PatientHEI_ID,[1stDNAPCRDate],[2ndDNAPCRDate], @@ -10,20 +10,20 @@ BEGIN HEIExitDate_Date,DateCreated_Date,DateModified_Date FROM [MhealthCentral].[dbo].[pmtct_MNCH_HEI](NoLock) P - ) AS b + ) AS b ON( - a.[UshauriPatientPK] = b.UshauriPatientPK - and a.SiteCode = b.SiteCode + a.[UshauriPatientPK] = b.UshauriPatientPK + and a.SiteCode = b.SiteCode and a.PatientHEI_ID = b.PatientHEI_ID - + ) - - WHEN NOT MATCHED THEN - INSERT(UshauriPatientPK,UshauriPatientPKHash,PartnerName,SiteCode,SiteType,Emr,Project,FacilityName,PatientMNCH_ID,PatientHEI_ID,[1stDNAPCRDate],[2ndDNAPCRDate],[3rdDNAPCRDate],ConfirmatoryPCRDate,BasellineVLDate,FinalyAntibodyDate,[1stDNAPCR],[2ndDNAPCR],[3rdDNAPCR],ConfirmatoryPCR,BasellineVL,FinalyAntibody,HEIExitDate,HEIHIVStatus,HEIExitCriteria,DateCreated,DateModified,[1stDNAPCRDate_Date],[2ndDNAPCRDate_Date],[3rdDNAPCRDate_Date],ConfirmatoryPCRDate_Date,BasellineVLDate_Date,FinalyAntibodyDate_Date,HEIExitDate_Date,DateCreated_Date,DateModified_Date,LoadDate) + + WHEN NOT MATCHED THEN + INSERT(UshauriPatientPK,UshauriPatientPKHash,PartnerName,SiteCode,SiteType,Emr,Project,FacilityName,PatientMNCH_ID,PatientHEI_ID,[1stDNAPCRDate],[2ndDNAPCRDate],[3rdDNAPCRDate],ConfirmatoryPCRDate,BasellineVLDate,FinalyAntibodyDate,[1stDNAPCR],[2ndDNAPCR],[3rdDNAPCR],ConfirmatoryPCR,BasellineVL,FinalyAntibody,HEIExitDate,HEIHIVStatus,HEIExitCriteria,DateCreated,DateModified,[1stDNAPCRDate_Date],[2ndDNAPCRDate_Date],[3rdDNAPCRDate_Date],ConfirmatoryPCRDate_Date,BasellineVLDate_Date,FinalyAntibodyDate_Date,HEIExitDate_Date,DateCreated_Date,DateModified_Date,LoadDate) VALUES(UshauriPatientPK,UshauriPatientPKHash,PartnerName,SiteCode,SiteType,Emr,Project,FacilityName,PatientMNCH_ID,PatientHEI_ID,[1stDNAPCRDate],[2ndDNAPCRDate],[3rdDNAPCRDate],ConfirmatoryPCRDate,BasellineVLDate,FinalyAntibodyDate,[1stDNAPCR],[2ndDNAPCR],[3rdDNAPCR],ConfirmatoryPCR,BasellineVL,FinalyAntibody,HEIExitDate,HEIHIVStatus,HEIExitCriteria,DateCreated,DateModified,[1stDNAPCRDate_Date],[2ndDNAPCRDate_Date],[3rdDNAPCRDate_Date],ConfirmatoryPCRDate_Date,BasellineVLDate_Date,FinalyAntibodyDate_Date,HEIExitDate_Date,DateCreated_Date,DateModified_Date,Getdate()) - + WHEN MATCHED THEN - UPDATE SET + UPDATE SET a.[PartnerName] = b.[PartnerName], a.[SiteType] = b.[SiteType], a.[Emr] = b.[Emr], @@ -57,5 +57,5 @@ BEGIN a.[DateModified_Date] = b.[DateModified_Date] ; - - END \ No newline at end of file + + END diff --git a/Scripts/ODS/Mhealth/Load_Ushauri_HW_Exposures.sql b/Scripts/ODS/Mhealth/Load_Ushauri_HW_Exposures.sql index cb19ffc2..b4258965 100644 --- a/Scripts/ODS/Mhealth/Load_Ushauri_HW_Exposures.sql +++ b/Scripts/ODS/Mhealth/Load_Ushauri_HW_Exposures.sql @@ -1,7 +1,7 @@ ---- Loads Ushauri_HW_Exposures data from MhealthCentral to ODS --- truncate table [ODS].[dbo].[Ushauri_HW_Exposure] +-- truncate table [ODS].[dbo].[Mhealth_Ushauri_HWExposure] BEGIN - MERGE [ODS].[dbo].[Ushauri_HW_Exposure] + MERGE [ODS].[dbo].[Mhealth_Ushauri_HWExposure] AS a USING ( SELECT DISTINCT diff --git a/Scripts/ODS/Mhealth/Load_Ushauri_Patient.sql b/Scripts/ODS/Mhealth/Load_Ushauri_Patient.sql index 59e0c390..39775652 100644 --- a/Scripts/ODS/Mhealth/Load_Ushauri_Patient.sql +++ b/Scripts/ODS/Mhealth/Load_Ushauri_Patient.sql @@ -1,7 +1,7 @@ BEGIN - MERGE [ODS].[dbo].[Ushauri_Patient] AS a + MERGE [ODS].[dbo].[Mhealth_Ushauri_Patient] AS a USING(SELECT Distinct PatientPK,MPIPKV,null PatientPKHash,PartnerName,SiteCode,SiteType,PatientID,null PatientIDHash,FacilityID,Emr,Project,FacilityName, Gender,DOB_Date AS DOB,RegistrationDate_Date As RegistrationDate,RegistrationAtCCC_Date As RegistrationAtCCC,RegistrationAtPMTCT_Date As RegistrationAtPMTCT,RegistrationAtTBClinic,PatientSource,Region,District, @@ -11,18 +11,18 @@ BEGIN PatientResidentWard,PatientType,PopulationType,TransferInDate,Occupation,DateCreated_Date As DateCreated,DateModified_Date As DateModified,StatelitteName, Date_Created_Date As Date_Created,Date_Modified_Date As Date_Modified,PKV,NUPI FROM [mhealthCentral].[dbo].[CT_Patient](NoLock) P - ) AS b + ) AS b ON( - a.[UshauriPatientPK] = b.PatientPK - and a.SiteCode = b.SiteCode + a.[UshauriPatientPK] = b.PatientPK + and a.SiteCode = b.SiteCode ) - - WHEN NOT MATCHED THEN - INSERT([UshauriPatientPK],MPIPKV,PatientPKHash,PartnerName,SiteCode,SiteType,PatientID,PatientIDHash,FacilityID,Emr,Project,FacilityName,Gender,DOB,RegistrationDate,RegistrationAtCCC,RegistrationAtPMTCT,RegistrationAtTBClinic,PatientSource,Region,District,Village,ContactRelation,LastVisit,MaritalStatus,EducationLevel,DateConfirmedHIVPositive,PreviousARTExposure,PreviousARTStartDate,StatusAtCCC,StatusAtPMTCT,StatusAtTBClinic,Inschool,KeyPopulationType,Orphan,County,PatientResidentLocation,PatientResidentSubCounty,PatientResidentSubLocation,PatientResidentVillage,PatientResidentWard,PatientType,PopulationType,TransferInDate,Occupation,DateCreated,DateModified,StatelitteName,Date_Created,Date_Modified,PKV,NUPI,LoadDate) + + WHEN NOT MATCHED THEN + INSERT([UshauriPatientPK],MPIPKV,PatientPKHash,PartnerName,SiteCode,SiteType,PatientID,PatientIDHash,FacilityID,Emr,Project,FacilityName,Gender,DOB,RegistrationDate,RegistrationAtCCC,RegistrationAtPMTCT,RegistrationAtTBClinic,PatientSource,Region,District,Village,ContactRelation,LastVisit,MaritalStatus,EducationLevel,DateConfirmedHIVPositive,PreviousARTExposure,PreviousARTStartDate,StatusAtCCC,StatusAtPMTCT,StatusAtTBClinic,Inschool,KeyPopulationType,Orphan,County,PatientResidentLocation,PatientResidentSubCounty,PatientResidentSubLocation,PatientResidentVillage,PatientResidentWard,PatientType,PopulationType,TransferInDate,Occupation,DateCreated,DateModified,StatelitteName,Date_Created,Date_Modified,PKV,NUPI,LoadDate) VALUES(PatientPK,MPIPKV,PatientPKHash,PartnerName,SiteCode,SiteType,PatientID,PatientIDHash,FacilityID,Emr,Project,FacilityName,Gender,DOB,RegistrationDate,RegistrationAtCCC,RegistrationAtPMTCT,RegistrationAtTBClinic,PatientSource,Region,District,Village,ContactRelation,LastVisit,MaritalStatus,EducationLevel,DateConfirmedHIVPositive,PreviousARTExposure,PreviousARTStartDate,StatusAtCCC,StatusAtPMTCT,StatusAtTBClinic,Inschool,KeyPopulationType,Orphan,County,PatientResidentLocation,PatientResidentSubCounty,PatientResidentSubLocation,PatientResidentVillage,PatientResidentWard,PatientType,PopulationType,TransferInDate,Occupation,DateCreated,DateModified,StatelitteName,Date_Created,Date_Modified,PKV,NUPI,Getdate()) - + WHEN MATCHED THEN - UPDATE SET + UPDATE SET a.[MPIPKV] = b.[MPIPKV], a.[PartnerName] = b.[PartnerName], a.[SiteType] = b.[SiteType], @@ -77,13 +77,13 @@ BEGIN Select Sitecode, UshauriPatientPK, - + ROW_NUMBER() OVER (PARTITION BY UshauriPatientPK,Sitecode ORDER BY UshauriPatientPK) Row_Num - FROM [ODS].[dbo].[Ushauri_Patient](NoLock) + FROM [ODS].[dbo].[Mhealth_Ushauri_Patient](NoLock) ) - delete from cte + delete from cte Where Row_Num >1 ; - + END diff --git a/Scripts/ODS/Mhealth/Load_Ushauri_PatientAppointments.sql b/Scripts/ODS/Mhealth/Load_Ushauri_PatientAppointments.sql index 864630fd..73cef494 100644 --- a/Scripts/ODS/Mhealth/Load_Ushauri_PatientAppointments.sql +++ b/Scripts/ODS/Mhealth/Load_Ushauri_PatientAppointments.sql @@ -1,6 +1,6 @@ BEGIN - MERGE [ODS].[dbo].[Ushauri_PatientAppointments] AS a + MERGE [ODS].[dbo].[Mhealth_Ushauri_PatientAppointments] AS a USING(SELECT Distinct PatientPK,Null As PatientPKHash,SiteCode,SiteType,PatientID,Null As PatientIDHash, NUPI,PartnerName,FacilityID,FacilityName, DOB_Date As DOB,Gender,MaritalStatus,PatientResidentCounty,PatientResidentLocation,PatientResidentSubCounty, @@ -15,20 +15,20 @@ BEGIN MissedAppointmentSMSSent,MissedAppointmentSMSSendDate_Date As MissedAppointmentSMSSendDate,MissedAppointmentSMSDeliveryStatus,MissedAppointmentSMSDeliveryFailureReason, TracingCost,TracingOutcome,TracingOutcomeDate_Date As TracingOutcomeDate,DateReturnedToCare_Date As DateReturnedToCare,DaysDefaulted FROM [mhealthCentral].[dbo].[CT_PatientAppointments](NoLock) P - ) AS b + ) AS b ON( - a.SiteCode = b.SiteCode + a.SiteCode = b.SiteCode and a.[AppointmentID] = b.[AppointmentID] ) - - WHEN NOT MATCHED THEN - INSERT([UshauriPatientPK],PatientPKHash,SiteCode,SiteType,PatientID,PatientIDHash,NUPI,PartnerName,FacilityID,FacilityName,DOB,Gender,MaritalStatus,PatientResidentCounty,PatientResidentLocation,PatientResidentSubCounty,PatientResidentSubLocation,PatientResidentVillage,PatientResidentWard,RegistrationDate,RegistrationAtCCC,RegistrationAtPMTCT,RegistrationAtTBClinic,StatusAtCCC,StatusAtPMTCT,StatusAtTBClinic,AgeAtAppointment,AppointmentID,AppointmentDate,AppointmentType,AppointmentStatus,EntryPoint,VisitType,DateAttended,ConsentForSMS,SMSLanguage,SMSTargetGroup,SMSPreferredSendTime,FourWeekSMSSent,FourWeekSMSSendDate,FourWeekSMSDeliveryStatus,FourWeekSMSDeliveryFailureReason,ThreeWeekSMSSent,ThreeWeekSMSSendDate,ThreeWeekSMSDeliveryStatus,ThreeWeekSMSDeliveryFailureReason,TwoWeekSMSSent,TwoWeekSMSSendDate,TwoWeekSMSDeliveryStatus,TwoWeekSMSDeliveryFailureReason,OneWeekSMSSent,OneWeekSMSSendDate,OneWeekSMSDeliveryStatus,OneWeekSMSDeliveryFailureReason,OneDaySMSSent,OneDaySMSSendDate,OneDaySMSDeliveryStatus,OneDaySMSDeliveryFailureReason,MissedAppointmentSMSSent,MissedAppointmentSMSSendDate,MissedAppointmentSMSDeliveryStatus,MissedAppointmentSMSDeliveryFailureReason,TracingOutCost,TracingOutcome,TracingOutcomeDate,DateReturnedToCare,DaysDefaulted,LoadDate) + + WHEN NOT MATCHED THEN + INSERT([UshauriPatientPK],PatientPKHash,SiteCode,SiteType,PatientID,PatientIDHash,NUPI,PartnerName,FacilityID,FacilityName,DOB,Gender,MaritalStatus,PatientResidentCounty,PatientResidentLocation,PatientResidentSubCounty,PatientResidentSubLocation,PatientResidentVillage,PatientResidentWard,RegistrationDate,RegistrationAtCCC,RegistrationAtPMTCT,RegistrationAtTBClinic,StatusAtCCC,StatusAtPMTCT,StatusAtTBClinic,AgeAtAppointment,AppointmentID,AppointmentDate,AppointmentType,AppointmentStatus,EntryPoint,VisitType,DateAttended,ConsentForSMS,SMSLanguage,SMSTargetGroup,SMSPreferredSendTime,FourWeekSMSSent,FourWeekSMSSendDate,FourWeekSMSDeliveryStatus,FourWeekSMSDeliveryFailureReason,ThreeWeekSMSSent,ThreeWeekSMSSendDate,ThreeWeekSMSDeliveryStatus,ThreeWeekSMSDeliveryFailureReason,TwoWeekSMSSent,TwoWeekSMSSendDate,TwoWeekSMSDeliveryStatus,TwoWeekSMSDeliveryFailureReason,OneWeekSMSSent,OneWeekSMSSendDate,OneWeekSMSDeliveryStatus,OneWeekSMSDeliveryFailureReason,OneDaySMSSent,OneDaySMSSendDate,OneDaySMSDeliveryStatus,OneDaySMSDeliveryFailureReason,MissedAppointmentSMSSent,MissedAppointmentSMSSendDate,MissedAppointmentSMSDeliveryStatus,MissedAppointmentSMSDeliveryFailureReason,TracingOutCost,TracingOutcome,TracingOutcomeDate,DateReturnedToCare,DaysDefaulted,LoadDate) VALUES(PatientPK,PatientPKHash,SiteCode,SiteType,PatientID,PatientIDHash,NUPI,PartnerName,FacilityID,FacilityName,DOB,Gender,MaritalStatus,PatientResidentCounty,PatientResidentLocation,PatientResidentSubCounty,PatientResidentSubLocation,PatientResidentVillage,PatientResidentWard,RegistrationDate,RegistrationAtCCC,RegistrationAtPMTCT,RegistrationAtTBClinic,StatusAtCCC,StatusAtPMTCT,StatusAtTBClinic,AgeAtAppointment,AppointmentID,AppointmentDate,AppointmentType,AppointmentStatus,EntryPoint,VisitType,DateAttended,ConsentForSMS,SMSLanguage,SMSTargetGroup,SMSPreferredSendTime,FourWeekSMSSent,FourWeekSMSSendDate,FourWeekSMSDeliveryStatus,FourWeekSMSDeliveryFailureReason,ThreeWeekSMSSent,ThreeWeekSMSSendDate,ThreeWeekSMSDeliveryStatus,ThreeWeekSMSDeliveryFailureReason,TwoWeekSMSSent,TwoWeekSMSSendDate,TwoWeekSMSDeliveryStatus,TwoWeekSMSDeliveryFailureReason,OneWeekSMSSent,OneWeekSMSSendDate,OneWeekSMSDeliveryStatus,OneWeekSMSDeliveryFailureReason,OneDaySMSSent,OneDaySMSSendDate,OneDaySMSDeliveryStatus,OneDaySMSDeliveryFailureReason,MissedAppointmentSMSSent,MissedAppointmentSMSSendDate,MissedAppointmentSMSDeliveryStatus,MissedAppointmentSMSDeliveryFailureReason,TracingCost,TracingOutcome,TracingOutcomeDate,DateReturnedToCare,DaysDefaulted,Getdate()) - + WHEN MATCHED THEN - UPDATE SET + UPDATE SET a.[SiteType] = b.[SiteType], - a.[PatientID] = b.[PatientID], + a.[PatientID] = b.[PatientID], a.[NUPI] = b.[NUPI], a.[PartnerName] = b.[PartnerName], a.[FacilityName] = b.[FacilityName], @@ -88,5 +88,5 @@ BEGIN a.[DateReturnedToCare] = b.[DateReturnedToCare], a.[DaysDefaulted] = b.[DaysDefaulted]; - - END \ No newline at end of file + + END diff --git a/Scripts/ODS/Mhealth/Load_Ushauri_PatientReferral.sql b/Scripts/ODS/Mhealth/Load_Ushauri_PatientReferral.sql index 62e3a06b..241a0452 100644 --- a/Scripts/ODS/Mhealth/Load_Ushauri_PatientReferral.sql +++ b/Scripts/ODS/Mhealth/Load_Ushauri_PatientReferral.sql @@ -1,7 +1,7 @@ BEGIN - MERGE [ODS].[dbo].[Ushauri_PatientReferral] AS a + MERGE [ODS].[dbo].[Mhealth_FacilityReferral_Patient] AS a USING(SELECT DISTINCT [ReferralPK] ,[ReferralPKHash] ,[PatientID] @@ -17,18 +17,18 @@ BEGIN ,[TransferInFacilityName] ,[TransferStatus] FROM [MhealthCentral].[dbo].[CT_PatientReferral] - ) AS b + ) AS b ON( a.[UshauriReferralPK] = b.[ReferralPK] and - a.TransferoutDate = b.TransferoutDate + a.TransferoutDate = b.TransferoutDate ) - - WHEN NOT MATCHED THEN - INSERT([UshauriReferralPK],[UshauriReferralPKHash],PatientID,PatientIDHash,ReferralType,TransferOutDate,TransferOutPartnerName,TransferOutSiteCode,TransferOutFacilityName,TransferInDate,TransferInPartnerName,TransferInSiteCode,TransferInFacilityName,TransferStatus,LoadDate) + + WHEN NOT MATCHED THEN + INSERT([UshauriReferralPK],[UshauriReferralPKHash],PatientID,PatientIDHash,ReferralType,TransferOutDate,TransferOutPartnerName,TransferOutSiteCode,TransferOutFacilityName,TransferInDate,TransferInPartnerName,TransferInSiteCode,TransferInFacilityName,TransferStatus,LoadDate) VALUES(ReferralPK,ReferralPKHash,PatientID,PatientIDHash,ReferralType,TransferOutDate,TransferOutPartnerName,TransferOutSiteCode,TransferOutFacilityName,TransferInDate,TransferInPartnerName,TransferInSiteCode,TransferInFacilityName,TransferStatus,getdate()) - + WHEN MATCHED THEN - UPDATE SET + UPDATE SET a.[ReferralType]=b.[ReferralType], a.[TransferOutDate]=b.[TransferOutDate], a.[TransferOutPartnerName]=b.[TransferOutPartnerName], @@ -39,5 +39,5 @@ BEGIN a.[TransferInSiteCode]=b.[TransferInSiteCode], a.[TransferInFacilityName]=b.[TransferInFacilityName], a.[TransferStatus]=b.[TransferStatus]; - + END From 826d7ff5bb8478a5e47e32c4bf2e3d36b56ee5b7 Mon Sep 17 00:00:00 2001 From: NewtonMutugi Date: Tue, 23 Jul 2024 15:05:44 +0300 Subject: [PATCH 10/21] Update table names for Mhealth applications (post load scripts) --- .../1_RecreatePatient_PatientPKColumns.sql | 40 +++++++++--------- ...tePatientAppointments_PatientPKColumns.sql | 36 ++++++++-------- .../Post_Load/3_Update_PatientPatientPK.sql | 4 +- ...4_Update_PatientAppointments_PatientPK.sql | 4 +- .../5_Recreate_Hei_PatientPKColumn.sql | 42 +++++++++---------- .../Post_Load/6_Update_HEI_PatientPK.sql | 6 +-- ...PatientPK_ReferralPatientPKHashcolumns.sql | 34 +++++++-------- ...date_PatientReferral_ReferralPatientPK.sql | 2 +- .../Post_Load/9_Update_CT_PatientLabs.sql | 4 +- 9 files changed, 86 insertions(+), 86 deletions(-) diff --git a/Scripts/ODS/Mhealth/Post_Load/1_RecreatePatient_PatientPKColumns.sql b/Scripts/ODS/Mhealth/Post_Load/1_RecreatePatient_PatientPKColumns.sql index 7b53d5bf..9df6b275 100644 --- a/Scripts/ODS/Mhealth/Post_Load/1_RecreatePatient_PatientPKColumns.sql +++ b/Scripts/ODS/Mhealth/Post_Load/1_RecreatePatient_PatientPKColumns.sql @@ -1,46 +1,46 @@ /* Renaming patientPK column coming from mhealth to UshauriPatientPK for the purpose of matching patient from C&T to ones coming from ushauri. */ BEGIN - - IF EXISTS (SELECT * FROM sys.columns /* 1st if confirms if the PatientPK column exists on [ODS].[dbo].[Ushauri_Patient] exists on ODS */ - WHERE Name = N'PatientPK' - AND Object_ID = Object_ID(N'[ODS].[dbo].[Ushauri_Patient]')) + + IF EXISTS (SELECT * FROM sys.columns /* 1st if confirms if the PatientPK column exists on [ODS].[dbo].[Mhealth_Ushauri_Patient] exists on ODS */ + WHERE Name = N'PatientPK' + AND Object_ID = Object_ID(N'[ODS].[dbo].[Mhealth_Ushauri_Patient]')) BEGIN IF NOT EXISTS (SELECT * /* If above condition is met, check if Ushauri_Patient exists. If it exists escape. If it doesn't exist create it*/ FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Ushauri_Patient' - AND COLUMN_NAME = 'UshauriPatientPK') + AND COLUMN_NAME = 'UshauriPatientPK') BEGIN - EXEC sp_rename '[ODS].[dbo].[Ushauri_Patient].PatientPK', 'UshauriPatientPK', 'COLUMN'; + EXEC sp_rename '[ODS].[dbo].[Mhealth_Ushauri_Patient].PatientPK', 'UshauriPatientPK', 'COLUMN'; END END - IF EXISTS (SELECT * FROM sys.columns /* 1st if confirms if the PatientPK column exists on [ODS].[dbo].[Ushauri_Patient] exists on ODS */ - WHERE Name = N'PatientPKHash' - AND Object_ID = Object_ID(N'[ODS].[dbo].[Ushauri_Patient]')) + IF EXISTS (SELECT * FROM sys.columns /* 1st if confirms if the PatientPK column exists on [ODS].[dbo].[Mhealth_Ushauri_Patient] exists on ODS */ + WHERE Name = N'PatientPKHash' + AND Object_ID = Object_ID(N'[ODS].[dbo].[Mhealth_Ushauri_Patient]')) BEGIN IF NOT EXISTS (SELECT * /* If above condition is met, check if Ushauri_Patient exists. If it exists escape. If it doesn't exist create it*/ FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_NAME = 'Ushauri_Patient' - AND COLUMN_NAME = 'PatientPKHash') + WHERE TABLE_NAME = 'Mhealth_Ushauri_Patient' + AND COLUMN_NAME = 'PatientPKHash') BEGIN - EXEC sp_rename '[ODS].[dbo].[Ushauri_Patient].PatientPKHash', 'PatientPKHash', 'COLUMN'; + EXEC sp_rename '[ODS].[dbo].[Mhealth_Ushauri_Patient].PatientPKHash', 'PatientPKHash', 'COLUMN'; END END - + IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE - TABLE_NAME = 'Ushauri_Patient' AND COLUMN_NAME = 'patientPK') + TABLE_NAME = 'Mhealth_Ushauri_Patient' AND COLUMN_NAME = 'patientPK') BEGIN - ALTER TABLE [ODS].[dbo].[Ushauri_Patient] + ALTER TABLE [ODS].[dbo].[Mhealth_Ushauri_Patient] ADD patientPK int NULL END; @@ -50,9 +50,9 @@ BEGIN FROM INFORMATION_SCHEMA.COLUMNS WHERE - TABLE_NAME = 'Ushauri_Patient' AND COLUMN_NAME = 'PatientPKHash') + TABLE_NAME = 'Mhealth_Ushauri_Patient' AND COLUMN_NAME = 'PatientPKHash') BEGIN - alter table [ODS].[dbo].[Ushauri_Patient] + alter table [ODS].[dbo].[Mhealth_Ushauri_Patient] add PatientPKHash nvarchar(150) null END; @@ -62,10 +62,10 @@ BEGIN FROM INFORMATION_SCHEMA.COLUMNS WHERE - TABLE_NAME = 'Ushauri_Patient' AND COLUMN_NAME = 'UshauriPatientPKHash') + TABLE_NAME = 'Mhealth_Ushauri_Patient' AND COLUMN_NAME = 'UshauriPatientPKHash') BEGIN - alter table [ODS].[dbo].[Ushauri_Patient] + alter table [ODS].[dbo].[Mhealth_Ushauri_Patient] add UshauriPatientPKHash nvarchar(150) null END; -END \ No newline at end of file +END diff --git a/Scripts/ODS/Mhealth/Post_Load/2_RecreatePatientAppointments_PatientPKColumns.sql b/Scripts/ODS/Mhealth/Post_Load/2_RecreatePatientAppointments_PatientPKColumns.sql index a40cc460..1657f387 100644 --- a/Scripts/ODS/Mhealth/Post_Load/2_RecreatePatientAppointments_PatientPKColumns.sql +++ b/Scripts/ODS/Mhealth/Post_Load/2_RecreatePatientAppointments_PatientPKColumns.sql @@ -3,45 +3,45 @@ BEGIN IF EXISTS (SELECT * FROM sys.columns /* 1st if confirms if the PatientPK column exists on [ODS].[dbo].[Ushauri_Patient] exists on ODS */ - WHERE Name = N'PatientPK' - AND Object_ID = Object_ID(N'[ODS].[dbo].[Ushauri_PatientAppointments]')) + WHERE Name = N'PatientPK' + AND Object_ID = Object_ID(N'[ODS].[dbo].[Mhealth_Ushauri_PatientAppointments]')) BEGIN IF NOT EXISTS (SELECT * /* If above condition is met, check if Ushauri_Patient exists. If it exists escape. If it doesn't exist create it*/ FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_NAME = 'Ushauri_PatientAppointments' - AND COLUMN_NAME = 'UshauriPatientPK') + WHERE TABLE_NAME = 'Mhealth_Ushauri_PatientAppointments' + AND COLUMN_NAME = 'UshauriPatientPK') BEGIN - EXEC sp_rename '[ODS].[dbo].[Ushauri_PatientAppointments].PatientPK', 'UshauriPatientPK', 'COLUMN'; + EXEC sp_rename '[ODS].[dbo].[Mhealth_Ushauri_PatientAppointments].PatientPK', 'UshauriPatientPK', 'COLUMN'; END END IF EXISTS (SELECT * FROM sys.columns /* 1st if confirms if the PatientPK column exists on [ODS].[dbo].[Ushauri_Patient] exists on ODS */ - WHERE Name = N'PatientPKHash' - AND Object_ID = Object_ID(N'[ODS].[dbo].[Ushauri_PatientAppointments]')) + WHERE Name = N'PatientPKHash' + AND Object_ID = Object_ID(N'[ODS].[dbo].[Mhealth_Ushauri_PatientAppointments]')) BEGIN IF NOT EXISTS (SELECT * /* If above condition is met, check if Ushauri_Patient exists. If it exists escape. If it doesn't exist create it*/ FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_NAME = 'Ushauri_PatientAppointments' - AND COLUMN_NAME = 'PatientPKHash') + WHERE TABLE_NAME = 'Mhealth_Ushauri_PatientAppointments' + AND COLUMN_NAME = 'PatientPKHash') BEGIN - EXEC sp_rename '[ODS].[dbo].[Ushauri_PatientAppointments].PatientPKHash', 'PatientPKHash', 'COLUMN'; + EXEC sp_rename '[ODS].[dbo].[Mhealth_Ushauri_PatientAppointments].PatientPKHash', 'PatientPKHash', 'COLUMN'; END END - + IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE - TABLE_NAME = 'Ushauri_PatientAppointments' AND COLUMN_NAME = 'patientPK') + TABLE_NAME = 'Mhealth_Ushauri_PatientAppointments' AND COLUMN_NAME = 'patientPK') BEGIN - ALTER TABLE [ODS].[dbo].[Ushauri_PatientAppointments] + ALTER TABLE [ODS].[dbo].[Mhealth_Ushauri_PatientAppointments] ADD patientPK int NULL END; @@ -51,9 +51,9 @@ BEGIN FROM INFORMATION_SCHEMA.COLUMNS WHERE - TABLE_NAME = 'Ushauri_PatientAppointments' AND COLUMN_NAME = 'PatientPKHash') + TABLE_NAME = 'Mhealth_Ushauri_PatientAppointments' AND COLUMN_NAME = 'PatientPKHash') BEGIN - alter table [ODS].[dbo].[Ushauri_PatientAppointments] + alter table [ODS].[dbo].[Mhealth_Ushauri_PatientAppointments] add PatientPKHash nvarchar(150) null END; @@ -63,10 +63,10 @@ BEGIN FROM INFORMATION_SCHEMA.COLUMNS WHERE - TABLE_NAME = 'Ushauri_PatientAppointments' AND COLUMN_NAME = 'UshauriPatientPKHash') + TABLE_NAME = 'Mhealth_Ushauri_PatientAppointments' AND COLUMN_NAME = 'UshauriPatientPKHash') BEGIN - alter table [ODS].[dbo].[Ushauri_PatientAppointments] + alter table [ODS].[dbo].[Mhealth_Ushauri_PatientAppointments] add UshauriPatientPKHash nvarchar(150) null END; -END \ No newline at end of file +END diff --git a/Scripts/ODS/Mhealth/Post_Load/3_Update_PatientPatientPK.sql b/Scripts/ODS/Mhealth/Post_Load/3_Update_PatientPatientPK.sql index cbc7eb43..a8941a38 100644 --- a/Scripts/ODS/Mhealth/Post_Load/3_Update_PatientPatientPK.sql +++ b/Scripts/ODS/Mhealth/Post_Load/3_Update_PatientPatientPK.sql @@ -3,11 +3,11 @@ registered in the EMR. Indentified using the patientCCCNumber */ UPDATE a SET a.PatientPK = null,a.PatientPKHash =null -FROM [ODS].[dbo].[Ushauri_Patient] a; +FROM [ODS].[dbo].[Mhealth_Ushauri_Patient] a; UPDATE a SET a.PatientPK = p.PatientPK -FROM [ODS].[dbo].[Ushauri_Patient] a +FROM [ODS].[dbo].[Mhealth_Ushauri_Patient] a JOIN [ODS].[dbo].[CT_Patient] p ON a.sitecode = p.sitecode AND a.patientID = p.patientID; diff --git a/Scripts/ODS/Mhealth/Post_Load/4_Update_PatientAppointments_PatientPK.sql b/Scripts/ODS/Mhealth/Post_Load/4_Update_PatientAppointments_PatientPK.sql index 12ee9f4c..c67b81d3 100644 --- a/Scripts/ODS/Mhealth/Post_Load/4_Update_PatientAppointments_PatientPK.sql +++ b/Scripts/ODS/Mhealth/Post_Load/4_Update_PatientAppointments_PatientPK.sql @@ -1,12 +1,12 @@ UPDATE a SET a.PatientPK = null,a.PatientPKHash =null -FROM [ODS].[dbo].[Ushauri_PatientAppointments] a; +FROM [ODS].[dbo].[Mhealth_Ushauri_PatientAppointments] a; /* Update of the PatientPK from the C&T patients.This are for the patients who are in ushauri and have been registered in the EMR. Indentified using the patientCCCNumber */ UPDATE a SET a.PatientPK = p.PatientPK -FROM [ODS].[dbo].[Ushauri_PatientAppointments] a +FROM [ODS].[dbo].[Mhealth_Ushauri_PatientAppointments] a JOIN [ODS].[dbo].[CT_Patient] p ON a.sitecode = p.sitecode AND a.patientID = p.patientID; diff --git a/Scripts/ODS/Mhealth/Post_Load/5_Recreate_Hei_PatientPKColumn.sql b/Scripts/ODS/Mhealth/Post_Load/5_Recreate_Hei_PatientPKColumn.sql index 039e1140..264892ac 100644 --- a/Scripts/ODS/Mhealth/Post_Load/5_Recreate_Hei_PatientPKColumn.sql +++ b/Scripts/ODS/Mhealth/Post_Load/5_Recreate_Hei_PatientPKColumn.sql @@ -1,46 +1,46 @@ /* Renaming patientPK column coming from mhealth to UshauriPatientPK for the purpose of matching patient from C&T to ones coming from ushauri. */ BEGIN - + IF EXISTS (SELECT * FROM sys.columns /* 1st if confirms if the PatientPK column exists on [ODS].[dbo].[Ushauri_Patient] exists on ODS */ - WHERE Name = N'PatientPK' - AND Object_ID = Object_ID(N'[ODS].[dbo].[Ushauri_HEI]')) + WHERE Name = N'PatientPK' + AND Object_ID = Object_ID(N'[ODS].[dbo].[Mhealth_Ushauri_HEI]')) BEGIN - IF NOT EXISTS (SELECT * /* If above condition is met, check if [ODS].[dbo].[Ushauri_HEI] exists. If it exists escape. If it doesn't exist create it*/ + IF NOT EXISTS (SELECT * /* If above condition is met, check if [ODS].[dbo].[Mhealth_Ushauri_HEI] exists. If it exists escape. If it doesn't exist create it*/ FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_NAME = 'Ushauri_Patient' - AND COLUMN_NAME = 'UshauriPatientPK') + WHERE TABLE_NAME = 'Mhealth_Ushauri_Patient' + AND COLUMN_NAME = 'UshauriPatientPK') BEGIN - EXEC sp_rename '[ODS].[dbo].[Ushauri_HEI].PatientPK', 'UshauriPatientPK', 'COLUMN'; + EXEC sp_rename '[ODS].[dbo].[Mhealth_Ushauri_HEI].PatientPK', 'UshauriPatientPK', 'COLUMN'; END END - IF EXISTS (SELECT * FROM sys.columns /* 1st if confirms if the PatientPK column exists on [ODS].[dbo].[Ushauri_HEI] exists on ODS */ - WHERE Name = N'PatientPKHash' - AND Object_ID = Object_ID(N'[ODS].[dbo].[Ushauri_HEI]')) + IF EXISTS (SELECT * FROM sys.columns /* 1st if confirms if the PatientPK column exists on [ODS].[dbo].[Mhealth_Ushauri_HEI] exists on ODS */ + WHERE Name = N'PatientPKHash' + AND Object_ID = Object_ID(N'[ODS].[dbo].[Mhealth_Ushauri_HEI]')) BEGIN IF NOT EXISTS (SELECT * /* If above condition is met, check if Ushauri_Patient exists. If it exists escape. If it doesn't exist create it*/ FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_NAME = 'Ushauri_HEI' - AND COLUMN_NAME = 'PatientPKHash') + WHERE TABLE_NAME = 'Mhealth_Ushauri_HEI' + AND COLUMN_NAME = 'PatientPKHash') BEGIN - EXEC sp_rename '[ODS].[dbo].[Ushauri_HEI].PatientPKHash', 'PatientPKHash', 'COLUMN'; + EXEC sp_rename '[ODS].[dbo].[Mhealth_Ushauri_HEI].PatientPKHash', 'PatientPKHash', 'COLUMN'; END END - + IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE - TABLE_NAME = 'Ushauri_HEI' AND COLUMN_NAME = 'patientPK') + TABLE_NAME = 'Mhealth_Ushauri_HEI' AND COLUMN_NAME = 'patientPK') BEGIN - ALTER TABLE [ODS].[dbo].[Ushauri_HEI] + ALTER TABLE [ODS].[dbo].[Mhealth_Ushauri_HEI] ADD patientPK int NULL END; @@ -50,9 +50,9 @@ BEGIN FROM INFORMATION_SCHEMA.COLUMNS WHERE - TABLE_NAME = 'Ushauri_HEI' AND COLUMN_NAME = 'PatientPKHash') + TABLE_NAME = 'Mhealth_Ushauri_HEI' AND COLUMN_NAME = 'PatientPKHash') BEGIN - alter table [ODS].[dbo].[Ushauri_HEI] + alter table [ODS].[dbo].[Mhealth_Ushauri_HEI] add PatientPKHash nvarchar(150) null END; @@ -62,11 +62,11 @@ BEGIN FROM INFORMATION_SCHEMA.COLUMNS WHERE - TABLE_NAME = 'Ushauri_HEI' AND COLUMN_NAME = 'UshauriPatientPKHash') + TABLE_NAME = 'Mhealth_Ushauri_HEI' AND COLUMN_NAME = 'UshauriPatientPKHash') BEGIN - alter table [ODS].[dbo].[Ushauri_HEI] + alter table [ODS].[dbo].[Mhealth_Ushauri_HEI] add UshauriPatientPKHash nvarchar(150) null END; END -------------------------------------------------------- \ No newline at end of file +------------------------------------------------------- diff --git a/Scripts/ODS/Mhealth/Post_Load/6_Update_HEI_PatientPK.sql b/Scripts/ODS/Mhealth/Post_Load/6_Update_HEI_PatientPK.sql index a80efe3f..25c41ee9 100644 --- a/Scripts/ODS/Mhealth/Post_Load/6_Update_HEI_PatientPK.sql +++ b/Scripts/ODS/Mhealth/Post_Load/6_Update_HEI_PatientPK.sql @@ -3,11 +3,11 @@ registered in the EMR. There seem to be no match. Need to know the link from the UPDATE a SET a.PatientPK = null,a.PatientPKHash =null -FROM [ODS].[dbo].[Ushauri_HEI] a; +FROM [ODS].[dbo].[Mhealth_Ushauri_HEI] a; --Lanyo to advice on the leankage between Ushauri Hei and MNCH_Heis --UPDATE a --SET a.PatientPK = p.PatientPK ---FROM [ODS].[dbo].[Ushauri_HEI] a +--FROM [ODS].[dbo].[Mhealth_Ushauri_HEI] a -- JOIN ods.dbo.MNCH_HEIs p ---ON a.sitecode = p.sitecode AND a.UshauriPatientPK = p.PatientPk; \ No newline at end of file +--ON a.sitecode = p.sitecode AND a.UshauriPatientPK = p.PatientPk; diff --git a/Scripts/ODS/Mhealth/Post_Load/7_RecreateReferralPatientPK_ReferralPatientPKHashcolumns.sql b/Scripts/ODS/Mhealth/Post_Load/7_RecreateReferralPatientPK_ReferralPatientPKHashcolumns.sql index bad8fd1e..de44183f 100644 --- a/Scripts/ODS/Mhealth/Post_Load/7_RecreateReferralPatientPK_ReferralPatientPKHashcolumns.sql +++ b/Scripts/ODS/Mhealth/Post_Load/7_RecreateReferralPatientPK_ReferralPatientPKHashcolumns.sql @@ -2,46 +2,46 @@ BEGIN - IF EXISTS (SELECT * FROM sys.columns /* 1st if confirms if the ReferralPK column exists on [ODS].[dbo].[Ushauri_PatientReferral] exists on ODS */ - WHERE Name = N'ReferralPK' - AND Object_ID = Object_ID(N'[ODS].[dbo].[Ushauri_PatientReferral]')) + IF EXISTS (SELECT * FROM sys.columns /* 1st if confirms if the ReferralPK column exists on [ODS].[dbo].[Mhealth_FacilityReferral_Patient] exists on ODS */ + WHERE Name = N'ReferralPK' + AND Object_ID = Object_ID(N'[ODS].[dbo].[Mhealth_FacilityReferral_Patient]')) BEGIN IF NOT EXISTS (SELECT * /* If above condition is met, check if Ushauri_PatientReferral exists. If it exists escape. If it doesn't exist create it*/ FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Ushauri_PatientReferral' - AND COLUMN_NAME = 'UshauriReferralPK') + AND COLUMN_NAME = 'UshauriReferralPK') BEGIN - EXEC sp_rename '[ODS].[dbo].[Ushauri_PatientReferral].ReferralPK', 'UshauriReferralPK', 'COLUMN'; + EXEC sp_rename '[ODS].[dbo].[Mhealth_FacilityReferral_Patient].ReferralPK', 'UshauriReferralPK', 'COLUMN'; END END - IF EXISTS (SELECT * FROM sys.columns /* 1st if confirms if the ReferralPKHash column exists on [ODS].[dbo].[Ushauri_PatientReferral] exists on ODS */ - WHERE Name = N'ReferralPKHash' - AND Object_ID = Object_ID(N'[ODS].[dbo].[Ushauri_PatientReferral]')) + IF EXISTS (SELECT * FROM sys.columns /* 1st if confirms if the ReferralPKHash column exists on [ODS].[dbo].[Mhealth_FacilityReferral_Patient] exists on ODS */ + WHERE Name = N'ReferralPKHash' + AND Object_ID = Object_ID(N'[ODS].[dbo].[Mhealth_FacilityReferral_Patient]')) BEGIN IF NOT EXISTS (SELECT * /* If above condition is met, check if Ushauri_PatientReferral exists. If it exists escape. If it doesn't exist create it*/ FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_NAME = 'Ushauri_PatientReferral' - AND COLUMN_NAME = 'UshariReferralPKHash') + WHERE TABLE_NAME = 'Mhealth_FacilityReferral_Patient' + AND COLUMN_NAME = 'UshariReferralPKHash') BEGIN - EXEC sp_rename '[ODS].[dbo].[Ushauri_PatientReferral].ReferralPKHash', 'UshauriReferralPKHash', 'COLUMN'; + EXEC sp_rename '[ODS].[dbo].[Mhealth_FacilityReferral_Patient].ReferralPKHash', 'UshauriReferralPKHash', 'COLUMN'; END END - + IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE - TABLE_NAME = 'Ushauri_PatientReferral' AND COLUMN_NAME = 'ReferralpatientPK') + TABLE_NAME = 'Mhealth_FacilityReferral_Patient' AND COLUMN_NAME = 'ReferralpatientPK') BEGIN - ALTER TABLE [ODS].[dbo].[Ushauri_PatientReferral] + ALTER TABLE [ODS].[dbo].[Mhealth_FacilityReferral_Patient] ADD ReferralpatientPK int NULL END; @@ -51,10 +51,10 @@ BEGIN FROM INFORMATION_SCHEMA.COLUMNS WHERE - TABLE_NAME = 'Ushauri_PatientReferral' AND COLUMN_NAME = 'ReferralPatientPKHash') + TABLE_NAME = 'Mhealth_FacilityReferral_Patient' AND COLUMN_NAME = 'ReferralPatientPKHash') BEGIN - alter table [ODS].[dbo].[Ushauri_PatientReferral] + alter table [ODS].[dbo].[Mhealth_FacilityReferral_Patient] add ReferralPatientPKHash nvarchar(150) null END; -END \ No newline at end of file +END diff --git a/Scripts/ODS/Mhealth/Post_Load/8_Update_PatientReferral_ReferralPatientPK.sql b/Scripts/ODS/Mhealth/Post_Load/8_Update_PatientReferral_ReferralPatientPK.sql index eba9e4af..f57c479a 100644 --- a/Scripts/ODS/Mhealth/Post_Load/8_Update_PatientReferral_ReferralPatientPK.sql +++ b/Scripts/ODS/Mhealth/Post_Load/8_Update_PatientReferral_ReferralPatientPK.sql @@ -3,6 +3,6 @@ registered in the EMR. Indentified using the patientCCCNumber */ UPDATE a SET a.ReferralpatientPK = p.PatientPK -FROM [ODS].[dbo].[Ushauri_PatientReferral] a +FROM [ODS].[dbo].[Mhealth_FacilityReferral_Patient] a JOIN [ODS].[dbo].[CT_Patient] p ON a.PatientID = p.PatientID; diff --git a/Scripts/ODS/Mhealth/Post_Load/9_Update_CT_PatientLabs.sql b/Scripts/ODS/Mhealth/Post_Load/9_Update_CT_PatientLabs.sql index 3ecd0f53..67a1c11e 100644 --- a/Scripts/ODS/Mhealth/Post_Load/9_Update_CT_PatientLabs.sql +++ b/Scripts/ODS/Mhealth/Post_Load/9_Update_CT_PatientLabs.sql @@ -4,12 +4,12 @@ SET a.PatientPK = NULL, a.PatientPKHash = NULL FROM - [ODS].[dbo].[Ushauri_PatientLabs] a; + [ODS].[dbo].[Mhealth_mLab_PatientLab] a; UPDATE a SET a.PatientPK = p.PatientPK FROM - [ODS].[dbo].[Ushauri_PatientLabs] a + [ODS].[dbo].[Mhealth_mLab_PatientLab] a JOIN [ODS].[dbo].[CT_Patient] p ON a.PatientID = p.PatientID; From 1f627ffc04be71a4f142794bca7b9f62f09913ea Mon Sep 17 00:00:00 2001 From: NewtonMutugi Date: Tue, 23 Jul 2024 15:10:38 +0300 Subject: [PATCH 11/21] Refactored remaining table names that were not updated --- .../Mhealth/Post_Load/1_RecreatePatient_PatientPKColumns.sql | 2 +- ...7_RecreateReferralPatientPK_ReferralPatientPKHashcolumns.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Scripts/ODS/Mhealth/Post_Load/1_RecreatePatient_PatientPKColumns.sql b/Scripts/ODS/Mhealth/Post_Load/1_RecreatePatient_PatientPKColumns.sql index 9df6b275..45258836 100644 --- a/Scripts/ODS/Mhealth/Post_Load/1_RecreatePatient_PatientPKColumns.sql +++ b/Scripts/ODS/Mhealth/Post_Load/1_RecreatePatient_PatientPKColumns.sql @@ -8,7 +8,7 @@ BEGIN BEGIN IF NOT EXISTS (SELECT * /* If above condition is met, check if Ushauri_Patient exists. If it exists escape. If it doesn't exist create it*/ FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_NAME = 'Ushauri_Patient' + WHERE TABLE_NAME = 'Mhealth_Ushauri_Patient' AND COLUMN_NAME = 'UshauriPatientPK') BEGIN diff --git a/Scripts/ODS/Mhealth/Post_Load/7_RecreateReferralPatientPK_ReferralPatientPKHashcolumns.sql b/Scripts/ODS/Mhealth/Post_Load/7_RecreateReferralPatientPK_ReferralPatientPKHashcolumns.sql index de44183f..59ad53a1 100644 --- a/Scripts/ODS/Mhealth/Post_Load/7_RecreateReferralPatientPK_ReferralPatientPKHashcolumns.sql +++ b/Scripts/ODS/Mhealth/Post_Load/7_RecreateReferralPatientPK_ReferralPatientPKHashcolumns.sql @@ -8,7 +8,7 @@ BEGIN BEGIN IF NOT EXISTS (SELECT * /* If above condition is met, check if Ushauri_PatientReferral exists. If it exists escape. If it doesn't exist create it*/ FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_NAME = 'Ushauri_PatientReferral' + WHERE TABLE_NAME = 'Mhealth_FacilityReferral_Patient' AND COLUMN_NAME = 'UshauriReferralPK') BEGIN From 4d31e25b6810f59985f4767bbc635707a397843f Mon Sep 17 00:00:00 2001 From: NewtonMutugi Date: Tue, 23 Jul 2024 15:48:13 +0300 Subject: [PATCH 12/21] Updated Mhealth table names in utils column hashing --- .../mhealth/Ushauri_PatientPK_PatientCCCNumber.sql | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Scripts/ODS/utils/COLUMN_HASHING/mhealth/Ushauri_PatientPK_PatientCCCNumber.sql b/Scripts/ODS/utils/COLUMN_HASHING/mhealth/Ushauri_PatientPK_PatientCCCNumber.sql index 01d918d3..5fa7a118 100644 --- a/Scripts/ODS/utils/COLUMN_HASHING/mhealth/Ushauri_PatientPK_PatientCCCNumber.sql +++ b/Scripts/ODS/utils/COLUMN_HASHING/mhealth/Ushauri_PatientPK_PatientCCCNumber.sql @@ -1,20 +1,20 @@ ---Ushauri_PatientAppointments UPDATE a SET PatientPKHash = convert(nvarchar(64), hashbytes('SHA2_256', cast(PatientPk as nvarchar(36))), 2) -FROM [ODS].[dbo].[Ushauri_PatientAppointments] a +FROM [ODS].[dbo].[Mhealth_Ushauri_PatientAppointments] a WHERE PatientPKHash IS NULL; UPDATE a SET UshauriPatientPKHash = convert(nvarchar(64), hashbytes('SHA2_256', cast(a.[UshauriPatientPk] as nvarchar(36))), 2), PatientIDHash = convert(nvarchar(64), hashbytes('SHA2_256', cast(a.PatientID as nvarchar(36))), 2) , NUPIHash = convert(nvarchar(64), hashbytes('SHA2_256', cast(a.[NUPI] as nvarchar(36))), 2) -FROM [ODS].[dbo].[Ushauri_PatientAppointments] a +FROM [ODS].[dbo].[Mhealth_Ushauri_PatientAppointments] a ----End ---Ushauri_Patient UPDATE a SET PatientPKHash = convert(nvarchar(64), hashbytes('SHA2_256', cast(PatientPk as nvarchar(36))), 2) -FROM [ODS].[dbo].[Ushauri_Patient] a +FROM [ODS].[dbo].[Mhealth_Ushauri_Patient] a WHERE PatientPKHash IS NULL; @@ -22,25 +22,25 @@ UPDATE a SET UshauriPatientPKHash = convert(nvarchar(64), hashbytes('SHA2_256', cast(a.[UshauriPatientPk] as nvarchar(36))), 2), PatientIDHash = convert(nvarchar(64), hashbytes('SHA2_256', cast(a.PatientID as nvarchar(36))), 2) , NUPIHash = convert(nvarchar(64), hashbytes('SHA2_256', cast(a.[NUPI] as nvarchar(36))), 2) -FROM [ODS].[dbo].[Ushauri_Patient] a +FROM [ODS].[dbo].[Mhealth_Ushauri_Patient] a UPDATE a SET UshauriPatientPKHash = convert(nvarchar(64), hashbytes('SHA2_256', cast(a.[UshauriPatientPk] as nvarchar(36))), 2), PatientHEI_IDHash = convert(nvarchar(64), hashbytes('SHA2_256', cast(a.PatientHEI_ID as nvarchar(36))), 2), PatientMNCH_IDHash = convert(nvarchar(64), hashbytes('SHA2_256', cast(a.PatientMNCH_ID as nvarchar(36))), 2) -FROM [ODS].[dbo].[Ushauri_HEI] a +FROM [ODS].[dbo].[Mhealth_Ushauri_HEI] a UPDATE a SET ReferralPatientPKHash = convert(nvarchar(64), hashbytes('SHA2_256', cast(a.ReferralPatientPK as nvarchar(36))), 2) -FROM [ODS].[dbo].[Ushauri_PatientReferral] a +FROM [ODS].[dbo].[Mhealth_FacilityReferral_Patient] a ----End ----Ushauri_PatientLabs UPDATE a SET PatientPKHash = convert(nvarchar(64), hashbytes('SHA2_256', cast(PatientPk as nvarchar(36))), 2) -FROM [ODS].[dbo].[Ushauri_PatientLabs] a +FROM [ODS].[dbo].[Mhealth_mLab_PatientLab] a WHERE PatientPKHash IS NULL; ----End From 3aa4bd99f854149740fbbcd3bee7cb911412de50 Mon Sep 17 00:00:00 2001 From: NewtonMutugi Date: Thu, 25 Jul 2024 10:26:14 +0300 Subject: [PATCH 13/21] Updated ushauri table names on NDWH scripts --- .../NDWH/ALL DIMENSIONS/load_DimPatient.sql | 24 +++++++++---------- .../load_FactUshauriAppointments.sql | 4 ++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql b/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql index 52db4aa3..c45935c7 100644 --- a/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql +++ b/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql @@ -43,7 +43,7 @@ BEGIN AND outcomes.sitecode = patients.sitecode LEFT JOIN ODS.dbo.CT_ARTPatients as art on art.PatientPKHash = patients.PatientPKHash AND art.SiteCode = patients.SiteCode - ), + ), hts_patient_source AS (SELECT DISTINCT htsnumberhash, patientpkhash, @@ -55,7 +55,7 @@ BEGIN nupihash, clients.voided FROM ods.dbo.hts_clients AS clients - + ), prep_patient_source AS (SELECT DISTINCT patientpkhash, @@ -69,7 +69,7 @@ BEGIN maritalstatus ,voided FROM ods.dbo.prep_patient), - + pmtct_patient_source AS (SELECT DISTINCT patientpkhash, patientpk, @@ -124,7 +124,7 @@ BEGIN AND ct_patient_source.sitecode = hts_patient_source.sitecode), - + combined_data_ct_hts_prep AS (SELECT COALESCE(combined_data_ct_hts.patientpkhash, prep_patient_source.patientpkhash) @@ -171,7 +171,7 @@ BEGIN AND prep_patient_source.sitecode = combined_data_ct_hts.sitecode), - + combined_data_ct_hts_prep_pmtct AS (SELECT COALESCE(combined_data_ct_hts_prep.patientpkhash, pmtct_patient_source.patientpkhash) @@ -216,7 +216,7 @@ BEGIN AND combined_data_ct_hts_prep.sitecode = pmtct_patient_source.sitecode), ushauri_patient_source_nonEMR - AS (SELECT DISTINCT + AS (SELECT DISTINCT ushauri.UshauriPatientPkHash, ushauri.PatientIDHash, ushauri.patientpk, @@ -228,9 +228,9 @@ BEGIN ushauri.maritalstatus, ushauri.nupihash, ushauri.SiteType - FROM ods.dbo.Ushauri_Patient AS ushauri + FROM [ODS].[dbo].[Mhealth_Ushauri_Patient] AS ushauri where ushauri.PatientPKHash is null and SiteCode is not null - + ) , combined_data_ct_hts_prep_pmtct_Ushauri @@ -286,8 +286,8 @@ BEGIN Null FirstEnrollmentAtMnchDateKey FROM ushauri_patient_source_nonEMR ushauri where sitecode is not null - - + + ) MERGE NDWH.[dbo].[DimPatient] AS a @@ -321,7 +321,7 @@ BEGIN FROM combined_data_ct_hts_prep_pmtct_Ushauri) AS b ON ( a.sitecode = b.sitecode AND a.patientpkhash = b.patientpkhash - + ) WHEN NOT matched THEN INSERT(patientidhash, @@ -376,4 +376,4 @@ BEGIN a.PrepEnrollmentDateKey = b.PrepEnrollmentDateKey, a.voided = b.voided, a.DateConfirmedHIVPositiveKey = b.DateConfirmedHIVPositiveKey; -END \ No newline at end of file +END diff --git a/Scripts/NDWH/C&T FACT TABLES/load_FactUshauriAppointments.sql b/Scripts/NDWH/C&T FACT TABLES/load_FactUshauriAppointments.sql index aa2a711e..f6fe56f7 100644 --- a/Scripts/NDWH/C&T FACT TABLES/load_FactUshauriAppointments.sql +++ b/Scripts/NDWH/C&T FACT TABLES/load_FactUshauriAppointments.sql @@ -55,7 +55,7 @@ BEGIN Daysdefaulted, Nupihash INTO NDWH.dbo.FactUshauriAppointments - FROM Ods.Dbo.Ushauri_patientappointments AS Apt + FROM [ODS].[dbo].[Mhealth_Ushauri_PatientAppointments] AS Apt LEFT JOIN Ndwh.Dbo.Dimfacility AS Facility ON Facility.Mflcode = Apt.Sitecode LEFT JOIN Mfl_partner_agency_combination @@ -101,4 +101,4 @@ BEGIN ALTER TABLE Ndwh.Dbo.FactUshauriAppointments ADD PRIMARY KEY(Factkey); -END \ No newline at end of file +END From a51b187f745a845faa2dee859fe5dc8dacc27b5b Mon Sep 17 00:00:00 2001 From: Mumo Ngungu Date: Thu, 25 Jul 2024 17:41:51 +0300 Subject: [PATCH 14/21] adding elicitation report for case survillence --- .../load_FactContactElicitation.sql | 68 +++++++++++++++++++ .../load_cs_aggregate_index_elicitation.sql | 49 +++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 Scripts/NDWH/C&T FACT TABLES/load_FactContactElicitation.sql create mode 100644 Scripts/REPORTING/load_cs_aggregate_index_elicitation.sql diff --git a/Scripts/NDWH/C&T FACT TABLES/load_FactContactElicitation.sql b/Scripts/NDWH/C&T FACT TABLES/load_FactContactElicitation.sql new file mode 100644 index 00000000..0640efd5 --- /dev/null +++ b/Scripts/NDWH/C&T FACT TABLES/load_FactContactElicitation.sql @@ -0,0 +1,68 @@ +IF OBJECT_ID(N'[NDWH].[dbo].[FactContactElicitation]', N'U') IS NOT NULL + DROP TABLE [NDWH].[dbo].[FactContactElicitation]; + +BEGIN + +with MFL_partner_agency_combination as ( + select + distinct MFL_Code, + SDP, + [SDP_Agency] as Agency + from ODS.dbo.All_EMRSites +), +subset_data as ( + select + PatientPK, + CONVERT(NVARCHAR(64), HASHBYTES('SHA2_256', CAST(PatientPK as NVARCHAR(36))), 2) as PatientPKHash, + SiteCode, + ContactPatientPK, + CONVERT(NVARCHAR(64), HASHBYTES('SHA2_256', CAST(ContactPatientPK as NVARCHAR(36))), 2) as ContactPatientPKHash, + ContactSex, + ContactAge, + ContactMaritalStatus, + RelationshipWithPatient, + DateCreated + from ODS.dbo.CT_ContactListing + where voided = 0 +), +tested_contacts as( + select + distinct ContactPatientPK, + subset_data.SiteCode, + TestDate + from subset_data + inner join ODS.dbo.HTS_ClientTests as tests on tests.PatientPk = subset_data.ContactPatientPK + and tests.SiteCode =subset_data.SiteCode +) +select + Factkey = IDENTITY(INT, 1, 1), + index_pat.PatientKey as IndexPatientKey, + contact_pat.PatientKey as ContactPatientKey, + facility.FacilityKey, + age_group.AgeGroupKey, + partner.PartnerKey, + agency.AgencyKey, + ContactSex, + ContactAge, + ContactMaritalStatus, + RelationshipWithPatient, + created.DateKey as DateCreatedKey, + case when tested_contacts.ContactPatientPK is not null then 1 else 0 end as Tested +into [NDWH].[dbo].[FactContactElicitation] +from subset_data +left join tested_contacts on tested_contacts.ContactPatientPK = subset_data.ContactPatientPK + and tested_contacts.SiteCode = subset_data.SiteCode +left join NDWH.dbo.DimPatient as index_pat on index_pat.PatientPKHash = subset_data.PatientPKHash + and index_pat.SiteCode = subset_data.SiteCode +left join NDWH.dbo.DimPatient as contact_pat on contact_pat.PatientPKHash = subset_data.ContactPatientPKHash + and contact_pat.SiteCode = subset_data.SiteCode +left join NDWH.dbo.DimFacility as facility on facility.MFLCode = subset_data.SiteCode +left join NDWH.dbo.DimAgeGroup as age_group on age_group.Age = datediff(year, index_pat.DOB,eomonth(dateadd(mm,-1,getdate()))) +left join MFL_partner_agency_combination on MFL_partner_agency_combination.MFL_Code = subset_data.SiteCode +left join NDWH.dbo.DimPartner as partner on partner.PartnerName = MFL_partner_agency_combination.SDP +left join NDWH.dbo.DimAgency as agency on agency.AgencyName = MFL_partner_agency_combination.Agency +left join NDWH.dbo.DimDate as created on created.[Date] = subset_data.DateCreated + +alter table [NDWH].[dbo].[FactContactElicitation] add primary key(FactKey); + +END \ No newline at end of file diff --git a/Scripts/REPORTING/load_cs_aggregate_index_elicitation.sql b/Scripts/REPORTING/load_cs_aggregate_index_elicitation.sql new file mode 100644 index 00000000..d293f433 --- /dev/null +++ b/Scripts/REPORTING/load_cs_aggregate_index_elicitation.sql @@ -0,0 +1,49 @@ +IF OBJECT_ID(N'[HIVCaseSurveillance].[dbo].[CSAggregateIndexEliciation]', N'U') IS NOT NULL + DROP TABLE [HIVCaseSurveillance].[dbo].[CSAggregateIndexEliciation]; + +with initial_data as ( + select + elicitation.FactKey, + elicitation.IndexPatientKey, + elicitation.ContactPatientKey, + confirm_date.[Date] as DateConfirmedHIVPositive, + date_created.[Date] as DateElicitated, + facility.FacilityName, + facility.County, + facility.SubCounty, + partenr.PartnerName, + agency.AgencyName, + DATIMAgeGroup as Agegroup, + elicitation.Tested + from [NDWH].[dbo].[FactContactElicitation] as elicitation + left join NDWH.dbo.DimPatient as patient on patient.PatientKey = elicitation.IndexPatientKey --joining on IndexPatientKey to get details of the index client + left join NDWH.dbo.DimFacility as facility on facility.FacilityKey = elicitation.FacilityKey + left join NDWH.dbo.DimPartner as partenr on partenr.PartnerKey = elicitation.PartnerKey + left join NDWH.dbo.DimAgency as agency on agency.AgencyKey = elicitation.AgencyKey + left join NDWH.dbo.DimAgeGroup as agegroup on agegroup.AgeGroupKey = elicitation.AgegroupKey + left join NDWH.dbo.DimDate as confirm_date on confirm_date.DateKey = patient.DateConfirmedHIVPositiveKey + left join NDWH.dbo.DimDate as date_created on date_created.DateKey = elicitation.DateCreatedKey + where date_created.DateKey is not null +) +select + eomonth(DateConfirmedHIVPositive) as CohortYearMonth, + eomonth(DateElicitated) as DateELiciatedYearMonth, + FacilityName, + AgeGroup, + County, + SubCounty, + AgencyName, + PartnerName, + count(FactKey) as NoElicited, + sum(Tested) as NoTested +into HIVCaseSurveillance.dbo.CSAggregateIndexEliciation +from initial_data +group by + eomonth(DateConfirmedHIVPositive), + eomonth(DateElicitated), + FacilityName, + AgeGroup, + County, + SubCounty, + AgencyName, + PartnerName \ No newline at end of file From 0e4b2ec7bf38f0c09bd6cec301ef45c0add82f18 Mon Sep 17 00:00:00 2001 From: Marymary-dev Date: Thu, 25 Jul 2024 17:54:24 +0300 Subject: [PATCH 15/21] Adding Disclosure column --- .../NDWH/ALL DIMENSIONS/load_DimPatient.sql | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql b/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql index 52db4aa3..ed944fa9 100644 --- a/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql +++ b/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql @@ -232,7 +232,28 @@ BEGIN where ushauri.PatientPKHash is null and SiteCode is not null ) , - + Disclosure as ( + Select + row_number() OVER (PARTITION BY SiteCode,PatientPKHash ORDER BY VisitDate DESC) AS NUM, + PatientPKHash, + Sitecode, + PaedsDisclosure, + PwP + from ODS.dbo.CT_PatientVisits as visits + WHERE PwP LIKE '%|disclosure|%' + OR PwP LIKE 'disclosure|%' + OR PwP LIKE '%|disclosure' + OR PwP = 'disclosure' + or PaedsDisclosure ='full disclosure' or PwP='Disclosure' + ), + LatestDisclosure as ( + SELECT + PatientPKHash, + SiteCode, + Coalesce (PaedsDisclosure, PWP) as Disclosure + From Disclosure + where NUM=1 + ), combined_data_ct_hts_prep_pmtct_Ushauri as( SELECT combined_data_ct_hts_prep_pmtct.patientpkhash AS PatientPKHash, @@ -252,6 +273,7 @@ BEGIN combined_data_ct_hts_prep_pmtct.DateConfirmedHIVPositiveKey, combined_data_ct_hts_prep_pmtct.htsnumberhash, NUll As sitetype, + LatestDisclosure.Disclosure, Cast(Getdate() AS DATE) AS LoadDate, combined_data_ct_hts_prep_pmtct.Voided, combined_data_ct_hts_prep_pmtct.PrepNumber, @@ -259,6 +281,7 @@ BEGIN combined_data_ct_hts_prep_pmtct.PatientMnchIDHash, combined_data_ct_hts_prep_pmtct.FirstEnrollmentAtMnchDateKey FROM combined_data_ct_hts_prep_pmtct + left join LatestDisclosure on LatestDisclosure.patientpkhash=combined_data_ct_hts_prep_pmtct.patientpkhash and LatestDisclosure.sitecode=combined_data_ct_hts_prep_pmtct.sitecode UNION SELECT concat(ushauri.UshauriPatientPKHash,'_Ushauri') AS PatientPKHash, @@ -278,6 +301,7 @@ BEGIN Null DateConfirmedHIVPositiveKey, Null htsnumberhash, sitetype, + LatestDisclosure.Disclosure, Null LoadDate, Null Voided, Null PrepNumber, @@ -285,8 +309,9 @@ BEGIN Null PatientMnchIDHash, Null FirstEnrollmentAtMnchDateKey FROM ushauri_patient_source_nonEMR ushauri - where sitecode is not null - + left join LatestDisclosure on LatestDisclosure.patientpkhash=ushauri.UshauriPatientPKHash and LatestDisclosure.sitecode=ushauri.sitecode + + where ushauri.SiteCode is not null ) From 35a32bd5477bae9737bf6c4ac7dd22bb7d885de6 Mon Sep 17 00:00:00 2001 From: Marymary-dev Date: Mon, 29 Jul 2024 10:43:06 +0300 Subject: [PATCH 16/21] Filtering asofdate in the Visits CTE --- Scripts/REPORTING/load_CsadvancedHIVDisease.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Scripts/REPORTING/load_CsadvancedHIVDisease.sql b/Scripts/REPORTING/load_CsadvancedHIVDisease.sql index cc0ae540..705e7429 100644 --- a/Scripts/REPORTING/load_CsadvancedHIVDisease.sql +++ b/Scripts/REPORTING/load_CsadvancedHIVDisease.sql @@ -58,7 +58,7 @@ With Visitdata As ( Left Join Ndwh.Dbo.Dimpartner As Partner On Partner.Partnerkey = Visits.Partnerkey Left Join Ndwh.Dbo.Dimagency As Agency On Agency.Agencykey = Visits.Agencykey Where - Visits.Patientkey Is Not Null + Visits.Patientkey Is Not Null and Visitdatekey <= @as_of_date ), Rankedvisits As ( Select Visitdata.Patientkey, @@ -144,7 +144,7 @@ From Left Join Ndwh.Dbo.Dimpatient As Pat On Pat.Patientkey = Visits.Patientkey Left Join Ndwh.Dbo.Dimdate As Dateconfirmed On Dateconfirmed.Datekey = Pat.Dateconfirmedhivpositivekey Left Join Ndwh.Dbo.Dimagegroup Age On Age.Age = Visits.Age - where VisitDate <= @as_of_date + fetch next from cursor_AsOfDates into @as_of_date end From 4d852c148785de6b51d5d2fbd47e37385413dbeb Mon Sep 17 00:00:00 2001 From: Marymary-dev Date: Mon, 29 Jul 2024 11:39:05 +0300 Subject: [PATCH 17/21] Adding Disclosure in the main merge statement --- Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql b/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql index ed944fa9..c2b7ee4a 100644 --- a/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql +++ b/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql @@ -339,6 +339,7 @@ BEGIN combined_data_ct_hts_prep_pmtct_Ushauri.prepenrollmentdatekey, combined_data_ct_hts_prep_pmtct_Ushauri.istxcurr, combined_data_ct_hts_prep_pmtct_Ushauri.DateConfirmedHIVPositiveKey, + combined_data_ct_hts_prep_pmtct_Ushauri.Disclosure, combined_data_ct_hts_prep_pmtct_Ushauri.patientmnchidhash, combined_data_ct_hts_prep_pmtct_Ushauri.firstenrollmentatmnchdatekey, combined_data_ct_hts_prep_pmtct_Ushauri.loaddate, @@ -362,7 +363,8 @@ BEGIN patientsource, enrollmentwhokey, datebaselinewhokey, - baselinewhokey,PrepEnrollmentDateKey, + baselinewhokey, + PrepEnrollmentDateKey, istxcurr, DateConfirmedHIVPositiveKey, loaddate, From cd9bf2bb6296c14f5b8a2b414d4a60a3dc1c18ea Mon Sep 17 00:00:00 2001 From: Marymary-dev Date: Mon, 29 Jul 2024 12:49:37 +0300 Subject: [PATCH 18/21] Adding Disclosure in the merge statement --- Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql b/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql index c2b7ee4a..51b989b5 100644 --- a/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql +++ b/Scripts/NDWH/ALL DIMENSIONS/load_DimPatient.sql @@ -367,6 +367,7 @@ BEGIN PrepEnrollmentDateKey, istxcurr, DateConfirmedHIVPositiveKey, + disclosure, loaddate, voided) VALUES(patientidhash, @@ -386,6 +387,7 @@ BEGIN PrepEnrollmentDateKey, istxcurr, DateConfirmedHIVPositiveKey, + disclosure, loaddate, voided) WHEN matched THEN @@ -402,5 +404,7 @@ BEGIN a.baselinewhokey = b.baselinewhokey, a.PrepEnrollmentDateKey = b.PrepEnrollmentDateKey, a.voided = b.voided, - a.DateConfirmedHIVPositiveKey = b.DateConfirmedHIVPositiveKey; -END \ No newline at end of file + a.DateConfirmedHIVPositiveKey = b.DateConfirmedHIVPositiveKey, + a.Disclosure = b.Disclosure; +END + From fefceb7f9e03cdc4093ddfc484af6987edaf510b Mon Sep 17 00:00:00 2001 From: NewtonMutugi Date: Mon, 29 Jul 2024 19:26:28 +0300 Subject: [PATCH 19/21] Fixed syntax error on CT_PatientLabs --- .../Remove_CT_ODS_Duplicates/CT_PatientLabs.sql | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_PatientLabs.sql b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_PatientLabs.sql index ebbea47a..d6b6590c 100644 --- a/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_PatientLabs.sql +++ b/Scripts/ODS/CT_DOCKET/Remove_CT_ODS_Duplicates/CT_PatientLabs.sql @@ -7,11 +7,10 @@ ROW_NUMBER() OVER (PARTITION BY PatientPK,Sitecode,OrderedbyDate,TestResult,TestName ORDER BY OrderedbyDate) Row_Num FROM [ODS].[dbo].[CT_PatientLabs](NoLock) - ) - DELETE from cte + DELETE from cte Where Row_Num >1 ; INSERT INTO [ODS_logs].[dbo].[CT_PatientLabsCount_Log]([SiteCode],[CreatedDate],[PatientLabsCount]) -SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS PatientLabsCount -FROM [ODS].[dbo].[CT_PatientLabs] -GROUP BY SiteCode; \ No newline at end of file +SELECT SiteCode,GETDATE(),COUNT(concat(Sitecode,PatientPK)) AS PatientLabsCount +FROM [ODS].[dbo].[CT_PatientLabs] +GROUP BY SiteCode; From 1a5d8de389d16415b16a5b19d340a20d16ec8c77 Mon Sep 17 00:00:00 2001 From: Mumo Ngungu Date: Tue, 30 Jul 2024 01:19:11 +0300 Subject: [PATCH 20/21] adding a new reporting table for risk factors for CS --- ..._aggregate_alcoho_sex_sti_risk_factors.sql | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Scripts/REPORTING/load_cs_aggregate_alcoho_sex_sti_risk_factors.sql diff --git a/Scripts/REPORTING/load_cs_aggregate_alcoho_sex_sti_risk_factors.sql b/Scripts/REPORTING/load_cs_aggregate_alcoho_sex_sti_risk_factors.sql new file mode 100644 index 00000000..df8574f5 --- /dev/null +++ b/Scripts/REPORTING/load_cs_aggregate_alcoho_sex_sti_risk_factors.sql @@ -0,0 +1,79 @@ +IF OBJECT_ID(N'HIVCaseSurveillance.dbo.CsAggregateAlcoholSex&STIRiskFactors', N'U') IS NOT NULL + DROP TABLE HIVCaseSurveillance.dbo.CsAggregateAlcoholSex&STIRiskFactors; + +with cases as ( + select + art.PatientKey, + art.FacilityKey, + art.AgeGroupKey, + art.PartnerKey, + art.AgencyKey, + pat.Gender, + confirm_date.[Date] as DateConfirmedHIVPos + from NDWH.dbo.FactART as art + left join NDWH.dbo.DimPatient as pat on pat.PatientKey = art.PatientKey + left join NDWH.dbo.DimDate as confirm_date on confirm_date.DateKey = pat.DateConfirmedHIVPositiveKey + where confirm_date.Date > DATEADD(MONTH, -12, EOMONTH(GETDATE())) +), +eligibility_indicators as ( + select + row_number() over (partition by eligibility_data.PatientKey order by eligibilitydate.date desc) as rank, + eligibility_data.PatientKey, + eligibilitydate.Date as ELigibilityVisitDate, + case + when AlcoholSex in ('Always', 'Sometimes') then 1 + else 0 + end as SexwithAlcohoDrugs, + case + when EverHadSTI = 'Yes' then 1 + else 0 + end as EverHadSTI +from NDWH.dbo.FactHTSEligibilityextract as eligibility_data +left join NDWH.dbo.DimDate as eligibilitydate on eligibilitydate.DateKey = eligibility_data.VisitDateKey +), +latest_eligibility_indicators_per_patient as ( + select + * + from eligibility_indicators + where rank = 1 +), +joined_data as ( + select + cases.PatientKey, + cases.FacilityKey, + cases.AgeGroupKey, + cases.PartnerKey, + cases.AgencyKey, + cases.Gender, + cases.DateConfirmedHIVPos, + eligibility.ELigibilityVisitDate, + SexwithAlcohoDrugs, + EverHadSTI + from cases + inner join latest_eligibility_indicators_per_patient as eligibility on eligibility.PatientKey = cases.PatientKey +) +select + eomonth(confirm_date.Date) as CohortYearMonth, + facility.FacilityName, + facility.County, + facility.SubCounty, + partner.PartnerName, + agency.AgencyName, + count(joined_data.PatientKey) as NoOfCases, + sum(SexwithAlcohoDrugs) HasSexwithAlcohoDrugs, + sum(EverHadSTI) as EverHadSTI +into HIVCaseSurveillance.dbo.CsAggregateAlcoholSex&STIRiskFactors +from joined_data +left join NDWH.dbo.DimPartner as partner on partner.PartnerKey = joined_data.PartnerKey +left join NDWH.dbo.DimAgency as agency on agency.AgencyKey = joined_data.AgencyKey +left join NDWH.dbo.DimAgeGroup as agegroup on agegroup.AgeGroupKey = joined_data.AgeGroupKey +left join NDWH.dbo.DimPatient as patient on patient.PatientKey = joined_data.PatientKey +left join NDWH.dbo.DimFacility as facility on facility.FacilityKey = joined_data.FacilityKey +left join NDWH.dbo.DimDate as confirm_date on confirm_date.DateKey = patient.DateConfirmedHIVPositiveKey +group by + eomonth(confirm_date.Date), + facility.FacilityName, + facility.County, + facility.SubCounty, + partner.PartnerName, + agency.AgencyName; \ No newline at end of file From b8759d2190870cd7f0301e01f8ab85cd874a47c9 Mon Sep 17 00:00:00 2001 From: Mumo Ngungu Date: Tue, 30 Jul 2024 17:05:00 +0300 Subject: [PATCH 21/21] removing date filter; --- .../load_cs_aggregate_alcoho_sex_sti_risk_factors.sql | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Scripts/REPORTING/load_cs_aggregate_alcoho_sex_sti_risk_factors.sql b/Scripts/REPORTING/load_cs_aggregate_alcoho_sex_sti_risk_factors.sql index df8574f5..e85b9781 100644 --- a/Scripts/REPORTING/load_cs_aggregate_alcoho_sex_sti_risk_factors.sql +++ b/Scripts/REPORTING/load_cs_aggregate_alcoho_sex_sti_risk_factors.sql @@ -1,5 +1,5 @@ -IF OBJECT_ID(N'HIVCaseSurveillance.dbo.CsAggregateAlcoholSex&STIRiskFactors', N'U') IS NOT NULL - DROP TABLE HIVCaseSurveillance.dbo.CsAggregateAlcoholSex&STIRiskFactors; +IF OBJECT_ID(N'HIVCaseSurveillance.dbo.CsAggregateAlcoholSexSTIRiskFactors', N'U') IS NOT NULL + DROP TABLE HIVCaseSurveillance.dbo.CsAggregateAlcoholSexSTIRiskFactors; with cases as ( select @@ -13,7 +13,6 @@ with cases as ( from NDWH.dbo.FactART as art left join NDWH.dbo.DimPatient as pat on pat.PatientKey = art.PatientKey left join NDWH.dbo.DimDate as confirm_date on confirm_date.DateKey = pat.DateConfirmedHIVPositiveKey - where confirm_date.Date > DATEADD(MONTH, -12, EOMONTH(GETDATE())) ), eligibility_indicators as ( select @@ -62,7 +61,7 @@ select count(joined_data.PatientKey) as NoOfCases, sum(SexwithAlcohoDrugs) HasSexwithAlcohoDrugs, sum(EverHadSTI) as EverHadSTI -into HIVCaseSurveillance.dbo.CsAggregateAlcoholSex&STIRiskFactors +into HIVCaseSurveillance.dbo.CsAggregateAlcoholSexSTIRiskFactors from joined_data left join NDWH.dbo.DimPartner as partner on partner.PartnerKey = joined_data.PartnerKey left join NDWH.dbo.DimAgency as agency on agency.AgencyKey = joined_data.AgencyKey