我已经阅读了一些资源,例如本网站Are brackets in the WHERE clause standard sql上的帖子。但是,我仍然无法使所有 where过滤器正常工作。
运行以下代码时,我的结果仍然返回包含除我尝试过滤的值以外的值的记录。
我正在尝试返回所有具有(1)PT_ACUITY
等于3 - Two+ Resources
且也具有(2)Track_Group equaling 'ED Tracking Group'
和(3)其{{1 }}属于state和end date变量,(4)也不具有包含任何通配符项的CHECKIN_DATE_TIME
。
我希望返回的记录满足所有四个参数。谢谢!
REASON_FOR_VISIT
答案 0 :(得分:4)
首先,对查询进行格式化将使其更具可读性,并且更易于可视化和剖析。
如果我理解您的问题。
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%'
)
答案 1 :(得分:2)
仅基于您的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都在其中
答案 2 :(得分:2)
如果您希望变量@x不等于1或2,则可以编写
@X <> 1和@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%' )