vigilance wx
vigilance wx

Reputation: 23

max value in colum for last 10 rows

I would like the max value for one column but only for the last 10 rows

sqlite3 Accel.db 'SELECT * FROM Acceleration ORDER BY timeTS DESC LIMIT 10;'

The above will list the last 10 rows in table Acceleration

sqlite3 Accel.db 'SELECT MAX(AccelX) FROM Acceleration;'

will list the maximum value in the complete column AccelX

How do i combine the two? I want the max value in column AccelX for the last 10 rows

for example

sqlite3 Accel.db 'SELECT MAX(AccelX) FROM Acceleration ORDER BY timeTS DESC LIMIT 10;'

will list the max valve in the complete table/column it does not run the query only on the last 10 rows

Upvotes: 0

Views: 147

Answers (1)

forpas
forpas

Reputation: 164214

Use a subquery:

SELECT MAX(AccelX) FROM (
  SELECT AccelX 
  FROM Acceleration 
  ORDER BY timeTS DESC 
  LIMIT 10
)

Upvotes: 4

Related Questions