简体   繁体   English

SQL / SSRS 2008-如何在WHERE子句中将2个条件组合成一个条件?

[英]SQL / SSRS 2008 - How to combine 2 conditions into a single condition in the WHERE clause?

I have a requirement which appears simple enough but i cannot get this to work. 我有一个看起来很简单的要求,但我无法使它正常工作。 I want to exclude a particular record in my resultset that meets TWO conditions, click the following image to see my current resultset: https://i.stack.imgur.com/z93m3.png . 我想在我的结果集中排除满足两个条件的特定记录,请单击以下图片查看我当前的结果集: https : //i.stack.imgur.com/z93m3.png I want to exclude any lines that have both a selling_price of £84.99 AND a vat_value of £40.83, in my case that would mean i want the last line of my resultset image excluded. 我要排除任何同时具有Selling_price£84.99和vat_value£40.83的行,在我的情况下,这意味着我希望排除结果集图像的最后一行。

I've attempted to use the following as my SQL code: 我尝试使用以下代码作为我的SQL代码:

SELECT     created_by, sales_order_number, sales_order_credit_line, qty_credited, sales_order_line, selling_price, vat_value

FROM         sales_order_credit

WHERE    (selling_price <> 84.99 AND vat_value <> 40.83)

However, as soon as i go to then run the query, the Query Designer reformats my code to read: 但是,一旦我去运行查询,查询设计器就会将我的代码重新格式化为:

SELECT     created_by, sales_order_number, sales_order_credit_line, qty_credited, sales_order_line, selling_price, vat_value

FROM         sales_order_credit

WHERE     (selling_price <> 84.99) AND (vat_value <> 40.83)

Because my code now reads like that, it excludes all lines with a selling price of £84.99 and all lines with a vat_value of £40.83, which is not what i want - i only want to exclude lines that adhere to BOTH of those conditions. 因为我的代码现在是这样写的,所以它排除了所有售价为£84.99的行和所有vat_value为£40.83的行,这不是我想要的-我只想排除同时满足这两个条件的行。

I've ran into this before and never known how this should be done - any help is really appreciated. 我以前曾遇到过这个问题,但从未知道应该怎么做-真的很感谢您的帮助。

Jacob 雅各布

EDIT - Responses to Comments 编辑-评论回应

VKP - thanks for your suggestion, when i add your code it changes to: VKP-感谢您的建议,当我添加您的代码时,它更改为:

SELECT     created_by, sales_order_number, sales_order_credit_line, qty_credited, sales_order_line, selling_price, vat_value
FROM         sales_order_credit
WHERE     (NOT (selling_price = 84.99)) 
          OR
          (NOT (vat_value = 40.83))

Again, it takes the two and puts them into completely seperate conditions seperated by OR - which is not what im looking for 再一次,它需要将两者置于完全独立的条件(由OR分隔)中-这不是我正在寻找的东西

Ubiquitous Developers - there is a difference, if you do the reformatted version of the code it will exclude line 4 AND 2, whereas i only want line 4 excluded as it meets both conditions, whereas line 2 only met 1 of the conditions (selling_price) 无处不在的开发人员 -有所不同,如果执行代码的重新格式化版本,它将排除第4行和第2行,而我只希望第4行被排除,因为它同时满足这两个条件,而第2行仅满足其中一个条件(selling_price)

WHERE (selling_price <> 84.99) OR (vat_value <> 40.83) WHERE(销售价格<> 84.99)或(vat_value <> 40.83)

Try it yourself: rextester 自己尝试: rextester

I think the right thing to do in your case is the next: 我认为接下来要针对您的情况做正确的事情:

SELECT     created_by, sales_order_number, sales_order_credit_line,  qty_credited, sales_order_line, selling_price, vat_value
FROM         sales_order_credit
WHERE     NOT (selling_price = 84.99 AND vat_value = 40.83)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将max(number)条件放入SQL Server 2008中的where子句 - How to put max(number) condition into where clause in SQL Server 2008 LARAVEL 查询生成器:如何将 WHERE 子句中的 AND 和 OR 条件与多个条件结合起来 - LARAVEL Query Builder: How to combine AND and OR conditions in WHERE clause with multiple conditions 如何在 SQL Server 中两列的 where 子句中组合条件 - How do I combine conditions in where clause for two columns in SQL Server 基于SQL Server 2008中where子句的多重条件 - Multiple condition based on where clause in sql server 2008 SQL 中的 where 子句条件 - where clause conditions in SQL 如何在SQL的WHERE子句中动态使用条件 - How to use conditions dynamically in WHERE clause in SQL SQL:如何在带有“ NOT IN”条件的“ Where”子句中使用“ and”和“ or” - SQL : How to use “and” and “or” in a “Where” clause with “NOT IN” conditions SQL查询-如何在WHERE子句中放置条件 - SQL query - how to put conditions in WHERE clause 如何在 SQL Where 子句中使用多个 And 条件 - How to use multiple And conditions in SQL Where clause 如何在SQL中使用LIKE子句及其通配符函数将一个条件/参数组合在一起? - How in SQL to combine in One Single Condition/Parameter using LIKE Clause and its Wildcard Functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM