【问题标题】:Filtering a table with multiple where criteria过滤具有多个 where 条件的表
【发布时间】:2019-08-25 03:30:15
【问题描述】:

我正在尝试从仅提取符合我标准的数据的表中进行选择。 我基本上想

排除 2019 年,
排除英亩 = 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


【解决方案1】:

我想你想要这个:

where 
  (cropyear <> 2019) and
  (policynumber is not null) and 
  (PolicyAcres <> 0) and 
  (Policyliability <> 0 or PolicyAcres <= 0)

【讨论】:

    【解决方案2】:

    我认为你的逻辑需要一些工作。我怀疑你需要“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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-10
      • 2016-02-18
      • 2016-11-16
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      相关资源
      最近更新 更多