MS Access Switch Aggregate Function

时间:2017-01-11 08:42:33

标签: sql ms-access aggregate

我有以下SQL语句。但是,它无法运行。它说:

  

您的查询不包含指定的表达式   'DateValue(开关(小时([日期时间])> = 18和小时([日期时间])< 22,[日期   时间],小时([日期时间])> = 22,[日期时间],小时([日期时间])> = 0和   小时([日期时间])< 2,[日期时间],小时([日期时间])> = 2和小时([日期]   时间])< 6,[日期时间] -1,小时([日期时间])> = 6和小时([日期]   时间])< 10,[日期时间] -1,小时([日期时间])> = 10和小时([日期]   时间])< 14,[日期时间] -1,小时([日期时间])> = 14和小时([日期]   时间])< 18,[日期时间] -1))作为DateValue'作为聚合的一部分   功能

SELECT DateValue(Switch(Hour([Date Time])>=18 And Hour([Date Time])<22,[Date Time],Hour([Date Time])>=22,[Date Time],Hour([Date Time])>=0 And Hour([Date Time])<2,[Date Time],Hour([Date Time])>=2 And Hour([Date Time])<6,[Date Time]-1,Hour([Date Time])>=6 And Hour([Date Time])<10,[Date Time]-1,Hour([Date Time])>=10 And Hour([Date Time])<14,[Date Time]-1,Hour([Date Time])>=14 And Hour([Date Time])<18,[Date Time]-1)) As DateValue, Avg([MFR g/10min]) AS Average, Max([MFR g/10min]) - Min([MFR g/10min]) As Difference
FROM [In process analysis result]
GROUP BY DateValue([Date Time]);

有什么问题吗?

编辑1: 如果我运行以下查询,它将起作用:

SELECT DateValue(Switch(Hour([Date Time])>=18 And Hour([Date Time])<22,[Date Time],Hour([Date Time])>=22,[Date Time],Hour([Date Time])>=0 And Hour([Date Time])<2,[Date Time],Hour([Date Time])>=2 And Hour([Date Time])<6,[Date Time]-1,Hour([Date Time])>=6 And Hour([Date Time])<10,[Date Time]-1,Hour([Date Time])>=10 And Hour([Date Time])<14,[Date Time]-1,Hour([Date Time])>=14 And Hour([Date Time])<18,[Date Time]-1)) AS DateValue, Avg([MFR g/10min]) AS Average, Max([MFR g/10min]) - Min([MFR g/10min]) As Difference FROM [In process analysis result] GROUP BY Switch(Hour([Date Time])>=18 And Hour([Date Time])<22,[Date Time],Hour([Date Time])>=22,[Date Time],Hour([Date Time])>=0 And Hour([Date Time])<2,[Date Time],Hour([Date Time])>=2 And Hour([Date Time])<6,[Date Time]-1,Hour([Date Time])>=6 And Hour([Date Time])<10,[Date Time]-1,Hour([Date Time])>=10 And Hour([Date Time])<14,[Date Time]-1,Hour([Date Time])>=14 And Hour([Date Time])<18,[Date Time]-1);

然而,结果不是我想要的。我想按照列{1}}的日期进行分组:

enter image description here

DateValue()没有分组,我无法得到最大值和最小值之间的平均值和差值。

请帮忙......

1 个答案:

答案 0 :(得分:0)

您的分组功能应与选择相同(在您的分组条件中添加:DateValue()):

SELECT DateValue(Switch(Hour([Date Time])>=18 And Hour([Date Time])<22,[Date Time],Hour([Date Time])>=22,[Date Time],Hour([Date Time])>=0 And Hour([Date Time])<2,[Date Time],Hour([Date Time])>=2 And Hour([Date Time])<6,[Date Time]-1,Hour([Date Time])>=6 And Hour([Date Time])<10,[Date Time]-1,Hour([Date Time])>=10 And Hour([Date Time])<14,[Date Time]-1,Hour([Date Time])>=14 And Hour([Date Time])<18,[Date Time]-1)) AS DateValue, Avg([MFR g/10min]) AS Average, Max([MFR g/10min]) - Min([MFR g/10min]) As Difference FROM [In process analysis result] GROUP BY DateValue(Switch(Hour([Date Time])>=18 And Hour([Date Time])<22,[Date Time],Hour([Date Time])>=22,[Date Time],Hour([Date Time])>=0 And Hour([Date Time])<2,[Date Time],Hour([Date Time])>=2 And Hour([Date Time])<6,[Date Time]-1,Hour([Date Time])>=6 And Hour([Date Time])<10,[Date Time]-1,Hour([Date Time])>=10 And Hour([Date Time])<14,[Date Time]-1,Hour([Date Time])>=14 And Hour([Date Time])<18,[Date Time]-1));