Group By ....使SQL Query无法正常执行

时间:2013-03-24 07:57:57

标签: sql ms-access

我想看到日期范围内销售的总产品数量,即

 -------------------------
 |  PRODUCT   | QUANTITY |
 -------------------------
 |   A        |    120   |
 -------------------------
 |   B        |     75   |
 -------------------------

我在 MS Access 2003

中有以下sql查询
SELECT Product, Sum(PurchaseQuantity) AS Quantity
FROM tblInvoice
GROUP BY Product
HAVING PurchaseDate BETWEEN #3/19/2013# and #3/22/2013#;

但这并没有执行。 MS Access给出了如下错误:

  

“您尝试执行不包含指定的查询   表达'PurchaseDate BETWEEN#3/19/2013#和#3/22/2013#'作为一部分   集合函数。“

到目前为止,我知道我需要在Group By子句中添加PurchaseDate。但是,如果不将PurchaseDate放在Group By子句中,有没有办法做到这一点?

提前谢谢。

1 个答案:

答案 0 :(得分:3)

您应该在“WHERE”子句中使用purchaseDate条件:

SELECT Product, Sum(PurchaseQuantity) AS Quantity 
FROM tblInvoice
WHERE PurchaseDate BETWEEN #3/19/2013# and #3/22/2013#
GROUP BY Product