FROM
(SELECT DISTINCT Study_Site_ID, Study_Participant_ID, 'Confirmed' As Status FROM dbEDRN$.dbo.vwRptFinalG
UNION
SELECT DISTINCT Study_Site_ID, Study_Participant_ID, 'Ineligible' As Status FROM dbEDRN$.dbo.vwRptIneligible
UNION
SELECT DISTINCT Study_Site_ID, Study_Participant_ID, 'Pending' As Status FROM dbEDRN$.dbo.vwRptPending) PptList
LEFT JOIN
dbEDRN$.dbo.tblFinalG FG ON PptList.STUDY_PARTICIPANT_ID = FG.STUDY_PARTICIPANT_ID AND FG.DeleteFlag = 0
INNER JOIN
dbEDRN$.dbo.tblBLConsent C ON PptList.STUDY_PARTICIPANT_ID = C.STUDY_PARTICIPANT_ID AND C.DeleteFlag = 0 AND C.EntryFlag = 1
LEFT JOIN
dbEDRN$.dbo.tblBLParticipant PQ ON PptList.STUDY_PARTICIPANT_ID = PQ.STUDY_PARTICIPANT_ID AND PQ.DeleteFlag = 0 AND PQ.EntryFlag = 1
LEFT JOIN
dbEDRN$.dbo.tblBLClinical PC ON PptList.STUDY_PARTICIPANT_ID = PC.STUDY_PARTICIPANT_ID AND PC.DeleteFlag = 0 AND PC.EntryFlag = 1
LEFT JOIN
dbEDRN$.dbo.tblProtocolDeviation PD ON PptList.STUDY_PARTICIPANT_ID = PD.STUDY_PARTICIPANT_ID AND PD.DeleteFlag = 0 AND PD.EntryFlag = 1
LEFT JOIN
dbEDRN$.dbo.tblIneligibility Ineli ON PptList.STUDY_PARTICIPANT_ID = Ineli.STUDY_PARTICIPANT_ID AND Ineli.DeleteFlag = 0 AND Ineli.EntryFlag = 1
LEFT JOIN
(SELECT Study_Participant_ID, targetDate FROM dbEDRN$.dbo.vwContactSchedule WHERE getdate() BETWEEN contactWindowLower AND contactWindowUpper) AS CS ON PptList.Study_Participant_ID = CS.STUDY_PARTICIPANT_ID
LEFT JOIN
db_LIMS_EDRN_STS.dbo.vwSpecimenSetStatus$ ss ON PptList.Study_Participant_ID = ss.PptID
LEFT JOIN
(Select Top 1 DateColumn As MaxDate, Study_Participant_ID
From (
Select Top 1 STUDY_CONSENT_DATE As DateColumn, Study_Participant_ID
From dbEDRN$.dbo.tblBLConsent
Where Study_Participant_ID = PptList.Study_Participant_ID
Order By STUDY_CONSENT_DATE DESC
Union All
Select Top 1 PPT_DATACOLLECTED_CONTACT_DATE As DateColumn, Study_Participant_ID
From dbEDRN$.dbo.tblBLClinical
Where Study_Participant_ID = PptList.Study_Participant_ID
Order By PPT_DATACOLLECTED_CONTACT_DATE DESC
Union All
Select Top 1 PPT_DATACOLLECTED_CONTACT_DATE As DateColumn, Study_Participant_ID
From dbEDRN$.dbo.tblBLLab
Where Study_Participant_ID = PptList.Study_Participant_ID
Order By PPT_DATACOLLECTED_CONTACT_DATE DESC
Union All
Select Top 1 PPT_DATACOLLECTED_CONTACT_DATE, Study_Participant_ID
From dbEDRN$.dbo.tblBLParticipant
Where Study_Participant_ID = PptList.Study_Participant_ID
Order By PPT_DATACOLLECTED_CONTACT_DATE DESC
Union All
Select Top 1 PPT_DATACOLLECTED_CONTACT_DATE As DateColumn, Study_Participant_ID
From dbEDRN$.dbo.tblFUConsent
Where Study_Participant_ID = PptList.Study_Participant_ID
Order By PPT_DATACOLLECTED_CONTACT_DATE DESC
Union All
Select Top 1 PPT_DATACOLLECTED_CONTACT_DATE As DateColumn, Study_Participant_ID
From dbEDRN$.dbo.tblFULab
Where Study_Participant_ID = PptList.Study_Participant_ID
Order By PPT_DATACOLLECTED_CONTACT_DATE DESC
Union All
Select Top 1 PPT_DATACOLLECTED_CONTACT_DATE As DateColumn, Study_Participant_ID
From dbEDRN$.dbo.tblFUParticipant
Where Study_Participant_ID = PptList.Study_Participant_ID
Order By PPT_DATACOLLECTED_CONTACT_DATE DESC
Union All
Select Top 1 PPT_DATACOLLECTED_CONTACT_DATE As DateColumn, Study_Participant_ID
From dbEDRN$.dbo.tblFUParticipantCa
Where Study_Participant_ID = PptList.Study_Participant_ID
Order By PPT_DATACOLLECTED_CONTACT_DATE DESC
Union All
Select Top 1 PPT_DATACOLLECTED_CONTACT_DATE As DateColumn, Study_Participant_ID
From dbEDRN$.dbo.tblFUParticipantRelCa
Where Study_Participant_ID = PptList.Study_Participant_ID
Order By PPT_DATACOLLECTED_CONTACT_DATE DESC
Union All
Select Top 1 PPT_DATACOLLECTED_CONTACT_DATE As DateColumn, Study_Participant_ID
From dbEDRN$.dbo.tblFUTreatment
Where Study_Participant_ID = PptList.Study_Participant_ID
Order By PPT_DATACOLLECTED_CONTACT_DATE DESC
) As DateStuff
Order By DateStuff.DateColumn DESC
) AS MD ON PptList.Study_Participant_ID = MD.Study_Participant_ID
ORDER BY PptList.Study_Site_ID, PptList.Study_Participant_ID, PptList.Status