查询连接表,如果条件无效,则选择null

时间:2017-06-16 13:40:54

标签: 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 [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%'

现在查询的问题是,它只为患有所有条件的患者选择患者姓名。 我想能够选择患者,如果他们有以下1个条件是真的:

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个活动。

我怎样才能做到这一点?

4 个答案:

答案 0 :(得分:2)

我重写了你的查询(和Sean一样),但我也试图用LEFT(OUTER)JOIN来解决你的问题。

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

答案 1 :(得分:1)

尝试阅读有关LEFT OUTER JOIN

的内容

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

答案 2 :(得分:1)

这不会很好地作为评论格式化(而且它太长了)。这是您使用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%'

答案 3 :(得分:1)

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