2016-11-02 77 views
0

爲什麼SQL案例不能正常工作? 'in'的作品雖然不符合'不在'。SQL案例不能正常工作

或者也許有更好的方法來做到這一點?

我試圖將結果應用於滿足條件集的匹配,只要它不符合其他條件集。

謝謝

---------- Newsletters ---------- 
 

 
CASE WHEN \t (\t C.ID in \t \t (SELECT [ContactID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel = 0 AND ReferenceTable <> 'Enquiry') 
 
\t \t \t AND C.ID not in \t (SELECT [ContactID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel > 0 AND ReferenceTable <> 'Enquiry') 
 
\t \t \t) 
 
\t \t \t OR 
 
\t \t \t (\t C.ID in \t \t (SELECT [ContactSpouseID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel = 0 AND ReferenceTable <> 'Enquiry') 
 
\t \t \t AND C.ID not in \t (SELECT [ContactSpouseID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel > 0 AND ReferenceTable <> 'Enquiry') 
 
\t \t \t) 
 
\t \t \t OR 
 
\t \t \t (\t ESC.ContactEnquiryID in \t \t (SELECT [ContactID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel = 0 AND ReferenceTable = 'Enquiry') 
 
\t \t \t AND ESC.ContactEnquiryID not in \t (SELECT [ContactID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel > 0 AND ReferenceTable = 'Enquiry') 
 
\t \t \t) 
 
THEN 'K' 
 

 
ELSE 
 

 
CASE WHEN \t (\t C.ID in \t \t (SELECT [ContactID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel > 0 AND YearLevel < 7 AND ReferenceTable <> 'Enquiry') 
 
\t \t \t AND C.ID not in \t (SELECT [ContactID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel = 0 AND ReferenceTable <> 'Enquiry') 
 
\t \t \t AND C.ID not in \t (SELECT [ContactID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel > 6 AND ReferenceTable <> 'Enquiry') 
 
\t \t \t) 
 
\t \t \t OR 
 
\t \t \t (\t C.ID in \t \t (SELECT [ContactSpouseID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel > 0 AND YearLevel < 7 AND ReferenceTable <> 'Enquiry') 
 
\t \t \t AND C.ID not in \t (SELECT [ContactSpouseID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel = 0 AND ReferenceTable <> 'Enquiry') 
 
\t \t \t AND C.ID not in \t (SELECT [ContactSpouseID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel > 6 AND ReferenceTable <> 'Enquiry') 
 
\t \t \t) 
 
\t \t \t OR 
 
\t \t \t (\t ESC.ContactEnquiryID in \t \t (SELECT [ContactID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel > 0 AND YearLevel < 7 AND ReferenceTable = 'Enquiry') 
 
\t \t \t AND ESC.ContactEnquiryID not in \t (SELECT [ContactID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel = 0 AND ReferenceTable = 'Enquiry') 
 
\t \t \t AND ESC.ContactEnquiryID not in \t (SELECT [ContactID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel > 6 AND ReferenceTable = 'Enquiry') 
 
\t \t \t) 
 
\t \t \t 
 
THEN '1-6' 
 

 
    ELSE 
 

 
CASE WHEN \t (\t C.ID in \t \t (SELECT [ContactID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel > 6 AND YearLevel < 13 AND ReferenceTable <> 'Enquiry') 
 
\t \t \t AND C.ID not in \t (SELECT [ContactID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel < 7 AND ReferenceTable <> 'Enquiry') 
 
\t \t \t) 
 
\t \t \t OR 
 
\t \t \t (\t C.ID in \t \t (SELECT [ContactSpouseID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel > 6 AND YearLevel < 13 AND ReferenceTable <> 'Enquiry') 
 
\t \t \t AND C.ID not in \t (SELECT [ContactSpouseID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel < 7 AND ReferenceTable <> 'Enquiry') 
 
\t \t \t) 
 
\t \t \t OR 
 
\t \t \t (\t ESC.ContactEnquiryID in \t \t (SELECT [ContactSpouseID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel > 6 AND YearLevel < 13 AND ReferenceTable = 'Enquiry') 
 
\t \t \t AND ESC.ContactEnquiryID not in \t (SELECT [ContactSpouseID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel < 7 AND ReferenceTable = 'Enquiry') 
 
\t \t \t) 
 
THEN '7-6' 
 

 
    ELSE 
 

 
CASE WHEN \t (\t C.ID in \t \t (SELECT [ContactID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND ReferenceTable <> 'Enquiry') 
 
\t \t \t) 
 
\t \t \t OR 
 
\t \t \t (\t C.ID in \t \t (SELECT [ContactSpouseID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND ReferenceTable <> 'Enquiry') 
 
\t \t \t) 
 
\t \t \t OR 
 
\t \t \t (\t ESC.ContactEnquiryID in \t \t (SELECT [ContactID] FROM [Customers].[dbo].[uvLSProspectiveContacts] WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND ReferenceTable = 'Enquiry') 
 
\t \t \t) 
 

 
THEN 'General' 
 

 
    ELSE '' END END 
 

 
\t END 
 
END AS ProspectiveNews,

+0

嘗試'AND NOT EXISTS(SELECT * FROM [客戶] [DBO] [uvLSProspectiveContacts],其中[EnrolDate]> '2016年12月31日00:00:00.000' AND YearLevel> 0 AND ReferenceTable <>'Inquiry'AND [ContactID] = C.ID)' –

+0

謝謝,儘管它看起來不像以下語法: – SSS

+0

Msg 156,Level 15,State 1,Line 10 關鍵字'EXISTS'。 – SSS

回答

0

嘗試NOT EXISTS:。

AND NOT EXISTS (SELECT * FROM [Customers].[dbo].[uvLSProspectiveContacts] 
WHERE [EnrolDate] > '2016-12-31 00:00:00.000' AND YearLevel > 0 AND 
ReferenceTable <> 'Enquiry' AND [ContactID] = C.ID) 
0

歡迎您到空值地獄。
更換每

x not in (select col from t) 

x not in (select col from t where col is not null) 

(除非你知道x被定義爲NOT NULL


x not in (a,b,c,...)

((x <> a) and (x <> b) and (x <> c) and ...)快捷方式
如果其中一個值(例如, b)爲NULL,則條件爲NULL <> b
由於沒有什麼不同於NULL,所以這個謂詞沒有被滿足,並且它不能滿足整個條件。

+0

@SSS,請看一看 –

+0

當與'NULL'比較時,謂詞是* not *'FALSE',它是* unknown *。你可以用'NOT(...)'包裝整個謂詞,並且你會發現它不會變成'TRUE'(如果它最初是'FALSE'的話)。它仍然是'UNKNOWN' –

+0

@Damien_The_Unbeliever。謝謝。我的錯。 –