在一个时间范围内分组为5分钟的间隔我想要做的mySQL命令有些困难。SELECT a.timestamp, name, count(b.name) FROM time a, id b WHERE a.user = b.user AND a.id = b.id AND b.name = 'John' AND a.timestamp BETWEEN '2010-11-16 10:30:00' AND '2010-11-16 11:00:00' GROUP BY a.timestamp这是我目前的输出声明。timestamp name count(b.name)------------------- ---- -------------2010-11-16 10:32:22 John 22010-11-16 10:35:12 John 72010-11-16 10:36:34 John 12010-11-16 10:37:45 John 22010-11-16 10:48:26 John 82010-11-16 10:55:00 John 92010-11-16 10:58:08 John 2如何将它们分组为5分钟的间隔结果?我希望我的输出像timestamp name count(b.name)------------------- ---- -------------2010-11-16 10:30:00 John 22010-11-16 10:35:00 John 102010-11-16 10:40:00 John 02010-11-16 10:45:00 John 82010-11-16 10:50:00 John 02010-11-16 10:55:00 John 11
3 回答
米琪卡哇伊
TA贡献1998条经验 获得超6个赞
这适用于每个间隔。
PostgreSQL的
SELECT TIMESTAMP WITH TIME ZONE 'epoch' + INTERVAL '1 second' * round(extract('epoch' from timestamp) / 300) * 300 as timestamp, name, count(b.name)FROM time a, id WHERE …GROUP BY round(extract('epoch' from timestamp) / 300), name
MySQL的
SELECT timestamp, -- not sure about that name, count(b.name)FROM time a, id WHERE …GROUP BY UNIX_TIMESTAMP(timestamp) DIV 300, name
大话西游666
TA贡献1817条经验 获得超14个赞
您应该使用GROUP BY UNIX_TIMESTAMP(time_stamp) DIV 300
而不是round(../ 300),因为舍入我发现一些记录被计入两个分组结果集。
添加回答
举报
0/150
提交
取消