SQL Case NOT IN无法正常工作

时间:2016-11-02 06:34:09

标签: sql sql-server

为什么SQL Case'不在'不行? '在'虽然它不匹配'不在'。

或许有更好的方法可以做到这一点?

我正在尝试将结果应用于满足条件设置的匹配项,只要它不符合其他标准集。

谢谢



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

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

 ELSE 

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

  ELSE 

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

  ELSE 

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

 THEN 'General'

  ELSE ''  END END

	END 
END AS ProspectiveNews,
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

欢迎来到NULL地狱 替换每一个

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,所以这个谓词没有得到满足,并且它在整个条件下都失败了。

答案 1 :(得分: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)