Sql 查询以联接表,如果条件无效,则选择null

Sql 查询以联接表,如果条件无效,则选择null,sql,sql-server,Sql,Sql Server,这是一个可怕的标题。让我澄清一下 我正在尝试编写一个查询,该查询将从已联接的各个表中选择某些列。以下是查询的内容: select distinct p.PAtientID, p.FirstNAme, p.LAstNAme, nrc.RiskClassification as [Risk Classification], ptt.NExtScheduledDAte as [Toxicology Next Scheduled Date], ppc.NextScheduledDate as [Pil

这是一个可怕的标题。让我澄清一下

我正在尝试编写一个查询,该查询将从已联接的各个表中选择某些列。以下是查询的内容:

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%'
现在我的查询的问题是,它只为所有条件都为true的患者选择患者姓名。 我还希望能够选择符合以下条件之一的患者:

p.PAtientID = ptt.PAtientID
and ppc.PatientID = p.PAtientID
and pha.PAtientID = p.PAtientID
and ppr.PatientID = p.PAtientID
and pcsa.PatientID = p.PAtientID
并选择null或空字符串来代替不验证条件的列

目前,我必须编写5个独立的查询,如下所示:

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
简而言之,我想为每个患者显示5个不同活动表中的“下一个计划活动”列,其中一些患者可能没有完成全部5个活动


我怎样才能做到这一点呢?

试着读一些关于

假设您有两个表A和B具有左外部联接,您将得到
来自A和B的所有数据,其中条件匹配,此外,您将从与B不匹配的表中获取所有记录

假设您有两个表A和B具有左外部联接,您将得到
来自A和B的所有数据,其中条件匹配,此外,您将从与B不匹配的表中获取所有记录,这将不能很好地格式化为注释(而且太长)。以下是使用ANSI-92样式联接(以及列中的一些格式)重写的查询。注意这是多么容易阅读。它不是一堵文字墙

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
join PAtientRiskClassification prc on p.PAtientID = prc.PAtientID
join NetworkRiskClassification nrc on prc.NetworkRiskClassificationID = nrc.NetworkRiskClassificationID
join PAtientToxicologyTesting ptt on p.PAtientID = ptt.PAtientID
join PAtientPillCount ppc on ppc.PatientID = p.PAtientID
join PAtientHEalthAssessment on pha pha.PAtientID = p.PAtientID
join PatientPRescriptionRegistry ppr on ppr.PatientID = p.PAtientID
join PAtientControlledSubstanceAgreement pcsa on pcsa.PatientID = p.PAtientID
join Prescriber pr on pr.PrescriberID = np.PrescriberID
join NetworkPRescriber np on np.NetworkPrescriberID = p.NEtworkPrescriberID
where pr.Display like '%Kohles Brian%'

这不能很好地格式化为注释(而且太长了)。以下是使用ANSI-92样式联接(以及列中的一些格式)重写的查询。注意这是多么容易阅读。它不是一堵文字墙

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
join PAtientRiskClassification prc on p.PAtientID = prc.PAtientID
join NetworkRiskClassification nrc on prc.NetworkRiskClassificationID = nrc.NetworkRiskClassificationID
join PAtientToxicologyTesting ptt on p.PAtientID = ptt.PAtientID
join PAtientPillCount ppc on ppc.PatientID = p.PAtientID
join PAtientHEalthAssessment on pha pha.PAtientID = p.PAtientID
join PatientPRescriptionRegistry ppr on ppr.PatientID = p.PAtientID
join PAtientControlledSubstanceAgreement pcsa on pcsa.PatientID = p.PAtientID
join Prescriber pr on pr.PrescriberID = np.PrescriberID
join NetworkPRescriber np on np.NetworkPrescriberID = p.NEtworkPrescriberID
where pr.Display like '%Kohles Brian%'

我确信@sean的查询是有效的。您可以加入它们,而不是where子句。尝试内部连接或左连接

我确信@sean的查询是有效的。您可以加入它们,而不是where子句。尝试内部联接或左联接

我重写了您的查询(就像Sean一样),但我也尝试使用左(外部)联接解决您的问题

我重写了您的查询(Sean也是),但我也尝试使用左(外部)连接来解决您的问题


切勿在
FROM
子句中使用逗号。始终使用正确的显式
JOIN
语法。有什么原因吗?是的,有很多原因不使用旧的、不推荐使用的隐式联接语法。其中最常见的一种情况是,您不太可能因为忘记连接子句而犯错误。第二个问题是,您的查询更容易阅读、理解和维护。第三个原因是它有助于理解和解决您当前遇到的问题。请阅读本文,了解为什么您应该使用“更新的”联接样式。“这有什么原因吗?”而且公司很少愿意花钱将89个标准编写的代码更新到92个。但是,这并不意味着新代码应该在89标准中编写。切勿在
FROM
子句中使用逗号。始终使用正确的显式
JOIN
语法。有什么原因吗?是的,有很多原因不使用旧的、不推荐使用的隐式联接语法。其中最常见的一种情况是,您不太可能因为忘记连接子句而犯错误。第二个问题是,您的查询更容易阅读、理解和维护。第三个原因是它有助于理解和解决您当前遇到的问题。请阅读本文,了解为什么您应该使用“更新的”联接样式。“这有什么原因吗?”而且公司很少愿意花钱将89个标准编写的代码更新到92个。但是,这并不意味着新代码应该用89标准编写。谢谢。这就是我要找的。谢谢。这就是我要找的。谢谢你重写我的查询,谢谢你重写我的查询,很快:)我希望你注意到使用显式连接子句的优点?您只剩下一个实WHERE子句:)这很快:)我希望您注意到使用显式连接子句的优点?您只剩下一个实WHERE子句:)