我已经阅读了本网站Are brackets in the WHERE clause standard sql上的一些资源和帖子。但是,我仍然无法让所有过滤器工作。
运行以下代码时,我的结果仍会返回包含我尝试过滤掉的值以外的值的记录。
我试图返回所有具有(1)PT_ACUITY
等于3 - Two+ Resources
并且还有(2)Track_Group equaling 'ED Tracking Group'
and(3)的CHECKIN_DATE_TIME
属于状态和结束日期变量的值以及(4)谁也没有REASON_FOR_VISIT
包含任何通配符。
我希望返回的记录能够满足所有四个参数。谢谢!
use EMTCQIData
DECLARE @StartDate Date
DECLARE @EndDate Date
Set @StartDate = '01/01/2017'
Set @EndDate = '12/21/2018'
Select pt_fin, TRACKING_GROUP, PT_MRN, RESULT_DT_TM, NOTE_TYPE,
RESULT_TITLE_TEXT, AUTHOR, ACTION_PRSNL, Result
from ED_NOTES_MASTER
WHERE (TRACK_GROUP = 'ED Tracking Group')
AND (PT_ACUITY = '3 - Two+ Resources')
AND (CHECKIN_DATE_TIME > @StartDate and CHECKIN_DATE_TIME < @EndDate)
AND (REASON_FOR_VISIT Not Like '%asthma%')
Or (REASON_FOR_VISIT Not Like '%psych%')
Or (REASON_FOR_VISIT Not Like '%suicide%')
Or (REASON_FOR_VISIT Not Like '%suicidal%')
Or (REASON_FOR_VISIT Not Like '%homicide%')
Or (REASON_FOR_VISIT Not Like '%homicidal%')
Or (REASON_FOR_VISIT Not Like '%FD-12%')
Or (REASON_FOR_VISIT Not Like '%behavioral%')
OR (REASON_FOR_VISIT Not Like '%overdose%')
Or (REASON_FOR_VISIT Not Like '%aggression%')
如果你想要一个变量@x不等于1或2,那么你可以写
@X <> 1 AND @X <> 2
但如果你写了
@X <> 1或@X <> 2
那么对于任何数字都是如此,所以如果@X为1,则下半部分为真,使整个事情成真
WHERE TRACK_GROUP = 'ED Tracking Group' AND PT_ACUITY='3 - Two+ Resources' AND CHECKIN_DATE_TIME > @StartDate and CHECKIN_DATE_TIME < @EndDate
AND
(REASON_FOR_VISIT Not Like '%asthma%' --it needs to be NOT like any of these, so all the conditions have to be true
AND REASON_FOR_VISIT Not Like '%psych%') --that is to say, each of the conditions is NOT like the value in quotes
AND REASON_FOR_VISIT Not Like '%suicide%'
AND REASON_FOR_VISIT Not Like '%suicidal%'
AND REASON_FOR_VISIT Not Like '%homicide%'
AND REASON_FOR_VISIT Not Like '%homicidal%'
AND REASON_FOR_VISIT Not Like '%FD-12%'
AND REASON_FOR_VISIT Not Like '%behavioral%'
AND REASON_FOR_VISIT Not Like '%overdose%'
AND REASON_FOR_VISIT Not Like '%aggression%' )
首先,格式化查询将使您更容易阅读和剖析。
如果我理解你的问题。
WHERE TRACK_GROUP = 'ED Tracking Group'
AND PT_ACUITY='3 - Two+ Resources'
AND CHECKIN_DATE_TIME > @StartDate
AND CHECKIN_DATE_TIME < @EndDate
AND ( REASON_FOR_VISIT Not Like '%asthma%'
AND REASON_FOR_VISIT Not Like '%psych%'
AND REASON_FOR_VISIT Not Like '%suicide%'
AND REASON_FOR_VISIT Not Like '%suicidal%'
AND REASON_FOR_VISIT Not Like '%homicide%'
AND REASON_FOR_VISIT Not Like '%homicidal%'
AND REASON_FOR_VISIT Not Like '%FD-12%'
AND REASON_FOR_VISIT Not Like '%behavioral%'
AND REASON_FOR_VISIT Not Like '%overdose%'
AND REASON_FOR_VISIT Not Like '%aggression%'
)
好吧,基于你的WHERE子句,你可能不需要任何括号,但我将它们组合在一起如下:
WHERE
(TRACK_GROUP = 'ED Tracking Group' AND PT_ACUITY='3 - Two+ Resources'
AND CHECKIN_DATE_TIME > @StartDate and CHECKIN_DATE_TIME < @EndDate)
AND (REASON_FOR_VISIT Not Like '%asthma%'
AND REASON_FOR_VISIT Not Like '%psych%'
AND REASON_FOR_VISIT Not Like '%suicide%'
AND REASON_FOR_VISIT Not Like '%suicidal%'
AND REASON_FOR_VISIT Not Like '%homicide%'
AND REASON_FOR_VISIT Not Like '%homicidal%'
AND REASON_FOR_VISIT Not Like '%FD-12%'
AND REASON_FOR_VISIT Not Like '%behavioral%'
AND REASON_FOR_VISIT Not Like '%overdose%'
AND REASON_FOR_VISIT Not Like '%aggression%')
你的问题是那里有所有那些OR