我有一个mysql表,我正在存储我的Feed收到的股票报价数据。数据如下表所示,并且有多个表格,其中包含至少3M行..
id datetime size value 7321798, '2010-12-11 00:00:00', 3, 1384.6 7321799, '2010-12-11 00:00:00', 1, 1384.6 7321800, '2010-12-11 00:00:00', 1, 1384.7 7321801, '2010-12-11 00:00:00', 1, 1384.8 7321802, '2010-12-11 00:00:03', 1, 1384.7 7321803, '2010-12-11 00:00:04', 1, 1384.6 7321804, '2010-12-11 00:00:04', 2, 1384.6 7321805, '2010-12-11 00:00:04', 1, 1384.6 7321806, '2010-12-11 00:00:04', 1, 1384.7 7321807, '2010-12-11 00:00:05', 1, 1384.5 7321808, '2010-12-11 00:00:05', 1, 1384.3 7321809, '2010-12-11 00:00:06', 3, 1384.3 7321810, '2010-12-11 00:00:06', 2, 1384.3 7321811, '2010-12-11 00:00:06', 1, 1384.3 7321812, '2010-12-11 00:00:06', 1, 1384.4 7321813, '2010-12-11 00:00:06', 1, 1384.5 7321814, '2010-12-11 00:00:06', 4, 1384.6 7321815, '2010-12-11 00:00:07', 1, 1384.6 7321816, '2010-12-11 00:00:07', 5, 1384.6 7321817, '2010-12-11 00:00:08', 1, 1384.6 7321818, '2010-12-11 00:00:09', 3, 1384.7 ............ ............. ............... ..................
现在我想要时尚的250行,我可以将每一行分成1分钟,分割的数据将是该范围内的最高值,范围中的最低值和该时间范围的列大小总数: / p>
所以数据提取应该类似于
time highval lowval total of size 2010-12-11 00:00:00 1300 1200 235 2010-12-11 00:01:00 1258 1125 3000 2010-12-11 00:02:00 1440 1312 15000
依旧......
(在上述结果中,如果我请求数据,它将获得最大值的最高值,最低值和总和(总和),最初给出1分钟时间帧,然后继续直到行的总结果变为250)
如果这可以通过单行查询实现,或者请向我展示创建存储过程的步骤。
答案 0 :(得分:1)
类似的东西:
SELECT
DATE_FORMAT(datetime,"%Y-%m-%c %H:%i") AS date,
MAX(value) AS max_value,
MIN(value) AS min_value,
SUM(size) AS size_total
FROM table
GROUP BY DATE(datetime),HOUR(datetime),MINUTE(datetime)
我相信其他海报会有一个更优雅的解决方案,但是这些分组在所有时间字段上从几分钟开始给你分钟分组。
编辑: 分组超过一分钟,最后一天只能通过以下方式进行分组:
SELECT
DATE_FORMAT(MIN(datetime),"%Y-%m-%c %H:%i") AS startDate,
DATE_FORMAT(MAX(datetime),"%Y-%m-%c %H:%i") AS endDate,
MAX(value) AS max_value,
MIN(value) AS min_value,
SUM(size) AS size_total
FROM table
WHERE datetime > DATE_SUB(NOW(), INTERVAL 1 DAY)
GROUP BY DATE(datetime),HOUR(datetime),FLOOR(MINUTE(datetime)/2)
这确保了,例如,分钟0和1具有相同的组。 再次,我相信其他海报可能会有更优雅的解决方案。
有关此方法的详情,请参阅date_sub上的文档。