Ankit Chauhan
Ankit Chauhan

Reputation: 2405

How to get a Total of Last Five Values In Sqlite?

In my Database,

There is Data like this,

 ID  average_time
 1    5
 2    10
 3    15
 4    20
 5    25 
 6    30

I create a query;

 select total(average_time) from tbl_Timer order by id desc limit 5

It"s give the total of all values.

I want Total of LastFive Data,

How can i Do that?

Upvotes: 0

Views: 112

Answers (2)

Marco
Marco

Reputation: 57583

Try this:

select  sum(avtime) from
  (SELECT average_time AS avTime 
   FROM tbl_Timer
   ORDER BY id DESC LIMIT 5)

Upvotes: 2

John
John

Reputation: 721

If ID is sequential how about something like this

select sum(average_time) from data where id >= (select max(id)-4 from data);

Upvotes: 0

Related Questions