微软访问。按日期分组记录
MS Access. Grouping records by date
使用 MS Access。我正在尝试编写一份报告,按日期分组为周(周一至周日),并给出一周内所有交易的总数。
只有两个表。
这里是 sql:
SELECT tblBuyer.BuyerTradeDate, [booksold]-[bookpaid]-[booktradefee] AS Outcome
FROM tblSale INNER JOIN tblBuyer ON tblSale.BuyerID = tblBuyer.buyerID;
我的查询 returns 两列,BuyerTradeDate 和 Outcome。
下图为部分测试数据
我需要报告显示:
如果有任何建议,我将不胜感激。谢谢
在源查询或报表中创建一个字段,将此表达式作为控制源:
=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
这是我想出的,提供了我需要的解决方案。
在报告中,我添加了一个 BuyerTradeDate header 和一个包含以下内容的文本框:
="Week Number: " & Format$([BuyerTradeDate],"ww",0,0)
我添加了一个带有文本框的 BuyerTradeDate 页脚,其中包含:
="Summary for week beginning: " & " " & [BuyerTradeDate] & " (" & Count(*) & " " & IIf(Count(*)=1,"Trade","Trades") & ")"
报告看起来像这样:
使用 MS Access。我正在尝试编写一份报告,按日期分组为周(周一至周日),并给出一周内所有交易的总数。
只有两个表。
这里是 sql:
SELECT tblBuyer.BuyerTradeDate, [booksold]-[bookpaid]-[booktradefee] AS Outcome
FROM tblSale INNER JOIN tblBuyer ON tblSale.BuyerID = tblBuyer.buyerID;
我的查询 returns 两列,BuyerTradeDate 和 Outcome。
下图为部分测试数据
我需要报告显示:
如果有任何建议,我将不胜感激。谢谢
在源查询或报表中创建一个字段,将此表达式作为控制源:
=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
这是我想出的,提供了我需要的解决方案。
在报告中,我添加了一个 BuyerTradeDate header 和一个包含以下内容的文本框:
="Week Number: " & Format$([BuyerTradeDate],"ww",0,0)
我添加了一个带有文本框的 BuyerTradeDate 页脚,其中包含:
="Summary for week beginning: " & " " & [BuyerTradeDate] & " (" & Count(*) & " " & IIf(Count(*)=1,"Trade","Trades") & ")"
报告看起来像这样: