我正在尝试在按给定时间间隔分组的时间范围内从MySQL表中选择值。我的实际代码如下所示:
SET @dateformat = '%Y-%m-%d %H:%i:%s';
SET @start = '2014-05-07 17:44:15';
SET @stop = '2014-10-27 15:46:58';
SET @interval = 3600;
SELECT
t1.id,
t1.timestamp,
REPLACE(AVG(t1.A_1), '.', ',') AS avg_val1
FROM eco_547702f977d27 AS t1 WHERE timestamp > @start AND timestamp <= @stop GROUP BY UNIX_TIMESTAMP(timestamp) DIV @interval;
这可以工作,但按整小时(18:00,19:00,20:00,......)对值进行分组。我想根据@start对结果进行分组 - 比如18:44:15,19:44:15,20:44:15,......
谢谢!
答案 0 :(得分:0)
你在这里使用一个非常简单的技巧:
将44:15添加到时间值,然后执行相同的GROUP BY
,您将获得所需组中的数据
答案 1 :(得分:0)
我认为最简单的方法就是从值中减去44 * 60 + 15秒,并将其用于区间运算。鉴于你如何表达它:
SELECT t1.id, t1.timestamp, REPLACE(AVG(t1.A_1), '.', ',') AS avg_val1
FROM eco_547702f977d27 AS t1
WHERE timestamp > @start AND timestamp <= @stop
GROUP BY (UNIX_TIMESTAMP(timestamp) - (44*60 + 15)) DIV @interval;
我倾向于把时间放在SELECT
:
SELECT t1.id
FROM_UNIX_TIMESTAMP((UNIX_TIMESTAMP(timestamp) - (44*60 + 15)) DIV @interval) as timestamp,
REPLACE(AVG(t1.A_1), '.', ',') AS avg_val1
FROM eco_547702f977d27 AS t1
WHERE timestamp > @start AND timestamp <= @stop
GROUP BY (UNIX_TIMESTAMP(timestamp) - (44*60 + 15)) DIV @interval;