MS Access - 滚动总和(4个月)

问题描述 投票:0回答:2

我一直在构建一个访问查询,计算“滚动4个月”的销售数据。我一直在尝试使用DSUM,但我似乎只能获得特定组的小计或运行总计(而不是移动总计)。我试图说明我在下面尝试做什么。

    Date        Product  Value  Rolling_4_Month_Sum
    January     A        100    100
    February    A        200    300
    March       A        300    600
    April       A        300    900
    May         A        200    1000
    June        A        400    1200
    July        A        500    1400
    August      A        700    1800

是否可以仅运行4行/月的运行总计?

谢谢!

最好的问候,菲利普

access
2个回答
1
投票
SELECT
 a.Date,
 a.Product,
 a.Value,
 SUM(b.value)
FROM
 Table a
 INNER JOIN Table b ON a.Product=b.Product
   AND b.Date <= a.Date
   AND b.Date >= DateAdd("q",1, a.Date)
GROUP BY
  a.Date, a.Product

这应该在我看来是有效的。

表a是您的“单月”行日期。表b是自联接以检索最近4个前几个月。这是通过添加b.Date >= DateAdd("q",1, a.Date)作为自连接标准来完成的。


0
投票

这是一个很好的例子,说明这些事情是如何运作的。

数据:

OrderDetailID   OrderID ProductID   Price
1   1234    1   $5.00
2   1234    2   ($2.00)
3   1234    3   $4.00
4   1235    1   $5.00
5   1235    3   $4.00
6   1235    5   $12.00
7   1235    2   ($2.00)

SQL:

SELECT OD.OrderDetailID, OD.OrderID, OD.ProductID, OD.Price, (SELECT Sum(Price) FROM tblOrderDetails
   WHERE OrderDetailID <= OD.OrderDetailID) AS RunningSum
FROM tblOrderDetails AS OD;

enter image description here

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.