简体   繁体   English

我可以使用 HAVING 过滤简单记录集的 AND 和 OR 吗? (MS 访问 SQL)

[英]Can I use HAVING to filter for AND and OR of simple record set? (MS Access SQL)

I have a small record set of invoices.我有一小部分发票记录。 I'd like to extract all records 180 days old and newer for a statement.我想提取 180 天以前和更新的所有记录作为声明。 In some cases there are unpaid invoices over 180 days old that I ALSO want to display.在某些情况下,我还想显示超过 180 天的未付发票。 When I added the second AND with (Orders.[OrderPaid])=NO) the 0-180 days records are not displayed.当我使用(Orders.[OrderPaid])=NO)添加第二个 AND 时,不显示 0-180 天的记录。 The 0-180 day criteria works perfect without the (Orders.[OrderPaid])=NO) addition. 0-180 天标准在没有(Orders.[OrderPaid])=NO)添加的情况下完美运行。 The (Orders.[OrderPaid])=NO) works without the 0-180 day criteria. (Orders.[OrderPaid])=NO)在没有 0-180 天标准的情况下有效。

The code I have tried is is:我试过的代码是:

HAVING (((Customers.[CustomerID])=[Forms],[OrdersByCustomer],[CustomerID]) AND (DateAdd("d". -180. Now())<=Orders.[Invoice Date]) AND (Orders.[OrderPaid])=NO)

And: HAVING (((Customers.[CustomerID])=[Forms],[OrdersByCustomer],[CustomerID]) AND (DateAdd("d". -180. Now())<=Orders.[Invoice Date]) OR (Orders.[OrderPaid])=NO)并且: HAVING (((Customers.[CustomerID])=[Forms],[OrdersByCustomer],[CustomerID]) AND (DateAdd("d". -180. Now())<=Orders.[Invoice Date]) OR (Orders.[OrderPaid])=NO)

Could someone suggest the correct syntax or method?有人可以建议正确的语法或方法吗?

Parentheses are crucial here, it helps to remove the auto-generated ones and format the SQL.括号在这里至关重要,它有助于删除自动生成的括号并格式化 SQL。

HAVING Customers.[CustomerID] = [Forms]![OrdersByCustomer]![CustomerID]
  AND (
       DateAdd("d", -180, Now()) <= Orders.[Invoice Date]
       OR 
       Orders.[OrderPaid] = NO
      )

The main parentheses need to be around the 2nd and 3rd clauses (OR), not 1st and 2nd.主括号需要围绕第 2 和第 3 子句 (OR),而不是第 1 和第 2。

Reason: in SQL the priority of AND is higher than that of OR.原因:在SQL中AND的优先级高于OR的优先级。

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

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