-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added FactHistoricalViralLoad script. Yet to add PBFW logi #454
Open
DennisGibz
wants to merge
5
commits into
dev
Choose a base branch
from
HistoricalViralLoad
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3b19d3f
Added FactHistoricalViralLoad script. Yet to add PBFW logi
DennisGibz 66c03d5
Added a new model CsLinelistHistoricalViralLoad
DennisGibz 40ddf36
Merge branch 'dev' of https://github.com/palladiumkenya/dwh-etl into …
DennisGibz 6cf74f5
Aligned the historical fact viral load and all the computions aligned…
DennisGibz 88bef26
Added a comment disclaimer
DennisGibz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
191 changes: 191 additions & 0 deletions
191
Scripts/NDWH/C&T FACT TABLES/FactHistoricalViralLoad.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
TRUNCATE TABLE ndwh.dbo.FactViralLoad_Hist; | ||
|
||
DECLARE @start_date DATE; | ||
|
||
SELECT @start_date = dateadd(month, -12, eomonth(dateadd(month, -2, getdate()))); | ||
|
||
DECLARE @end_date DATE; | ||
|
||
SELECT @end_date = eomonth(dateadd(month, -2, 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 viralLoad As ( | ||
SELECT DISTINCT | ||
Labs.PatientPk, | ||
Labs.SiteCode, | ||
VisitID, | ||
art.StartARTDate, | ||
[OrderedbyDate], | ||
[ReportedbyDate], | ||
Patient.DOB, | ||
AgeAsOfDate = Datediff(year,Patient.DOB,@as_of_date), | ||
[TestName], | ||
TestResult, | ||
Labs.[Emr], | ||
Labs.[Project], | ||
CASE | ||
WHEN datediff(month,art.StartARTDate, @as_of_date) >=3 THEN 1 | ||
WHEN DATEDIFF(MONTH, art.StartARTDate, @as_of_date) <3 THEN 0 | ||
END As EligibleVL, | ||
CASE | ||
WHEN datediff(month,[OrderedbyDate],@as_of_date) <= 6 and Datediff(year,Patient.DOB,@as_of_date) <= 24 THEN 1 | ||
WHEN datediff(month,[OrderedbyDate],@as_of_date) <= 12 and Datediff(year,Patient.DOB,@as_of_date) > 24 THEN 1 | ||
ELSE 0 | ||
END As IsValidVL, | ||
CASE | ||
WHEN IsNumeric([TestResult]) = 1 THEN | ||
CASE | ||
WHEN cast(replace([TestResult], ',', '') as float) < 200.00 THEN 1 | ||
ELSE 0 | ||
END | ||
ELSE | ||
CASE | ||
WHEN [TestResult] in ('undetectable','NOT DETECTED','0 copies/ml','LDL','Less than Low Detectable Level') THEN 1 | ||
ELSE 0 | ||
END | ||
END as VLSup, | ||
@as_of_date As AsOfDate, | ||
getdate() As LoadDate | ||
---into ndwh.dbo.FactViralLoad_Hist | ||
FROM ODS.dbo.CT_ARTPatients art | ||
join ODS.dbo.CT_PatientLabs Labs ---Visits to know whether the the pregnant or not(Pbfw) | ||
ON art.PatientPK = labs.Patientpk and | ||
art.SiteCode = labs.SiteCode | ||
Left join ODS.dbo.CT_patient patient | ||
on art.PatientPK = patient.Patientpk and | ||
art.SiteCode = patient.SiteCode | ||
|
||
where TestName = 'Viral Load' | ||
and TestName <>'CholesterolLDL (mmol/L)' and TestName <> 'Hepatitis C viral load' | ||
and TestResult is not null | ||
and Labs.SiteCode =12601 and art.PatientPK = 10 | ||
and [OrderedbyDate] <= @as_of_date | ||
), | ||
|
||
Visits As( | ||
|
||
select Visits.SiteCode, | ||
Visits.PatientPK, | ||
VisitDate,Pregnant, | ||
Breastfeeding, | ||
NextAppointmentDate , | ||
@as_of_date As AsOfDate | ||
from ODS.DBO.CT_PatientVisits Visits | ||
join viralLoad | ||
on Visits.SiteCode = viralLoad.Sitecode and Visits.PatientPK = viralLoad.PatientPK | ||
where Visits.VisitDate <= @as_of_date and Visits.SiteCode =12601 and Visits.PatientPK = 10 | ||
|
||
), | ||
|
||
MaxVisitsByAsOf As ( | ||
select | ||
row_number() over(partition by SiteCode, PatientPK,AsOfDate order by VisitDate desc) as rank, | ||
SiteCode, | ||
PatientPK, | ||
VisitDate, | ||
AsOfDate, | ||
NextAppointmentDate, | ||
Pregnant, | ||
Breastfeeding | ||
from Visits | ||
), | ||
RankMaxVisitDateAsOf As( | ||
select SiteCode,PatientPK, VisitDate,Pregnant,Breastfeeding,NextAppointmentDate,AsOfDate | ||
from MaxVisitsByAsOf | ||
where rank =1 | ||
|
||
|
||
), | ||
|
||
MaxOrderedbyDateByAsOfDate As ( | ||
select | ||
row_number() over(partition by SiteCode, PatientPK,AsOfDate order by [OrderedbyDate] desc) as rank, | ||
SiteCode, | ||
PatientPK, | ||
StartARTDate, | ||
DOB, | ||
AgeAsOfDate, | ||
[OrderedbyDate], | ||
[ReportedbyDate], | ||
EligibleVL, | ||
IsValidVL, | ||
VLSup, | ||
AsOfDate, | ||
[TestName], | ||
TestResult, | ||
[Emr], | ||
[Project] | ||
from viralLoad | ||
where [OrderedbyDate] <= AsOfDate | ||
|
||
), | ||
MaxOrderedbyDateByAsOfDate_Final As ( | ||
select SiteCode,PatientPK,StartARTDate,DOB,AgeAsOfDate,OrderedbyDate,ReportedbyDate,EligibleVL,IsValidVL,VLSup,AsOfDate,TestName,TestResult,Emr,Project | ||
from MaxOrderedbyDateByAsOfDate | ||
where rank =1 | ||
|
||
), | ||
Combined As( | ||
Select OrderedFinal.SiteCode,OrderedFinal.PatientPK,StartARTDate,DOB,AgeAsOfDate,OrderedbyDate,MaxVisitAsOf.VisitDate,MaxVisitAsOf.Pregnant,MaxVisitAsOf.Breastfeeding,ReportedbyDate,EligibleVL,IsValidVL,VLSup,OrderedFinal.AsOfDate,TestName,TestResult,Emr,Project | ||
from MaxOrderedbyDateByAsOfDate_Final OrderedFinal | ||
join RankMaxVisitDateAsOf MaxVisitAsOf | ||
on OrderedFinal.SiteCode = MaxVisitAsOf.SiteCode and OrderedFinal.PatientPK = MaxVisitAsOf.PatientPK | ||
and OrderedFinal.AsOfDate = MaxVisitAsOf.AsOfDate | ||
|
||
) | ||
|
||
insert into ndwh.dbo.FactViralLoad_Hist( SiteCode,PatientPK,StartARTDate,DOB,VisitDate,Pregnant,Breastfeeding,AgeAsOfDate,OrderedbyDate,ReportedbyDate,EligibleVL,IsValidVL,VLSup,AsOfDate,TestName,TestResult,Emr,Project) | ||
|
||
select SiteCode,PatientPK,StartARTDate,DOB,VisitDate,Pregnant,Breastfeeding,AgeAsOfDate,OrderedbyDate,ReportedbyDate,EligibleVL,IsValidVL,VLSup,AsOfDate,TestName,TestResult,Emr,Project | ||
from Combined | ||
|
||
Update a | ||
set IsPBFW= case | ||
WHEN (Pregnant ='yes' or Breastfeeding ='yes') THEN 1 | ||
ELSE 0 | ||
END | ||
from ndwh.dbo.FactViralLoad_Hist a; | ||
|
||
Update a | ||
set IsValidVL= case | ||
WHEN datediff(month,[OrderedbyDate],a.AsOfDate) <= 6 THEN 1 | ||
END | ||
from ndwh.dbo.FactViralLoad_Hist a | ||
where IsPBFW = 1 | ||
|
||
|
||
|
||
fetch next from cursor_AsOfDates into @as_of_date | ||
end | ||
drop table #months | ||
close cursor_AsOfDates | ||
deallocate cursor_AsOfDates | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DennisGibz the logic is incorrect here, you are saying if datediff ordered date and asof <12 and they are adults then they have invalid vls which is wrong, we can link up kesho to discuss this