简体   繁体   English

SQL Server中所有列都不为零的多个子句

[英]Multiple clauses in SQL Server where all columns do not equal zero

Take the example table in SQL Server SQL Server的示例表为例

col1, col2, col3, col4
 id1, 0.00, 0.00, 0.00
 id2, 0.00, 1.00, 0.00
 id3, 5.55, 2.22, 0.00
 id4, 0.00, 0.00, 0.00
 id5, 1.11, 2.22, -3.33

I'd like to implement a WHERE clause so that when all the values in col2, col3 and col4 equal zero that the line is excluded from the results. 我想实现一个WHERE子句,以便当col2,col3和col4中的所有值都等于零时,该行将从结果中排除。

I've tried putting the following WHERE clause 我尝试将以下WHERE子句放入
where col2!=0 and col3!=0 and col4!=0

This returns only the id5 row, when what I'm after is to return id2, id3 and id5. 当我需要返回id2,id3和id5时,这仅返回id5行。

I know the where clause is wrong, but not sure what other things to try out. 我知道where子句是错误的,但不确定要尝试其他什么方法。

I've thought about doing a sum across the columns but this isn't desirable, as the decimals can go in either direction and might on the off chance equal zero even when all the values are populated 我曾考虑过对列进行求和,但这不是可取的,因为小数可以向任一方向移动,并且即使所有值都已填充,也可能在关闭时的机率等于零

What you intended is: 您的意图是:

where not (col2=0 and col3=0 and col4=0)

interprets as all rows that don't have all three zero . 解释为不具有全部三个零的所有行

or 要么

where col2!=0 or col3!=0 or col4!=0

which interprets as all rows which have at least one of the columns non zero . 将其解释为具有至少一列非零的所有行

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM