使用多个where条件过滤表

问题描述 投票:0回答:2

我试图从表中选择只提取符合我标准的数据。我基本上想要

不包括2019年, 排除where = 0的情况, 在policynumber为null的地方排除 排除where(liab = 0和亩> 0)。

我可能会得到一些错误的否定,因为我没有达到预期的行数。有一种方法可以在一系列步骤中执行此操作,就像我在excel中一样,但我显然不想这样做它在excel。

from  [dbo].[Hail_Data_2013_2018_incl_SSN]
where cropyear <> 2019 or (policynumber is not null) or (PolicyAcres <> 0) 
or (Policyliability <> 0 or PolicyAcres < 0) 

一步一步地这样做是有效的,是否有可以重写这个步骤或者我的'或'组合错误?

sql sql-order-by filtering where
2个回答
0
投票

我认为你的逻辑需要一些工作。我怀疑你需要“AND”而不是“OR”。发布数据表和预期结果,我们可以帮助您追逐这一点。

declare @mydata as table (policyacres int, policynumber varchar(10), Policyliability int, cropyear int)

INSERT INTO @mydata
Select -1, '123x', 0, 2019 UNION ALL
Select 1,'224y', 0, 2019 UNION ALL
Select -1,'223d',0,2020

Select * from @mydata 
 where cropyear <> 2019 and (policynumber is not null) and (PolicyAcres <> 0) and (Policyliability = 0 and PolicyAcres < 0)

0
投票

我想你想要这个:

where 
  (cropyear <> 2019) and
  (policynumber is not null) and 
  (PolicyAcres <> 0) and 
  (Policyliability <> 0 or PolicyAcres <= 0)
© www.soinside.com 2019 - 2024. All rights reserved.