英文:
SQL SELECT and WHERE
问题
我想选择PL类别52105下的交易和交易代码9007和9803,但结果选择了除这两个之外的其他交易代码。我错在哪里?
FROM [Steward].[dbo].[vwNONFUNDED_RECOMP_04]
WHERE PL_CATEGORY_CATEG_STMT IN ('52105') OR PL_CATEGORY_CATEG_STMT IS NULL AND TRANSACTION_CODE_STMT IN ('9007', '9803') AND REVERSAL_MARKER_STMT NOT LIKE ('R')
order by BOOKING_DATE_STMT
英文:
I Want to select transactions under PL Category 52105 and transaction codes 9007 & 9803, but the results are are picking other transaction codes other than those 2. Where am I getting it wrong?
FROM [Steward].[dbo].[vwNONFUNDED_RECOMP_04]
WHERE PL_CATEGORY_CATEG_STMT IN ('52105') OR PL_CATEGORY_CATEG_STMT IS NULL AND TRANSACTION_CODE_STMT IN ('9007', '9803') AND REVERSAL_MARKER_STMT NOT LIKE ('R')
order by BOOKING_DATE_STMT
答案1
得分: 1
这是优先级的顺序,使得OR
语句后面的所有内容都被视为这样处理。
尝试以下方式,通过使用括号强制指定顺序:
WHERE (PL_CATEGORY_CATEG_STMT IN ('52105') OR PL_CATEGORY_CATEG_STMT IS NULL) AND TRANSACTION_CODE_STMT IN ('9007', '9803') AND REVERSAL_MARKER_STMT NOT LIKE ('R')
请注意,在参与OR
子句的两个语句周围有两个括号。
英文:
Its the order of precedence that yields everything behind the OR
statement to be treated as just that.
Try the following instead, forcing the order by using brackets:
WHERE (PL_CATEGORY_CATEG_STMT IN ('52105') OR PL_CATEGORY_CATEG_STMT IS NULL) AND TRANSACTION_CODE_STMT IN ('9007', '9803') AND REVERSAL_MARKER_STMT NOT LIKE ('R')
Note the two brackets around the two statements participating in the OR
clause.
答案2
得分: 0
Consider rewriting like this:
FROM [Steward].[dbo].[vwNONFUNDED_RECOMP_04]
WHERE TRANSACTION_CODE_STMT IN ('9007', '9803') AND REVERSAL_MARKER_STMT NOT LIKE ('R%') AND (PL_CATEGORY_CATEG_STMT IN ('52105') OR PL_CATEGORY_CATEG_STMT IS NULL)
ORDER BY BOOKING_DATE_STMT
你可能需要检查你的 "not like" 如何编写,它应该包括通配符,具体取决于你想要实现的目标,例如,not like ('R%') - 其中 R 不是文本的开头。
英文:
Consider rewriting like this:
FROM [Steward].[dbo].[vwNONFUNDED_RECOMP_04]
WHERE TRANSACTION_CODE_STMT IN ('9007', '9803') AND REVERSAL_MARKER_STMT NOT LIKE ('R') AND (PL_CATEGORY_CATEG_STMT IN ('52105') OR PL_CATEGORY_CATEG_STMT IS NULL)
order by BOOKING_DATE_STMT
You might want to look at how you have written your "not like", it should include the wildcards depending on what you want it to achieve
for example, not like ('R%') - where R does not start the Text
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论