SQL: How to get max value of a range of rows?

Red Squirrel

No Lifer
May 24, 2003
70,080
13,532
126
www.anyf.ca
I have a bunch of rows that have a date and ping col, I want to grab the latest 288 rows, and get the max ping within that range?

You'd think this query would work, but it does not:

SELECT max(stresponse) FROM stats WHERE stshardid=31 ORDER BY stdate DESC LIMIT 288;

The max() just gets the overall max and ignores the LIMIT flag.
 

KLin

Lifer
Feb 29, 2000
30,208
558
126
Using a subquery should allow you to get the max record from a subset of 288 records.

SELECT max(stresponse) As MaxPing FROM (SELECT columns FROM stats where stshardid=31 ORDER BY stdate DESC LIMIT 288) sub