MS Access SQL范围和平均值

时间:2017-06-11 09:04:25

标签: sql ms-access aggregate-functions ms-access-2016

[Date Time]和[MFR g / 10min]是[In process analysis result]表中的字段。我应该找到“每天[MFR g / 10min]的范围(最大 - 最小)”和“[MFR g / 10min]的平均值”。 一天定义为“前一天”18:00:00至“今天”17:59:00。

SELECT [In process analysis result].[Date Time], Format([In process analysis result].[Date Time],"dd/mmm/yy") AS [Extracted Date], Format([In process analysis result].[Date Time],"hh:nn:ss") AS [Extracted Time], IIf([Extracted Time]<"18:00:00",DateAdd("d",-1,[Extracted Date]),[Extracted Date]) AS [New Date], [In process analysis result].[MFR g/10min]
FROM [In process analysis result];

我提取出[提取日期]和[提取时间]。根据[提取时间]小于18:00:00的条件,应视为前一天。最终结果是[新日期]。

然而,从这个查询中,我不知道如何得到“每天[MFR g / 10min]的范围(最大 - 最小)”和“[MFR g / 10min]的平均值”按[New日期]。我需要一些帮助和指导。感谢是否有人可以帮助我。 谢谢。

enter image description here

1 个答案:

答案 0 :(得分:1)

您走在正确的轨道上,但是如果DateValue([Date Time])为18或更高,则可以Hour([Date Time])并添加1天来简化日期计算。然后,将该查询用作执行实际计算的查询中的子查询:

SELECT 
    EffectiveDate, 
    MAX([MFR g/10min]) - MIN([MFR g/10min]) AS DailyRange, 
    AVG([MFR g/10min]) AS DailyAverage 
FROM
    (
        SELECT 
            DateAdd("d", IIf(Hour([Date Time]) < 18, 0, 1), DateValue([Date Time])) AS EffectiveDate, 
            [MFR g/10min] 
        FROM [In process analysis result]
    )
GROUP BY EffectiveDate