【问题标题】:Where Clause with And & Or带有 And & Or 的 Where 子句
【发布时间】:2014-01-14 17:07:00
【问题描述】:

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

这是我所拥有的:

  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 的保单。

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

感谢您的帮助!!!

【问题讨论】:

    标签: sql subquery where-clause


    【解决方案1】:

    所以,这有点令人困惑,因为您的代码表明您正在尝试做一些与您所要求的完全不同的事情。要完成您的要求,您需要按照以下方式做一些事情:

    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'
    

    这应该返回 2013 年 12 月 15 日到 2014 年 5 月 1 日之间的所有记录,且 Annual_premium

    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'
    

    【讨论】:

    • 这太棒了,我很抱歉,我想试图通过这个我更困惑自己(显然不难做到)。我需要退回的是 2013 年 12 月 16 日至 2014 年 4 月 31 日之间所有低于 1000 的保单,然后是全年其他所有保单。在您的回答的帮助下,我能够解决它!再次感谢!!!
    【解决方案2】:

    您需要用括号括住OR 子句并在括号之间使用:

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

    【讨论】:

    • 你有没有机会把它们扔到它们应该去的地方?我试图在我这边做,但没有成功。再次感谢您!
    猜你喜欢
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多