$query= SELECT channel1, channel2, channel3
FROM `Table`
WHERE `id` =1
AND `dateTime` >= '2011-10-15 00:00:01'
AND `dateTime` <= '2011-10-17 23:59:59'
根据以上查询,如何每2小时获取通道1,通道2,通道3列的平均值?
答案 0 :(得分:2)
这是一个tsql解决方案:
SELECT avg(channel1) channel 1, avg(channel2) channel2, avg(channel3) channel3,
dateadd(hour, datediff(hour, 0,datetime)/2*2,0) FROM Table
WHERE id =1 AND
dateTime >= '2011-10-15 00:00:01' AND
dateTime <= '2011-10-17 23:59:59'
group by datediff(hour, 0,datetime)/2*2
答案 1 :(得分:0)
假设MySQL:
SELECT date(`dateTime`) dateDay, 2*floor(date_format(`dateTime`,'%H')/2) dateHour,
avg(channel1), avg(channel2), avg(channel3)
FROM `Table`
WHERE `id` =1
AND `dateTime` >= '2011-10-15 00:00:01'
AND `dateTime` <= '2011-10-17 23:59:59'
group by date(`dateTime`), 2*floor(date_format(`dateTime`,'%H')/2)
答案 2 :(得分:0)
此查询的关键是每两小时对记录进行分组。
考虑:
floor =一个返回最大整数值的函数,该值等于或小于一个数字(在数据库中找到类似的一个)
time_hh ='hhmmm'格式的dateTime列,我的意思是,从'0000'到'23:59'(使用数据库函数格式化你的dateTime字段)
查询将是:
SELECT
floor( time_hh / 200 ),
min(datetime),
max(datetime),
avg(channel1),
avg(channel2),
avg(channel3)
FROM
`table`
GROUP BY
floor( time_hh / 200 )