It's a horrible title. Let me clarify.
I'm attempting to write a query that will select certain columns from various tables which have been joined. Here's what the query looks like:
select distinct p.PAtientID, p.FirstNAme, p.LAstNAme, nrc.RiskClassification as [Risk Classification],
ptt.NExtScheduledDAte as [Toxicology Next Scheduled Date],
ppc.NextScheduledDate as [Pill Count Next Scheduled Date],
pha.NextScheduledDate as [Mental Health Assessment Next Scheduled Date],
ppr.NExtScheduledDate as [PDMP Next Scheduled Date],
pcsa.NExtScheduledDate as [CSA Next Scheduled DAte]
from Patient p, PAtientRiskClassification prc, NetworkRiskClassification nrc,
PAtientToxicologyTesting ptt, PAtientPillCount ppc, PAtientHEalthAssessment pha,
PatientPRescriptionRegistry ppr, PAtientControlledSubstanceAgreement pcsa,
Prescriber pr, NetworkPRescriber np
where p.PAtientID = prc.PAtientID
and p.PAtientID = ptt.PAtientID
and ppc.PatientID = p.PAtientID
and pha.PAtientID = p.PAtientID
and ppr.PatientID = p.PAtientID
and pcsa.PatientID = p.PAtientID
and pr.PrescriberID = np.PrescriberID
and np.NetworkPrescriberID = p.NEtworkPrescriberID
and prc.NetworkRiskClassificationID = nrc.NetworkRiskClassificationID
and pr.Display like '%Kohles Brian%'
Now the problem with my query is, it only selects patient names for patients that have all of the conditions true. I wanna be able to also select patients if they have 1 of the below conditions true for them:
p.PAtientID = ptt.PAtientID
and ppc.PatientID = p.PAtientID
and pha.PAtientID = p.PAtientID
and ppr.PatientID = p.PAtientID
and pcsa.PatientID = p.PAtientID
and select null or empty string in place of columns that don't validate the condition.
Currently I'm having to write 5 separate queries like the one below:
select distinct p.PatientID, p.Display as [Patient Name],
nrc.RiskClassification as Risk,
ppr.NextScheduledDate as [PDMP Next Scheduled Date] from PatientPrescriptionRegistry ppr, Patient p
cross apply
(select top(1) RiskClassification, PatientID from NetworkRiskClassification nrc, PatientRiskClassification prc
where nrc.NetworkRiskClassificationID = prc.NetworkRiskCLassificationID and PatientID = p.PatientID order by PatientRiskClassificationID desc) nrc
where p.PatientID = ppr.PatientID and
(p.NetworkPRescriberID = 44 or p.NetworkPrescriberID = 403)
order by p.PatientID
In short, I wanna display the "Next Scheduled Activity" column from 5 different activity tables for every patient some of whom might not have all 5 activities completed.
How can I achieve this?