计算连续30天数据的平均值

时间:2016-08-10 13:23:49

标签: sql ms-access days

我正在寻找能够提供过去30天和最后30天数据的查询。

例如 - 如果今天是6/30 den,我应该能够将数据提取到5/30和4/30。我已经尝试了以下代码,但它只提供了过去30天的数据

SELECT Avg(Sub.Ticket) AS Average2
FROM (SELECT Fulldate, COUNT(Ticket_Number) AS Ticket
      FROM RawData
      GROUP BY Fulldate
     )  AS sub
WHERE ((([RawData].[FullDate])>=#6/30/2016#-30-30));

1 个答案:

答案 0 :(得分:1)

如果您希望结果分为1行,2列:

<div>
  <input class="category-group-1" type="checkbox" name="category[]" value="a">
  <input class="category-group-1" type="checkbox" name="category[]" value="b">
  <input class="category-group-1" type="checkbox" name="category[]" value="c">
</div>

<div>
  <input class="category-group-2" type="checkbox" name="category[]" value="x">
  <input class="category-group-2" type="checkbox" name="category[]" value="y">
  <input class="category-group-2" type="checkbox" name="category[]" value="z">
</div>

如果您希望结果包含2行1列:

SELECT 
  AVG(sub.TicketLast30) AS AverageLast30,
  AVG(sub.TicketPrev30) AS AveragePrev30
FROM (
  SELECT Fulldate, 
    COUNT(IIF(Fulldate BETWEEN (DATE()-30) AND DATE(), Ticket_Number, 0) AS TicketLast30,
    COUNT(IIF(Fulldate BETWEEN (DATE()-60) AND (DATE()-30), Ticket_Number, 0) AS TicketPrev30
  FROM RawData
  WHERE Fulldate BETWEEN (DATE()-60) AND DATE()
  GROUP BY Fulldate
) AS sub