在Access 2007中计算过去8周(汇总数据)

时间:2015-10-04 23:13:56

标签: sql vba ms-access ms-access-2007

我已经尝试了很多不同的方法来解决这个问题,但我仍然陷入困境。我可以使用以下SQL计算过去8周(工作日):

SELECT [Returns Data].[Returns]
, [Returns Data].[Return Reason]
, Count([ Returns Data].[Account]) AS [CountOf Account]
, Sum([Returns Data].Amount) AS SumOfAmount 
FROM [Returns Data] 
GROUP BY [Returns Data].[Returned in System]
, [Returns Data].[Return Reason] 
HAVING ((([Returns Data].[Returned in System]) 
  Between (DateSerial(Year(Date()),Month(Date()),-39)) And Date()));

我的猜测是操纵日期序列以使其持续8周的摘要。理想情况下,我希望在过去的8周内将数据放入主表中。

任何提示或建议?

2 个答案:

答案 0 :(得分:0)

您不应该需要日期序列

SELECT [Returns Data].[Returns], [Returns Data].[Return Reason], 
    Count([ Returns Data].[Account]) AS [CountOf Account], 
    Sum([Returns Data].Amount) AS SumOfAmount
FROM [Returns Data]
GROUP BY [Returns Data].[Returned in System], [Returns Data].[Return Reason]
HAVING [Returns Data].[Returned in System] BETWEEN Date() AND Date()-39;

答案 1 :(得分:0)

首先,您应该使用常规的where子句,而不是having子句。我根据您的计数或总和进行过滤,然后您将使用having子句。在这种情况下,一个常规的where子句就可以了。

其次,你要找的是dateadd函数,而不是dateserial。

这些内容可能会解决您的问题:

SELECT [Returns Data].[Returns]
, [Returns Data].[Return Reason]
, Count([ Returns Data].[Account]) AS [CountOf Account]
, Sum([Returns Data].Amount) AS SumOfAmount 
FROM [Returns Data] 
WHERE ([Returns Data].[Returned in System] BETWEEN
DateDiff("ww", -38, Date()) AND Date())
GROUP BY [Returns Data].[Returned in System]
, [Returns Data].[Return Reason] ;