MS Access。按日期对记录进行分组

时间:2015-11-26 05:24:35

标签: ms-access-2013

使用MS Access。我正在尝试编写一份报告,按日期分为几周(周一至周日),并在一周内总计所有交易。

只有两张桌子。

这是sql:

SELECT tblBuyer.BuyerTradeDate, [booksold]-[bookpaid]-[booktradefee] AS Outcome
FROM tblSale INNER JOIN tblBuyer ON tblSale.BuyerID = tblBuyer.buyerID;

我的查询返回两列,BuyerTradeDate和Outcome。

下图显示了一些测试数据。

Sample output from query

我需要显示报告:

Report Output

我很感激任何建议。感谢。

2 个答案:

答案 0 :(得分:0)

在源查询或报表中创建一个字段,并将此表达式作为控制源:

=Weekday([BuyerTradeDate],2)

然后在报告中对此进行分组。

要拥有一周的第一个和最后一个日期,请使用以下功能:

Public Function DateWeekFirst( _
  ByVal datDate As Date, _
  Optional ByVal lngFirstDayOfWeek As VbDayOfWeek = vbUseSystemDayOfWeek) _
  As Date

' Returns the first date of the week of datDate.
' lngFirstDayOfWeek defines the first weekday of the week.
' 2000-09-07. Cactus Data ApS.
' 2003-05-01. System settings used as default.
' 2012-10-44. Data type of lngFirstDayOfWeek changed to VbDayOfWeek.

  DateWeekFirst = DateAdd("d", vbSunday - Weekday(datDate, lngFirstDayOfWeek), datDate)

End Function

Public Function DateWeekLast( _
  ByVal datDate As Date, _
  Optional ByVal lngFirstDayOfWeek As Long = vbUseSystemDayOfWeek) _
  As Date

' Returns the last date of the week of datDate.
' lngFirstDayOfWeek defines the first weekday of the week.
' 2000-09-07. Cactus Data ApS.
' 2003-05-01. System settings used as default.
' 2012-10-44. Data type of lngFirstDayOfWeek changed to VbDayOfWeek.

  DateWeekLast = DateAdd("d", vbSaturday - Weekday(datDate, lngFirstDayOfWeek), datDate)

End Function

答案 1 :(得分:0)

以下是我提出的解决方案,提供了我需要的解决方案。

在报告中,我添加了一个带有包含以下内容的txt框的BuyerTradeDate标头:

="Week Number: " & Format$([BuyerTradeDate],"ww",0,0)

我添加了一个带有txt框的BuyerTradeDate页脚,其中包含:

="Summary for week beginning: " & " " & [BuyerTradeDate] & " (" & Count(*) & " " & IIf(Count(*)=1,"Trade","Trades") & ")"

报告如下:

Report Output