2017-06-16 86 views
1

這是一個可怕的標題。讓我澄清一下。查詢連接表,並選擇null如果條件無效

我試圖編寫一個查詢,將選擇已加入各種表的某些列。這裏的查詢是什麼樣子:

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 

,選擇到位不驗證條件列空或空字符串。

目前,我在寫類似下面的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個活動。

我該如何做到這一點?

+3

*從不*在'FROM'子句中使用逗號。 *總是*使用正確的,明確的'JOIN'語法。 –

+0

有什麼理由嗎? –

+1

是的,有許多理由不使用隱式聯接的舊的過時的語法。其中最常見的一種是,通過忘記加入條款,你不太可能犯錯誤。第二個是你的查詢更易於閱讀,理解和維護。第三個是它有助於理解和解決你目前擁有的問題。 – oerkelens

回答

2

我重寫了您的查詢(和肖恩一樣),但我也嘗試使用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%' 
+0

太棒了。這工作 –

+0

這很快:)我希望你注意到使用顯式連接子句的優勢?你只剩下一個真正的WHERE子句:) – oerkelens

1

嘗試讀一些有關LEFT OUTER JOIN

假設你有兩個表A和B 左外連接你會得到 其中其中條件匹配的所有從數據和B,此外,您將從A表中找不到所有記錄B

+0

謝謝。這是我正在尋找的。 –

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%' 
+0

謝謝你重寫我的查詢, –

1

我相信@肖恩的查詢工作。你可以加入他們而不是where子句。嘗試內部或左連接