如何计算每小时第一行的平均值
How to calculate the average of the first rows of each hour
我们有一个 table 值,每五分钟在 table 中出现一个新行,其中包含数值和时间,如何计算每个值的前 45 分钟的平均值小时。
我有一个计算每小时平均值的查询
SELECT hh=CONVERT(CHAR(13),[DateMeasure],126),
[Fe_Ka]=AVG([Fe_ka]) FROM Results1
GROUP BY CONVERT(CHAR(13),[DateMeasure],126)
ORDER BY hh;
但我不知道如何在每小时开始时选择 45 分钟
Initial table
Desired Format
初始table包括三列:测量日期(datetime)、测量id(int)、值(float)。每五分钟添加一个新行。需要一个table,其中包含按小时计算的时间和该小时前 45 分钟的平均值
使用 DATEPART()
获取分钟和 CASE
表达式并检查时间是否在 HH:45 分钟内
select TheDate = convert(date, DateMeasure),
TheHour = datepart(hour, DateMeasure),
TheAvg = avg(case when datepart(minute, DateMeasure) <= 45 then TheVal end)
from TheTable
group by convert(date, DateMeasure), datepart(hour, DateMeasure)
我们有一个 table 值,每五分钟在 table 中出现一个新行,其中包含数值和时间,如何计算每个值的前 45 分钟的平均值小时。
我有一个计算每小时平均值的查询
SELECT hh=CONVERT(CHAR(13),[DateMeasure],126),
[Fe_Ka]=AVG([Fe_ka]) FROM Results1
GROUP BY CONVERT(CHAR(13),[DateMeasure],126)
ORDER BY hh;
但我不知道如何在每小时开始时选择 45 分钟
Initial table
Desired Format
初始table包括三列:测量日期(datetime)、测量id(int)、值(float)。每五分钟添加一个新行。需要一个table,其中包含按小时计算的时间和该小时前 45 分钟的平均值
使用 DATEPART()
获取分钟和 CASE
表达式并检查时间是否在 HH:45 分钟内
select TheDate = convert(date, DateMeasure),
TheHour = datepart(hour, DateMeasure),
TheAvg = avg(case when datepart(minute, DateMeasure) <= 45 then TheVal end)
from TheTable
group by convert(date, DateMeasure), datepart(hour, DateMeasure)