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.
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!
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);