I have a table where each row contains a value and a datetime. It has hundreds of thousands of rows. I would like to select the highest (max) value every n rows.
我有一个表,其中每行包含一个值和一个日期时间。它有数十万行。我想每n行选择最高(最大)值。
I had previously used a query to get the highest value every hour, but this isn't quite what I am looking for:
我之前使用过查询来获得每小时最高的值,但这不是我想要的:
SELECT datetime, MAX(value)
FROM `table`
GROUP BY date_format(datetime, '%Y-%m-%d &h')
Any advice would be greatly appreciated!
任何建议将不胜感激!
1
You can enumerate the rows and then aggregate to your heart's desire:
您可以枚举行然后聚合到您心中的愿望:
select min(rn), max(rn), min(datetime), max(datetime), max(value)
from (select t.*, (@rn := @rn + 1) rn
from `table` t cross join
(select @rn := 0) params
order by datetime
) t
group by floor((rn - 1) / @n)
order by min(rn);
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/04/07/4f537661a8652c2f60be9c27a8fdff63.html。