2013-12-17 21 views
0

想知道如果有人會知道如何做到這一點?你如何排除兩種不同的條件

基本上我只需要返回結果,其中類型等於'我',所以我刪除了行----和PatientType ='我'----這就像一個魅力,但後來我發現我有一些相同的MRN記錄包含「I」和「O」兩種類型,如果他們有一個「O」,那麼我就不能計算他們的「I」,現在這意味着我必須刪除那些符合條件是的,你猜對了,我又卡住了。LOL

SELECT DISTINCT 
     p.id, 
     v.PatientID, 
     p.firstname, 
     p.lastname, 
     p.dob, 
     p.mrn, 
     s.SmokeStatus, 
     v.VisitNo, 
     s.VisitID, 
     v.ID, 
    v.AdmitedDate 

    FROM 
     tblPatient p 
     JOIN tblPatientVisit v ON p.id = v.PatientID 
     JOIN tblPatientSmokingScreenOrder s ON v.id = s.VisitID 
    WHERE 
     isdate(p.DOB) = 1 
     AND CONVERT(date,p.DOB) <'12/10/2000' 
     AND isdate(v.AdmitedDate) = 1 
     AND CONVERT(date,v.AdmitedDate) > '06/16/2013 00:00' 

    AND v.PatientType = 'I' 
    AND s.TobaccoType = 'Cigarettes' OR s.TobaccoType='Cigars' or s.TobaccoType='Pipes' 
    AND v.PatientType !='O' 
    AND v.PatientType !='2' 
    AND v.PatientType = 'I' 

    order by MRN 

回答

2

加上括號,因爲你或和與之間的運算符優先級的問題。

AND (s.TobaccoType = 'Cigarettes' OR s.TobaccoType='Cigars' or s.TobaccoType='Pipes') 

或更好,IN條款

AND s.TobaccoType IN ('Cigarettes', 'Cigars', 'Pipes') 

然後,添加一個NOT EXISTS條款(如果我沒有理解好),以排除患者「一O在任何訪問」

AND NOT EXISTS (select null from 
       tblPatientVisit 
       where PatientId = p.id 
       and PatientType = 'O') 

所以整個查詢應(不知道v.PatientType != '2'應該去哪裏)

SELECT DISTINCT 
     p.id, 
     v.PatientID, 
     p.firstname, 
     p.lastname, 
     p.dob, 
     p.mrn, 
     s.SmokeStatus, 
     v.VisitNo, 
     s.VisitID, 
     v.ID, 
     v.AdmitedDate 

    FROM 
     tblPatient p 
     JOIN tblPatientVisit v ON p.id = v.PatientID 
     JOIN tblPatientSmokingScreenOrder s ON v.id = s.VisitID 
    WHERE 
     isdate(p.DOB) = 1 
     AND CONVERT(date,p.DOB) <'12/10/2000' 
     AND isdate(v.AdmitedDate) = 1 
     AND CONVERT(date,v.AdmitedDate) > '06/16/2013 00:00' 
     AND v.PatientType = 'I' 
     AND v.PatientType !='2' 
     AND s.TobaccoType IN ('Cigarettes', 'Cigars', 'Pipes') 
     AND NOT EXISTS (select null from 
        tblPatientVisit 
        where PatientId = p.id 
        and PatientType = 'O') 
+0

@ user3093389對不起,有些錯誤,現在應該沒問題。 –

相關問題