您的问题的本质是您的 [Sales] 数字是按月汇总的,但商品的价格可能在月初和月底之间发生了变化。所以,如果你在 2012 年 8 月卖出了 50 只手表,你并不能确定在价格为 180 时卖出了多少,在价格降至 150 后卖出了多少。
在我看来,唯一合理的解决方案是使用基于一个月中各种价格有效的天数的平均每月价格。您可以通过使用这样的 VBA 函数来实现:
Option Compare Database
Option Explicit
Public Function AverageMonthlyPrice(ItemName As String, AnyDayInMonth As Date) As Variant
Dim qdf As DAO.QueryDef, rst As DAO.Recordset
Dim FirstDayOfMonth As Date, LastDayOfMonth As Date, NumDaysInMonth As Long
Dim IntervalStart As Date, IntervalEnd As Date
Dim WeightedAveragePrice As Variant
WeightedAveragePrice = Null
FirstDayOfMonth = DateSerial(year(AnyDayInMonth), Month(AnyDayInMonth), 1)
LastDayOfMonth = DateAdd("d", -1, DateSerial(year(AnyDayInMonth), Month(AnyDayInMonth) + 1, 1))
NumDaysInMonth = DateDiff("d", FirstDayOfMonth, LastDayOfMonth) + 1
Set qdf = CurrentDb.CreateQueryDef("", _
"SELECT * FROM [Item Price] " & _
"WHERE ItemName = [pItemName] " & _
"AND Enddate >= [pFirstDay] AND Startdate <= [pLastDay]")
qdf!pItemName = ItemName
qdf!pFirstDay = FirstDayOfMonth
qdf!pLastDay = LastDayOfMonth
Set rst = qdf.OpenRecordset(dbOpenSnapshot)
Do While Not rst.EOF
IntervalStart = IIf(rst!StartDate < FirstDayOfMonth, FirstDayOfMonth, rst!StartDate)
IntervalEnd = IIf(rst!EndDate > LastDayOfMonth, LastDayOfMonth, rst!EndDate)
WeightedAveragePrice = Nz(WeightedAveragePrice, 0) + rst!Price * (DateDiff("d", IntervalStart, IntervalEnd) + 1) / NumDaysInMonth
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Set qdf = Nothing
If Not IsNull(WeightedAveragePrice) Then
WeightedAveragePrice = CCur(WeightedAveragePrice)
End If
AverageMonthlyPrice = WeightedAveragePrice
End Function
因此,现在您可以在 Access 中运行查询,将 [Sales] 表中每一行的销售数量、平均价格和汇率汇总在一起:
SELECT
Sales.ItemName,
Sales.SaleMonth,
Sales.Number,
AverageMonthlyPrice(Sales.ItemName, Sales.SaleMonth) AS AveragePrice,
[Exchange Rate].Rate AS ExchangeRate
FROM
Sales
INNER JOIN
[Exchange Rate]
ON [Exchange Rate].ExchangeMonth = Sales.SaleMonth
...返回类似:
ItemName SaleMonth Number AveragePrice ExchangeRate
-------- ---------- ------ ------------ ------------
Watch 2012-01-01 93 180 1.225
Watch 2012-08-01 50 153.871 1.166
Watch 2013-06-01 74 200 1.234
Watch 2013-05-01 59 200 1.123
如果将该查询保存在 Access 中,则可以在其他查询中使用它(就像使用表一样)来执行其余的计算。