条款与&要么

时间:2014-01-14 17:07:00

标签: sql subquery where-clause

我正在尝试在where语句中添加一个Or子句,并且可以使用一些帮助。

这就是我所拥有的:

  Where
  W.Insured_Id = P.Insured_Id and
  P.Insured_Id = I.Insured_Id and
  I.Insured_Id = IU.Insured_Id and
  p.expiration_date > to_date ('30-Apr-2014','dd-mm-yyyy') and
  p.expiration_date < to_date ('16-Dec-2014','dd-mm-yyyy') or      
  p.expiration_date > to_date ('15-Dec-2013','dd-mm-yyyy') and
  p.expiration_date < to_date ('01-May-2014','dd-mm-yyyy') and
  p.annual_premium < '1000' or
  p.expiration_date > to_date ('15-Dec-2014','dd-mm-yyyy') and
  p.expiration_date < to_date ('01-Jan-2015','dd-mm-yyyy') and
  p.annual_premium < '1000'

我试图在5月1日到12月15日期间退回所有保单,并且只保留今年剩余时间内保费低于1000的政策。

如果我能提供更多信息,请告诉我。

感谢您的帮助!!!

2 个答案:

答案 0 :(得分:0)

您需要将OR子句与括号括起来并在括号之间使用:

  

(...... OR ...)和(...... OR ...)......

答案 1 :(得分:0)

所以,这有点令人困惑,因为你的代码暗示你正在尝试做一些与你所要求的完全不同的事情。为了完成你所要求的,你会想要做一些事情:

Where
W.Insured_Id = P.Insured_Id and
P.Insured_Id = I.Insured_Id and
I.Insured_Id = IU.Insured_Id and
p.expiration_date > to_date ('15-Dec-2013','dd-mm-yyyy') and
p.expiration_date < to_date ('01-May-2014','dd-mm-yyyy') and
p.annual_premium < '1000'

哪个应该返回12/15/2013和05/01/2014之间的所有记录,其中annual_premium&lt; 1000.您所包含的代码可以修改为使用括号工作:

Where
W.Insured_Id = P.Insured_Id and
P.Insured_Id = I.Insured_Id and
I.Insured_Id = IU.Insured_Id and
(
    (
        p.expiration_date > to_date ('30-Apr-2014','dd-mm-yyyy') and
        p.expiration_date < to_date ('16-Dec-2014','dd-mm-yyyy')
    )
    or
    (
        p.expiration_date > to_date ('15-Dec-2013','dd-mm-yyyy') and
        p.expiration_date < to_date ('01-May-2014','dd-mm-yyyy')
    )
    or
    (
        p.expiration_date > to_date ('15-Dec-2014','dd-mm-yyyy') and
        p.expiration_date < to_date ('01-Jan-2015','dd-mm-yyyy')
    )
    or
    (
        p.expiration_date > to_date ('15-Dec-2014','dd-mm-yyyy') and
        p.expiration_date < to_date ('01-Jan-2015','dd-mm-yyyy')
    )
)
and p.annual_premium < '1000'

然而,与您要求的相比,这将包括一堆记录。简化,由于日期重叠,此代码与此相同:

Where
W.Insured_Id = P.Insured_Id and
P.Insured_Id = I.Insured_Id and
I.Insured_Id = IU.Insured_Id and
p.expiration_date > to_date ('15-Dec-2013','dd-mm-yyyy') and
p.expiration_date < to_date ('01-Jan-2015','dd-mm-yyyy') and
p.annual_premium < '1000'